Reputation: 101
I have below search query and document how to use must not query. scenario is for matched shipment id it has to fetch the documents and it has to ignore the documents whenever there is a match for id below is document which is in the elastic search and the search query.
The Document
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.0,
"hits": [
{
"_index": "testing",
"_type": "recommendations",
"_id": "HkXEBnIB9OMCvyNvPDr-",
"_score": 1.0,
"_source": {
"id": "4d6b421b-c237-44b4-83d4-27bdcb26f8f2",
"clusterRank": null,
"recommendationRank": null,
"batchId": null,
"clusterId": "1",
"eventId": "1",
"exceptionId": "1",
"shipmentId": "123",
"eventType": "Delayed",
"item": "Item1",
"destinationLocation": "DC1",
"dueDate": "2019-01-12T05:30:00.000+0530",
"exceptionQuantity": 50,
"recommendationType": "stockTransfer",
"customerName": "Walmart",
"primaryRecommendation": true,
"priority": 1,
"sourceLocation": "DC3",
"transferQuantity": 40,
"shipDate": "2019-01-11T05:30:00.000+0530",
"arrivalDate": "2019-01-12T05:30:00.000+0530",
"transportMode": "Road",
"transferCost": 200.0,
"maxQtyAvailableForTransfer": 40,
"totalQtyAtSource": 40,
"operation": "Road-Item1-from-DC3-to-DC1",
"peggedStockDemandIds": null,
"revenueRecovered": 20000.0
}
},
{
"_index": "testing",
"_type": "recommendations",
"_id": "HUXEBnIB9OMCvyNvPDr-",
"_score": 1.0,
"_source": {
"id": "12d19e4e-90de-42e6-a6e6-cb83407cfcfb",
"clusterRank": null,
"recommendationRank": null,
"batchId": null,
"clusterId": "1",
"eventId": "1",
"exceptionId": "1",
"shipmentId": "1234",
"eventType": "Delayed",
"item": "Item1",
"destinationLocation": "DC1",
"dueDate": "2019-01-10T05:30:00.000+0530",
"exceptionQuantity": 100,
"recommendationType": "stockTransfer",
"customerName": "Walmart",
"primaryRecommendation": true,
"priority": 1,
"sourceLocation": "DC2",
"transferQuantity": 30,
"shipDate": "2019-01-09T05:30:00.000+0530",
"arrivalDate": "2019-01-10T05:30:00.000+0530",
"transportMode": "Road",
"transferCost": 300.0,
"maxQtyAvailableForTransfer": 30,
"totalQtyAtSource": 40,
"operation": "Road-Item1-from-DC2-to-DC1",
"peggedStockDemandIds": null,
"revenueRecovered": 15000.0
}
},
{
"_index": "testing",
"_type": "recommendations",
"_id": "H0XEBnIB9OMCvyNvPDr-",
"_score": 1.0,
"_source": {
"id": "c49c4440-dad6-4692-8ff0-ab4884f76ffe",
"clusterRank": null,
"recommendationRank": null,
"batchId": null,
"clusterId": "1",
"eventId": "1",
"exceptionId": "1",
"shipmentId": "123",
"eventType": "Delayed",
"item": "Item1",
"destinationLocation": "DC1",
"dueDate": "2019-01-12T05:30:00.000+0530",
"exceptionQuantity": 50,
"recommendationType": "stockTransfer",
"customerName": "Walmart",
"primaryRecommendation": true,
"priority": 2,
"sourceLocation": "DC2",
"transferQuantity": 10,
"shipDate": "2019-01-11T05:30:00.000+0530",
"arrivalDate": "2019-01-12T05:30:00.000+0530",
"transportMode": "Road",
"transferCost": 100.0,
"maxQtyAvailableForTransfer": 40,
"totalQtyAtSource": 40,
"operation": "Road-Item1-from-DC2-to-DC1",
"peggedStockDemandIds": null,
"revenueRecovered": 5000.0
}
}
]
}
}
The Search Query:
{
"size": 0,
"aggregations": {
"shippmentIdsMatch": {
"filter": {
"terms": {
"shipmentId.keyword": [
"1234"
],
"boost": 1
}
}
},
"query": {
"bool": {
"must_not": [
{
"term": {
"id.keyword": "1"
}
}
]
},
"aggregations": {
"by_shipmentId": {
"terms": {
"field": "shipmentId.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_exceptionId": {
"terms": {
"field": "exceptionId.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_item": {
"terms": {
"field": "item.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_destination": {
"terms": {
"field": "destinationLocation.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_trans": {
"terms": {
"field": "transportMode.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_sourcelocation": {
"terms": {
"field": "sourceLocation.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_shipdate": {
"terms": {
"field": "shipDate",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_arrival": {
"terms": {
"field": "arrivalDate",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"quantity": {
"sum": {
"field": "transferQuantity"
}
},
"transfercost": {
"sum": {
"field": "transferCost"
}
},
"revenueRecovered": {
"sum": {
"field": "revenueRecovered"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
The Response:
{
"error": {
"root_cause": [
{
"type": "named_object_not_found_exception",
"reason": "[15:21] unable to parse BaseAggregationBuilder with name [bool]: parser not found"
}
],
"type": "named_object_not_found_exception",
"reason": "[15:21] unable to parse BaseAggregationBuilder with name [bool]: parser not found"
},
"status": 400
}
Upvotes: 0
Views: 749
Reputation: 217474
You cannot have a query
inside the aggregations
section... so not sure how you built this. query
and aggregations
are siblings, so in principle you should have something like this:
{
"size": 0,
"query": { <--- query part starts here
"bool": {
"must_not": [
{
"term": {
"id.keyword": "1"
}
}
]
}
},
"aggregations": { <--- sibling aggregation part starts here
"shippmentIdsMatch": {
"filter": {
"terms": {
"shipmentId.keyword": [
"1234"
],
"boost": 1
}
},
...
Upvotes: 1