如何按DESC中的确定字段对Elasticsearch查询结果进行排序?

时间:2017-08-10 08:32:42

标签: sorting elasticsearch

假设我有以下问题:

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的sort函数和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
              }
            }
          }
        }
      }
    }
  }
}

2 个答案:

答案 0 :(得分:0)

首先,您需要这样的良好映射:

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

答案 1 :(得分:0)

谢谢大家。管理以使其使用此查询:

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

不需要aditional mapping。