elastic search 如何使用 must not query inside filter

elastic search how to use must not query inside filter

我下面有搜索查询和文档怎么用不得查询。场景是匹配的装运 ID 它必须获取文档,并且只要下面的 ID 匹配是弹性搜索和搜索查询中的文档,它就必须忽略文档。

文档

{
    "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
                }
            }
        ]
    }
}

搜索查询:

{
    "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"
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

回应:

{
    "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
}

aggregations 部分中不能有 query...所以不确定您是如何构建它的。 queryaggregations 是兄弟姐妹,所以原则上你应该有这样的东西:

{
  "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
        }
      },
      ...