简体   繁体   English

如何使用NEST准确表示此ElasticSearch查询?

[英]How do I accurately represent this ElasticSearch query using NEST?

Background / Goal 背景/目标

I have a query in ElasticSearch where I'm using filters on a several fields (relatively small data set and we know exactly what values should be in those fields at the time we query). 我在ElasticSearch中有一个查询,我在几个字段上使用过滤器(相对较小的数据集,我们确切地知道在查询时这些字段中应包含哪些值)。 The idea is that we'll perform a full-text query but only after we've filtered on some selections as made by the user. 这个想法是,我们将执行全文查询,但前提是我们已经过滤掉用户做出的某些选择。

I'm putting ElasticSearch behind a WebAPI controller and figured it made sense to use NEST to accomplish the query. 我将ElasticSearch放在WebAPI控制器后面,并认为使用NEST完成查询是有意义的。

The query, in plain English 查询,用简单的英语

We have filters for several fields. 我们有几个领域的过滤器。 Each inner filter is an or filter, but they're together as an AND. 每个内部过滤器都是“或”过滤器,但它们在一起都是“与”号。

In SQL, the pseudo-code equivalent would be select * from table where foo in (1,2,3) AND bar in (4,5,6) . 在SQL中,伪代码等效项是select * from table where foo in (1,2,3) AND bar in (4,5,6)

Questions 问题

  • Can I simplify the way I'm thinking about this query, based on what you see below? 我可以根据您在下面看到的内容简化思考该查询的方式吗? Am I overlooking some basic approach? 我是否忽略了一些基本方法? This seems heavy but I'm new to ES. 这看起来很沉重,但是我是ES的新手。
  • How would I properly represent the query below in NEST syntax? 我如何用NEST语法正确表示以下查询?
  • Is NEST the best choice for this? NEST是否是最佳选择? Should I be using the ElasticSearch library instead and going lower level? 我应该改为使用ElasticSearch库并进入较低级别吗?

The Query Text 查询文字

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "or": [
                { "term": { "foo": "something" } },
                { "term": { "foo": "somethingElse" } }
              ]
            },
            {
              "or": [
                { "term": { "bar": "something" } },
                { "term": { "bar": "somethingElse" } }
              ]
            }
          ]
        }
      }
    }
  },

  "size": 100
}

This kind of task is quite simple and popular in ES. 这种任务非常简单,在ES中很流行。 You can represent it in NEST like following: 您可以像下面这样在NEST中表示它:

var rs = es.Search<dynamic>(s => s
            .Index("your_index").Type("your_type")
            .From(0).Size(100)
            .Query(q => q
                .Filtered(fq => fq
                    .Filter(ff => ff
                        .Bool(b => b
                            .Must(
                                m1 => m1.Terms("foo", new string[] { "something", "somethingElse" }),
                                m2 => m2.Terms("bar", new string[] { "something", "somethingElse" })
                            )
                        )
                    )
                    .Query(qq => qq
                        .MatchAll()
                    )
                )
            )
        );

Some notes: 一些注意事项:

  • I use filtered query to filter what I need first, then search stuffs later. 我使用过滤查询来首先过滤需要的内容,然后再搜索内容。 In this case the it will filter for foo in ("something", "somethingElse") AND bar in ("something", "somethingElse") , then query all filtered results ( match_all ). 在这种情况下,它将过滤foo in ("something", "somethingElse") AND bar in ("something", "somethingElse") ,然后查询所有过滤后的结果( match_all )。 You can change match_all to what you need. 您可以将match_all更改为所需的内容。 filtered query it's for best performance as ES will only need to evaluate scores of documents in query part (after filtered), not all documents. filtered query是为了获得最佳性能,因为ES仅需要评估query部分中的文档分数(过滤后),而不是所有文档。
  • I use terms filter, which more simple and better performance than or . 我使用terms过滤器,该过滤器比or更加简单和更好的性能。 Default mode of terms is OR all input terms, you can refer more in document about available modes (AND, OR, PLAIN, ...). terms默认模式是“或”所有输入术语,您可以在文档中更多地了解可用模式(“与”,“或”,“普通”等)。

Nest is best choice for .NET in my opinion as it designed for simple & easy to use purposes. 我认为Nest是.NET的最佳选择,因为它是为简单易用的目的而设计的。 I only used lower API if I want to use new features that Nest does not support at that time, or if Nest have bugs in functions I use. 仅当我想使用当时Nest不支持的新功能,或者Nest在使用的功能中存在错误时,才使用较低的API。 You can refer here for a brief NEST tutorial: http://nest.azurewebsites.net/nest/writing-queries.html 您可以在此处参考以获取简短的NEST教程: http : //nest.azurewebsites.net/nest/writing-queries.html

Updated: Building bool filters dynamic: 更新:动态构建布尔过滤器:

var filters = new List<Nest.FilterContainer>();
filters.Add(Nest.Filter<dynamic>.Terms("foo", new string[] { "something", "somethingElse" }));
// ... more filter

then replace .Bool(b => b.Must(...)) with .Bool(b => b.Must(filters.ToArray())) 然后将.Bool(b => b.Must(...))替换为.Bool(b => b.Must(...)) .Bool(b => b.Must(filters.ToArray()))

Hope it help 希望对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在NodaTime中准确表示日期范围? - How do I accurately represent a Date Range in NodaTime? 如何使用C#使用Nest Elasticsearch编写查询? - How can i write a query by using Nest Elasticsearch by C# ? 如何使用NEST为Elasticsearch指定索引? - How do I specify an index for Elasticsearch using NEST? 如何使用NEST for Elasticsearch动态创建查询 - How to dynamically create query using NEST for Elasticsearch 我如何使用Nest API 5在Elasticsearch中进行重新索引以从Elastic Search 1.4迁移到ElasticSearch 5 - How can i do reindexing in elasticsearch to migrate from elastic search 1.4 to elasticsearch 5 using Nest api 5 如何使用NEST将RandomScore应用于FunctionScore查询 - How do I apply RandomScore to a FunctionScore query using NEST 如何使用 Nest C# 在弹性搜索查询 (EQL) 中使用不存在或空值? - How can I use not exist or null in elasticsearch query (EQL) by using Nest C#? 在ElasticSearch上使用NEST - 如何使用部分填充的对象作为搜索条件 - Using NEST on ElasticSearch - How do i use a partially populated object as a search criteria 如何使用NEST构造来自多个类型的字段且没有魔术字符串的ElasticSearch搜索 - How do I construct an ElasticSearch search using NEST with fields from multiple types without magic strings 如何使用 ElasticSearch Nest 版本 7.x 创建 IndexTemplate - How do I create an IndexTemplate using ElasticSearch Nest Version 7.x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM