Elasticsearch:如何在copy_to字段中搜索多个单词?
我目前正在学习Elasticsearch,并在下面描述的问题上陷入困境:
在现有索引上(我不知道是否重要),我添加了此新映射:
PUT user-index
{
"mappings": {
"properties": {
"common_criteria": { -- new property which aggregates other properties by copy_to
"type": "text"
},
"name": { -- already existed before this mapping
"type": "text",
"copy_to": "common_criteria"
},
"username": { -- already existed before this mapping
"type": "text",
"copy_to": "common_criteria"
},
"phone": { -- already existed before this mapping
"type": "text",
"copy_to": "common_criteria"
},
"country": { -- already existed before this mapping
"type": "text",
"copy_to": "common_criteria"
}
}
}
}
目标是仅在 common_criteria 。
说我们有:
{
"common_criteria": ["John Smith","johny","USA"]
}
我想实现的目标是在 common_criteria
的多个值上搜索的确切匹配:
- 如果我们使用
John Smith
或<<代码>美国 + John Smith 或使用Johny + USA
或使用USA
或Johny
,最后使用John Smith +美国 + johny
(单词顺序无关紧要), - 如果我们使用
John Smith + Germany
或Johny + England
等多个单词搜索结果,
我正在使用弹簧数据弹性来构建我的查询:
NativeSearchQueryBuilder nativeSearchQuery = new NativeSearchQueryBuilder();
BoolQueryBuilder booleanQuery = QueryBuilders.boolQuery();
String valueToSearch = "johny"
nativeSearchQuery.withQuery(booleanQuery.must(QueryBuilders.matchQuery("common_criteria", valueToSearch)
.fuzziness(Fuzziness.AUTO)
.operator(Operator.AND)));
记录发送给弹性的请求:
{
"bool" : {
"must" :
{
"match" : {
"common_criteria" : {
"query" : "johny",
"operator" : "AND",
"fuzziness" : "AUTO",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"auto_generate_synonyms_phrase_query" : true,
"boost" : 1.0
}
}
},
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
使用该请求,我有0个结果。我知道请求是不正确的,因为必须。
在此先感谢您的帮助和解释。
编辑:尝试 multi_match
查询。
遵循 @rabbitbr的建议,我尝试了 multi_match
查询,但似乎不起作用。这是发送给弹性的请求的示例(带有0结果):
{
"bool" : {
"must" : {
"multi_match" : {
"query" : "John Smith USA",
"fields" : [
"name^1.0",
"username^1.0",
"phone^1.0",
"country^1.0",
],
"type" : "best_fields",
"operator" : "AND",
"slop" : 0,
"fuzziness" : "AUTO",
"prefix_length" : 0,
"max_expansions" : 50,
"zero_terms_query" : "NONE",
"auto_generate_synonyms_phrase_query" : true,
"fuzzy_transpositions" : true,
"boost" : 1.0
}
},
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
该请求不会返回结果。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试使用在创建一个字段之前,将所有其他字段存储在一个地方。
I would try to use Multi-match query before creating a field to store all the others in one place.