Elasticsearch如何根据sammple结果的max_score对存储桶进行排序

时间:2017-01-16 06:25:42

标签: elasticsearch

我正在使用elasticesearch搜索城市信息,当我搜索省时,我想获得属于这个省的城市列表。我已经搜索了很多关于如何搜索和排序存储桶的答案,例如aggs, script,而此解决方案只能按原始文档中的字段排序。

我希望按示例结果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": "襄樊市"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

我希望获得按max_score或`_score

排序的aggs存储桶

1 个答案:

答案 0 :(得分: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"
          }
        }
      }
    }
  }
}