热门搜索中的弹性搜索过滤器

时间:2018-10-27 02:45:11

标签: elasticsearch

我在ES中有如下数据。我希望结果集具有部门1,因此只有位置55和60应该出现,而区域销售应该具有所有部门的总和$ 550。任何想法如何实现以上。非常感谢!

DivNbr |区| Locn |销量

1 | 616 | 55 | $ 100

2 | 616 | 61 | 200美元

1 | 616 | 60 | $ 250

{
"size": 0,
"aggs": {
  "by_district_id": {
    "terms": {
      "field": "district"
   },
  "aggs": {
      "top_session_hits": {
        "top_hits": {
          "_source": {
            "includes": [
              "locn",
              "sales"
             ]
          }
       }
      },
      "dist_sls": {
        "sum": {
          "field": "sales"
        }
      }
   }
  }
}

}

1 个答案:

答案 0 :(得分:0)

您可以添加Filter Aggregation,以便只有division 1可用的结果。

下面是查询及其示例响应

查询请求

POST <your_index_name>/_search
{  
   "size":0,
   "aggs":{  
      "by_district_id":{  
         "terms":{  
            "field":"district"
         },
         "aggs":{  
            "filter_agg":{  
               "filter":{  
                  "term":{  
                     "divnbr":"1"
                  }
               },
               "aggs":{  
                  "top_session_hits":{  
                     "top_hits":{  
                        "_source":{  
                           "includes":[  
                              "locn",
                              "sales"
                           ]
                        }
                     }
                  }
               }
            },
            "dist_sls":{  
               "sum":{  
                  "field":"sales"
               }
            }
         }
      }
   }
}

响应

{
  "took": 49,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "by_district_id": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "616",
          "doc_count": 3,
          "dist_sls": {
            "value": 550
          },
          "filter_agg": {
            "doc_count": 2,
            "top_session_hits": {
              "hits": {
                "total": 2,
                "max_score": 1,
                "hits": [
                  {
                    "_index": "sampleindex",
                    "_type": "mydocs",
                    "_id": "1",
                    "_score": 1,
                    "_source": {
                      "sales": 100
                    }
                  },
                  {
                    "_index": "sampleindex",
                    "_type": "mydocs",
                    "_id": "3",
                    "_score": 1,
                    "_source": {
                      "sales": 250
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

让我知道是否有帮助!