亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Hibernate-search 按數字列表搜索

Hibernate-search 按數字列表搜索

大話西游666 2023-09-20 17:24:46
我正在 Hibernate 搜索 Java 應用程序中工作,該應用程序具有一個具有索引數字字段的實體:@Field@NumericFieldprivate Long orgId;我想獲取與此屬性的 Long 值列表匹配的實體列表。我使用“simpleQueryString”,因為它允許使用“OR”邏輯與 char | 對于幾個客觀價值。我有這樣的事情:queryBuilder.simpleQueryString().onField("orgId").matching("1|3|8").createQuery()運行 mi 應用程序后我得到:指定的查詢“+(orgId:1 orgId:3 orgId:8)”包含基于字符串的子查詢,其目標是數字編碼字段“orgId”。檢查您的查詢或嘗試限制目標實體。那么,有人可以告訴我這段代碼有什么問題嗎?還有其他方法可以滿足我的需要嗎?===================================更新1:yrodiere'的答案解決了這個問題,但我還有另一個疑問,我想驗證實體是否與其他字段匹配,我知道我可以使用BooleanJuntion,但是我需要混合“必須”和“應該”用法,對吧?IE:BooleanJunction<?> bool = queryBuilder.bool();for (Integer orgId: orgIds) {   bool.should( queryBuilder.keyword().onField("orgId").matching(orgId).createQuery() );}bool.must(queryBuilder.keyword().onField("name").matching("anyName").createQuery() );然后,我驗證實體必須與“名稱”匹配,并且它們還與給定的 orgId 之一匹配,我說得對嗎?
查看完整描述

1 回答

?
烙印99

TA貢獻1829條經驗 獲得超13個贊

正如錯誤消息所說:


指定的查詢 [...] 包含基于字符串的子查詢,其目標是數字編碼字段“orgId”。


simpleQueryString只能用于定位文本字段。不支持數字字段。


如果您的字符串是通過編程生成的,并且您有一個整數列表,那么您需要執行以下操作:


List<Integer> orgIds = Arrays.asList(1, 3, 8);


BooleanJunction<?> bool = queryBuilder.bool();

for (Integer orgId: orgIds) {

   bool.should( queryBuilder.keyword().onField("orgId").matching(orgId).createQuery() );

}

LuceneQuery query = bool.createQuery();

query將匹配字段orgId包含1, 3OR的文檔8。


請參閱https://docs.jboss.org/hibernate/search/5.11/reference/en-US/html_single/#_combining_queries


編輯:如果您需要其他子句,我建議不要在同一個布爾連接中混合“must”和“should”,而是嵌套布爾連接。


例如:


BooleanJunction<?> boolForOrgIds = queryBuilder.bool();

for (Integer orgId: orgIds) {

   boolForOrgIds.should(queryBuilder.keyword().onField("orgId").matching(orgId).createQuery());

}



BooleanJunction<?> boolForWholeQuery = queryBuilder.bool();

boolForWholeQuery.must(boolForOrgIds.createQuery());

boolForWholeQuery.must(queryBuilder.keyword().onField("name").matching("anyName").createQuery());

// and add as many "must" as you need

LuceneQuery query = boolForWholeQuery.createQuery();

從技術上講,你可以混合使用“must”和“should”,但效果不會是你所期望的:“should”子句將成為可選的,并且只會在匹配時提高文檔的分數。所以,這不是您需要的。


查看完整回答
反對 回復 2023-09-20
  • 1 回答
  • 0 關注
  • 76 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號