首页 文章

Elasticsearch首先返回完全匹配,然后返回其他匹配

提问于
浏览
2

我有一些 PageDocument s我想根据 Headers 进行搜索,不包括带有一些特定文本的路径的 PageDocument . 分析该字段 . 我想要一些模糊来帮助用户解决拼写错误 . 我需要能够进行部分匹配,所以 some 将匹配 some textthis is some text .

如果我使用以下查询,由于tf-idf,我没有得到完全匹配作为第一个结果

{
  "size": 20,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": {
              "query": "myterm",
              "fuzziness": 1
            }
          }
        }
      ],
      "must_not": [
        {
          "wildcard": {
            "path": {
              "value": "/test/*"
            }
          }
        }
      ]
    }
  }
}

所以我在 title.not_analyzed 添加了 not_analyzed 版本的 Headers 字段,并尝试使用 term 添加功能分数来增加完全匹配的权重 .

{
  "query": {
    "function_score": {
      "functions": [
        {
          "weight": 2,
          "filter": {
            "fquery": {
              "query": {
                "term": {
                  "title.not_analyzed": {
                    "value": "myterm"
                  }
                }
              }
            }
          }
        }
      ],
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "title": {
                  "query": "myterm",
                  "fuzziness": 1
                }
              }
            }
          ],
          "must_not": [
            {
              "wildcard": {
                "path": {
                  "value": "/path/*"
                }
              }
            }
          ]
        }
      },
      "boost_mode": "multiply"
    }
  }
}

但这给了我相同的结果 . 如何获得先返回的完全匹配?

1 回答

  • 1

    我们通过添加 shouldboost 的组合找到了解决方案 .

    {
      "size": 20,
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "title": {
                  "query": "myterm",
                  "fuzziness": 1
                }
              }
            }
          ],
          "must_not": [
            {
              "wildcard": {
                "path": {
                  "value": "/path/*"
                }
              }
            }
          ],
          "should": [
            {
              "term": {
                "title": {
                  "value": "myterm",
                  "boost": 10
                }
              }
            }
          ]
        }
      }
    }
    

相关问题