ES字段折叠查询

文档对该查询的描述如下:

Allows to collapse search results based on field values.

The collapsing is done by selecting only the top sorted document per collapse key.

查询示例:

其中满足 ip_addr==113.57.227.11 && user_id==83 这个条件的结果只有 6 条。


{
  "_source": [
    "ip",
    "port",
    "created_at",
    "updated_at"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "ip_addr": "113.57.227.11"
          }
        },
        {
          "term": {
            "user_id": 83
          }
        }
      ]
    }
  },
  "collapse": {
    "field": "ip_addr",
    "inner_hits": [
      {
        "name": "most_recent",
        "size":10,
        "sort": [
          {
            "updated_at": "desc"
          }
        ],
        "_source": [
          "ip",
          "port",
          "created_at",
          "updated_at"
        ]
      }
    ]
  }
}

以上查询结果如下:

可以看到在 inner_hits.most_recent 中将 6 条结果全部折叠显示出来了,并且包含了宿主 3306 端口的数据。


{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 6,
        "max_score": 11.963775,
        "hits": [
            {
                "_index": "gh_ip_test",
                "_type": "gh_ip",
                "_id": "1899619083330683",
                "_score": 11.963775,
                "_source": {
                    "updated_at": "2019-08-29 15:57:00",
                    "port": 3306,
                    "ip": "113.57.227.11",
                    "created_at": "2019-07-24 19:02:37"
                },
                "fields": {
                    "ip_addr": [
                        "113.57.227.11"
                    ]
                },
                "inner_hits": {
                    "most_recent": {
                        "hits": {
                            "total": 6,
                            "max_score": null,
                            "hits": [
                                {
                                    "_index": "gh_ip_test",
                                    "_type": "gh_ip",
                                    "_id": "1899619083338983",
                                    "_score": null,
                                    "_source": {
                                        "updated_at": "2019-08-29 15:58:08",
                                        "port": 3389,
                                        "ip": "113.57.227.11",
                                        "created_at": "2019-07-24 21:21:33"
                                    },
                                    "sort": [
                                        1567094288000
                                    ]
                                },
                                {
                                    "_index": "gh_ip_test",
                                    "_type": "gh_ip",
                                    "_id": "1899619083102583",
                                    "_score": null,
                                    "_source": {
                                        "updated_at": "2019-08-29 15:57:45",
                                        "port": 1025,
                                        "ip": "113.57.227.11",
                                        "created_at": "2019-07-24 20:50:03"
                                    },
                                    "sort": [
                                        1567094265000
                                    ]
                                },
                                {
                                    "_index": "gh_ip_test",
                                    "_type": "gh_ip",
                                    "_id": "1899619083102883",
                                    "_score": null,
                                    "_source": {
                                        "updated_at": "2019-08-29 15:57:20",
                                        "port": 1028,
                                        "ip": "113.57.227.11",
                                        "created_at": "2019-07-24 18:58:26"
                                    },
                                    "sort": [
                                        1567094240000
                                    ]
                                },
                                {
                                    "_index": "gh_ip_test",
                                    "_type": "gh_ip",
                                    "_id": "1899619083330683",
                                    "_score": null,
                                    "_source": {
                                        "updated_at": "2019-08-29 15:57:00",
                                        "port": 3306,
                                        "ip": "113.57.227.11",
                                        "created_at": "2019-07-24 19:02:37"
                                    },
                                    "sort": [
                                        1567094220000
                                    ]
                                },
                                {
                                    "_index": "gh_ip_test",
                                    "_type": "gh_ip",
                                    "_id": "1899619083102683",
                                    "_score": null,
                                    "_source": {
                                        "updated_at": "2019-08-29 15:56:20",
                                        "port": 1026,
                                        "ip": "113.57.227.11",
                                        "created_at": "2019-07-24 19:36:19"
                                    },
                                    "sort": [
                                        1567094180000
                                    ]
                                },
                                {
                                    "_index": "gh_ip_test",
                                    "_type": "gh_ip",
                                    "_id": "1899619083800983",
                                    "_score": null,
                                    "_source": {
                                        "updated_at": "2019-08-29 15:56:14",
                                        "port": 8009,
                                        "ip": "113.57.227.11",
                                        "created_at": "2019-07-25 16:22:04"
                                    },
                                    "sort": [
                                        1567094174000
                                    ]
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}