2017-01-16 13 views
0

私はエジプトを検索して都道府県情報を検索していますが、私はこの州に属する都市リストを取得したいと考えています。 aggs, scriptのように、バケットの検索と並べ替えの方法に関する多くの回答を検索していますが、この解決策は元の文書のフィールドでのみ並べ替えることができます。私はここに、サンプル結果のmax_scoreでソートしたいElasticsearchサンプルの結果に基づいてバケツをソートする方法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" 
      } 
     } 
     } 
    } 
    } 
} 
関連する問題