0

I have index mapping:

{
  "dev.directory.3" : {
    "mappings" : {
      "profile" : {
        "properties" : {
          "email" : {
            "type" : "string",
            "index" : "not_analyzed"
          },
          "events" : {
            "type" : "nested",
            "properties" : {
              "id" : {
                "type" : "integer"
              },
              "name" : {
                "type" : "string",
                "index" : "not_analyzed"
              },
            }
          }
        }
      }
    }
  }
}

with data:

"hits" : [ {
    "_index" : "dev.directory.3",
    "_type" : "profile",
    "_id" : "1",
    "_score" : 1.0,
    "_source" : {
      "email" : "[email protected]",
      "events" : [ 
      {
        "id" : 111,
        "name" : "ABC",
      },
      {
        "id" : 222,
        "name" : "DEF",
      } 
      ],
    }
}]

I'd like to filter only matched nested elements instead of returning all events array - is this possible in ES?

Example query:

{
      "nested" : {
          "path" : "events",
          "query" : {
              "bool" : {
                  "filter" : [
                    { "match" : { "events.id" : 222 } },
                  ]
              }
          }
      }
  }

Eg. If I query for events.id=222 there should be only single element on the result list returned.

What strategy for would be the best to achieve this kind of requirement?

1 Answer 1

1

You can use inner_hits to only get the nested records which matched the query.

{
  "query": {
    "nested": {
      "path": "events",
      "query": {
        "bool": {
          "filter": [
            {
              "match": {
                "events.id": 222
              }
            }
          ]
        }
      },
      "inner_hits": {}
    }
  },
  "_source": false
}

I am also excluding the source to get only nested hits

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.