goda87
goda87

Reputation: 31

How can I exclude an array of ids from a query_string in Elastic Search?

I have a query String like this:

"(( name_first.raw:goda )) AND !( _uid:*566ade1cec8d83647a000061* OR _uid:*566ade1cec8d83647a000062* OR _uid:*566ade1cec8d83647a000063* OR _uid:*566ade1cec8d83647a000064*)"

How can I write this query in a more efficient way?

Upvotes: 2

Views: 1901

Answers (3)

Peter Dixon-Moses
Peter Dixon-Moses

Reputation: 3209

This should work, (+ => "must" and - => "must_not")

"+name_first.raw:goda -_uid:*566ade1cec8d83647a000061* -_uid:*566ade1cec8d83647a000062* -_uid:*566ade1cec8d83647a000063*  -_uid:*566ade1cec8d83647a000064*"

What worries me is that the processing of the leading wildcards could really slow this down. Do you absolutely need wildcards? If you find yourself needing to optimize this, and can start using the DSL, check out post_filter for your exclusion criteria.

Upvotes: 0

Linoy
Linoy

Reputation: 1395

I think this would work,

GET /yourindex/yourType/_search
{
    "query": {
        "filtered": {
           "query": {
           "match": {
              "name_first.raw": "goda"
           }
           },
           "filter": {
               "bool": {
                   "must_not": [
                      {
                          "terms": {
                             "_uid": [
                                "566ade1cec8d83647a000061",
                                "566ade1cec8d83647a000062",
                                "566ade1cec8d83647a000063"
                             ]
                          }
                      }
                   ]
               }
           }
        }
    }
}

Upvotes: 1

bittusarkar
bittusarkar

Reputation: 6357

You can use the query below:

POST <index>/<type>/_search
{
  "query": {
    "filtered": {
      "query": {
        "term": {
          "name_first.raw": "goda"
        }
      },
      "filter": {
        "bool": {
          "must_not": [
            {
              "terms": {
                "_uid": [
                  "566ade1cec8d83647a000061",
                  "566ade1cec8d83647a000062",
                  "566ade1cec8d83647a000063"
                ]
              }
            }
          ]
        }
      }
    }
  }
}

Upvotes: 1

Related Questions