[英]Why does filterQuery not work in Elastic Search's high level REST client for JAVA?
I am trying to create a function runs a fuzzy search on an Elastic Search index.我正在尝试创建一个对弹性搜索索引运行模糊搜索的函数。 I only get a match if I specify the term exactly as it is spelled in the index.如果我完全按照索引中的拼写指定术语,我只会得到匹配项。 If I intentionally mispell a single letter in that term like如果我故意拼错了该术语中的单个字母,例如
"Boc" “博克”
, I imagine the fuzzy search should still return that same match, but instead it returns none. ,我想模糊搜索仍应返回相同的匹配项,但它不返回任何匹配项。 Simliarly, if I replace fuzzyMatch with prefixQuery or termQuery, the search only returns a result if given the exact spelling同样,如果我用prefixQuery 或termQuery 替换fuzzyMatch,则搜索仅在给出确切拼写的情况下返回结果
"Bob" “鲍勃”
Why is this?为什么是这样? How do I fix this?我该如何解决? And where is there documentation explaining these methods?哪里有解释这些方法的文档?
Here is my code...这是我的代码...
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();
}
Here is the result of Get /index/_search in Postman...这是在 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"
}
}
]
}
}
I believe elastic confuses you a bit.我相信弹性会让你有点困惑。
Fuzzines for 3 letter term is 1, so it is fair enough you expect "Bob" returned. 3 个字母术语的模糊度为 1,因此您期望“Bob”返回就足够了。 However, I assume you use the standard analyzer which uses by default filter "lowercase".但是,我假设您使用默认过滤器“小写”的标准分析器。
So calculated Levenshtein distance between "Boc" and "bob" is 2 that's why it is not returned.因此计算出的“Boc”和“bob”之间的 Levenshtein 距离是2 ,这就是它没有返回的原因。
Try lowercase input term and I'm betting "Bob" will be returned.尝试小写输入术语,我打赌会返回“Bob”。
// no results
{
"query": {
"fuzzy" : { "firstname" : "Boc" }
}
}
// "Bob" returned
{
"query": {
"fuzzy" : { "firstname" : "boc" }
}
}
Does this make sense?这有意义吗?
Regarding your code:关于您的代码:
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.