弹性搜索高级 REST 客户端动态聚合字段

huangapple 未分类评论52阅读模式
英文:

Elasticsearch Rest High Level Client aggregate fields dynamically

问题

我试图根据输入动态生成查询,但在生成的查询中,我只看到有两个聚合被生成了。我如何使每个字段都有单独的聚合?以下是我尝试过的代码和我得到的响应。

main()中调用:

buildSearchCriteria("1");

在这里,我设置了聚合类型和相应的值:

public static void buildSearchCriteria(String... exceptionId) {
    // ... (其他代码)

    stringListMap.put("terms", asList(
        // ... (其他字段)
    ));

    stringListMap.put("sum", asList(
        // ... (其他字段)
    ));

    // ... (其他代码)
}

以下是将获取上述信息并构建查询的aggregate函数:

public static void aggregate(SearchCriteria searchCriteria) throws IOException {
    // ... (其他代码)

    nesteds.stream().forEach(l -> buildAggregations(l, aggregationBuilder));
    filter.stream().forEach(l -> buildAggregations(l, aggregationBuilder));
    terms.stream().forEach(l -> buildAggregations(l, aggregationBuilder));
    sums.stream().forEach(l -> buildAggregations(l, aggregationBuilder));

    // ... (其他代码)
}

buildAggregations方法如下:

private static AggregationBuilder buildAggregations(FieldNameAndPath fieldNameAndPath , AggregationBuilder parentAggregationBuilder) {
    // ... (其他代码)
    return parentAggregationBuilder;
}

SearchCriteria类如下:

@Data
public class SearchCriteria {
    Map<String, List<FieldNameAndPath>> stringListMap;
    private List<String> searchFields;
}

DTO FieldNameAndPath 如下:

public class FieldNameAndPath{
    private String aggType;
    private String fieldName;
    private String fieldPath;
    private List<String> fieldValues;
    private List<NestedAggsFields> nestedAggs;
    private int order;
}

以上代码的查询输出如下:

{
  "aggregations": {
    "parent": {
      "sampler": {
        "shard_size": 100
      },
      "aggregations": {
        "exceptionIds": {
          "filter": {
            "terms": {
              "exceptionIdsMatch": [
                "1"
              ],
              "boost": 1
            }
          }
        },
        // ... (其他字段)
      }
    }
  }
}

期望的查询如下:

{
  "size": 0,
  "aggregations": {
    "exceptionIds": {
      "nested": {
        "path": "recommendations"
      },
      "aggregations": {
        "exceptionIdsMatch": {
          "filter": {
            "terms": {
              "recommendations.exceptionId.keyword": [
                "1"
              ],
              "boost": 1
            }
          },
          "aggregations": {
            "by_exceptionId": {
              "terms": {
                "field": "recommendations.exceptionId.keyword",
                "size": 10,
                "min_doc_count": 1,
                "shard_min_doc_count": 0,
                "show_term_doc_count_error": false,
                "order": [
                  {
                    "_count": "desc"
                  },
                  {
                    "_key": "asc"
                  }
                ]
              },
              "aggregations": {
                // ... (其他字段)
              }
            }
          }
        }
      }
    }
  }
}
英文:

I am trying to generate query dynamically based on the inputs but in the generated query i can see there are only two aggregations are getting generated how can i make each fields to have the separate aggregations below is the code what i have tried and the response what i'm getting.

From main() i'm calling

buildSearchCriteria(&quot;1&quot;);

Here i am setting the aggregation type and respective values:

