简体   繁体   English

使用Java根据来自Elasticsearch数组的条件获取特定值

[英]get a specific value based on criteria from elasticsearch array using java

My elastic search data looks like 我的弹性搜寻资料看起来像

{
  "took": 12,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "eswar",
        "_type": "azure",
        "_id": "AV6y005oafdLlkt7Fe-R",
        "_score": 1,
        "_source": {
          "costs": [
            {
              "cost": 3.6,
              "endDate": "2017-02-15T00:00:00+00:00",
              "startDate": "2017-02-14T00:00:00+00:00"
            },
            {
              "cost": 2,
              "endDate": "2017-02-14T00:00:00+00:00",
              "startDate": "2017-02-13T00:00:00+00:00"
            }
          ],
          "externalUUID": "/subscriptions/9ee6993f-a036-4118-9eab-c66d9fda1ef3/resourceGroups/VISTARAGATEWAYIMAGE/providers/Microsoft.Compute/disks/VistaraGateway01_disk1_ec7798e17f934e6483ed5d2490e80d98",
          "clientId": 154,
          "region": "useast",
          "cloudProviderId": 57063
        }
      },
      {
        "_index": "eswar",
        "_type": "azure",
        "_id": "AV6y00rmafdLlkt7Fe-Q",
        "_score": 1,
        "_source": {
          "costs": [
            {
              "cost": 0,
              "endDate": "2017-02-14T00:00:00+00:00",
              "startDate": "2017-02-13T00:00:00+00:00"
            },
            {
              "cost": 3,
              "endDate": "2017-02-17T00:00:00+00:00",
              "startDate": "2017-02-16T00:00:00+00:00"
            }
          ],
          "externalUUID": "/subscriptions/9ee6993f-a036-4118-9eab-c66d9fda1ef3/resourceGroups/vistaragatewayimage/providers/Microsoft.Compute/virtualMachines/VistaraGateway",
          "clientId": 154,
          "region": "eastus",
          "cloudProviderId": 57063
        }
      }
    ]
  }
}

I want to get costs.cost:3.6 as aggregation result,but I am getting result as 5 how can I filter data even in array? 我想获得costs.cost:3.6作为聚合结果,但是我得到的结果是5,如何即使在数组中也可以过滤数据?

 RangeQueryBuilder startDateRQB = QueryBuilders.rangeQuery("costs.startDate").gte("2017-02-14T00:00:00+00:00");
        RangeQueryBuilder endDateRQB = QueryBuilders.rangeQuery("costs.endDate").lte("2017-02-15T00:00:00+00:00");
        RegexpQueryBuilder deviceNameREQB= QueryBuilders.regexpQuery("region", "useast.*");
        BoolQueryBuilder bQB=QueryBuilders.boolQuery().must(deviceNameREQB).must(startDateRQB).must(endDateRQB);
        BoolQueryBuilder sQB=QueryBuilders.boolQuery().must(startDateRQB).must(endDateRQB);
        SearchResponse response = client.prepareSearch(index).setQuery(bQB).addAggregation(AggregationBuilders.sum("Totalcost").field("costs.cost")).execute().actionGet();
        Sum sum=response.getAggregations().get("Totalcost");
        double cost=sum.getValue();
        System.out.println(cost);

I suggest you to define costs as nested object. 我建议您将costs定义为嵌套对象。 Than, you will be able to add conditions on the data inside (nested documents). 然后,您将可以在内部数据(嵌套文档)上添加条件。

This approach can open a wide range of possibilities to your queries. 这种方法可以为您的查询提供广泛的可能性。

Have a look at the following solution: 看一下以下解决方案:

{
  "size": 0,
  "aggregations": {
    "costs_agg": {
      "nested": {
        "path": "costs"
      },
      "aggregations": {
        "bool_agg": {
          "must": [
            {
              "range": {
                "costs.startDate": {
                  "gte": "2017-02-14T00:00:00+00:00"
                }
              }
            },
            {
              "range": {
                "costs.endDate": {
                  "lte": "2017-02-15T00:00:00+00:00"
                }
              }
            },
            {
              "wildcard": {
                "costs.region": "useast.*"
              }
            }
          ]
        },
        "aggregations": {
          "cost_sum_agg": {
            "sum": {
              "field": "costs.cost"
            }
          }
        }
      }
    }
  }
}

Let me explain every aggregation (by its name): 让我解释一下每个聚合(按其名称):

  • costs_agg: nested aggregation to dive into costs scope costs_agg:嵌套聚合以进入成本范围
  • bool_agg: the thing with aggregation over nested object its that, a query above the aggregation won't filter by nested objects. bool_agg:在嵌套对象上进行聚合的东西,聚合上方的查询不会被嵌套对象过滤。 The solution here is to filter the needed nested-documents inside the aggregation itself 此处的解决方案是在聚合本身内部过滤所需的嵌套文档
  • cost_sum_agg: final sum cost_sum_agg:最终金额

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM