ElasticSearch C#客户端(NEST):使用ES 5.5.0获取聚合结果

时间:2017-09-20 15:06:52

标签: elasticsearch nest elasticsearch-aggregation

这是索引代码。 具体而言,City字段是用于搜索和聚合的Multi字段。 还使用了一些分析仪。只需要汇总城市字段。

        client.CreateIndex(IndexName, c => c
        .Settings(s => s
        .Analysis(a => a
       .Analyzers(an => an
       .Custom("index_analyzer", ca => ca
       .Tokenizer("standard")
       .Filters("standard", "lowercase", "stop"))
       .Custom("search_analyzer", ca => ca
       .Tokenizer("standard")
       .Filters("standard", "lowercase", "stop"))
       )
        )
        )
        .Mappings(m => m
        .Map<EmployeeInfo>(mm => mm
        .AutoMap()
        .Properties(p => p
        .Text(t => t
        .Name(n => n.Employee_Num)
        .Analyzer("index_analyzer")
        .SearchAnalyzer("search_analyzer"))
        .Text(t => t
        .Name( n => n.First_Name)
        .Analyzer("index_analyzer")
        .SearchAnalyzer("search_analyzer"))
        .Text(t => t
        .Name(n => n.Last_Name)
        .Analyzer("index_analyzer")
        .SearchAnalyzer("search_analyzer"))
        .Text(t => t
        .Name(n => n.Address)
        .Analyzer("index_analyzer")
        .SearchAnalyzer("search_analyzer"))
        .Text(t => t
        .Name(n => n.City)
        .Fields(f => f
        .Text(tt => tt
        .Name("mytext")
        .Analyzer("index_analyzer")
        .SearchAnalyzer("search_analyzer"))
        .Keyword(k => k
        .Name("keyword")
        .IgnoreAbove(256)
        )))

.....

我像这样汇总......

            var result = client.Search<EmployeeInfo>(s => s
               .Index("employee")
               .Aggregations(a => a
                .Terms("field1", t => t.Field(k => k.City.Suffix("keyword"))
                .MinimumDocumentCount(2)
                .Size(100)
                .ExecutionHint(TermsAggregationExecutionHint.Map))
                )
                );

            bool valid = result.IsValid;
            var dataList = new List<Tuple<string, long>>();

            var terms = result.Aggs.Terms("field1");

            if (terms != null)
            {
                foreach (var bucket in terms.Buckets)
                {
                    string data = bucket.Key;
                    long count = (long)bucket.DocCount;

                    dataList.Add(new Tuple<string, long>(data, count));
                }


                // To Sort by Alphabets
                if (sortAlphbetically)
                    return dataList.OrderBy(t => t.Item1).ToList();

                // To Sort by count
                return dataList.OrderByDescending(t => t.Item2).ToList();
            }

            return dataList;
        }

条款.Buckets总是空的。如何获取城市的汇总值? 我使用的是ES 5.5.0

1 个答案:

答案 0 :(得分:0)

得到答案,索引存在问题...找出了解决方法。索引将按以下方式完成

        client.CreateIndex(IndexName, i => i
        .Settings(s => s
            .Analysis(a => a
                .TokenFilters(tf => tf
                    .EdgeNGram("edge_ngrams", e => e
                        .MinGram(1)
                        .MaxGram(50)
                        .Side(EdgeNGramSide.Front)))
                    .Analyzers(analyzer => analyzer
                        .Custom("partial_text", ca => ca
                            .Filters(new string[] { "lowercase", "edge_ngrams" })
                            .Tokenizer("standard"))
                        .Custom("full_text", ca => ca
                            .Filters(new string[] { "standard", "lowercase" })
                            .Tokenizer("standard")))))

                            .Mappings(m => m
                            .Map<EmployeeInfo>(mm => mm
                            .AutoMap()
                            .Properties(p => p
                            .Text(t => t
                            .Name(n => n.Employee_Num)
                            .Analyzer("partial_text")
                            .SearchAnalyzer("full_text"))
                            .Text(t => t
                            .Name(n => n.First_Name)
                            .Analyzer("partial_text")
                            .SearchAnalyzer("full_text"))
                            .Text(t => t
                            .Name(n => n.Last_Name)
                            .Analyzer("partial_text")
                            .SearchAnalyzer("full_text"))
                            .Text(t => t
                            .Name(n => n.Address)
                            .Analyzer("partial_text")
                            .SearchAnalyzer("full_text"))
                            .Text(t => t
                            .Name(n => n.City)
                            .Fields(f => f
                            .Text(tt => tt
                            .Name("mytext")
                            .Analyzer("partial_text")
                            .SearchAnalyzer("full_text"))
                            .Keyword(k => k
                            .Name("keyword")
                            .IgnoreAbove(256)
                            )))

                            )))