public static void buildSearchCriteria(String... exceptionId) {

        SearchCriteria searchCriteria = new SearchCriteria();

        Map&lt;String, List&lt;FieldNameAndPath&gt;&gt; stringListMap = new HashMap&lt;&gt;();

        stringListMap.put(&quot;nested&quot;, asList(new FieldNameAndPath(&quot;nested&quot;, &quot;recommendations&quot;,
                &quot;recommendations&quot;, null, emptyList(), 1)));

        stringListMap.put(&quot;filter&quot;, asList(new FieldNameAndPath(&quot;filter&quot;, &quot;exceptionIds&quot;, &quot;recommendations.exceptionId.keyword&quot;,
                asList(exceptionId),
                asList(new NestedAggsFields(&quot;terms&quot;, &quot;exceptionIdsMatch&quot;)), 2)));

        stringListMap.put(&quot;terms&quot;, asList(new FieldNameAndPath(&quot;terms&quot;, &quot;by_exceptionId&quot;, &quot;recommendations.exceptionId.keyword&quot;, null, emptyList(), 3),
                new FieldNameAndPath(&quot;terms&quot;, &quot;by_item&quot;, &quot;recommendations.item.keyword&quot;, null, emptyList(), 4),
                new FieldNameAndPath(&quot;terms&quot;, &quot;by_destination&quot;, &quot;recommendations.location.keyword&quot;, null, emptyList(), 5),
                new FieldNameAndPath(&quot;terms&quot;, &quot;by_trans&quot;, &quot;recommendations.transportMode.keyword&quot;, null, emptyList(), 6),
                new FieldNameAndPath(&quot;terms&quot;, &quot;by_sourcelocation&quot;, &quot;recommendations.sourceLocation.keyword&quot;, null, emptyList(), 7),
                new FieldNameAndPath(&quot;terms&quot;, &quot;by_shipdate&quot;, &quot;recommendations.shipDate&quot;, null, emptyList(), 8),
                new FieldNameAndPath(&quot;terms&quot;, &quot;by_arrival&quot;, &quot;recommendations.arrivalDate&quot;, null, emptyList(), 9)));

        stringListMap.put(&quot;sum&quot;, asList(new FieldNameAndPath(&quot;sum&quot;, &quot;quantity&quot;, &quot;recommendations.transferQuantity&quot;, null, emptyList(), 10),
                new FieldNameAndPath(&quot;sum&quot;, &quot;transfercost&quot;, &quot;recommendations.transferCost&quot;, null, emptyList(), 11),
                new FieldNameAndPath(&quot;sum&quot;, &quot;revenueRecovered&quot;, &quot;recommendations.revenueRecovered&quot;, null, emptyList(), 12)));

        System.out.println(stringListMap);


        searchCriteria.setStringListMap(stringListMap);
        aggregate(searchCriteria);
    }

Below is the aggregate function which will get the the above information and builds query:

public static void aggregate(SearchCriteria searchCriteria) throws IOException {

            Map&lt;String, List&lt;FieldNameAndPath&gt;&gt; map = searchCriteria.getStringListMap();

            List&lt;FieldNameAndPath&gt;  nesteds = map.get(&quot;nested&quot;);
            List&lt;FieldNameAndPath&gt;  filter = map.get(&quot;filter&quot;);
            List&lt;FieldNameAndPath&gt;  terms = map.get(&quot;terms&quot;);
            List&lt;FieldNameAndPath&gt;  sums = map.get(&quot;sum&quot;);

            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

            AggregationBuilder aggregationBuilder = new SamplerAggregationBuilder(&quot;parent&quot;);


            nesteds.stream().forEach(l -&gt; buildAggregations(l, aggregationBuilder));
            filter.stream().forEach(l -&gt; buildAggregations(l,  aggregationBuilder));
            terms.stream().forEach(l -&gt; buildAggregations(l,  aggregationBuilder));
            sums.stream().forEach(l -&gt; buildAggregations(l, aggregationBuilder));


            SearchRequest searchRequest = new SearchRequest();
            searchRequest.indices(&quot;index&quot;);
            searchRequest.types(&quot;type&quot;);
            sourceBuilder.aggregation(aggregationBuilder);
            searchRequest.source(sourceBuilder);
            System.out.println(searchRequest.source().toString());
    }

buildAggregations method:

