Hibernate搜索不从数据库索引项目

时间:2015-06-13 11:24:53

标签: java spring hibernate lucene hibernate-search

我试图在我的应用程序中集成Hibernate Search。 粗略总结需要做什么:

  • Spring Batch读取XML文件并将对象持久保存到数据库。这是使用de JDBCBatchItemWriter完成的。由于性能低下,不是HibernateItemWriter。
  • 插入所有项目后,我想构建Hibernate Search / Lucene索引。

问题出在最后一步。没有任何东西被编入索引。

设置如下:

有一个JobListener,我在其中索引数据库中的数据,如下所示:

Session session = sessionFactory
 .withOptions()
 .openSession();
FullTextSession fullTextSession = Search.getFullTextSession(session);
try {
  fullTextSession.createIndexer().startAndWait();
} catch (InterruptedException e) {
  e.printStackTrace();
}

但是当我用Luke检查我的索引时,我找不到任何记录。 我想这与交易有关,但我无法弄清楚如何使其发挥作用。

这是我的持久性配置:

    @Bean
  public EntityManagerFactory entityManagerFactory() {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setPackagesToScan("xxx.data.domain");
    entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
    entityManagerFactoryBean.setDataSource(dataSource());
    entityManagerFactoryBean.getJpaPropertyMap().putAll(jpaProperties());
    entityManagerFactoryBean.afterPropertiesSet();

    return entityManagerFactoryBean.getObject();
  }


  @Bean
  public FullTextEntityManager fullTextEntityManager(EntityManager entityManagerFactory) throws InterruptedException {
    FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManagerFactory);
    fullTextEntityManager.createIndexer().startAndWait();
    return fullTextEntityManager;
  }

  private Map<String, String> jpaProperties() {
    Map<String, String> jpaProperties = new HashMap<String, String>();
    jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
    jpaProperties.put("hibernate.search.default.directory_provider", "filesystem");
    jpaProperties.put("hibernate.search.default.indexBase", "/tmp/lucene/indexes");
    jpaProperties.put("hibernate.search.indexing_strategy", "manual");
    return jpaProperties;
  }


  @Bean
  public SessionFactory sessionFactory(DataSource dataSource) throws IOException {
    LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
    localSessionFactoryBean.setDataSource(dataSource);
    localSessionFactoryBean.setPackagesToScan("xxx.data.domain");
    localSessionFactoryBean.afterPropertiesSet();

    return localSessionFactoryBean.getObject();
  }

要编入索引的实体:

@Entity
@Table(name = "MY_TABLE")
@Indexed
public class EntryEntity {
  public static final String SEQUENCE_NAME = "SEQ_MY_TABLE";

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE_NAME)
  @SequenceGenerator(name = SEQUENCE_NAME, sequenceName = SEQUENCE_NAME, allocationSize = 1)
  @Column(name = "ID")
  private Long id;

  @Column(name = "ENTRY_ID")
  @Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
  private String entryIdentifier;

  @Column
  @Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
  private String value;

  public String getValue() {
    return value;
  }

  public void setValue(String value) {
    this.value = value;
  }
}

1 个答案:

答案 0 :(得分:2)

我认为问题在于您在 jpaProperties 中定义了由实体管理器处理的搜索属性。但是,当您调用群发索引器时,您使用的是简单的会话。在这种情况下,将不会获取JPA属性。我的猜测是,相对于启动JVM的位置,创建了一些索引目录。 实际上你应该总是使用EntityManager。还有一个org.hibernate.search.jpa.Search,可让您获得FullTextEntityManager