Roman
Roman

Reputation: 9441

Display parent information for inner_hits response

Apologies, the configs make this question quite long

I have a schema that looks something like this:

{
    "mappings": {
        "mytype": {
            "properties": {
                "event_datetime": {"type": "date"},
                "elements": {
                    "type": "nested",
                    "properties": {
                        "name": {"type": "keyword"},
                        "id": {"type": "text"},
                        "type": {"type": "keyword"},
                        "content": {
                            "type": "nested",
                            "properties": {
                                "html": {"type": "text"},
                                "label": {"type": "keyword"},
                                "text": {"type": "text"},
                                "title": {"type": "text"}
                            }
                        }
                    }
                }
            }
        }
    }
}

I want to find entries where the elements.content.html contains, say http:

{
    "_source": ["event_datetime"],
    "query": {
        "nested": {
            "path": "elements.content",
            "query": {
                "wildcard": {"elements.content.html": "*http*"}
            },
            "inner_hits": {}
        }
    }
}

This is what the relevant part of the response looks like

"_source": {
    "event_datetime": "2019-01-22T05:24:21"
},
"inner_hits": {
    "elements.content": {
        "hits": {
            "total": 1,
            "max_score": 1,
            "hits": [{
                "_index": "my_index",
                "_type": "mytype",
                "_id": "ABC",
                "_nested": {
                    "field": "elements",
                    "offset": 73,
                    "_nested": {
                        "field": "content",
                        "offset": 0
                    }
                },
                "_score": 1,
                "_source": {
                    "html": "blah https blah"
                }
    }...}

What I would like

I would like to elements.type to be included in the response.

I've tried various permutations of adding "_source" : ["elements.content.html", "elements.type"] into various levels but haven't figured out how to get the elements.type information attached to the response object (somewhere, don't really care where, as long as its attached and easily navigable).

Upvotes: 0

Views: 72

Answers (1)

Nishant
Nishant

Reputation: 7874

To do this you need inner_hits at parent level i.e. at elements level. You can therefore rephrase you query as below which is nested query inside nested query.

{
  "_source": [
    "event_datetime"
  ],
  "query": {
    "nested": {
      "path": "elements",
      "inner_hits": {
        "_source": [
          "elements.type",
          "elements.content.html"
        ]
      },
      "query": {
        "nested": {
          "path": "elements.content",
          "query": {
            "wildcard": {
              "elements.content.html": "*http*"
            }
          }
        }
      }
    }
  }
}

Upvotes: 1

Related Questions