private static AggregationBuilder buildAggregations(FieldNameAndPath fieldNameAndPath , AggregationBuilder parentAggregationBuilder) {

        if(fieldNameAndPath.getAggType().equals(&quot;nested&quot;)){
            parentAggregationBuilder = AggregationBuilders.nested(fieldNameAndPath.getFieldName(), fieldNameAndPath.getFieldPath());
        }

        if(fieldNameAndPath.getAggType().equals(&quot;filter&quot;)){
            parentAggregationBuilder.subAggregation(AggregationBuilders
                    .filter(fieldNameAndPath.getFieldName(),
                            QueryBuilders.termsQuery(fieldNameAndPath.getNestedAggs()
                                    .stream().map(nestedAggsFields -&gt; nestedAggsFields.getFieldName()).findFirst().get(), fieldNameAndPath.getFieldValues())));
        }

        if(fieldNameAndPath.getAggType().equals(&quot;terms&quot;)){
            parentAggregationBuilder.subAggregation(AggregationBuilders.terms(fieldNameAndPath.getFieldName())
                    .field(fieldNameAndPath.getFieldPath()));
        }

        if(fieldNameAndPath.getAggType().equals(&quot;sum&quot;)){
            parentAggregationBuilder.subAggregation(AggregationBuilders.
                    sum(fieldNameAndPath.getFieldName()).field(fieldNameAndPath.getFieldPath()));
        }
        return parentAggregationBuilder;

    }

SearchCriteria class:

@Data
public class SearchCriteria {
    Map&lt;String, List&lt;FieldNameAndPath&gt;&gt; stringListMap;
    private List&lt;String&gt; searchFields;
}

And the DTO FieldNameAndPath:

public class FieldNameAndPath{
    private String aggType;
    private String fieldName;
    private String fieldPath;
    private List&lt;String&gt; fieldValues;
    private List&lt;NestedAggsFields&gt; nestedAggs;
    private int order;
}

And the query output from the above code is:

{
  &quot;aggregations&quot;: {
    &quot;parent&quot;: {
      &quot;sampler&quot;: {
        &quot;shard_size&quot;: 100
      },
      &quot;aggregations&quot;: {
        &quot;exceptionIds&quot;: {
          &quot;filter&quot;: {
            &quot;terms&quot;: {
              &quot;exceptionIdsMatch&quot;: [
                &quot;1&quot;
              ],
              &quot;boost&quot;: 1
            }
          }
        },
        &quot;by_exceptionId&quot;: {
          &quot;terms&quot;: {
            &quot;field&quot;: &quot;recommendations.exceptionId.keyword&quot;,
            &quot;size&quot;: 10,
            &quot;min_doc_count&quot;: 1,
            &quot;shard_min_doc_count&quot;: 0,
            &quot;show_term_doc_count_error&quot;: false,
            &quot;order&quot;: [
              {
                &quot;_count&quot;: &quot;desc&quot;
              },
              {
                &quot;_key&quot;: &quot;asc&quot;
              }
            ]
          }
        },
        &quot;by_item&quot;: {
          &quot;terms&quot;: {
            &quot;field&quot;: &quot;recommendations.item.keyword&quot;,
            &quot;size&quot;: 10,
            &quot;min_doc_count&quot;: 1,
            &quot;shard_min_doc_count&quot;: 0,
            &quot;show_term_doc_count_error&quot;: false,
            &quot;order&quot;: [
              {
                &quot;_count&quot;: &quot;desc&quot;
              },
              {
                &quot;_key&quot;: &quot;asc&quot;
              }
            ]
          }
        },
        &quot;by_destination&quot;: {
          &quot;terms&quot;: {
            &quot;field&quot;: &quot;recommendations.location.keyword&quot;,
            &quot;size&quot;: 10,
            &quot;min_doc_count&quot;: 1,
            &quot;shard_min_doc_count&quot;: 0,
            &quot;show_term_doc_count_error&quot;: false,
            &quot;order&quot;: [
              {
                &quot;_count&quot;: &quot;desc&quot;
              },
              {
                &quot;_key&quot;: &quot;asc&quot;
              }
            ]
          }
        },
        &quot;by_trans&quot;: {
          &quot;terms&quot;: {
            &quot;field&quot;: &quot;recommendations.transportMode.keyword&quot;,
            &quot;size&quot;: 10,
            &quot;min_doc_count&quot;: 1,
            &quot;shard_min_doc_count&quot;: 0,
            &quot;show_term_doc_count_error&quot;: false,
            &quot;order&quot;: [
              {
                &quot;_count&quot;: &quot;desc&quot;
              },
              {
                &quot;_key&quot;: &quot;asc&quot;
              }
            ]
          }
        },
        &quot;by_sourcelocation&quot;: {
          &quot;terms&quot;: {
            &quot;field&quot;: &quot;recommendations.sourceLocation.keyword&quot;,
            &quot;size&quot;: 10,
            &quot;min_doc_count&quot;: 1,
            &quot;shard_min_doc_count&quot;: 0,
            &quot;show_term_doc_count_error&quot;: false,
            &quot;order&quot;: [
              {
                &quot;_count&quot;: &quot;desc&quot;
              },
              {
                &quot;_key&quot;: &quot;asc&quot;
              }
            ]
          }
        },
        &quot;by_shipdate&quot;: {
          &quot;terms&quot;: {
            &quot;field&quot;: &quot;recommendations.shipDate&quot;,
            &quot;size&quot;: 10,
            &quot;min_doc_count&quot;: 1,
            &quot;shard_min_doc_count&quot;: 0,
            &quot;show_term_doc_count_error&quot;: false,
            &quot;order&quot;: [
              {
                &quot;_count&quot;: &quot;desc&quot;
              },
              {
                &quot;_key&quot;: &quot;asc&quot;
              }
            ]
          }
        },
        &quot;by_arrival&quot;: {
          &quot;terms&quot;: {
            &quot;field&quot;: &quot;recommendations.arrivalDate&quot;,
            &quot;size&quot;: 10,
            &quot;min_doc_count&quot;: 1,
            &quot;shard_min_doc_count&quot;: 0,
            &quot;show_term_doc_count_error&quot;: false,
            &quot;order&quot;: [
              {
                &quot;_count&quot;: &quot;desc&quot;
              },
              {
                &quot;_key&quot;: &quot;asc&quot;
              }
            ]
          }
        },
        &quot;quantity&quot;: {
          &quot;sum&quot;: {
            &quot;field&quot;: &quot;recommendations.transferQuantity&quot;
          }
        },
        &quot;transfercost&quot;: {
          &quot;sum&quot;: {
            &quot;field&quot;: &quot;recommendations.transferCost&quot;
          }
        },
        &quot;revenueRecovered&quot;: {
          &quot;sum&quot;: {
            &quot;field&quot;: &quot;recommendations.revenueRecovered&quot;
          }
        }
      }
    }
  }
}

