Tom
Tom

Reputation: 6342

ElasticSearch query nested path filter OR

I have following index:

PUT /ab11
{
  "mappings": {
    "properties": {
      "product_id": {
        "type": "keyword"
      },
      "data": {
        "type": "nested",
        "properties": {
          "p_id": {
            "type": "keyword"
          }
        }
      }
    }
  }
}


PUT /ab11/_doc/1
{
  "product_id": "123",
  "data": [
    {
      "p_id": "a"
    },
    {
      "p_id": "b"
    },
    {
      "p_id": "c"
    }
  ]
}

I want to do query like following sql does(NOTE: I want to do filter not query, because I don't care about score) :

select * from abc11 where data.pid = "a" or data.pid = "b"

Upvotes: 0

Views: 96

Answers (1)

Val
Val

Reputation: 217554

You can do it like this because the terms query has OR semantics by default:

{
  "query": {
    "nested": {
      "path": "data",
      "query": {
        "terms": {
          "data.p_id": [
            "a",
            "b"
          ]
        }
      }
    }
  }
}

Basically, select all documents which have either "a" or "b" in their data.p_id nested docs.

Upvotes: 1

Related Questions