首页 > 解决方案 > 在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"))
                            )
                            );

标签: elasticsearchnest

解决方案


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


推荐阅读