首页 > 解决方案 > ElasticSearch 6.8 不首先按完全匹配排序

问题描述

几天来我一直在寻找这种问题,但我没有成功。我遵循了这样的步骤没有成功。

所以基本上,我在 ElasticSearch 上有以下数据:

{ title: "Black Dust" },
{ title: "Dust In The Wind" },
{ title: "Gold Dust Woman" },
{ title: "Another One Bites The Dust" }

问题是我想按“灰尘”字搜索,我希望结果的排序如下:

{ title: "Dust In The Wind" },
{ title: "Black Dust" },
{ title: "Gold Dust Woman" },
{ title: "Another One Bites The Dust" }

其中“灰尘”必须出现在结果的顶部。

发布映射和查询比继续解释问题本身要好。

    settings: {
      analysis: {
        normalizer: {
          lowercase: {
            type: 'custom',
            filter: ['lowercase']
          }
        }
      }
    },
    mappings: {
      _doc: {
        properties: {
          title: {
            type: 'text',
            analyzer: 'standard',
            fields: {
              raw: {
                type: 'keyword',
                normalizer: 'lowercase'
              },
              fuzzy: {
                type: 'text',
              },
            },
          }
        }
      }
    }

我的查询是:

"query": {
    "bool": {
      "must": {
        "query_string": {
          "fields": [
            "title"
          ],
          "default_operator": "AND",
          "query": "dust"
        }
      },
      "should": {
        "prefix": {
          "title.raw": "dust"
        }
      }
    }
  }

有人可以帮我吗?谢谢!

解决方案!

我想通了,我通过执行以下查询来解决:

"query": {
    "bool": {
      "must": {
        "bool": {
          "should": [
            {
              "prefix": {
                "title.raw": {
                  "value": "dust",
                  "boost": 1000000
                }
              }
            },
            {
              "match": {
                "title": {
                  "query": "dust",
                  "boost": 50000
                }
              }
            },
            {
              "match": {
                "title": {
                  "query": "dust",
                  "boost": 10,
                  "fuzziness": 1
                }
              }
            }
          ]
        }
      }
    }
  }

但是,在编写测试时,我发现了一个小问题。因此,我正在生成一个随机数uuid并将以下内容添加到数据库中:

{ title: `${uuid} A` }
{ title: `${uuid} W` }
{ title: `${uuid} Z` }
{ title: `A ${uuid}` }
{ title: `z ${uuid}` }
{ title: `Z ${uuid}` }

当我执行上面的查询以查找 时uuid,我得到:

uuid Z
uuid A
uuid W
Z uuid

我实现了我的第一个目标,即获得uuid第一的位置,但为什么 Z 在 A 之前?(第一个和第二个结果)

标签: elasticsearch

解决方案


当其他一切都失败时,您可以使用一个简单的子字符串位置排序,如下所示:

{
  "query": {
    "bool": {
      "must": {
       ...
      },
      "should": {
        ...
      }
    }
  },
  "sort": [
    {
      "_script": {
        "script": "return doc['title.raw'].value.indexOf('dust')",
        "type": "number",
        "order": "asc"     <--
      }
    }
  ]
}

我将顺序设置为,asc因为子字符串索引越低,“分数”就越高。


编辑

我们必须考虑index == -1所以将上面的脚本替换为:

"script": "def pos = doc['title.raw'].value.indexOf('dust'); return pos == -1 ? Integer.MAX_VALUE : pos"

推荐阅读