【问题标题】:How to sort an Elasticsearch query result by a determined field in DESC?如何按 DESC 中的确定字段对 Elasticsearch 查询结果进行排序?
【发布时间】:2017-08-10 17:25:27
【问题描述】:

假设我有以下查询:

curl -XGET 'localhost:9200/library/document/_search?pretty=true'

这将返回以下示例结果:

{
  "took" : 108,
  "timed_out" : false,
  "_shards" : {
     "total" : 5,
        "successful" : 5,
        "failed" : 0
},
"hits" : {
   "total" : 5,
   "max_score" : 1.0,
   "hits" : [
     {
       "_index" : "library",
       "_type" : "document",
       "_id" : "5",
       "_score" : 1.0,
       "_source" : {
         "page content" : [
           "Page 0:",
           "Page 1: something"
          ],
         "publish date" : "2015-12-05",
         "keywords" : "sample, example, article, alzheimer",
         "author" : "Author name",
         "language" : "",
         "title" : "Sample article",
         "number of pages" : 2
       }
     },
     {
       "_index" : "library",
       "_type" : "document",
       "_id" : "2",
       "_score" : 1.0,
       "_source" : {
        "page content" : [
           "Page 1: eBay",
           "Page 2: Paypal",
           "Page 3: Google"
         ],
         "publish date" : "2017-08-03",
         "keywords" : "something, another, thing",
         "author" : "Alex",
         "language" : "english",
         "title" : "Microsoft Word - TL0032.doc",
         "number of pages" : 21
       }
     },
     ...

我想按发布日期id(不同的查询)排序,以便最近的一个显示在列表的第一位。有可能吗?我知道我必须将 Elasticsearch 的排序功能与 DESC 参数一起使用。但不知何故,它对我不起作用。

编辑:字段映射

curl -XGET 'localhost:9200/library/_mapping/document?pretty'
{
  "library" : {
    "mappings" : {
      "document" : {
        "properties" : {
          "author" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "keywords" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "language" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "number of pages" : {
            "type" : "long"
          },
          "page content" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "publish date" : {
            "type" : "date"
          },
          "title" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

【问题讨论】:

  • 您想查看基于publish date 的最新消息,对吗?你能分享一下上述索引的映射吗?
  • @Yaswanth 编辑了它

标签: sorting elasticsearch


【解决方案1】:

首先你需要像这样的良好映射:

PUT my_index 
{
  "mappings": {
    "documents": {
      "properties": {
        "post_date" : {
          "type": "date"
          , "format": "yyyy-MM-dd HH:mm:ss"
        }
      }
    }
  }
}

然后搜索:

GET my_index/_search
{
  "sort": [
    {
      "post_date": {
        "order": "desc"
      }
    }
  ]
}

【讨论】:

    【解决方案2】:

    谢谢大家。设法让它与这个查询一起工作:

    curl -XGET 'localhost:9200/library/document/_search?pretty=true' -d '{"query": {"match_all": {}},"sort": [{"publish date": {"order": "desc"}}]}'
    

    不需要额外的映射。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      • 2012-04-09
      • 2011-07-06
      相关资源
      最近更新 更多