0

我在elasticsearch作爲一個例子批量文件我已經採取了elasticsearch文檔例如銀行
聚集在特定的參數elasticsearch

{ 
"_index": "bank", 
"_type": "account", 
"_id": "25", 
"_score": 1, 
"_source": { 
"account_number": 25, 
"balance": 40540, 
"firstname": "Virginia", 
"lastname": "Ayala", 
"age": 39, 
"gender": "F", 
"address": "171 Putnam Avenue", 
"employer": "Filodyne", 
"email": "[email protected]", 
"city": "Nicholson", 
"state": "PA" 
} 
} 
, 
{ 
"_index": "bank", 
"_type": "account", 
"_id": "44", 
"_score": 1, 
"_source": { 
"account_number": 44, 
"balance": 34487, 
"firstname": "Aurelia", 
"lastname": "Harding", 
"age": 37, 
"gender": "M", 
"address": "502 Baycliff Terrace", 
"employer": "Orbalix", 
"email": "[email protected]", 
"city": "Yardville", 
"state": "DE" 
} 
} 
, 
{ 
"_index": "bank", 
"_type": "account", 
"_id": "99", 
"_score": 1, 
"_source": { 
"account_number": 99, 
"balance": 47159, 
"firstname": "Ratliff", 
"lastname": "Heath", 
"age": 39, 
"gender": "F", 
"address": "806 Rockwell Place", 
"employer": "Zappix", 
"email": "[email protected]", 
"city": "Shaft", 
"state": "ND" 
} 
} 
, 
{ 
"_index": "bank", 
"_type": "account", 
"_id": "119", 
"_score": 1, 
"_source": { 
"account_number": 119, 
"balance": 49222, 
"firstname": "Laverne", 
"lastname": "Johnson", 
"age": 28, 
"gender": "F", 
"address": "302 Howard Place", 
"employer": "Senmei", 
"email": "[email protected]", 
"city": "Herlong", 
"state": "DC" 
} 
} 
, 
{ 
"_index": "bank", 
"_type": "account", 
"_id": "126", 
"_score": 1, 
"_source": { 
"account_number": 126, 
"balance": 3607, 
"firstname": "Effie", 
"lastname": "Gates", 
"age": 39, 
"gender": "F", 
"address": "620 National Drive", 
"employer": "Digitalus", 
"email": "[email protected]", 
"city": "Blodgett", 
"state": "MD" 
} 
} 


現在有一個字段名爲狀態,價格在每個文檔中。
如何編寫一個查詢,它只返回包含排序順序爲asc順序的平衡狀態的不同狀態的結果。

我在嘗試使用術語聚合,但沒用。
UPDATE

POST _search 
{ 
    "size": 0, 
    "aggs": { 
     "states": { 
      "terms": { 
       "field": "state" 
      }, 
      "aggs": { 
       "balances": { 
        "top_hits": { 
         "from" : 0, 
         "size": 1, 
         "sort": {"balance": "asc"} 
        } 
       } 
      } 
     } 
    } 
} 


現在此查詢我將與所有頂級命中與價格是關鍵「狀態」進行排序返回。但我想要的是一個排序結果w.r.t餘額和獨特的狀態字段。
對於上面的查詢,我得到的響應如下

"buckets": [ 
      { 
       "key": "tx", 
       "doc_count": 30, 
       "balances": { 
        "hits": { 
        "total": 30, 
        "max_score": null, 
        "hits": [ 
         { 
          "_index": "bank", 
          "_type": "account", 
          "_id": "161", 
          "_score": null, 
          "_source": { 
           "account_number": 161, 
           "balance": 4659, 
           "firstname": "Doreen", 
           "lastname": "Randall", 
           "age": 37, 
           "gender": "F", 
           "address": "178 Court Street", 
           "employer": "Calcula", 
           "email": "[email protected]", 
           "city": "Belmont", 
           "state": "TX" 
          }, 
          "sort": [ 
           4659 
          ] 
         } 
        ] 
        } 
       } 
      }, 
      { 
       "key": "md", 
       "doc_count": 28, 
       "balances": { 
        "hits": { 
        "total": 28, 
        "max_score": null, 
        "hits": [ 
         { 
          "_index": "bank", 
          "_type": "account", 
          "_id": "527", 
          "_score": null, 
          "_source": { 
           "account_number": 527, 
           "balance": 2028, 
           "firstname": "Carver", 
           "lastname": "Peters", 
           "age": 35, 
           "gender": "M", 
           "address": "816 Victor Road", 
           "employer": "Housedown", 
           "email": "[email protected]", 
           "city": "Nadine", 
           "state": "MD" 
          }, 
          "sort": [ 
           2028 
          ] 
         } 
        ] 
        } 
       } 
      }, 
      { 
       "key": "id", 
       "doc_count": 27, 
       "balances": { 
        "hits": { 
        "total": 27, 
        "max_score": null, 
        "hits": [ 
         { 
          "_index": "bank", 
          "_type": "account", 
          "_id": "402", 
          "_score": null, 
          "_source": { 
           "account_number": 402, 
           "balance": 1282, 
           "firstname": "Pacheco", 
           "lastname": "Rosales", 
           "age": 32, 
           "gender": "M", 
           "address": "538 Pershing Loop", 
           "employer": "Circum", 
           "email": "[email protected]", 
           "city": "Elbert", 
           "state": "ID" 
          }, 
          "sort": [ 
           1282 
          ] 
         } 
        ] 
        } 
       } 
      }, 

這是不是在價格排列。

回答

0

嘗試這樣的:

POST bank/_search 
{ 
    "size": 0, 
    "aggs": { 
     "states": { 
      "terms": { 
       "field": "state", 
       "order": { 
        "balances": "asc" 
       } 
      }, 
      "aggs": { 
       "balances": { 
        "sum": { 
         "field": "balance" 
        } 
       } 
      } 
     } 
    } 
} 

注:我沒有看到一個price場,但balance一個,也許這就是你的意思是一個。

如果您有興趣參與由按價格排列狀態的所有文件,那麼你可以試試這個,太:

POST bank/_search 
{ 
    "size": 0, 
    "aggs": { 
     "states": { 
      "terms": { 
       "field": "state" 
      }, 
      "aggs": { 
       "balances": { 
        "top_hits": { 
         "size": 5, 
         "sort": {"balance": "asc"} 
        } 
       } 
      } 
     } 
    } 
} 
+0

我需要不同的狀態值 但我仍然得到重複...! ! – Rockon

+0

你能說說你得到了哪些「重複」嗎?你能否在你的問題中展示你期望的迴應? – Val

+0

我沒有得到明確的值 您可以嘗試此示例。它是索引爲銀行的電子文檔文檔中最常用的例子。我需要我的結果來包含不同的狀態值。 – Rockon