Using ES 5.2. I've indexed activities (classes, local events, etc) that are put on by businesses. Along these lines:
{
"name": "Johnny's BBQ Fundraiser",
"price": "$15/plate",
"date": "2016-04-28 12:00:00",
"location": "City Hall",
"business": {
"id": 423,
"name": "Johnny's Smokey BBQ Restaurant"
}
}
There are many different businesses, and each business runs (owns) many different activities.
I want to query for results such that each result is from a unique business.id
. How would I go about doing this?
Currently I'm inefficiently looping:
- query ES for 30 results (I'm only looking for 16)
- taking the first activity for each encountered
business.id
, temporarily store the encounteredbusiness.id
in a list, skipping other activities in the loop with already encounteredbusiness.id
s - on the subsequent loops, I query for another 30 results excluding (via
bool
:must_not
:terms
filter) previously encounteredbusiness.id
s from the list
This is terrible. The way our data is structured and how businesses interact with our site means that there are sometimes 5 or 6 loops, just to get 16 results unique by business.id
.
There must be a better way.