【问题标题】:Multiple OR and AND conditions with Elasticsearch php使用 Elasticsearch php 的多个 OR 和 AND 条件
【发布时间】:2021-09-18 23:23:38
【问题描述】:

我想用 ElasticSearch 实现以下查询:

product_shipper = 1 AND ((product_type = product and price>0) OR (product_type = product_variation and price=0))

我已经构建了以下查询,但它不起作用,给我发送了一个空结果:

              "query"=> [
                  "bool"=> [
                     "must"=> [
                        [
                           "bool"=> [
                              "should" => [
                                    "bool"=> [
                                        "must"=> [
                                            [ "match" => [ "product_type" => "product" ] ],
                                            [ 
                                                'range'=>['price'=>['gt'=>0]]
                                            ],
                                        ]
                                    ],
                                     "bool"=> [
                                        "must"=> [
                                            [ "match" => [ "product_type" => "product_variation" ] ],
                                            [ "match" => [ "price" => 0 ] ]
                                        ]
                                    ]
                              ]
                           ]
                        ],
                        [ 'match' => [ 'product_shipper' => $shipper ] ],
                     ]
                  ]
               ]

我做错了什么?

【问题讨论】:

    标签: elasticsearch elasticsearch-php


    【解决方案1】:

    我在 should 子句级别的语法有一个小问题,这是工作代码示例,希望它可以帮助很多从 Elasticsearch 开始的人。

    "query"=> [
    "bool"=> [
        "must"=> [
            [
                "bool"=> [
                    "should" => [
                        [
                            "bool"=> [
                                "must"=> [
                                    [ "match" => [ "product_type" => "product" ] ],
                                    [ "range"=>['price'=>['gt'=>0]]],
                                ]
                            ],
                        ],
                        [
                            "bool"=> [
                                "must"=> [
                                    [ "match" => [ "product_type" => "product_variation" ] ],
                                    ['range'=>['price'=>['lte'=>0]]]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            [
                'match' => [ 'product_shipper' => $shipper ]
            ],
        ]
    ]
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      • 2019-04-10
      相关资源
      最近更新 更多