首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何构建考虑到单词之间距离的Elasticsearch查询?

如何构建考虑到单词之间距离的Elasticsearch查询?
EN

Stack Overflow用户
提问于 2022-03-28 19:09:23
回答 1查看 43关注 0票数 1

我和elasticsearch:7.6.2一起跑

我有一个包含4个简单文档的索引:

代码语言:javascript
复制
    PUT demo_idx/_doc/1
    {
      "content": "Distributed nature, simple REST APIs, speed, and scalability, Elasticsearch is the central component of the Elastic Stack, the end"
    }

    PUT demo_idx/_doc/2
    {
      "content": "Distributed tmp nature, simple REST APIs, speed, and scalability"
    }

    PUT demo_idx/_doc/3
    {
      "content": "Distributed nature, simple REST APIs, speed, and scalability"
    }

    PUT demo_idx/_doc/4
    {
      "content": "Distributed tmp tmp nature"
    }

我想搜索文本:distributed nature并获得以下结果顺序:

代码语言:javascript
复制
Doc id: 3 
Doc id: 1
Doc id: 2
Doc id: 4

也就是说,与之完全匹配的文档(doc 3和doc 1)将在具有小斜率的文档(doc 2)之前显示,而具有大斜率匹配的文档将最后显示(doc 4)。

我读了这篇文章:如何构建一个考虑到单词之间的距离和单词的精确性的Elasticsearch查询,但对我没有帮助

我尝试了以下seach查询:

代码语言:javascript
复制
"query": {
            "bool": {
                "must":
                    [{
                        "match_phrase": {
                            "content": {
                                "query": query,
                                "slop": 2
                            }
                        }
                    }]
            }
        }

但它并没有给我所需要的结果。

我得到了以下结果:

代码语言:javascript
复制
Doc id: 3  ,Score: 0.22949813
Doc id: 4  ,Score: 0.15556586
Doc id: 1  ,Score: 0.15401536 
Doc id: 2  ,Score: 0.14397088

如何编写查询以获得我想要的结果?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-29 04:34:31

您可以使用bool子句显示与“分布式性质”完全匹配的文档。第一个子句将提高那些与“分布式性质”完全匹配的文档的得分,而不需要任何斜率。

代码语言:javascript
复制
POST demo_idx/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "content": {
              "query": "Distributed nature"
            }
          }
        },
        {
          "match_phrase": {
            "content": {
              "query": "Distributed nature",
              "slop": 2
            }
          }
        }
      ]
    }
  }
}

搜索响应将是:

代码语言:javascript
复制
"hits" : [
      {
        "_index" : "demo_idx",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.45899627,
        "_source" : {
          "content" : "Distributed nature, simple REST APIs, speed, and scalability"
        }
      },
      {
        "_index" : "demo_idx",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.30803072,
        "_source" : {
          "content" : "Distributed nature, simple REST APIs, speed, and scalability, Elasticsearch is the central component of the Elastic Stack, the end"
        }
      },
      {
        "_index" : "demo_idx",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.15556586,
        "_source" : {
          "content" : "Distributed tmp tmp nature"
        }
      },
      {
        "_index" : "demo_idx",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.14397088,
        "_source" : {
          "content" : "Distributed tmp nature, simple REST APIs, speed, and scalability"
        }
      }
    ]

更新1:

为了避免搜索查询评分中“字段长度”参数的影响,需要使用更新映射API禁用“内容”字段的“规范”参数

代码语言:javascript
复制
PUT demo_idx/_mapping
{
  "properties": {
    "content": {
      "type": "text",
      "norms": "false"
    }
  }
}

之后,重新编制文档索引,这样规范就不会立即被删除。

现在点击搜索查询,搜索响应将按照您希望得到的顺序进行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71652540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档