2017-08-10 42 views
0

比方说,我有以下查询:如何根据DESC中的确定字段对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(不同querys)以便最近的一个在列表中首先显示。有可能吗?我知道我必须使用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 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

你想看到最近的基于'发布日期'的权利?你可以请分享以上索引的映射吗? – Seeker

+0

@Yaswanth编辑它 –

回答

0

谢谢大家。管理得到它与这个查询一起工作:

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

不需要aditional映射。

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" 
     } 
    } 
    ] 
} 
相关问题