Elasticsearch как сортировать ведро на основе max_score результата sammple

Я использую 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": "襄樊市"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

Я надеюсь получить ведро aggs, которые сортируют по max_scoreor’Score

1 ответ

  1. Я решил эту проблему, это мой новый запрос:

    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"
              }
            }
          }
        }
      }
    }