Elastic search Not filter inside and filter

♀尐吖头ヾ 提交于 2019-12-25 02:57:05

问题


I am trying to add a "not" filter inside "and" filter

Sample input:

{
   "query":{
      "filtered":{
         "query":{
            "query_string":{
               "query":"error",
               "fields":[
                  "request"
               ]
            }
         },
         "filter":{
            and:[
               {
                  "terms":{
                     "hashtag":[
                        "br2"
                     ]
                  },
                  "not":{
                     "terms":{
                        "hashtag":[
                           "br1"
                        ]
                     }
                  }
               }
            ]
         }
      }
   }
},

}

But above is giving error, i also tried various combination but in vain. Above is just an example in short i require a query in which both "and", "not" filter are present.


回答1:


you forgot the "filters" array.

Write it like this :

{
    "from" : 0,
    "size" : 25,
    "query" : {
        "filtered" : {
            "query" : {
                "match_all" : {}

            },
            "filter" : {
                "and" : {
                    "filters" : [{
                            "term" : {
                                "field1" : "val1"
                            }                           
                        }, {
                            "not" : {
                                "filter" : {
                                    "term" : {
                                        "field2" : "val2",
                                    }
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}


来源:https://stackoverflow.com/questions/24238710/elastic-search-not-filter-inside-and-filter

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