2017-08-17 36 views
1

我想添加這3個屬性(如+評論+分享),也想排序3個屬性的總和。如何在Elasticsearch中對「3個屬性的總和」進行排序?

curl -XPUT 'http://XXX.X.XX.XXX:XXXXX/stores/' -d '{ 
    "settings" : { 
     "index" : { 
      "number_of_shards" : 3, 
      "number_of_replicas" : 1 
     } 
    }, 
    "mappings" : { 
     "store" : { 
      "_all":{ "enabled": true }, 
      "properties":{ 
      "url" : { "type" : "string", "analyzer" : "simple", "boost" : 3 }, 
      "title" : { "type" : "string", "boost" : 2 }, 
      "description" : { "type" : "string" }, 
      "like":{"type":"long"}, 
      "comment":{"type":"long"}, 
      "share":{"type":"long"}, 
      "time_added" : { "type" : "integer", "index" : "not_analyzed", "include_in_all": true } 
      } 
     } 
    } }' 

在elasticsearch中做什麼查詢。 我elasticsearch版本是2.4.0

回答

2

嘗試以下查詢:

GET /index/_search 
{ 
    "query": { 
     "match_all": {} 
    }, 
    "sort": [ 
     { 
      "_script": { 
       "script": "doc['share'].value + doc['like'].value+doc['comment'].value", 
       "type": "number", 
       "order": "desc" 
      } 
     } 
    ] 
} 

查詢將獲取所有數據,並根據(share+like+comment)總價值降序排序。

如果要按升序排列sort,請將order值更改爲asc

要運行script查詢,您需要在config/elasticsearch.yml文件中添加script.engine.groovy.inline.search: on

+0

感謝sunkuet02的快速回復,你能幫我找到「config/elasticsearch.yml」路徑...其中的yml文件。 – Ganesh

+0

你是如何安裝elasticsearch的? – sunkuet02

+1

如果您使用的是Ubuntu,並且如果您從'.zip'文件運行它,那麼它在'config/elasticsearch.yml'中,您可以在'/ etc/elasticsearch/elasticsearch.yml'中找到它。如果您使用Windows,則轉到安裝文件夾。 – sunkuet02

0

上面的answare是正確的,但它是得到現場錯誤,所以打擊查詢是正確的。

curl -XGET "localhost:9200/stores/store/_search?pretty=true&size=3" -d ' 
{ 
    "query": { 
     "match_all": {} 
    }, 
    "sort": [ 
     { 
      "_script": { 
       "script": "doc['"'share'"'].value + doc['"'like'"'].value+doc['"'comment'"'].value", 
       "type": "number", 
       "order": "desc" 
      } 
     } 
    ] 
}' 
相關問題