Nested inner hits in FreeText search use case

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 :frowning:

"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

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