Elasticsearch未对结果进行排序 [英] Elasticsearch is not sorting the results

查看:181
本文介绍了Elasticsearch未对结果进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Elasticsearch查询时遇到问题. 我希望能够对结果进行排序,但elasticsearch忽略了排序标签.这是我的查询:

{
    "sort": [{
        "title": {"order": "desc"}
    }],
    "query":{
        "term": { "title": "pagos" }
    }
}

但是,当我删除查询部分并且仅发送sort标记时,它可以工作. 谁能指出正确的方法?

我还尝试了以下查询,这是我所拥有的完整查询:

{
  "sort": [{
    "title": {"order": "asc"}
  }],
  "query":{
    "bool":{
      "should":[
        {
          "match":{
            "title":{
              "query":"Pagos",
              "boost":9
            }
          }
        },
        {
          "match":{
            "description":{
              "query":"Pagos",
              "boost":5
            }
          }
        },
        {
          "match":{
            "keywords":{
              "query":"Pagos",
              "boost":3
            }
          }
        },
        {
          "match":{
            "owner":{
              "query":"Pagos",
              "boost":2
            }
          }
        }
      ]
    }
  }
}

设置

{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "ngram",
          "min_gram": 3,
          "max_gram": 15,
          "token_chars": [
            "letter",
            "digit",
            "punctuation",
            "symbol"
          ]
        }
      },
      "analyzer": {
        "default" : {
          "tokenizer" : "standard",
          "filter" : ["standard", "lowercase", "asciifolding"]
        },
        "autocomplete": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "asciifolding",
            "autocomplete_filter"
          ]
        }
      }
    }
  }
}

映射

{
  "objects": {
    "properties": {
      "id":             { "type": "string", "index": "not_analyzed" },
      "type":           { "type": "string" },
      "title":          { "type": "string", "boost": 9, "analyzer": "autocomplete", "search_analyzer": "standard" },
      "owner":          { "type": "string", "boost": 2 },
      "description":    { "type": "string", "boost": 4 },
      "keywords":       { "type": "string", "boost": 1 }
    }
  }
}

提前谢谢!

解决方案

文档中的字段标题" 是经过 分析 的字符串字段,这也是一个多值字段,这意味着Elasticsearch会将字段的内容拆分为令牌,并将其分别存储在索引中. 您可能想按字母顺序在第一项中对标题" 字段进行排序,然后在第二项中进行排序,依此类推,但是elasticsearch在排序时没有可用的信息.

因此,您可以从以下位置更改标题" 字段的映射:

{
  "title": {
    "type": "string", "boost": 9, "analyzer": "autocomplete", "search_analyzer": "standard"
  }
}

到这样的多字段映射中:

{
  "title": {
    "type": "string", "boost": 9, "analyzer": "autocomplete", "search_analyzer":"standard",
    "fields": {
      "raw": {
        "type": "string",
        "index": "not_analyzed"
      }
    }
  }
}

现在根据 已分析 标题" 字段执行搜索,然后根据 not_analyzed 进行排序> "title.raw" 字段

{
    "sort": [{
        "title.raw": {"order": "desc"}
    }],
    "query":{
        "term": { "title": "pagos" }
    }
}

在此处进行了精美的解释:字符串排序和多字段

I'm having problem with an elasticsearch query. I want to be able to sort the results but elasticsearch is ignoring the sort tag. Here my query:

{
    "sort": [{
        "title": {"order": "desc"}
    }],
    "query":{
        "term": { "title": "pagos" }
    }
}

However, when I remove the query part and I send only the sort tag, it works. Can anyone point me out the correct way?

I also tried with the following query, which is the complete query that I have:

{
  "sort": [{
    "title": {"order": "asc"}
  }],
  "query":{
    "bool":{
      "should":[
        {
          "match":{
            "title":{
              "query":"Pagos",
              "boost":9
            }
          }
        },
        {
          "match":{
            "description":{
              "query":"Pagos",
              "boost":5
            }
          }
        },
        {
          "match":{
            "keywords":{
              "query":"Pagos",
              "boost":3
            }
          }
        },
        {
          "match":{
            "owner":{
              "query":"Pagos",
              "boost":2
            }
          }
        }
      ]
    }
  }
}

Settings

{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "ngram",
          "min_gram": 3,
          "max_gram": 15,
          "token_chars": [
            "letter",
            "digit",
            "punctuation",
            "symbol"
          ]
        }
      },
      "analyzer": {
        "default" : {
          "tokenizer" : "standard",
          "filter" : ["standard", "lowercase", "asciifolding"]
        },
        "autocomplete": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "asciifolding",
            "autocomplete_filter"
          ]
        }
      }
    }
  }
}

Mappings

{
  "objects": {
    "properties": {
      "id":             { "type": "string", "index": "not_analyzed" },
      "type":           { "type": "string" },
      "title":          { "type": "string", "boost": 9, "analyzer": "autocomplete", "search_analyzer": "standard" },
      "owner":          { "type": "string", "boost": 2 },
      "description":    { "type": "string", "boost": 4 },
      "keywords":       { "type": "string", "boost": 1 }
    }
  }
}

Thanks in advance!

解决方案

The field "title" in your document is an analyzed string field, which is also a multivalued field, which means elasticsearch will split the contents of the field into tokens and stores it separately in the index. You probably want to sort the "title" field alphabetically on the first term, then on the second term, and so forth, but elasticsearch doesn’t have this information at its disposal at sort time.

Hence you can change your mapping of the "title" field from:

{
  "title": {
    "type": "string", "boost": 9, "analyzer": "autocomplete", "search_analyzer": "standard"
  }
}

into a multifield mapping like this:

{
  "title": {
    "type": "string", "boost": 9, "analyzer": "autocomplete", "search_analyzer":"standard",
    "fields": {
      "raw": {
        "type": "string",
        "index": "not_analyzed"
      }
    }
  }
}

Now execute your search based on analyzed "title" field and sort based on the not_analyzed "title.raw" field

{
    "sort": [{
        "title.raw": {"order": "desc"}
    }],
    "query":{
        "term": { "title": "pagos" }
    }
}

It is beautifully explained here: String Sorting and Multifields

这篇关于Elasticsearch未对结果进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