c# - 列表项上的ElasticSearch聚合

标签 c# elasticsearch nest

如何按列表项(哈希文本)进行汇总

这是我的代码

public class Tweet
{
     public ulong id { get; set; }
     public string text { get; set; }
     [Nest.Nested]
     public List<hashtags> hashtags { get; set; }
}


public class hashtags
{ 
     public string hashtext { get; set; } 
}

我的索引数据示例如下:
 "hashtags" : [
        {
          "hashtext" : "aaaa"
        },
        {
          "hashtext" : "bbbb"
        },
        {
          "hashtext" : "ccccc"
        }
      ],
    }



.Aggregations(childAggs => childAggs
                .Nested("project_tags", n => n
                    .Path(p => p.hashtags)
                    .Aggregations(nestedAggs => nestedAggs
                        .Terms("by_tags", t => t
                            .Field(f => f.hashtags.First().hashtext)
                    )
                )
            ))

如何仅对哈希文本属性进行聚合查询
例如我想获取所有带有计数的哈希文本
aaaa     3   times 
ccccc    5  times

最佳答案

只要将hashtext上的hashtags映射为keyword数据类型(或使用fielddata enabled映射为text数据类型,并附带相应的注意事项),您所拥有的将起作用。

这是一个有效的例子

private static void Main()
{
    var defaultIndex = "my_index";
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var settings = new ConnectionSettings(pool)
        .DefaultIndex(defaultIndex);

    var client = new ElasticClient(settings);

    if (client.IndexExists(defaultIndex).Exists)
        client.DeleteIndex(defaultIndex);

    var createIndexResponse = client.CreateIndex(defaultIndex, c => c
        .Mappings(m => m
            .Map<Tweet>(mm => mm
                .AutoMap()
            )
        )
    );

    var bulkResponse = client.Bulk(b => b
        .IndexMany(new []
        {
            new Tweet 
            { 
                id = 1, 
                text = "foo",
                hashtags = new List<hashtags>
                { 
                    new hashtags { hashtext = "aaaa" },
                    new hashtags { hashtext = "bbbb" },
                    new hashtags { hashtext = "cccc" }
                }
            }, 
            new Tweet
            {
                id = 2,
                text = "bar",
                hashtags = new List<hashtags>
                {
                    new hashtags { hashtext = "aaaa" },
                    new hashtags { hashtext = "bbbb" }
                }
            },
            new Tweet
            {
                id = 3,
                text = "baz",
                hashtags = new List<hashtags>
                {
                    new hashtags { hashtext = "aaaa" },
                    new hashtags { hashtext = "cccc" }
                }
            }
        })
        .Refresh(Refresh.WaitFor)
    );

    var searchResponse = client.Search<Tweet>(s => s
        .Size(0)
        .Aggregations(childAggs => childAggs
                .Nested("hashtags", n => n
                    .Path(p => p.hashtags)
                    .Aggregations(nestedAggs => nestedAggs
                        .Terms("by_tags", t => t
                            .Field(f => f.hashtags.First().hashtext)
                        )
                    )
                )
            )
        );

    var hashtagBuckets = searchResponse.Aggregations.Nested("hashtags").Terms("by_tags").Buckets;

    foreach(var bucket in hashtagBuckets)
    {
        Console.WriteLine($"{bucket.Key}\t{bucket.DocCount} times");
    }
}

public class Tweet
{
    public ulong id { get; set; }
    public string text { get; set; }
    [Nested]
    public List<hashtags> hashtags { get; set; }
}


public class hashtags
{
    [Keyword]
    public string hashtext { get; set; }
}

将以下内容写入控制台
aaaa  3 times
bbbb  2 times
cccc  2 times

关于c# - 列表项上的ElasticSearch聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54211269/

相关文章:

c# - CS0311 C# 该类型不能用作泛型类型或方法中的类型参数 'TContext'。 Entity Framework 核心

c# - 如何读取时间值,然后将其插入 TimeSpan 变量

c# - InsertOnSubmit 没有附加?

geolocation - ElasticSearch:指定用于存储数据的分片

elasticsearch - 2.3.3中的FunctionScoreFunctionsDescriptor和FunctionScoreFunction的等效类型是什么

c# - 逗号分隔vs Elasticsearch 中的列表

c# - 如何在 .net 中找到当前线程的最大堆栈大小?

elasticsearch - Elasticsearch通配符查询忽略一些单词

elasticsearch - ES delete_by_query不起作用

c# - 如何使用自定义分析器创建ElasticSearch NEST v.5客户端的索引?