0

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

在下面的场景中,使用indexEmbedded()会导致“找不到字段错误”。

@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)

谢谢你的帮助!

4

1 回答 1

0

您想搜索 arr.id,而不是 arr。

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

于 2016-06-10T13:11:46.927 回答