elasticsearch - 在elasticsearch nest中查询一个字段有多个值
问题描述
我有两个查询与 Elasticsearch 和嵌套的组合,第一个是对特定术语的全文搜索,第二个是过滤或查询另一个字段,它是文件路径,但它应该适用于许多文件路径和路径可以是部分路径或完整路径,我可以查询一个文件路径,但我无法为许多文件路径执行此操作,有什么建议吗?
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"))
)
);
解决方案
要搜索多个路径,您可以在 elasticsearch 中使用 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 和 Bool Query 的 ES 官方文档: https ://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html https://www.elastic .co/guide/en/elasticsearch/client/net-api/current/bool-queries.html https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query。 html
推荐阅读
- node.js - Multer 不能在 http 对象之外工作
- c# - IEnumerable 自身的异常
- angular - 为什么运行 yarn start 时出现语法错误
- android - JobIntentService onHandleWork 无法正常工作
- postgresql - Postgres:如何检测查询中的空间不足情况?
- jenkins - 未知的条件 beforeAgent。有效条件为:allOf、anyOf、branch、buildingTag、changeRequest、changelog、changeset、environment、equals
- python - pandas DataFrame 中的内存处理
- c# - ASP.NET Core 2.2 集成测试 DbContext 服务未注册
- c# - 如何解决取消保护会话 cookie 的错误
- python - 查看标准库和外部库的 Python 源代码