1

I am using elasticsearch-6.4.3

I have created an index test_index with the following mapping:

PUT test_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "name": {
          "type": "keyword"
        },
        "segments": {
          "type": "nested",
          "properties": {
            "f1": {
              "type": "keyword"
            },
            "f2": {
              "type": "integer"
            }
          }
        }
      }
    }
  }
}

I inserted a document in the index with the following command:

POST /test_index/_doc
{
  "name": "ABC",
  "segments": [{
    "f1": "abc",
    "f2": 123
  }, 
  {
    "f1": "def",
    "f2": 124
  }]
}

I have an array of values say arr = ["def", "xyz"]. I want to match all the documents whose atleast one of the f1 field in segments field match with any of the values in the given array arr.

You can call it something like array_intersect to easier to understand.

I am trying something like this:

GET /test_index/_search
{
"query": {
    "nested": {
      "path": "segments",
      "query": {
        "bool": {
          "must": [
            {
              "terms": {
                "f1": [ 
                  "def",
                  "xyz"
                ]
              }
            }
          ]
        }
      }
    }
  }
}

I am getting no results as the output.
Expected output: The document should match as "def" is present as value of f1 in the document.

1 Answer 1

1

You would need to specify the full path in your terms query as segments.f1 instead of f1 for nested query.

Modify the query as below:

POST test_index/_search
{ 
   "query":{ 
      "nested":{ 
         "path":"segments",
         "query":{ 
            "bool":{ 
               "must":[ 
                  { 
                     "terms": {
                       "segments.f1": ["def","xyz"]    <---- Mention full path here

                     }
                  }
               ]
            }
         }
      }
   }
}

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Isn't it the same as IN clause equivalent?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.