Elasticsearch查询简单类别搜索,按价格排序并按范围获取记录

时间:2016-03-05 10:12:22

标签: laravel elasticsearch

我想用以下条件编写查询:

  • 按类别搜索(例如category ='cat1')
  • 且价格范围(且价格在100到500之间)
  • 按价格排序(从低到高)

我试过了:

    $params = [
                'index' => 'my_index',
                'type' => 'product',
                'body' =>  [
                        //"from" => 0, "size" => 2,
                        "sort" => [
                                    ["default_product_low_price.sale_price" => ["order" => "asc"]]
                                ],
                        'query'=> $query,
                        "aggs" => [
                        "default_product_low_price" => [
                        "nested" => [
                            "path" => "default_product_low_price"
                            ],
                        "aggs" => [
                                "range" => ["default_product_low_price.sale_price" => [ "gte" => '790',
                                "lte" => '1000' ]],
                                //"max_price" => ["max" => [ "field" => "default_product_low_price.sale_price" ]]
                            ],
                        ]
                    ]
                ]
            ];

但我收到错误

  

GuzzleConnection.php第277行中的Bad Request 400异常:错误。

请指导我错在哪里?什么是正确的查询?

1 个答案:

答案 0 :(得分:0)

我认为这应该是您的查询:

      $params = [
                'index' => 'my_index',
                'type' => 'product',
                'body' =>  [
                    "sort" =>[
                                ["default_product_low_price.sale_price" => ["order" => "asc"]]
                            ],
                        "query"=> [
                          "filtered"=> [
                             "query"=> [
                                "match_all"=> []
                             ],
                             "query"=>[
                                       "term"=> [
                                          "category.name"=> "jeans"
                                       ]
                                    ],
                             "filter"=> [
                                "nested"=> [
                                   "path"=> "default_product_low_price",
                                   "filter"=> [
                                      "bool"=> [
                                         "must"=> [
                                            [
                                               "range"=> [
                                                  "default_product_low_price.sale_price"=> [
                                                    "gte"=> 100,
                                                    "lte"=> 200,
                                                  ]
                                               ]
                                            ]
                                         ]
                                      ]
                                   ]
                                ]
                             ]
                          ]
                       ]
                ]
            ];