我需要返回具有相同myField值、重复范围时间的文档的ids。也就是说,我有clientName字段,并且希望找到所有文档的in,其中clientName = 'John‘,但前提是这个名称在2-3个文档中找到。
我有以下查询
_search?filter_path=hits.total,hits.hits._id
"query": {
some filters that return matching document ids
},
"post_filter": {
"bool": {
"should": [
{
"range": {
"myField": {
"lte": 3,
"gte": 2
}
}
}
],
"minimum_should_match": 1
}
}但是它返回myField值的范围,而不是myField值计数。我尝试使用聚合,但聚合筛选器不适用于主要查询结果。如何修改我的post_filter以获得所需的结果?
发布于 2020-03-15 06:50:34
Post_filter:
post_filter应用于搜索请求末尾的搜索命中,在已经计算了聚合之后。
不能根据查询或post_filter中字段的出现次数筛选文档,也不能使用聚合结果过滤查询中的文档。
解决这个问题的方法有两种
1.获取给定次数的项,并搜索这些项的文档(2次对弹性搜索的调用)
2.使用 顶部获取聚合文档本身
制图:
{
"index48" : {
"mappings" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}查询:
{
"size": 0,
"aggs": {
"NAME": {
"terms": {
"field": "name.keyword",
"size": 10
},
"aggs": {
"documents": {
"top_hits": {
"size": 10
}
},
"bucket_count": {
"bucket_selector": {
"buckets_path": {
"path": "_count"
},
"script": "if(params.path>=1 && params.path<=3) return true"
}
}
}
}
}
}结果:
"aggregations" : {
"NAME" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "John",
"doc_count" : 3,
"documents" : {
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "index48",
"_type" : "_doc",
"_id" : "ZRPC3HABF9RqmGpImxj_",
"_score" : 1.0,
"_source" : {
"name" : "John"
}
},
{
"_index" : "index48",
"_type" : "_doc",
"_id" : "ZhPC3HABF9RqmGpIpBh4",
"_score" : 1.0,
"_source" : {
"name" : "John"
}
},
{
"_index" : "index48",
"_type" : "_doc",
"_id" : "ZxPC3HABF9RqmGpIqRhj",
"_score" : 1.0,
"_source" : {
"name" : "John"
}
}
]
}
}
},
{
"key" : "Doe",
"doc_count" : 2,
"documents" : {
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "index48",
"_type" : "_doc",
"_id" : "aBPC3HABF9RqmGpIyhhU",
"_score" : 1.0,
"_source" : {
"name" : "Doe"
}
},
{
"_index" : "index48",
"_type" : "_doc",
"_id" : "aRPC3HABF9RqmGpIzhh7",
"_score" : 1.0,
"_source" : {
"name" : "Doe"
}
}
]
}
}
}
]
}
}https://stackoverflow.com/questions/60686769
复制相似问题