使用.NET Core中的NEST对ElasticSearch中的嵌套集合执行查询

问题描述 投票:0回答:1

我正在尝试搜索以下对象的索引:

public class IndexedElement
{

    public Guid Id { get; set; }
    public long RowId { get; set; }
    public IndexedElementType Type { get; set; }
    public string Summary { get; set; }
    public string Description { get; set; }

    public IList<string> Tags { get; set; }

}

目的是通过Summary属性进行搜索,或者匹配Tags集合中的任何字符串

我现在拥有的是:

    public IEnumerable<IndexedElement> Search(string description)
    {
        var query = GetClient().Search<IndexedElement>(s => s.From(0).Size(5)
            .Query(
                q => q.Term(p => p.Summary, description)
                ||
                q.Nested(n => n.Path(p => p.Tags).Query(q2 => q2.Terms(t => t.Field(f => f.Tags).Terms(description))))                    
            ));

        return query.Documents.ToList();
    }

但嵌套部分不起作用,我不知道我是否以正确的方式使用它,或者我必须找到另一个解决方案。

有任何想法吗?

谢谢大家

c# elasticsearch asp.net-core .net-core nest
1个回答
1
投票

您不需要执行nested查询来查询Tags字段,因为每个标记只是一个原始JSON值,即string。只需terms查询即可。

在需要nested查询的地方,Tags是具有多个属性的POCO,并被映射为nested数据类型。

© www.soinside.com 2019 - 2024. All rights reserved.