Jack Pilowsky
Jack Pilowsky

Reputation: 2303

Elasticsearch: How to combine regex query with filter

I have a search that in some situations needs to be searched by a regex query

GET my-index/_search
{
  "query": {
    "regexp":{  
      "name":".*something.*"
    }  
  }

}

And sometimes needs to be filtered, like so:

GET /my-index/_search
{  
  "query":{  
      "bool":{  
      "filter":[  
           {  
              "term":{  
                 "createdByEmail.keyword":"[email protected]"
              }
           }
      ]
  }
}  

I want to combine these 2 so that it will only show me resolts where the name matches the regex AND the createdByEmail matches the email address I'm sending in.

Upvotes: 0

Views: 1295

Answers (1)

Nishant
Nishant

Reputation: 7864

You can add first query inside must clause of second as below:

{
  "query": {
    "bool": {
      "must": [
        {
          "regexp": {
            "name": ".*something.*"
          }
        }
      ],
      "filter": [
        {
          "term": {
            "createdByEmail.keyword": "[email protected]"
          }
        }
      ]
    }
  }
}

Upvotes: 5

Related Questions