从Java elasticsearch querybuilder中删除默认字段

时间:2019-04-19 22:32:58

标签: java elasticsearch query-builder

Elasticsearch Java客户端的QueryBuider实例正在添加类似
的属性 1)订购
2)min_doc_count
3)shard_min_doc_count
4)show_term_doc_count_error
5)lang
6)gap_policy

到最终的Json查询。没有这些属性,我的查询将按预期工作。我想防止将这些属性添加到我的最终查询中。

Java:

FilterAggregationBuilder aggregation = AggregationBuilders.filter("id", QueryBuilders.termsQuery("id",
                        "my-name"));
TermsAggregationBuilder lev1Agg = AggregationBuilders.terms("id").field("id");
lev1Agg.size(1);               
lev1Agg.subAggregation(AggregationBuilders.sum("familyMemberCount").field("membersInFamily"));                
lev1Agg.subAggregation(AggregationBuilders.sum("totalKidsInFamily").field("kidsInFamily"));
Map<String, String> bucketsPathsMap = new HashMap<>();
bucketsPathsMap.put("familyMemberCount", "familyMemberCount");
bucketsPathsMap.put("totalKidsInFamily", "totalKidsInFamily");
Script script = new Script("params.familyMemberCount / params.totalKidsInFamily");

BucketScriptPipelineAggregationBuilder bs = PipelineAggregatorBuilders
                        .bucketScript("myScript", bucketsPathsMap, script);

lev1Agg.subAggregation(bs);
aggregation.subAggregation(lev1Agg);
searchSourceBuilder = new SearchSourceBuilder().aggregation(aggregation);
searchSourceBuilder.size(0);

由以上代码构建的查询

GET my-alias/_search
{
  "size" : 0,
  "aggregations" : {
    "id" : {
      "filter" : {
        "terms" : {
          "name" : [
            "my-name"
          ],
          "boost" : 1.0
        }
      },
      "aggregations" : {
        "id" : {
          "terms" : {
            "field" : "name",
            "size" : 1,
            "min_doc_count" : 1,
            "shard_min_doc_count" : 0,
            "show_term_doc_count_error" : false,
            "order" : [
              {
                "_count" : "desc"
              },
              {
                "_term" : "asc"
              }
            ]
          },
          "aggregations" : {
            "familyMemberCount" : {
              "sum" : {
                "field" : "membersInFamily"
              }
            },
            "totalKidsInFamily" : {
              "sum" : {
                "field" : "kidsInFamily"
              }
            },
            "myScript" : {
              "bucket_script" : {
                "buckets_path" : {
                  "familyMemberCount" : "familyMemberCount",
                  "totalKidsInFamily" : "totalKidsInFamily"
                },
                "script" : {
                  "source" : "params.familyMemberCount / params.totalKidsInFamily",
                  "lang" : "painless"
                },
                "gap_policy" : "skip"
              }
            }
          }
        }
      }
    }
  }
}

0 个答案:

没有答案