【问题标题】:Elasticsearch return exact match first then other matchesElasticsearch首先返回完全匹配然后其他匹配
【发布时间】:2017-01-27 16:20:14
【问题描述】:

我有一些PageDocuments,我想根据标题进行搜索,不包括路径以某些特定文本开头的PageDocuments。分析该字段。我想要一些模糊性来帮助用户解决拼写错误。我需要能够进行部分匹配,所以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 版本,并尝试使用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"
    }
  }
}

但这给了我相同的结果。我怎样才能得到最先返回的精确匹配?

【问题讨论】:

    标签: elasticsearch


    【解决方案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
                }
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      • 2014-08-23
      • 1970-01-01
      相关资源
      最近更新 更多