使用 .NET Core 中的 NEST 对 ElasticSearch 中的嵌套集合执行查询
Perform a query over a nested collection in ElasticSearch with NEST in .NET Core
我正在尝试搜索以下对象的索引:
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; }
}
目的是按摘要 属性 或匹配标签集合中的任何字符串进行搜索
我目前拥有的是:
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();
}
但是嵌套部分不起作用,我不知道我是否以正确的方式使用它,或者我可能必须为此找到其他解决方案。
有什么想法吗?
提前谢谢大家
您不需要执行 nested
查询来查询 Tags
字段,因为每个标签只是一个原始 JSON 值,即 string
。只需 terms
查询就足够了。
需要 nested
查询的地方是 Tags
是具有多个属性的 POCO,并且映射为 nested
数据类型。
我正在尝试搜索以下对象的索引:
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; }
}
目的是按摘要 属性 或匹配标签集合中的任何字符串进行搜索
我目前拥有的是:
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();
}
但是嵌套部分不起作用,我不知道我是否以正确的方式使用它,或者我可能必须为此找到其他解决方案。
有什么想法吗?
提前谢谢大家
您不需要执行 nested
查询来查询 Tags
字段,因为每个标签只是一个原始 JSON 值,即 string
。只需 terms
查询就足够了。
需要 nested
查询的地方是 Tags
是具有多个属性的 POCO,并且映射为 nested
数据类型。