Elasticsearch - combining "match" and boolean "must_not"

Viewed 56

Looks like I can not combine "match" and "boolean". Is there any way I can write the elastic search query with ( search_string like '%test1%' AND search_string like '%test2%' AND access="PRIVATE") ? Here I can have any number of "test1" "test2", "test3"... with "and" operator

GET /my_data/_search
    {
      "query": {
        "match": {
          "search_string": {
            "query": "test1 test2",
            "operator": "and"
          }
        },
        "bool": {
          "must_not": {
            "term": {
              "access": "PRIVATE"
            }
          }
        }
      }
    }
1 Answers

You can wrap them both with a boolean must query, like so:

GET /my_data/_searc
{
    "query": {
        bool: {
            must: [
                {

                    "match": {
                        "search_string": {
                            "query": "test1 test2",
                            "operator": "and"
                        }
                    }
                },
                {
                    "bool": {
                        "must_not": {
                            "term": {
                                "access": "PRIVATE"
                            }
                        }
                    }
                }
            ]
        }
    }
};
Related