使用弹性聚合(NEST)时为null_pointer_exception

时间:2018-11-05 09:17:24

标签: elasticsearch nest elasticsearch-aggregation

我想使用弹性(6.2.4)来显示刻面。

构面应显示一个值以及在特定字段中每个值包含此值的文档数(按desc文档数排序)。

根据文档,我认为我需要使用术语汇总。

这是我的代码:

        var indexName = "newindx";
        ElasticClient client = new ElasticClient(
            new ConnectionSettings(
                new SingleNodeConnectionPool(
                    new Uri(@"http://localhost:9200"))
            ).DefaultIndex(indexName)
            .IncludeServerStackTraceOnError(true)
        );

        client.CreateIndex(indexName, c => c
         .Mappings(ms => ms
             .Map(typeof(Document), m => m
                 .AutoMap(typeof(Document))
             ))
          );

        var d1 = new Document()
        {
             id = 1,
             name = "aa"
        };

        var d2 = new Document()
        {
            id = 2,
            name = "aa"
        };

        var d3 = new Document()
        {
            id = 3,
            name = "bb"
        };

        client.Index(d1, i => i.Index(indexName).Type(typeof(Document)).Refresh(Refresh.True));
        client.Index(d2, i => i.Index(indexName).Type(typeof(Document)).Refresh(Refresh.True));
        client.Index(d3, i => i.Index(indexName).Type(typeof(Document)).Refresh(Refresh.True));

        var searchResults = client.Search<Document>(s => s
             .Query(q => q.MatchAll())
             .Aggregations(aggs => aggs.Children<Document>("name_agg", child => child
                .Aggregations(childAggs => childAggs
                   .Terms("name_agg_vs", vc => vc.Field("name"))
                        )
                    )
                )
             );

我已经创建了3个文档并为其建立了索引,并希望我按名称进行2次聚合(值1和2的值分别为“ aa”和doc 3的值“ bb”)。

相反,我得到了“ null_pointer_exception”

; nested: NullPointerException;
at org.elasticsearch.ElasticsearchException.guessRootCauses(ElasticsearchException.java:619)
at org.elasticsearch.action.search.SearchPhaseExecutionException.guessRootCauses(SearchPhaseExecutionException.java:170)
at org.elasticsearch.ElasticsearchException.guessRootCauses(ElasticsearchException.java:617)
at org.elasticsearch.ElasticsearchException.generateFailureXContent(ElasticsearchException.java:564)
at org.elasticsearch.rest.BytesRestResponse.build(BytesRestResponse.java:138)
at org.elasticsearch.rest.BytesRestResponse.<init>(BytesRestResponse.java:96)
at org.elasticsearch.rest.BytesRestResponse.<init>(BytesRestResponse.java:91)
at org.elasticsearch.rest.action.RestActionListener.onFailure(RestActionListener.java:58)
at org.elasticsearch.action.support.TransportAction$1.onFailure(TransportAction.java:91)
at org.elasticsearch.action.search.AbstractSearchAsyncAction.raisePhaseFailure(AbstractSearchAsyncAction.java:222)
at org.elasticsearch.action.search.AbstractSearchAsyncAction.onPhaseFailure(AbstractSearchAsyncAction.java:274)
at org.elasticsearch.action.search.AbstractSearchAsyncAction.executeNextPhase(AbstractSearchAsyncAction.java:132)
at org.elasticsearch.action.search.AbstractSearchAsyncAction.onPhaseDone(AbstractSearchAsyncAction.java:243)
at org.elasticsearch.action.search.InitialSearchPhase.onShardFailure(InitialSearchPhase.java:107)
at org.elasticsearch.action.search.InitialSearchPhase.access$100(InitialSearchPhase.java:49)
at org.elasticsearch.action.search.InitialSearchPhase$2.lambda$onFailure$1(InitialSearchPhase.java:217)
at org.elasticsearch.action.search.InitialSearchPhase.maybeFork(InitialSearchPhase.java:171)
at org.elasticsearch.action.search.InitialSearchPhase.access$000(InitialSearchPhase.java:49)
at org.elasticsearch.action.search.InitialSearchPhase$2.onFailure(InitialSearchPhase.java:217)
at org.elasticsearch.action.search.SearchExecutionStatsCollector.onFailure(SearchExecutionStatsCollector.java:73)
at org.elasticsearch.action.ActionListenerResponseHandler.handleException(ActionListenerResponseHandler.java:51)
at org.elasticsearch.action.search.SearchTransportService$ConnectionCountingHandler.handleException(SearchTransportService.java:527)
at org.elasticsearch.transport.TransportService$ContextRestoreResponseHandler.handleException(TransportService.java:1098)
at org.elasticsearch.transport.TransportService$DirectResponseChannel.processException(TransportService.java:1191)
at org.elasticsearch.transport.TransportService$DirectResponseChannel.sendResponse(TransportService.java:1175)
at org.elasticsearch.transport.TaskTransportChannel.sendResponse(TaskTransportChannel.java:66)
at org.elasticsearch.action.search.SearchTransportService$6$1.onFailure(SearchTransportService.java:385)
at org.elasticsearch.search.SearchService$2.onFailure(SearchService.java:324)
at org.elasticsearch.search.SearchService$2.onResponse(SearchService.java:318)
at org.elasticsearch.search.SearchService$2.onResponse(SearchService.java:312)
at org.elasticsearch.search.SearchService$3.doRun(SearchService.java:1002)
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:672)
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:41)
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at org.elasticsearch.join.aggregations.ChildrenAggregationBuilder.joinFieldResolveConfig(ChildrenAggregationBuilder.java:106)
at org.elasticsearch.join.aggregations.ChildrenAggregationBuilder.resolveConfig(ChildrenAggregationBuilder.java:97)
at org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder.doBuild(ValuesSourceAggregationBuilder.java:287)
at org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder.doBuild(ValuesSourceAggregationBuilder.java:36)
at org.elasticsearch.search.aggregations.AbstractAggregationBuilder.build(AbstractAggregationBuilder.java:132)
at org.elasticsearch.search.aggregations.AggregatorFactories$Builder.build(AggregatorFactories.java:329)
at org.elasticsearch.search.SearchService.parseSource(SearchService.java:749)
at org.elasticsearch.search.SearchService.createContext(SearchService.java:558)
at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:534)
at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:330)
at org.elasticsearch.search.SearchService$2.onResponse(SearchService.java:316)
... 9 more

这种情况发生在我使用的每个汇总(不仅是条款)上,所以我想我在做一些根本错误的事情。

1 个答案:

答案 0 :(得分:1)

terms aggregation不需要嵌套在child aggregation中,只需嵌套

var searchResults = client.Search<Document>(s => s
     .Query(q => q.MatchAll())
     .Aggregations(aggs => aggs
         .Terms("names", vc => vc
             .Field("name")
         )
     )
);

var termsAggregation = searchResults.Aggregations.Terms("names");

foreach (var termBucket in termsAggregation.Buckets)
{
    // do something with buckets
    Console.WriteLine(termBucket.Key);
    Console.WriteLine(termBucket.Count);
}

child聚合在您拥有Parent/Child relationship时使用。