Read part 1 of this series here.

Last time we saw how to use range fields to store range values in Elasticsearch. You could store two discontinuous brightness ranges supported by a TV operating in two modes (SDR and HDR). However, we did not associate the ranges with their respective modes.

To do that properly, we need to make use of nested types in Elasticsearch.

PUT televisions_nested_index
{
    "mappings": {
        "properties": {
            "name": {
                "type": "text"
            },
            "brightness": {
                "type": "nested",
                "properties": {
                    "mode": {
                        "type": "text"
                    },
                    "value": {
                        "type": "integer_range"
                    }
                }
            }
        }
    }
}

Let’s put in the same TVs as last time, but with the mode information attached.

PUT televisions_nested_index/_doc/sony_bravia
{
    "name": "Sony Bravia",
    "brightness": [
        {
            "mode": "SDR",
            "value": {
                "gte": 100,
                "lte": 300
            }
        }
    ]
}

PUT televisions_nested_index/_doc/lg_oled
{
    "name": "LG OLED",
    "brightness": [
        {
            "mode": "SDR",
            "value": {
                "gte": 100,
                "lte": 400
            }
        },
        {
            "mode": "HDR",
            "value": {
                "gte": 500,
                "lte": 1000
            }
        }
    ]
}

Now we can query for a specific brightness range in a particular mode. So let’s search for a TV that supports 600 to 700 nits in the HDR mode.

GET televisions_nested_index/_search
{
    "query": {
        "bool": {
            "must": [
                "nested": {
                    "inner_hits": {},
                    "path": "brightness",
                    "query": {
                        "bool": {
                            "must": [
                                {
                                    "term": {
                                        "brightness.mode": {
                                            "value": "HDR"
                                        }
                                    }
                                },
                                {
                                    "range": {
                                        "brightness.value": {
                                            "gte": 600,
                                            "lte": 700
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }
}

This will return the LG TV since it is the only one that supports our required brightness in the HDR mode.

Thanks to the person who answered my question on Stack Overflow. It helped me make progress when I was completely stuck.