2017-01-16 27 views
0

我使用elasticesearch來搜索城市信息,當我搜索省,我想要得到屬於這個省的城市列表。我搜索了很多關於如何搜索和分類存儲桶的答案,如aggs, script,而此解決方案只能按原始文檔中的字段進行排序。Elasticsearch如何基於sammple結果的排序桶max_score

我希望通過抽樣結果的max_score,這裏是我的查詢排序:

GET /config/city/_search? 
{ 
    "query": 
    { 
    "match":{ 
     "province":"湖北" 
    } 
    }, 
    "aggs": { 
     "city": { 
      "terms": { 
      "field": "city_name.keyword", 
      "order":{ 
       "_term": "desc" 
      } 
     }, 
     "aggs": { 
       "sample": { 
       "top_hits": { 
        "size": 1 
       } 
      } 
      } 
     } 
    } 
} 

查詢和AGGS結果是:

{ 
    "took": 5, 
    "timed_out": false, 
    "_shards": { 
    "total": 1, 
    "successful": 1, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 434, 
    "max_score": 8.002925, 
    "hits": [ 
     { 
     "_index": "config", 
     "_type": "city", 
     "_id": "56d55fc72513a6cc6b18d413", 
     "_score": 8.002925, 
     "_source": { 
      "province": "湖北省", 
      "updateTime": "2017-01-12T21:28:49.562065", 
      "region_name": "武漢市", 
      "phone_code": "027", 
      "area_code": "430000", 
      "city_name": "武漢市" 
     } 
     }, 
     { 
     "_index": "config", 
     "_type": "city", 
     "_id": "56d55fc72513a6cc6b18d414", 
     "_score": 8.002925, 
     "_source": { 
      "province": "湖北省", 
      "updateTime": "2017-01-12T21:28:49.577459", 
      "region_name": "江岸區", 
      "phone_code": "027", 
      "area_code": "430014", 
      "city_name": "武漢市" 
     } 
     }, 
     { 
     "_index": "config", 
     "_type": "city", 
     "_id": "56d55fc72513a6cc6b18d417", 
     "_score": 8.002925, 
     "_source": { 
      "province": "湖北省", 
      "updateTime": "2017-01-12T21:28:49.591009", 
      "region_name": "洪山區", 
      "phone_code": "027", 
      "area_code": "430070", 
      "city_name": "武漢市" 
     } 
     }, 
     { 
     "_index": "config", 
     "_type": "city", 
     "_id": "56d55fc72513a6cc6b18d418", 
     "_score": 8.002925, 
     "_source": { 
      "province": "湖北省", 
      "updateTime": "2017-01-12T21:28:49.621532", 
      "region_name": "漢南區", 
      "phone_code": "027", 
      "area_code": "430090", 
      "city_name": "武漢市" 
     } 
     }, 
     { 
     "_index": "config", 
     "_type": "city", 
     "_id": "56d55fc72513a6cc6b18d419", 
     "_score": 8.002925, 
     "_source": { 
      "province": "湖北省", 
      "updateTime": "2017-01-12T21:28:49.632309", 
      "region_name": "江夏區", 
      "phone_code": "027", 
      "area_code": "430200", 
      "city_name": "武漢市" 
     } 
     }, 
     { 
     "_index": "config", 
     "_type": "city", 
     "_id": "56d55fc72513a6cc6b18d41a", 
     "_score": 8.002925, 
     "_source": { 
      "province": "湖北省", 
      "updateTime": "2017-01-12T21:28:49.641164", 
      "region_name": "新洲區", 
      "phone_code": "027", 
      "area_code": "431400", 
      "city_name": "武漢市" 
     } 
     }, 
     { 
     "_index": "config", 
     "_type": "city", 
     "_id": "56d55fc72513a6cc6b18d41c", 
     "_score": 8.002925, 
     "_source": { 
      "province": "湖北省", 
      "updateTime": "2017-01-12T21:28:49.649542", 
      "region_name": "漢陽區", 
      "phone_code": "027", 
      "area_code": "430050", 
      "city_name": "武漢市" 
     } 
     }, 
     { 
     "_index": "config", 
     "_type": "city", 
     "_id": "56d55fc72513a6cc6b18d41d", 
     "_score": 8.002925, 
     "_source": { 
      "province": "湖北省", 
      "updateTime": "2017-01-12T21:28:49.659664", 
      "region_name": "青山區", 
      "phone_code": "027", 
      "area_code": "430080", 
      "city_name": "武漢市" 
     } 
     }, 
     { 
     "_index": "config", 
     "_type": "city", 
     "_id": "56d55fc72513a6cc6b18d41e", 
     "_score": 8.002925, 
     "_source": { 
      "province": "湖北省", 
      "updateTime": "2017-01-12T21:28:49.664932", 
      "region_name": "東西湖區", 
      "phone_code": "027", 
      "area_code": "430040", 
      "city_name": "武漢市" 
     } 
     }, 
     { 
     "_index": "config", 
     "_type": "city", 
     "_id": "56d55fc72513a6cc6b18d420", 
     "_score": 8.002925, 
     "_source": { 
      "province": "湖北省", 
      "updateTime": "2017-01-12T21:28:49.670879", 
      "region_name": "黃陂區", 
      "phone_code": "027", 
      "area_code": "432200", 
      "city_name": "武漢市" 
     } 
     } 
    ] 
    }, 
    "aggregations": { 
    "city": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 324, 
     "buckets": [ 
     { 
      "key": "黃石市", 
      "doc_count": 6, 
      "sample": { 
      "hits": { 
       "total": 6, 
       "max_score": 8.002925, 
       "hits": [ 
       { 
        "_index": "config", 
        "_type": "city", 
        "_id": "56d55fc72513a6cc6b18d422", 
        "_score": 8.002925, 
        "_source": { 
        "province": "湖北省", 
        "updateTime": "2017-01-12T21:28:49.681599", 
        "region_name": "黃石港區", 
        "phone_code": "0714", 
        "area_code": "435000", 
        "city_name": "黃石市" 
        } 
       } 
       ] 
      } 
      } 
     }, 
     { 
      "key": "黃岡市", 
      "doc_count": 11, 
      "sample": { 
      "hits": { 
       "total": 11, 
       "max_score": 8.002925, 
       "hits": [ 
       { 
        "_index": "config", 
        "_type": "city", 
        "_id": "56d55fc82513a6cc6b18d464", 
        "_score": 8.002925, 
        "_source": { 
        "province": "湖北省", 
        "updateTime": "2017-01-12T21:28:49.983465", 
        "region_name": "黃州區", 
        "phone_code": "0713", 
        "area_code": "438000", 
        "city_name": "黃岡市" 
        } 
       } 
       ] 
      } 
      } 
     }, 
     { 
      "key": "隨州市", 
      "doc_count": 4, 
      "sample": { 
      "hits": { 
       "total": 4, 
       "max_score": 8.002925, 
       "hits": [ 
       { 
        "_index": "config", 
        "_type": "city", 
        "_id": "56d55fc82513a6cc6b18d475", 
        "_score": 8.002925, 
        "_source": { 
        "province": "湖北省", 
        "updateTime": "2017-01-12T21:28:50.052454", 
        "region_name": "隨州市", 
        "phone_code": "0722", 
        "area_code": "441300", 
        "city_name": "隨州市" 
        } 
       } 
       ] 
      } 
      } 
     }, 
     { 
      "key": "長沙市", 
      "doc_count": 10, 
      "sample": { 
      "hits": { 
       "total": 10, 
       "max_score": 2.4707317, 
       "hits": [ 
       { 
        "_index": "config", 
        "_type": "city", 
        "_id": "56d55fc22513a6cc6b18ce38", 
        "_score": 2.4707317, 
        "_source": { 
        "province": "湖南省", 
        "updateTime": "2017-01-12T21:28:44.071826", 
        "region_name": "長沙市", 
        "phone_code": "0731", 
        "area_code": "410000", 
        "city_name": "長沙市" 
        } 
       } 
       ] 
      } 
      } 
     }, 
     { 
      "key": "鄂州市", 
      "doc_count": 4, 
      "sample": { 
      "hits": { 
       "total": 4, 
       "max_score": 8.002925, 
       "hits": [ 
       { 
        "_index": "config", 
        "_type": "city", 
        "_id": "56d55fc82513a6cc6b18d448", 
        "_score": 8.002925, 
        "_source": { 
        "province": "湖北省", 
        "updateTime": "2017-01-12T21:28:49.853779", 
        "region_name": "鄂州市", 
        "phone_code": "0711", 
        "area_code": "436000", 
        "city_name": "鄂州市" 
        } 
       } 
       ] 
      } 
      } 
     }, 
     { 
      "key": "郴州市", 
      "doc_count": 12, 
      "sample": { 
      "hits": { 
       "total": 12, 
       "max_score": 2.4707317, 
       "hits": [ 
       { 
        "_index": "config", 
        "_type": "city", 
        "_id": "56d55fc32513a6cc6b18ce8c", 
        "_score": 2.4707317, 
        "_source": { 
        "province": "湖南省", 
        "updateTime": "2017-01-12T21:28:44.354917", 
        "region_name": "郴州市", 
        "phone_code": "0735", 
        "area_code": "423000", 
        "city_name": "郴州市" 
        } 
       } 
       ] 
      } 
      } 
     }, 
     { 
      "key": "邵陽市", 
      "doc_count": 13, 
      "sample": { 
      "hits": { 
       "total": 13, 
       "max_score": 2.4707317, 
       "hits": [ 
       { 
        "_index": "config", 
        "_type": "city", 
        "_id": "56d55fc32513a6cc6b18ce61", 
        "_score": 2.4707317, 
        "_source": { 
        "province": "湖南省", 
        "updateTime": "2017-01-12T21:28:44.186168", 
        "region_name": "北塔區", 
        "phone_code": "0739", 
        "area_code": "422000", 
        "city_name": "邵陽市" 
        } 
       } 
       ] 
      } 
      } 
     }, 
     { 
      "key": "邯鄲市", 
      "doc_count": 20, 
      "sample": { 
      "hits": { 
       "total": 20, 
       "max_score": 2.3039548, 
       "hits": [ 
       { 
        "_index": "config", 
        "_type": "city", 
        "_id": "56d55fc52513a6cc6b18d12c", 
        "_score": 2.3039548, 
        "_source": { 
        "province": "河北省", 
        "updateTime": "2017-01-12T21:28:46.892258", 
        "region_name": "邯鄲市", 
        "phone_code": "0310", 
        "area_code": "56000", 
        "city_name": "邯鄲市" 
        } 
       } 
       ] 
      } 
      } 
     }, 
     { 
      "key": "邢臺市", 
      "doc_count": 20, 
      "sample": { 
      "hits": { 
       "total": 20, 
       "max_score": 2.3039548, 
       "hits": [ 
       { 
        "_index": "config", 
        "_type": "city", 
        "_id": "56d55fc52513a6cc6b18d140", 
        "_score": 2.3039548, 
        "_source": { 
        "province": "河北省", 
        "updateTime": "2017-01-12T21:28:46.964727", 
        "region_name": "邢臺市", 
        "phone_code": "0319", 
        "area_code": "54000", 
        "city_name": "邢臺市" 
        } 
       } 
       ] 
      } 
      } 
     }, 
     { 
      "key": "襄樊市", 
      "doc_count": 10, 
      "sample": { 
      "hits": { 
       "total": 10, 
       "max_score": 8.002925, 
       "hits": [ 
       { 
        "_index": "config", 
        "_type": "city", 
        "_id": "56d55fc72513a6cc6b18d43e", 
        "_score": 8.002925, 
        "_source": { 
        "province": "湖北省", 
        "updateTime": "2017-01-12T21:28:49.806962", 
        "region_name": "襄樊市", 
        "phone_code": "0710", 
        "area_code": "441000", 
        "city_name": "襄樊市" 
        } 
       } 
       ] 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

我希望能得到AGGS鬥其排序max_score或`_score

回答

0

我已經解決了這個問題,這是我的新查詢:

GET config/city/_search 
{ 
    "query": { 
    "match": { 
     "province":"湖北省" 
    } 
    }, 
    "size": 1, 
    "aggs": { 
    "city": { 
     "terms": { 
     "field": "city_name.keyword", 
     "size": 10, 
     "order": [ 
     { 
      "max_score_cap": "desc" 
     }, 
     { 
      "max_code": "asc" 
     } 
     ] 
     }, 
     "aggs": { 
     "top_hit":{ 
      "top_hits": { 
       "size": 1 
      } 
     }, 
     "max_score_cap":{ 
      "max":{ 
      "script": { 
       "lang": "painless", 
       "inline": "_score" 
      } 
      } 
     }, 
     "max_code":{ 
      "max":{ 
      "field": "area_code" 
      } 
     } 
     } 
    } 
    } 
} 
相關問題