【问题标题】:Query one field with multiple values in elasticsearch nest在elasticsearch nest中查询一个字段有多个值
【发布时间】:2020-12-19 03:21:49
【问题描述】:

我有两个查询与 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 nest


【解决方案1】:

要搜索多个路径,您可以在 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

【讨论】:

  • 感谢您的回复,但您认为我可以使用全文搜索来代替文件路径吗!
  • @m.alahdab 这取决于您对 FilePath 字段的分析器设置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-15
  • 2013-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
相关资源
最近更新 更多