How to use “OR” in Dev Tool Query

血红的双手。 提交于 2021-01-29 12:51:52

问题


Hi Bellow Search provides me Log where it has both "value": "HB" and "value": "1234567" as, I am using Term, however, What I am looking for this if this match

("value": "HB" OR "value": "TR" ) AND "value": "1234567"

but not understanding how to do in below, Can anyone please help me

GET _search

{ "query": { "bool": { "must": [ { "match": {"log.file.path":"mylog.log" } }


     {
       "term": {
          "GPS-LOG.COMMAND": {
           "value": "HB"
         }
       }
     },
      {
      "term": {
          "GPS-LOG.IMEI": {
           "value": "1234567"
         }
       }
     }



   ],   "filter": {
     "range": {
       "@timestamp": {
         "gte": "now-10m"
       }
     }   }
     } }

回答1:


At first glace, it seems like this should have a simple solution. However, since you are using the term query, you can only search one value at a time. I don't know your mapping but if you are using a text field you shouldn't be using term query.

However, to solve this using the term query, you have to create the OR operator using the minimum_should_match combined with should.

See the following code:

GET _search 
   { 
   "query":{ 
      "bool":{ 
         "must":[ 
            { 
               "match":{ 
                  "log.file.path":"mylog.log"
               }
            },
            { 
               "term":{ 
                  "GPS-LOG.IMEI":{ 
                     "value":"1234567"
                  }
               }
            },
            { 
               "bool":{ 
                  "should":[ 
                     { 
                        "term":{ 
                           "GPS-LOG.COMMAND":{ 
                              "value":"HB"
                           }
                        }
                     },
                     { 
                        "term":{ 
                           "GPS-LOG.COMMAND":{ 
                              "value":"TR"
                           }
                        }
                     }
                  ],
                  "minimum_should_match":1
               }
            }
         ],
         "filter":{ 
            "range":{ 
               "@timestamp":{ 
                  "gte":"now-10m"
               }
            }
         }
      }
   }
}


来源:https://stackoverflow.com/questions/59522942/how-to-use-or-in-dev-tool-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!