Aggregate deduplication query statistics in elastic search

Using json to implement the query function in elastic search is different from the sql language, and it is more complicated to understand, especially when there are some nested relationships. For some special query requirements, record them for convenience. 

1. Deduplicate query and display fields

Assume that the fields are userUUID, userName and score. There are a large number of repeated userUUIDs in the index, and each record has a score. Now we need to count the highest scores, but we need different users and display the user information at the same time. Go directly to the json code.

{
  "size": 0,
  "aggs": {
    "unique_users": {
      "terms": {
        "field": "userUUID.keyword",
        "size": 10,
        "order": {
          "max_score": "desc"
        }
      },
      "aggs": {
        "max_score": {
          "max": {
            "field": "score"
          }
        },

        "top_documents": {
          "top_hits": {
            "_source": {
              "includes": ["userUUID", "userName"]
            },
            "size": 1
 
          }
        }
      }
    }
  }
}

Possible output results:

{
	"took": 3,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 781,
			"relation": "eq"
		},
		"max_score": null,
		"hits": []
	},
	"aggregations": {
		"unique_users": {
			"doc_count_error_upper_bound": -1,
			"sum_other_doc_count": 584,
			"buckets": [
				{
					"key": "xxxxxxxxxxx",
					"doc_count": 176,
					"max_score": {
						"value": "137731.0"
					},
					"top_documents": {
						"hits": {
							"total": {
								"value": 176,
								"relation": "eq"
							},
							"max_score": "1.0",
							"hits": [
								{
									"_index": "xxx",
									"_id": "xxxxxx",
									"_score": "1.0",
									"_source": {
										"userUUID": "xxxxxxxxxxx"
									}
								}
							]
						}
					}
				},
				{
					"key": "xxxxxxxxxxxx",
					"doc_count": 21,
					"max_score": {
						"value": "129747.0"
					},
					"top_documents": {
						"hits": {
							"total": {
								"value": 21,
								"relation": "eq"
							},
							"max_score": "1.0",
							"hits": [
								{
									"_index": "xxx",
									"_id": "xxxxxx",
									"_score": "1.0",
									"_source": {
										"userUUID": "xxxxxxxxxxxxxxxx",
										"userName": "玩家7160"
									}
								}
							]
						}
					}
				}
			]
		}
	}
}

2. The highest score of aggregation statistics with conditions

The same as others, just increase the query conditions.

{
	"size": 0,
	"aggs": {
		"test": {
			"filter": {
				"range": {
					"score": {
						"gt": 12000
					}
				}
			},
			"aggs": {
				"userUUID": {
					"terms": {
						"field": "userUUID.keyword",
						"size": 10,
						"order": {
							"max_score": "desc"
						}
					},
					"aggs": {
						"max_score": {
							"max": {
								"field": "score"
							}
						},
						"top_ddocuments": {
						  
							"top_hits": {
							  "size": 1,
								"_source": {
									"includes": [
										"userUUID",
                                        "userName",
										"score"
									]
								}
								
							}
						}
					}
				}
			}
		}
	}
}

3. After deduplication according to id, count the total number of certain conditions

Because there is no need to return specific data, the top_hits segment is not considered, as follows:

{
	"query": {
		"bool": {
			"must": [
				{
					"range": {
						"score": {
							"gt": "132000"
						}
					}
				}
			]
		}
	},
	"from": 0,
	"size": 0,
	"aggregations": {
		"total_unique_count": {
			"cardinality": {
				"field": "userUUID.keyword"
			}
		}
	}
}

Possible feedback results are as follows:

{
	"took": 3,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 495,
			"relation": "eq"
		},
		"max_score": null,
		"hits": []
	},
	"aggregations": {
		"total_unique_count": {
			"value": 56
		}
	}
}

The following is an example of my game, you can try it:

 

Guess you like

Origin blog.csdn.net/a17432025/article/details/130434973