简体   繁体   English

Hibernate搜索/ lucene搜索多个字段的子集合

[英]Hibernate search / lucene search in multiple fields child collections

I am using Hibernate Search in my application. 我在我的应用程序中使用Hibernate Search。 One of child collections is mapped as IndexedEmbedded. 其中一个子集合映射为IndexedEmbedded。 Child object have two fields, one id and other is date (using date resoultion to milliseconds). 子对象有两个字段,一个是id,另一个是date(使用date resoultion到milliseconds)。 When I search for id=1 (or some value) and date equals to another, I get the results of all the cases where first and second match. 当我搜索id = 1(或某个值)并且date等于另一个时,我得到第一个和第二个匹配的所有情况的结果。 I want to get only records where both fields are matched in the same child, but I get matches in different childs, resulting in much higher results. 我想只得到两个字段在同一个孩子中匹配的记录,但我在不同的孩子中得到匹配,从而产生更高的结果。 Here is the snippet of the code 这是代码的片段

Main class is User 主要类是用户

@Indexed
public class User {...

@IndexedEmbedded(prefix = "score_")
public List<Score> getScores() {...}

Score class is @Indexed public class Score {... 分数类是@Indexed公共类分数{...

@Id
@DocumentId
public Integer getId() {...}

@Field
public Integer value

@Field(analyze = Analyze.NO, indexNullAs="_null_")
@DateBridge(resolution = Resolution.MILLISECOND)
public Date getScoreTime()

In my search code, I am doing the following 在我的搜索代码中,我正在执行以下操作

QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(User.class).get();

BooleanQuery subQuery = new BooleanQuery();
subQuery.add(queryBuilder.keyword().onField("score_id").matching(20).createQuery(), BooleanClause.Occur.MUST);
subQuery.add(queryBuilder.keyword().onField("score_scoreTime").ignoreFieldBridge().matching("_null").createQuery(), BooleanClause.Occur.MUST);

Search on value field (instead of scoreTime) have the same behavior. 搜索值字段(而不是scoreTime)具有相同的行为。 If I search separately on any of the fields, I get correct result. 如果我在任何字段上单独搜索,我会得到正确的结果。

Any suggestions? 有什么建议么?

Have you tried this syntax: 你试过这个语法吗?

//look for users whos id is 1 and createDate is more than lastMonth
Date lastMonth = ...;
QueryBuilder userQB = searchFactory.buildQueryBuilder().forEntity( User.class ).get();

Query luceneQuery = userQB
    .bool()
      .must( userQB.keyword().onField("id").matching("1").createQuery() )
      .must( userQB.range().onField("creationDate").above(lastMonth)
        .createQuery() )
    .createQuery();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM