我在一个列中为Elasticsearch中的名称编制索引,某些名称只有名字,其他名称是名字和姓氏,我使用过match_phrase,match_phrase_prefix,multi_match但它没有给出正确的结果,因此我可以使用任何嵌套查询如果名字匹配则匹配,然后仅匹配姓氏?
multi_match,match_phrase,match_phrase_prefix
我尝试了match_phrase,match_phrase_prefix查询,
范例1:
输入:詹姆斯。
实际输出: Smith James,David James。
预期输出: James Smith,James,James Thomas等。
范例2:
输入: James Walker。
输出:“Nothing”
预期输出: James
最佳答案
如果您的目的是仅获取名称列表。您可以使用完成提示
以下是我的索引
PUT my_index
{
"mappings": {
"properties" : {
"name" : {
"type" : "completion"
}
}
}
}
询问
POST my_index/_search?pretty
{
"suggest": {
"name-suggest" : {
"prefix" : "<Search text>",
"completion" : {
"field" : "name"
}
}
}
}
以下是我的完整文件
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "joqRxGoBW0RKSbIqHqsw",
"_score" : 1.0,
"_source" : {
"name" : "Smith James"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "j4qRxGoBW0RKSbIqP6uW",
"_score" : 1.0,
"_source" : {
"name" : "James Smith"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "kIqRxGoBW0RKSbIqUKvP",
"_score" : 1.0,
"_source" : {
"name" : "James"
}
}
]
}
Query:
POST my_index/_search?pretty
{
"suggest": {
"name-suggest" : {
"prefix" : "James",
"completion" : {
"field" : "name"
}
}
}
}
]
Result:
"suggest" : {
"name-suggest" : [
{
"text" : "James",
"offset" : 0,
"length" : 5,
"options" : [
{
"text" : "James",
"_index" : "my_index",
"_type" : "_doc",
"_id" : "kIqRxGoBW0RKSbIqUKvP",
"_score" : 1.0,
"_source" : {
"name" : "James"
}
},
{
"text" : "James Smith",
"_index" : "my_index",
"_type" : "_doc",
"_id" : "j4qRxGoBW0RKSbIqP6uW",
"_score" : 1.0,
"_source" : {
"name" : "James Smith"
}
}
]
}
]
关于elasticsearch - 如何在查询DSL中匹配名字和名字,然后匹配名字?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56142635/