I'm using Elasticsearch 6.8.2. I'm trying to get all winning auctions by user_id=1. If I pass user_id=1 I want to get "Auction 1" because user_id=1 was the highest bidder. I tried using Bucket Selector Aggregation but wasn't able to make it work because of the nested doc type.
Here is the schema and the sample data:
Schema:
{
"mappings": {
"_doc": {
"properties": {
"bids": {
"type": "nested"
}
}
}
}
}
Data:
{
"title": "Auction 1",
"price": 50,
"bids": [
{"user_id": 1, "amount": 100},
{"user_id": 2, "amount": 200},
{"user_id": 1, "amount": 300}
]
},{
"title": "Auction 2",
"price": 200,
"bids": [
{"user_id": 3, "amount": 300},
{"user_id": 1, "amount": 400},
{"user_id": 5, "amount": 500}
]
}
And the query I got so far:
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "bids",
"query": {
"bool": {
"must": [
{
"terms": {
"bids.user_id": [
1
]
}
}
]
}
}
}
}
]
}
},
"sort": [
{
"price": "desc"
}
]
}
Please help me I'm desperate.