2014-12-04 102 views
3

我在我的應用程序中使用Hibernate Search。其中一個子集合被映射爲IndexedEmbedded。子對象有兩個字段,一個id和其他日期(使用日期resoultion到毫秒)。當我搜索id = 1(或某個值)並且日期等於另一個時,我得到所有第一次和第二次匹配的情況的結果。我只想得到兩個字段在同一個孩子中匹配的記錄,但是我在不同的孩子中得到匹配,導致更高的結果。 下面是代碼的片段Hibernate搜索/ lucene搜索多個字段子集合

主類是用戶

@Indexed 
public class User {... 

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

得分類是 @Indexed 公共類分數{...

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

@Field 
public Integer value 

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

在我的搜索代碼,我我在做以下工作

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

在值字段上搜索(而不是scoreTime)具有相同的行爲。如果我單獨搜索任何字段,我會得到正確的結果。

有什麼建議嗎?

+0

好像什麼,我試圖做是不可能的。唯一的解決方法是在持久時間生成結果值,並通過臨時字段將其作爲父對象的一部分進行索引。不理想的方式,但這在我的具體情況。 – gmansoor 2014-12-10 18:58:46

+0

你可以幫我解決你的問題嗎? – gschambial 2017-08-25 11:15:08

回答

0

你有沒有嘗試過這樣的語法:

//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(); 
+0

我試過了,但是這會導致id = 1的所有記錄,以及上個月的所有creationDate。它還會返回可能具有創建日期lastMonth但id <> 1的記錄。我正在尋找同一行子記錄中的兩個搜索。 – gmansoor 2014-12-04 15:08:10