# Nested inner hits in FreeText search use case

**URL:** <https://discuss.elastic.co/t/nested-inner-hits-in-freetext-search-use-case/191947>\
**Category:** Elasticsearch\
**Created:** [July 24, 2019, 5:28am UTC](https://discuss.elastic.co/t/nested-inner-hits-in-freetext-search-use-case/191947 "2019-07-24T05:28:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ros](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ros/32/11720_2.png) [@ros](https://discuss.elastic.co/u/ros)\
**Post date:** [July 24, 2019, 5:28am UTC](https://discuss.elastic.co/t/nested-inner-hits-in-freetext-search-use-case/191947/1 "2019-07-24T05:28:58Z")

</div>

I building a standard free text search on a site that sells cars.

In the search box the user can enter a search word that are passed on to the query where it is used to match both nested and non-nested properties.

I'm using `inner_hits` to limit the number of variants returned by the query (in this sample `variants` is not remove from `_source`)

When matching on a nested property `color` the `inner_hits` collection contains the correct variant as expected.

However when matching on a non-nested property `title` the `inner_hits` collection is empty. I understand why it's empty.

Can you suggest a better way to structure the query?

Another option would be to always just return at least 1 variant - but how can the be achieved?

**Mappings**

```
PUT test
{
  "mappings": {
    "car": {
      "properties": {
        "variants": {
          "type": "nested"
        }
      }
    }
  }
}

```

**Insert data**

```
PUT test/car/1
{
  "title": "VW Golf",
  "variants": [
    {
      "color": "red",
      "forsale": true
    },
    {
      "color": "blue",
      "forsale": false
    }
  ]
}

```

**Query by color**

```
GET test/_search
{
  "query": {
    "nested": {
      "path": "variants",
      "query": {
        "match": {
          "variants.color": "blue"
        }
      },
      "inner_hits": {}
    }
  }
}

```

**Color query: works as expected!**

```
"hits" : [
      {
        "_source" : {
          "title" : "VW Golf",
          "variants" : [
            {
              "color" : "red",
              "forsale" : true
            },
            {
              "color" : "blue",
              "forsale" : false
            }
          ]
        },
        "inner_hits" : {
          "variants" : {
            "hits" : {
              "total" : 1,
              "hits" : [
                {
                  "_nested" : {
                    "field" : "variants",
                    "offset" : 1
                  },
                  "_source" : {
                    "color" : "blue",
                    "forsale" : false
                  }
                }
              ]
            }
          }
        }
      }
    ]

```

**Query by brand**

```
GET test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "golf"
          }
        },
        {
          "nested": {
            "path": "variants",
            "query": {
              "match": {
                "variants.color": "golf"
              }
            },
            "inner_hits": {}
          }
        }
      ]
    }
  }
}

```

**Brand query result ☹**

```
"hits" : [
      {
        "_source" : {
          "title" : "VW Golf",
          "variants" : [
            {
              "color" : "red",
              "forsale" : true
            },
            {
              "color" : "blue",
              "forsale" : false
            }
          ]
        },
        "inner_hits" : {
          "variants" : {
            "hits" : {
              "total" : 0,
              "hits" : []
            }
          }
        }
      }

```

Cross posted here: [https://stackoverflow.com/questions/57166212/nested-inner-hits-in-freetext-search-use-case](https://stackoverflow.com/questions/57166212/nested-inner-hits-in-freetext-search-use-case)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 21, 2019, 5:29am UTC](https://discuss.elastic.co/t/nested-inner-hits-in-freetext-search-use-case/191947/2 "2019-08-21T05:29:03Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
