Elastic Search AND 和 OR 条件未返回正确的数据

问题描述 投票:0回答:1

我有两个文件来源的数据,一个是已购买状态,另一个是未购买状态。为了识别购买的状态,我使用了一个名为 purchase 的嵌套对象,它具有 purchase.location 和 purchase.purchased_on。因此,我能够确定该商品是在第二种情况下购买的。

我正在构建一个过滤器并想过滤购买的物品,它应该返回我的数据,文件的状态为已购买或应该有 purchase.location 作为值之一并且 purchase.purchased_on 应该大于 1.

我构建的弹性搜索查询是

{
 "query": {
   "bool": {
     "should": [
       {
         "terms": {
           "status": [
             "purchased"
           ]
         }
       },
       {
         "bool": {
           "should": [
             {
               "terms": {
                 "purchase.location": [
                   "flipkart",
                   "amazon",
                   "bestbuy"
                 ]
               }
             },
             {
               "range": {
                 "purchase.purchased_on": {
                   "gte": 1
                 }
               }
             }
           ]
         }
       }
     ]
   }
 }
}

只返回状态为purchase的数据,不返回其他状态的数据

elasticsearch elastic-stack aws-elasticsearch
1个回答
0
投票

使用嵌套查询,

{
    "query": {
        "bool": {
            "should": [
                {
                    "terms": {
                        "status": [
                            "purchased"
                        ]
                    }
                },
                {
                    "nested": {
                        "path": "purchase",
                        "query": {
                            "bool": {
                                "should": [
                                    {
                                        "terms": {
                                            "purchase.location": [
                                                "flipkart",
                                                "amazon",
                                                "bestbuy"
                                            ]
                                        }
                                    },
                                    {
                                        "range": {
                                            "purchase.purchased_on": {
                                                "gte": 1
                                            }
                                        }
                                    }
                                ],
                                "minimum_should_match": 1
                            }
                        }
                    }
                }
            ],
            "minimum_should_match": 1,
            "boost": 1.0
        }
    }
}

结果是,

{
    "took": 47,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.287682,
        "hits": [
            {
                "_index": "purchase",
                "_type": "_doc",
                "_id": "yb5za4cB6Rdc8HbD9stk",
                "_score": 1.287682,
                "_source": {
                    "purchase": [
                        {
                            "location": "flipkart",
                            "purchased_on": "2"
                        }
                    ]
                }
            },
            {
                "_index": "purchase",
                "_type": "_doc",
                "_id": "yr50a4cB6Rdc8HbD_cto",
                "_score": 1.0,
                "_source": {
                    "status": "purchased"
                }
            },
            {
                "_index": "purchase",
                "_type": "_doc",
                "_id": "y753a4cB6Rdc8HbDCcuM",
                "_score": 1.0,
                "_source": {
                    "purchase": [
                        {
                            "purchased_on": "4"
                        }
                    ]
                }
            }
        ]
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.