【问题标题】:Elasticsearch is not sorting the resultsElasticsearch 没有对结果进行排序
【发布时间】:2016-04-02 07:52:18
【问题描述】:

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

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

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

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

{
  "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 }
    }
  }
}

提前致谢!

【问题讨论】:

  • 你得到了什么结果,你期望什么?另请说明您是如何发送查询的(curl、Java、Python、Sense 等)?
  • 谢谢,我正在使用 python (elasticsearch-dsl.readthedocs.org/en/latest)。问题是弹性搜索总是返回相同的结果。查询部分可以正常工作,但是对于 asc 和 desc 订单返回相同的列表。
  • 你能展示你正在使用的python代码吗?
  • title字段的映射是什么?
  • 编辑了更多细节。谢谢。

标签: sorting elasticsearch elasticsearch-dsl elasticsearch-py


【解决方案1】:

你文档中的"title"字段是一个已分析字符串字段,也是一个多值字段,也就是说elasticsearch会分裂将该字段的内容转换为标记,并将其单独存储在索引中。 您可能希望在第一个词条上按字母顺序对 "title" 字段进行排序,然后按第二个词条进行排序,依此类推,但在排序时,elasticsearch 没有此信息可供使用。 p>

因此,您可以从以下位置更改 “title” 字段的映射:

{
  "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"
      }
    }
  }
}

现在根据 analyzed "title" 字段执行您的搜索,并根据 not_analyzed “title.raw”字段

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

这里解释得很漂亮:String Sorting and Multifields

【讨论】:

  • 谢谢。这正是问题所在。现在正在正常工作。你拯救了我的一天!。
  • 从 ES v. 5 开始,输入“文本”而不​​是“字符串”(用于接受的答案)。 stackoverflow.com/questions/47452770/…
猜你喜欢
  • 1970-01-01
  • 2016-05-21
  • 2021-10-21
  • 2019-07-12
  • 1970-01-01
  • 2014-02-21
  • 2018-07-09
  • 2019-04-02
  • 1970-01-01
相关资源
最近更新 更多