我的索引中包含以下单词:“Kem,Kemi,Kemah,Kemer,Kemerburgaz,Kemang,Kembs,Kemnay,Kempley,Kempsey,Kemerovo”。

当我搜索“Kem”时,我希望“Kemi”排在最前面,因为它是最接近的词。 (Kem + i = Kemi)。但这并没有我想要的方式。

指数:

{
"settings": {
    "number_of_shards": 1,
    "analysis": {
    "filter": {
        "autocomplete_filter": {
        "type": "edge_ngram",
        "min_gram": 2,
        "max_gram": 15
        }
    },
    "analyzer": {
        "autocomplete": {
        "type": "custom",
        "tokenizer": "standard",
        "filter": [
            "lowercase",
            "autocomplete_filter"
        ]
        }
    }
    }
},
"mappings": {
    "_doc": {
    "properties": {
        "name": {
            "fields": {
                "keyword": {
                    "type": "keyword"
                }
            },
        "type": "text",
        "similarity": "classic",
        "analyzer": "autocomplete",
        "search_analyzer": "standard"
        },
        "id": {
            "type": "keyword"
        },
        "slug": {
            "type": "keyword"
        },
        "type": {
            "type": "keyword"
        }
    }
    }
}
}

查询:
{
"from" : 0, "size" : 10,
"query": {
    "bool": {
    "must": [
        {
        "match": {
            "name": "Kem"
        }
        }
    ],
    "should": [
        {
        "term": {
            "name.keyword": {
            "value": "Kem"
            }
        }
        }
    ]
    }
}
}
'

结果:
{
"took" : 6,
"timed_out" : false,
"_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
},
"hits" : {
    "total" : 143,
    "max_score" : 20.795834,
    "hits" : [
    {
        "_index" : "destinations",
        "_type" : "_doc",
        "_id" : "lPL8Y2YBqxTX_xwrZlGc",
        "_score" : 20.795834,
        "_source" : {
        "id" : "c6317201",
        "name" : "Kem",
        "slug" : "yurtdisi/karelya-cumhuriyeti/kem"
        }
    },
    {
        "_index" : "destinations",
        "_type" : "_doc",
        "_id" : "se78Y2YBqxTX_xwrVFIU",
        "_score" : 8.61574,
        "_source" : {
        "id" : "c121023",
        "name" : "Kemah",
        "slug" : "yurtdisi/houston-ve-civari/kemah"
        }
    },
    {
        "_index" : "destinations",
        "_type" : "_doc",
        "_id" : "ze78Y2YBqxTX_xwrVFo5",
        "_score" : 8.61574,
        "_source" : {
        "id" : "c1783",
        "name" : "Kemerovo",
        "slug" : "yurtdisi/kemerovo-oblasti/kemerovo"
        }
    },
    {
        "_index" : "destinations",
        "_type" : "_doc",
        "_id" : "xe78Y2YBqxTX_xwrVFs9",
        "_score" : 8.61574,
        "_source" : {
        "id" : "c1786",
        "name" : "Kemi",
        "slug" : "yurtdisi/rovaniemi/kemi"
        }
    },
    {
        "_index" : "destinations",
        "_type" : "_doc",
        "_id" : "Tu78Y2YBqxTX_xwrVG-X",
        "_score" : 8.61574,
        "_source" : {
        "id" : "c1900",
        "name" : "Kempsey",
        "slug" : "yurtdisi/new-south-wales/kempsey"
        }
    },
    {
        "_index" : "destinations",
        "_type" : "_doc",
        "_id" : "Bu78Y2YBqxTX_xwrVILt",
        "_score" : 8.61574,
        "_source" : {
        "id" : "c3000010982",
        "name" : "Kempley",
        "slug" : "yurtdisi/dymock/kempley"
        }
    },
    {
        "_index" : "destinations",
        "_type" : "_doc",
        "_id" : "B-78Y2YBqxTX_xwrVILt",
        "_score" : 8.61574,
        "_source" : {
        "id" : "c3000010983",
        "name" : "Kemnay",
        "slug" : "yurtdisi/inverurie/kemnay"
        }
    },
    {
        "_index" : "destinations",
        "_type" : "_doc",
        "_id" : "CO78Y2YBqxTX_xwrVIb_",
        "_score" : 8.61574,
        "_source" : {
        "id" : "c3000013079",
        "name" : "Kemerburgaz",
        "slug" : "eyup/kemerburgaz"
        }
    },
    {
        "_index" : "destinations",
        "_type" : "_doc",
        "_id" : "-fL8Y2YBqxTX_xwrZQxf",
        "_score" : 8.61574,
        "_source" : {
        "id" : "c6190744",
        "name" : "Kembs",
        "slug" : "yurtdisi/haut-rhin-bolge/kembs"
        }
    },
    {
        "_index" : "destinations",
        "_type" : "_doc",
        "_id" : "xfL8Y2YBqxTX_xwrZSG-",
        "_score" : 8.61574,
        "_source" : {
        "id" : "c6216986",
        "name" : "Kemang",
        "slug" : "yurtdisi/cakarta/kemang"
        }
    }
    ]
}
}

现在他们得分相同,因为我猜每个人都有“Kem”。但是,如果我执行“match”或“match_phrase”,结果将是相同的。

最佳答案

在您的示例中,您似乎希望结果按长度排序。您可以使用脚本来做到这一点。

POST your_index/_doc/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "Kem"
          }
        }
      ],
      "should": [
        {
          "term": {
            "name.keyword": {
              "value": "Kem"
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "_score": {"order": "desc"}
    },
    {
      "_script": {
        "script": "doc['name.keyword'].value.length()",
        "type": "number",
        "order": "asc"
      }
    },
    {
      "name.keyword": {"order": "asc"}
    }
  ]
}

10-06 06:25