简体   繁体   English

Elasticsearch 按文本字段关键字排序

[英]Elasticsearch sort by text field keyword

I have index with this settings我有这个设置的索引

"analysis": {
  "filter": {
    "autocomplete_filter": {
      "type": "edge_ngram",
      "min_gram": 1,
      "max_gram": 10
    }
  },
  "analyzer": {
    "autocomplete": {
      "type": "custom",
      "tokenizer": "standard",
      "filter": [
        "lowercase",
        "autocomplete_filter",
        "asciifolding",
        "elision",
        "standard"
      ]
    },
    "autocomplete_search": {
      "tokenizer": "lowercase"
    }
  },
  "tokenizer": {
    "autocomplete": {
      "type": "edge_ngram",
      "min_gram": "3",
      "max_gram": "32"
    }
  }
}

and have mapping for the name field并具有名称字段的映射

"name": {
        "type": "text",
        "analyzer": "autocomplete",
        "search_analyzer": "autocomplete_search",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }

now I have several examples of names in documents.现在我有几个文档中的名称示例。 Name is one field with first name and last name inside.名称是一个包含名字和姓氏的字段。

  1. --макс--- --макс---
  2. -макс - -макс -
  3. {something} макс {某事} макс
  4. макс {something} макс {某事}

I am using this query to find the documents with that name with alphabetical sorting我正在使用此查询以按字母排序查找具有该名称的文档

{
  "query": {
    "match": {
      "name": {
        "query": "макс",
        "operator" : "and"
      }
    }
  },
    "sort": [
        {"name.keyword" : "asc"}
    ]
}

it is bringing results as I wrote.正如我所写的那样,它带来了结果。 but I expect that макс {something} will come for the first position than others because it is starting with a query which I wrote.但我希望 макс {something} 会出现在第一个 position 上,因为它是从我写的查询开始的。

Can somebody help be there有人可以帮忙吗

So the query is by default scoring documents based on "how well they matched", this score is used to rank the "best matches first".因此,默认情况下, query是根据“他们匹配的程度”对文档进行评分,这个分数用于对“最佳匹配优先”进行排名。 But as soon as you define an sort you are saying ignore the query score and only using this field to rank the results.但是一旦你定义了一个sort ,你就是说忽略查询分数,只使用这个字段来对结果进行排名。 Now the results are still restricted to only documents matching the query but the idea of best match is lost unless you keep the special value _score in your sort statement somewhere.现在结果仍然仅限于与查询匹配的文档,但最佳匹配的想法已经丢失,除非您将特殊值_score在排序语句中的某处。

Like this:像这样:

"sort": [
    {
      "productLine.keyword": {
        "order": "desc"
      }
    },
    {
      "_score": {
        "order": "desc"
      }
    }
  ]

Maybe you can just remove the sort and get the results you want based on default score sorting.也许您可以删除排序并根据默认分数排序获得所需的结果。 Include a few example documents to make this fully reproducible if you want more support from the SO community如果您需要来自 SO 社区的更多支持,请包含一些示例文档以使其完全可重现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM