在elasticsearch嵌套中查询一个具有多个值的字段



我使用Elasticsearch和nest组合了两个查询,第一个是对特定术语的全文搜索,第二个是筛选或查询另一个字段,该字段是文件路径,但应该是多个文件路径,路径可以是部分或完整路径,我可以查询一个文件路径但无法查询多个文件的路径,有什么建议吗?

Search<SearchResults>(s => s
.Query(q => q
.Match(m => m.Field(f => f.Description).Query("Search_term"))
&& q
.Prefix(t => t.Field(f => f.FilePath).Value("file_Path"))
)
);

对于搜索多个路径,您可以在弹性搜索中使用bool Query,然后使用Should Occur进行类似逻辑OR的搜索,因此您的代码应该如下所示:

Search<SearchResults>(s => s
.Query(q => q.
Bool(b => b
.Should(
bs => bs.Wildcard(p => p.FilePath, "*file_Pathfile_Path*"),
bs => bs.Wildcard(p => p.FilePath, "*file_Pathfile_Path*"),
....
))
&& q.Match(m => m.Field(f => f.description).Query("Search_term")
)));

此外,您应该使用通配符查询来获取可能是部分路径或完整路径的路径的结果。有关更多信息,请查看下面关于WildQuery和BoolQuery的ES官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.htmlhttps://www.elastic.co/guide/en/elasticsearch/client/net-api/current/bool-queries.htmlhttps://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html

最新更新