Expected Query is:

{
  &quot;size&quot;: 0,
  &quot;aggregations&quot;: {
    &quot;exceptionIds&quot;: {
      &quot;nested&quot;: {
        &quot;path&quot;: &quot;recommendations&quot;
      },
      &quot;aggregations&quot;: {
        &quot;exceptionIdsMatch&quot;: {
          &quot;filter&quot;: {
            &quot;terms&quot;: {
              &quot;recommendations.exceptionId.keyword&quot;: [
                &quot;1&quot;
              ],
              &quot;boost&quot;: 1
            }
          },
          &quot;aggregations&quot;: {
            &quot;by_exceptionId&quot;: {
              &quot;terms&quot;: {
                &quot;field&quot;: &quot;recommendations.exceptionId.keyword&quot;,
                &quot;size&quot;: 10,
                &quot;min_doc_count&quot;: 1,
                &quot;shard_min_doc_count&quot;: 0,
                &quot;show_term_doc_count_error&quot;: false,
                &quot;order&quot;: [
                  {
                    &quot;_count&quot;: &quot;desc&quot;
                  },
                  {
                    &quot;_key&quot;: &quot;asc&quot;
                  }
                ]
              },
              &quot;aggregations&quot;: {
                &quot;by_item&quot;: {
                  &quot;terms&quot;: {
                    &quot;field&quot;: &quot;recommendations.item.keyword&quot;,
                    &quot;size&quot;: 10,
                    &quot;min_doc_count&quot;: 1,
                    &quot;shard_min_doc_count&quot;: 0,
                    &quot;show_term_doc_count_error&quot;: false,
                    &quot;order&quot;: [
                      {
                        &quot;_count&quot;: &quot;desc&quot;
                      },
                      {
                        &quot;_key&quot;: &quot;asc&quot;
                      }
                    ]
                  },
                  &quot;aggregations&quot;: {
                    &quot;by_destination&quot;: {
                      &quot;terms&quot;: {
                        &quot;field&quot;: &quot;recommendations.location.keyword&quot;,
                        &quot;size&quot;: 10,
                        &quot;min_doc_count&quot;: 1,
                        &quot;shard_min_doc_count&quot;: 0,
                        &quot;show_term_doc_count_error&quot;: false,
                        &quot;order&quot;: [
                          {
                            &quot;_count&quot;: &quot;desc&quot;
                          },
                          {
                            &quot;_key&quot;: &quot;asc&quot;
                          }
                        ]
                      },
                      &quot;aggregations&quot;: {
                        &quot;by_trans&quot;: {
                          &quot;terms&quot;: {
                            &quot;field&quot;: &quot;recommendations.transportMode.keyword&quot;,
                            &quot;size&quot;: 10,
                            &quot;min_doc_count&quot;: 1,
                            &quot;shard_min_doc_count&quot;: 0,
                            &quot;show_term_doc_count_error&quot;: false,
                            &quot;order&quot;: [
                              {
                                &quot;_count&quot;: &quot;desc&quot;
                              },
                              {
                                &quot;_key&quot;: &quot;asc&quot;
                              }
                            ]
                          },
                          &quot;aggregations&quot;: {
                            &quot;by_sourcelocation&quot;: {
                              &quot;terms&quot;: {
                                &quot;field&quot;: &quot;recommendations.sourceLocation.keyword&quot;,
                                &quot;size&quot;: 10,
                                &quot;min_doc_count&quot;: 1,
                                &quot;shard_min_doc_count&quot;: 0,
                                &quot;show_term_doc_count_error&quot;: false,
                                &quot;order&quot;: [
                                  {
                                    &quot;_count&quot;: &quot;desc&quot;
                                  },
                                  {
                                    &quot;_key&quot;: &quot;asc&quot;
                                  }
                                ]
                              },
                              &quot;aggregations&quot;: {
                                &quot;by_shipdate&quot;: {
                                  &quot;terms&quot;: {
                                    &quot;field&quot;: &quot;recommendations.shipDate&quot;,
                                    &quot;size&quot;: 10,
                                    &quot;min_doc_count&quot;: 1,
                                    &quot;shard_min_doc_count&quot;: 0,
                                    &quot;show_term_doc_count_error&quot;: false,
                                    &quot;order&quot;: [
                                      {
                                        &quot;_count&quot;: &quot;desc&quot;
                                      },
                                      {
                                        &quot;_key&quot;: &quot;asc&quot;
                                      }
                                    ]
                                  },
                                  &quot;aggregations&quot;: {
                                    &quot;by_arrival&quot;: {
                                      &quot;terms&quot;: {
                                        &quot;field&quot;: &quot;recommendations.arrivalDate&quot;,
                                        &quot;size&quot;: 10,
                                        &quot;min_doc_count&quot;: 1,
                                        &quot;shard_min_doc_count&quot;: 0,
                                        &quot;show_term_doc_count_error&quot;: false,
                                        &quot;order&quot;: [
                                          {
                                            &quot;_count&quot;: &quot;desc&quot;
                                          },
                                          {
                                            &quot;_key&quot;: &quot;asc&quot;
                                          }
                                        ]
                                      },
                                      &quot;aggregations&quot;: {
                                        &quot;quantity&quot;: {
                                          &quot;sum&quot;: {
                                            &quot;field&quot;: &quot;recommendations.transferQuantity&quot;
                                          }
                                        },
                                        &quot;transfercost&quot;: {
                                          &quot;sum&quot;: {
                                            &quot;field&quot;: &quot;recommendations.transferCost&quot;
                                          }
                                        },
                                        &quot;revenueRecovered&quot;: {
                                          &quot;sum&quot;: {
                                            &quot;field&quot;: &quot;recommendations.revenueRecovered&quot;
                                          }
                                        }
                                      }
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

huangapple
  • 本文由 发表于 2020年5月4日 15:47:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/61587399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定