我正在尝试创建一个在弹性搜索索引上运行模糊搜索的函数。如果我完全按照索引中的拼写指定术语,我只会得到匹配项。如果我故意拼错该术语中的一个字母,例如
“博克”
,我想模糊搜索仍应返回相同的匹配项,但它不返回任何匹配项。同样,如果我用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"
}
}
]
}
}