# Get cheapest trip departure based on inner hits

**URL:** <https://discuss.elastic.co/t/get-cheapest-trip-departure-based-on-inner-hits/220125>\
**Category:** Elasticsearch\
**Created:** [February 20, 2020, 8:29am UTC](https://discuss.elastic.co/t/get-cheapest-trip-departure-based-on-inner-hits/220125 "2020-02-20T08:29:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndyFurniss](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andyfurniss/32/62999_2.png) [@AndyFurniss](https://discuss.elastic.co/u/AndyFurniss)\
**Post date:** [February 20, 2020, 8:29am UTC](https://discuss.elastic.co/t/get-cheapest-trip-departure-based-on-inner-hits/220125/1 "2020-02-20T08:29:16Z")

</div>

I have a index containing trips. Each trip will have multiple departures, each departure will have it's own departure date and a collection of prices (each with a different currency).

I want to be able to query and find all trips within a certain date range and then for each of those trips, I want to highlight the cheapest price from the matching departures.

My data structure looks something like this:

```
{
	"id": 123,
	"name": "Cool trip",
	"highlightedDeparture": {
			"startDate": "2020-02-01",
			"yearMonth": 202001,
			"prices": [
				{
					"currencyCode": "GBP",
					"price": 99
				}
			]
		},
	"departures": [
		{
			"startDate": "2020-01-07",
			"yearMonth": 202001,
			"prices": [
				{
					"currencyCode": "GBP",
					"price": 123
				}
			]
		},
		{
			"startDate": "2020-01-14",
			"yearMonth": 202001,
			"prices": [
				{
					"currencyCode": "GBP",
					"price": 456
				}
			]
		},
		{
			"startDate": "2020-01-21",
			"yearMonth": 202001,
			"prices": [
				{
					"currencyCode": "GBP",
					"price": 789
				}
			]
		},
		{
			"startDate": "2020-02-01",
			"yearMonth": 202001,
			"prices": [
				{
					"currencyCode": "GBP",
					"price": 99
				}
			]
		}
	]
}

```

Currently, when the trip is indexed, the highlighted departure is set by default to the overall cheapest departure and this is pre-calculated prior to indexing currently.

I want to be able to run the following query and somehow have the highlighted departure field 'overridden' by the cheapest for January 2020, which would be the one with a price of 123.

```
{ 
   "query":{ 
      "bool":{ 
         "must":[ 
            { 
               "nested":{ 
                  "inner_hits":{ 
                     "name":"highlightedAvailability",
                     "sort":[ 
                        { 
                           "departures.prices.price":{ 
                              "nested":{ 
                                 "filter":{ 
                                    "term":{ 
                                       "departures.prices.currencyCode":{ 
                                          "value":"GBP"
                                       }
                                    }
                                 },
                                 "path":"departures.prices"
                              },
                              "order":"asc"
                           }
                        }
                     ]
                  },
                  "path":"departures",
                  "query":{ 
                     "term":{ 
                        "departures.yearMonth":{ 
                           "value":"202001"
                        }
                     }
                  }
               }
            }
         ]
      }
   },
   "_source":{ 
      "excludes":[ 
         "departures"
      ],
      "includes":[ 
         "*"
      ]
   }
}

```

My desired response from this query would look like this:

```
{
	"id": 123,
	"name": "Cool trip",
	"highlightedDeparture": {
		"startDate": "2020-01-07",
		"yearMonth": 202001,
		"prices": [
			{
				"currencyCode": "GBP",
				"price": 123
			}
		]
	}
}

```

Currently, I am achieving this by post-processing the elastic response and selecting the cheapest from the inner hits. This feels inefficient and is adding latency. I'd like to know if this is possible with native Elasticsearch.

FYI - The indexing can be changed if necessary.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 20, 2020, 10:46am UTC](https://discuss.elastic.co/t/get-cheapest-trip-departure-based-on-inner-hits/220125/2 "2020-02-20T10:46:30Z")

</div>

I just tried your example locally and I think you are almost there. Adding ` "size": 1` to your inner\_hits query and then checking

```auto
hits.hits[0].inner_hits.highlightedAvailability.hits.hits[0]._source

```

looks like the part that you are after. I might be missing something though...

---

<div class="post-metadata">

**Author:** ![AndyFurniss](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andyfurniss/32/62999_2.png) [@AndyFurniss](https://discuss.elastic.co/u/AndyFurniss)\
**Post date:** [February 20, 2020, 10:50am UTC](https://discuss.elastic.co/t/get-cheapest-trip-departure-based-on-inner-hits/220125/3 "2020-02-20T10:50:09Z")

</div>

Yeah, so I'm able to get the inner hits as expected currently from the location you mentioned. I'm just wondering if there's a way for me to _ **override** _ the `headlineDeparture` field with the inner hit as part of the overall Elastic query itself. Currently I have to manually override it in my code once the query has finished and I have the response, which obviously adds a bit of latency to my overall process. It's not a drastic hit but I just want to be as efficient as possible and if there's a way to do it natively then I'd rather use that.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 20, 2020, 1:08pm UTC](https://discuss.elastic.co/t/get-cheapest-trip-departure-based-on-inner-hits/220125/4 "2020-02-20T13:08:04Z")

</div>

No, there is no way to override, as this is directly extracted from the JSON, that part will need to be done on the client side.

---

<div class="post-metadata">

**Author:** ![AndyFurniss](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andyfurniss/32/62999_2.png) [@AndyFurniss](https://discuss.elastic.co/u/AndyFurniss)\
**Post date:** [February 20, 2020, 3:58pm UTC](https://discuss.elastic.co/t/get-cheapest-trip-departure-based-on-inner-hits/220125/5 "2020-02-20T15:58:19Z")

</div>

Is this just flat out not possible in any way? I can change my indexing and document structure if necessary but no worries if not.

---

<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:** [March 19, 2020, 3:58pm UTC](https://discuss.elastic.co/t/get-cheapest-trip-departure-based-on-inner-hits/220125/6 "2020-03-19T15:58:21Z")

</div>

Closed on March 19, 2020, 3:58pm UTC

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