[英]Why does filterQuery not work in Elastic Search's high level REST client for JAVA?
我正在嘗試創建一個對彈性搜索索引運行模糊搜索的函數。 如果我完全按照索引中的拼寫指定術語,我只會得到匹配項。 如果我故意拼錯了該術語中的單個字母,例如
“博克”
,我想模糊搜索仍應返回相同的匹配項,但它不返回任何匹配項。 同樣,如果我用prefixQuery 或termQuery 替換fuzzyMatch,則搜索僅在給出確切拼寫的情況下返回結果
“鮑勃”
為什么是這樣? 我該如何解決? 哪里有解釋這些方法的文檔?
這是我的代碼...
public void searchResults(@PathParam("index_name") String index_name) throws IOException {
RestHighLevelClient client = createHighLevelRestClient();
int numberOfSearchHitsToReturn = 100; // defaults to 10
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.fuzzyQuery("firstname", "Bob"));
sourceBuilder.from(0);
sourceBuilder.size(numberOfSearchHitsToReturn);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
SearchRequest searchRequest = new SearchRequest(index_name).source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.print(searchResponse);
client.close();
}
這是在 Postman 中 Get /index/_search 的結果...
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "contacts",
"_type": "_doc",
"_id": "J1NDonABNQ4iHt4UOM4u",
"_score": 1.0,
"_source": {}
},
{
"_index": "contacts",
"_type": "_doc",
"_id": "153",
"_score": 1.0,
"_source": {
"firstname": "Bob",
"home_city": "San Diego",
"home_address": "1029 Loring Street",
"home_zip": "92109",
"contact_id": "153",
"email": "bsmith@gmail.com",
"lastname": "Smith",
"home_state": "California",
"cell_phone": "6192542981"
}
},
{
"_index": "contacts",
"_type": "_doc",
"_id": "154",
"_score": 1.0,
"_source": {
"firstname": "Alice",
"home_city": "Paia",
"home_address": "581 Pili Loko Street",
"home_zip": "00012",
"contact_id": "154",
"email": "aHernes@gmail.com",
"lastname": "Hernes",
"home_state": "Hawaii",
"cell_phone": "8083829103"
}
}
]
}
}
我相信彈性會讓你有點困惑。
3 個字母術語的模糊度為 1,因此您期望“Bob”返回就足夠了。 但是,我假設您使用默認過濾器“小寫”的標准分析器。
因此計算出的“Boc”和“bob”之間的 Levenshtein 距離是2 ,這就是它沒有返回的原因。
嘗試小寫輸入術語,我打賭會返回“Bob”。
// no results
{
"query": {
"fuzzy" : { "firstname" : "Boc" }
}
}
// "Bob" returned
{
"query": {
"fuzzy" : { "firstname" : "boc" }
}
}
這有意義嗎?
關於您的代碼:
public void searchResults(@PathParam("index_name") String index_name) throws IOException {
RestHighLevelClient client = createHighLevelRestClient();
int numberOfSearchHitsToReturn = 100; // defaults to 10
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// "Boc".toLowerCase() or simply "boc"
sourceBuilder.query(QueryBuilders.fuzzyQuery("firstname", "Boc".toLowerCase()));
sourceBuilder.from(0);
sourceBuilder.size(numberOfSearchHitsToReturn);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
SearchRequest searchRequest = new SearchRequest(index_name).source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.print(searchResponse);
client.close();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.