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

Jim*_*ies 4 elasticsearch

我有一些PageDocument要根据标题搜索的,但不包括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/*"
            }
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

因此,我在处添加了not_analyzedtitle字段的版本,title.not_analyzed并尝试使用来添加函数得分以增加完全匹配的权重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"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但这给了我相同的结果。我如何才能首先获得完全匹配的结果?

Jim*_*ies 5

我们加入的组合找到了解决这个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
            }
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)