当jpa创建实体时,使用JPA的hibernate搜索elasticsearch无法自动创建索引

时间:2017-02-07 10:02:50

标签: hibernate jpa elasticsearch hibernate-search

我正在使用 hibernate-search-elasticsearch ,hibernate doc告诉我们 当我们使用jpa创建实体时,将自动创建索引。我发现我的程序成功创建了实体,但没有创建索引。

使用hibernate-search和es我导入hibernate-search-elasticsearch jar

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-search-elasticsearch</artifactId>
    <version>5.6.0.Final</version>
</dependency>

的persistence.xml

<persistence-unit name="dev_unit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <mapping-file>META-INF/travel-service/orm.xml</mapping-file>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57InnoDBDialect" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="false" />
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />

        <!--Hibernate JPA-->
        <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
        <property name="hibernate.cache.use_second_level_cache" value="false" />
        <property name="hibernate.cache.use_query_cache" value="false" />

        <property name="hibernate.connection.provider_class"
                  value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider" />
        <property name="hibernate.hikari.dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
        <property name="hibernate.hikari.minimumIdle" value="5" />
        <property name="hibernate.hikari.maximumPoolSize" value="10" />
        <property name="hibernate.hikari.idleTimeout" value="30000" />
        <property name="hibernate.hikari.dataSource.url" value="jdbc:mysql://127.0.0.1:3306/travel?useUnicode=true&amp;charset=utf8mb4amp;autoReconnect=true" />
        <property name="hibernate.hikari.dataSource.user" value="root" />
        <property name="hibernate.hikari.dataSource.password" value="111111" />

        <!-- search -->
        <property name="hibernate.search.default.directory_provider" value="filesystem"/>
        <property name="hibernate.search.default.indexBase" value="/Users/yybbk/index"/>
        <property name="hibernate.search.elasticsearch.host" value="http://localhost:9200" />
        <property name="hibernate.search.default.indexmanager" value="elasticsearch"/>
        <property name="hibernate.search.default.elasticsearch.required_index_status" value="yellow"/>
        <property name="hibernate.search.default.elasticsearch.index_schema_management_strategy" value="CREATE"/>
        <property name="hibernate.search.default.elasticsearch.refresh_after_write" value="true"/>

    </properties>
</persistence-unit>

我的实体:

@Entity
@Table(name = "groups")
@Indexed(index = "group")
public class Group {
    private Long id;
    private Long projectId;
    private String name;

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Field
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "project_id")
    @Field
    public Long getProjectId() {
        return projectId;
    }

    public void setProjectId(Long projectId) {
        this.projectId = projectId;
    }


    @Basic
    @Column(name = "name")
    @Field
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }   
}

这就是我的所有配置,当我使用JPA保存方法创建实体时,我希望休眠搜索自动创建索引,但不是。

感谢。

2 个答案:

答案 0 :(得分:1)

为了让Hibernate Search插入Hibernate ORM,并在实体持久化时对其进行索引,您还需要添加org.hibernate:hibernate-search-orm模块作为项目的依赖项:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-search-orm</artifactId>
    <version>5.6.0.Final</version>
</dependency>

也许缺少这种依赖?

答案 1 :(得分:0)

使用此属性启用Elasticsearch indexmanager时:

<property name="hibernate.search.default.indexmanager" value="elasticsearch"/>

这将获得在Elasticsearch上创建的索引,而不是本地Lucene索引。

基本上这些其他选项被忽略了:

<property name="hibernate.search.default.directory_provider" value="filesystem"/>
<property name="hibernate.search.default.indexBase" value="/Users/yybbk/index"/>

Hibernate Search应该在Elasticsearch上自动创建索引。 您应该能够使用浏览器指向http://localhost:9200/group/?pretty(其中&#34; group&#34;是索引名称,我从@Indexed注释中复制了它)。