0

我有一个 Json 查询字符串:
"\"query\":{\"match_all\": {}},\"aggs\":{\"avg1\":{\"avg\":{\"field\":\"age\"} } }";

通过 执行查询时Jest Client,聚合值可用。

但是当这个查询被转换成一个 Query Builder ( WrapperQueryBuilder) 对象时,得到以下异常。

; nested: QueryParsingException[[st1index] [_na] query malformed, must start with start_object]; }{[ixJ-6RHNR5C6fC7HfJHqaw][st1index][4]: SearchParseException[[st1index][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{
  "query" : {
    "wrapper" : {
      "query" : "InF1ZXJ5Ijp7Im1hdGNoX2FsbCI6IHt9fSwiYWdncyI6eyJhdmcxIjp7ImF2ZyI6eyJmaWVsZCI6ImFnZSJ9IH0gfQ=="
    }
  }
}]]]; nested: QueryParsingException[[st1index] [_na] query malformed, must start with start_object]; }]

我该如何解决?

编辑1:代码分析:添加代码分析细节:

    public static void main(String[] args) throws Exception 
{
try{
    //Jest client building
            JestClientFactory factory = new JestClientFactory();
            HttpClientConfig config = new HttpClientConfig.
                    Builder("http://localhost:9201")
                    .connTimeout(10000)
                    .readTimeout(10000)
                    .multiThreaded(true).build();

            factory.setHttpClientConfig(config);
            JestClient jestClient = factory.getObject();

            String query ="{\"query\":{\"match_all\": {}},\"aggs\":{\"avg1\":{\"avg\":{\"field\":\"age\"} } }}";
             String query2 ="{\"match_all\": {}},\"aggs\":{\"avg1\":{\"avg\":{\"field\":\"age\"} } }}";

            WrapperQueryBuilder wrapQB = new WrapperQueryBuilder(query2);
            SearchSourceBuilder ssb = new SearchSourceBuilder();
            ssb.query(wrapQB);

//working code commented
            //   Search.Builder searchBuilder = new Search.Builder(query).addIndex("st1index").addType("st1type");

//code which needs to be fixed
            Search.Builder searchBuilder = new 
Search.Builder(ssb.toString()).addIndex("st1index").addType("st1type");
            SearchResult result = jestClient.execute(searchBuilder.build());
            System.out.println(result.getJsonString());
            }
            catch(Exception e)
            {
                System.out.println("inside exception block");
                e.printStackTrace();
            }

        }

使用字符串查询和带注释的 SearchSourceBuilder,会显示 aggs 结果。但是通过使用 WrapperQueryBuilder ,无法检索 aggs 结果

4

1 回答 1

1

你快到了,你只是缺少括号:

"{\"query\":{\"match_all\": {}},\"aggs\":{\"avg1\":{\"avg\":{\"field\":\"age\"}}}}";
 ^                                                                               ^
 |                                                                               |
this one...                                                         ...and this one

更新

在 中WrapperQueryBuilder,只能传递query零件的内容,不能传递aggregations零件。您需要像这样直接添加聚合部分SearchSourceBuilder

SearchSourceBuilder ssb = new SearchSourceBuilder();

// add the query part
String query ="{\"match_all\": {}}";
WrapperQueryBuilder wrapQB = new WrapperQueryBuilder(query);
ssb.query(wrapQB);

// add the aggregation part
AvgBuilder avgAgg = AggregationBuilders.avg("avg1").field("age");
ssb.aggregation(avgAgg);
于 2015-12-14T07:39:43.033 回答