在 Hibernate Search 中使用嵌入式索引进行编程映射导致无法找到字段错误

Programmatic mapping with embedded index in Hibernate Search results in unable to find field error

我必须对要使用 Hibernate Search 编制索引的字段进行编程配置。

在下面的场景中,使用 indexEmbedded() 会导致 "field not found error".

@Entity
public class AT {
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "ARRM_IDE", nullable = false)
    private A arr;

    private Date dateType;

    (and other fields)
}

@Entity
public class A {

    @Id
    @SequenceGenerator(name = "C_SEQUENCE", sequenceName = "S_ARRM_01")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "C_SEQUENCE")
    @Column(name = "IDE_ARR")
    private Long id;
}

SearchMapping mapping = new SearchMapping();
mapping.entity(AT.class).indexed()
        .property("dateType", ElementType.FIELD)
        .field()
        .store(Store.YES)
        .property("arr", ElementType.FIELD)
        .indexEmbedded()
        .entity(A.class).indexed()
        .property("id", ElementType.FIELD).documentId().name("arrId")
        .field()
        .store(Store.YES)
        ;

当我创建和保留实体时(我已将 Hibernate Search 与 Elasticsearch 集成),实体和索引也在 Elasticsearch 中创建。

Elasticsearch 上的内容:

      "_index" : "com.....at",
      "_type" : "com.....AT",
      "_id" : "7744",
      "_score" : 1.0,
      "_source" : {
        "dateType" : "2016-06-12T06:08:52.780Z",
        "arr" : {
          "id" : 6352
        }
      }
    } ]

但是当我尝试使用 Hibernate Search Lucene 查询进行查询时,它失败了:

FullTextEntityManager fullTextEntityManager =
        org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
QueryBuilder qb = fullTextEntityManager.getSearchFactory()
        .buildQueryBuilder().forEntity(AT.class).get();
org.apache.lucene.search.Query luceneQuery = qb.bool()
        .must(qb
                .range()
                .onField("dateType")
                .from(parseDate(startDate))
                .to(parseDate(endDate)).excludeLimit()
                .createQuery())
        .must(qb
        .keyword()
        .onField("arr")
        .matching(crsArrId).createQuery())
        .createQuery();
Sort sort = null;
if (order == OrderEnum.ASCENDING) {
    sort = new Sort(
            new SortField("dateType", SortField.Type.STRING));
} else {
    sort = new Sort(
            new SortField("dateType", SortField.Type.STRING, true));
}
FullTextQuery jpaQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, AT.class);
jpaQuery.setSort(sort);
jpaQuery.setFirstResult(offset);
jpaQuery.setMaxResults(maxReturnedEvents);

return jpaQuery.getResultList();

错误是:

org.hibernate.search.exception.SearchException: Unable to find field arr in com....AT
    at org.hibernate.search.engine.spi.DocumentBuilderIndexedEntity.objectToString(DocumentBuilderIndexedEntity.java:977)
    at org.hibernate.search.query.dsl.impl.FieldContext.objectToString(FieldContext.java:75)
    at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.buildSearchTerm(ConnectedMultiFieldsTermQueryBuilder.java:145)
    at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.createQuery(ConnectedMultiFieldsTermQueryBuilder.java:105)
    at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.createQuery(ConnectedMultiFieldsTermQueryBuilder.java:67)

感谢您的帮助!

您想搜索 arr.id,而不是

只需将 .onField("arr") 更改为 .onField("arr.id").