ElasticSearch - NEST - 如何组合AND和OR语句 [英] ElasticSearch - NEST - how to combine AND and OR statements

查看:1560
本文介绍了ElasticSearch - NEST - 如何组合AND和OR语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢ElasticSearch - NEST API。



我们正在尝试使用AND / OR子句准备多个条件。
例如 - (条件1或条件2)AND(条件3)



目前,我们正在连接查询条件。



任何人都可以使用NEST API提供更好的示例?

解决方案

有一个叫 Bool Query 在ElasticSearch中。匹配其他查询的布尔组合(AND,OR,NOT)的文档的查询。它使用一个或多个布尔子句构建,每个子句都带有类型的事件。发生类型是:



必须:(AND)子句(查询)必须出现在匹配的文档中。



应该:(OR)子句(查询)应该出现在匹配的文档中。在不带有must子句的布尔查询中,一个或多个should子句必须与文档匹配。



must_not:(NOT)子句(查询)不能出现在匹配的文档中。



所以对于您给出的示例,您将得到以下查询:

  bool 

条件1
条件2
bool
必须
条件3

在ElasticSearch中,代码将如下所示:

 filter:{
bool:{
should:[
{term:{tag:value1}},
{term:{tag:value2}}
],
bool:{
必须:{term:{tag:value3 }}
}
}
}

在NEST中,它将如下所示:

 过滤器(f => f 
.Bool(b => b
.Should(
o => o.Term(t => t.Tag,Value1),
o => o.Term(t => t.Tag,Value2)),
o => o.Bool(bo => bo
.Must(a => Term(t => t.Tag,Value3))



根据 NEST文档,而不是写这样乏味和详细的查询,您可以使用NEST的按位运算符,这很简单。

  .Query((q => q.Term tag,value1)|| q.Term(tag,value2))&& q.Term(tag,value3))


I am new to ElasticSearch - NEST API.

We are trying to prepare multiple conditions with AND/OR clauses. For example - (Condition 1 OR Condition 2) AND (Condition 3)

Currently we are trying by concatenating the query conditions.

Can anyone provide the better example using NEST API?

解决方案

Well there is something called Bool Query in ElasticSearch. A query that matches documents matching boolean combinations (AND, OR, NOT) of other queries. It is built using one or more boolean clauses, each clause with a typed occurrence. The occurrence types are:

must: (AND)The clause (query) must appear in matching documents.

should: (OR) The clause (query) should appear in the matching document. In a boolean query with no must clauses, one or more should clauses must match a document. The minimum number of should clauses to match can be set using the minimum_should_match parameter.

must_not: (NOT) The clause (query) must not appear in the matching documents.

So for the example you have given, you will get the following query:

bool 
    should
        condition 1
        condition 2
    bool
        must
            condition 3

And in ElasticSearch the code will look like:

"filter": {
           "bool": {
               "should": [
                             {"term": {"tag": value1}},
                             {"term": {"tag": value2}}
                         ],
                         "bool": {
                                 "must":{"term": {"tag": "value3"}}
                                 }
                    }
           }

And in NEST it will look like:

Filter(f=>f
   .Bool(b=>b
       .Should(
          o=>o.Term(t=>t.Tag, Value1),
          o=>o.Term(t=>t.Tag, Value2)),
          o=>o.Bool(bo=>bo
            .Must(a=>a.Term(t=>t.Tag, Value3))
        )
    )
)

According to the NEST documentation instead of writing such tedious and verbose queries you can use NEST's Bitwise operators, which is simple.

.Query((q=>q.Term(tag, value1) || q.Term(tag, value2)) && q.Term(tag, value3))

这篇关于ElasticSearch - NEST - 如何组合AND和OR语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