I have documents of the following format:
{name: 'A', website: 'example.com', date: 1, + other fields}
{name: 'A', website: 'example.com', date: 2, + other fields}
{name: 'B', website: 'example.com', date: 2, + other fields}
{name: 'A', website: 'something.com', date: 1, + other fields}
{name: 'A', website: 'something.com', date: 2, + other fields}
{name: 'C', website: 'something.com', date: 1, + other fields}
{name: 'C', website: 'something.com', date: 2, + other fields}
I would like to multi-query on name and website, while also returning only the latest result. My query looks like this:
query: {
bool: {
...optional filters...,
must: {
multi_match: {
query: input,
type: "most_fields",
fields: ["name^3", ..., "website"],
},
},
},
},
My desired output should look like this, ordered by _score:
{name: 'A', website: 'example.com', date: 2, + other fields}
{name: 'B', website: 'example.com', date: 2, + other fields}
{name: 'A', website: 'something.com', date: 2, + other fields}
{name: 'C', website: 'something.com', date: 2, + other fields}
Now I understand that an agg is required to get the latest result using top_hits, e.g.:
top_hits: {
size: 1,
sort: [{ date: "desc" }],
},
However, in the process of aggregating by website then by name, I lose the ordering by _score which is important for my query. I have already tried using composite agg, how it's not possible to order it by the score of the resulting records.