Hibernate Search 不索引数据库中的项目

Hibernate Search not indexing items from database

我正在尝试将 Hibernate Search 集成到我的应用程序中。 需要做的事情的粗略总结:

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

设置如下:

有一个 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;
  }
}

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