我希望在 Elasticsearch 中创建一个名为“story”的索引。该索引将存储多个文档,每个文档都包含一个名为“描述”的字段,该字段提供有关该文档所代表的故事的一些信息。我的目标是启用基于术语的子序列搜索查询。
例如:
Doc1 = {
description = "A quick brown fox jumps over a lazy dog"
}
Doc2 = {
description = "The fox is very agile and quick"
}
如果用户输入['fox', 'quick'],则只匹配Doc2,不匹配Doc1。本质上,这涉及通配符查询,但应用于术语,例如“term1 * term2 * term3”。
在Elasticsearch中实现这个可行吗?
我能想到的唯一方法是将所有空格和其他分隔字符替换为其他符号,以便每个描述成为一个术语。然后,我将对它们执行标准通配符查询。然而,考虑到描述可能相当大,我不确定这是否是一个理想的方法。
正如我评论的那样,您应该使用
match_phrase
查询
您的文件
PUT /story/_bulk
{"create":{"_id":1}}
{"description":"A quick brown fox jumps over a lazy dog"}
{"create":{"_id":2}}
{"description":"The fox is very agile and quick"}
查询
GET /story/_search?filter_path=hits.hits
{
"query": {
"match_phrase": {
"description": {
"query": "quick fox",
"slop": 1
}
}
}
}
您应该为
slop
参数设置一个正值
回应
{
"hits" : {
"hits" : [
{
"_index" : "story",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.22130066,
"_source" : {
"description" : "A quick brown fox jumps over a lazy dog"
}
}
]
}
}