简体   繁体   English

Elasticsearch:如何在过滤器上下文中编写“ OR”子句?

[英]Elasticsearch: How to write an 'OR' clause in filter context?

I'm looking for syntax/example compatible with ES version is 6.7. 我正在寻找与ES版本6.7兼容的语法/示例。 I have seen the docs, I don't see any examples for this and the explanation isn't clear enough to me. 我看过文档,但没有看到任何示例,对我来说解释还不够清楚。 I have tried writing query according to that, but I keep on getting syntax error. 我曾尝试据此编写查询,但我不断收到语法错误。 I have seen below questions on SO already but they don't help me: 我已经在SO上看到以下问题,但它们对我没有帮助:

Filter context for should in bool query (Elasticsearch) 布尔查询(Elasticsearch)中应过滤的上下文

It doesn't have any example. 没有任何例子。

Multiple OR filter in Elasticsearch Elasticsearch中的多重或过滤器

I get a syntax error 我收到语法错误

"type": "parsing_exception",
"reason": "no [query] registered for [filtered]",
"line": 1,
"col": 31

Maybe it's for a different version of ES. 也许是用于不同版本的ES。

All I need is a simple example with two 'or'ed conditions (mine is one range and one term but I guess that shouldn't matter much), both I would like to have in filter context (I don't care about scores, nor text search). 我需要的只是一个简单的示例,其中包含两个“或”条件(我是一个range和一个term但我想应该没什么大不了),我都希望在过滤器上下文中都拥有这两个条件(我不在乎分数) ,也不进行文本搜索)。

If you really need it, I can show my attempts (need to remove some 'sensitive'(duh) parts from it before posting), but they give parsing/syntax errors so I don't think there is any sense in them. 如果您确实需要它,我可以展示一下我的尝试(需要在发布之前从其中删除一些“敏感”(duh)部分),但是它们会带来解析/语法错误,因此我认为它们没有任何意义。 I am aware that questions which don't show any efforts are considered bad for SO but I don't see any logic in showing attempts that aren't even parsed successfully, and any example would help me understand the syntax. 我知道,没有付出任何努力的问题对于SO来说是不好的,但我看不出任何没有成功解析出尝试的逻辑,任何示例都可以帮助我理解语法。

You need to wrap your should query in a filter query. 您需要将您的should查询包装在filter查询中。

{  
   "query":{  
      "bool":{  
         "filter":[{  
            "bool":{  
               "should":[  
                  {  // Query 1 },
                  {  // Query 2 }
               ]
            }
         }]
      }
   }
}

I had a similar scenario (even the range and match filter), with one more nested level, two conditions to be 'or'ed (as in your case) and another condition to be logically 'and'ed with its result. 我有一个类似的场景(甚至是rangematch过滤器),还有一个嵌套的级别,两个条件要“或”(如您的情况),另一个条件要逻辑“与”其结果。 As @Pierre-Nicolas Mougel suggested in another answer I had nested bool clauses with one more level around the should clause. 作为@皮埃尔-萨科Mougel在另一个答案建议我已经嵌套bool条款多一个层次的周围should子句。

{
  "_source": [
    "my_field"
  ],
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "bool": {
                "should": [
                  {
                    "range": {
                      "start": {
                        "gt": "1558878457851",
                        "lt": "1557998559147"
                      }
                    }
                  },
                  {
                    "range": {
                      "stop": {
                        "gt": "1558898457851",
                        "lt": "1558899559147"
                      }
                    }
                  }
                ]
              }
            },
            {
              "match": {
                "my_id": "<My_Id>"
              }
            }
          ],
          "must_not": []
        }
      }
    }
  },
  "from": 0,
  "size": -1,
  "sort": [],
  "aggs": {}
}

I read in the docs that minimum_should_match can be used too for forcing filter context. 我在文档中看到, minimum_should_match也可以用于强制过滤器上下文。 This might help you if this query doesn't work. 如果此查询不起作用,这可能对您有帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM