如何在弹性搜索+ PHP中传递空过滤器

时间:2017-11-02 10:14:49

标签: php mysql elasticsearch

我想过滤搜索结果。我想搜索如果没有指定'manuf_id'应该返回所有记录。下面是我在弹性搜索查询。

$params = [
            'index' => $this->client->getIndex(),
            'type'  => $this->client->getType(),
            "from" => $from, "size" => $productPerPage,
            'body'  => [
           "query" => [
               "bool" => [
                  "must" => [
                     [
                          "multi_match" => [
                              "fields" => ["prod_name", "prod_seo_name"],
                              "type" => "phrase_prefix",
                              "query" => 'samsung'
                          ]
                     ],
                        //$conditionArr
                     [
                          "term"=> ["manuf_id"=>null]
                     ]
                  ]
               ]
            ]
            ],
       ];

以上查询未运行。有什么我想念的吗?任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

只有在必要时才能传递此条件,如下所示:

$params = [
    'index' => $this->client->getIndex(),
    'type'  => $this->client->getType(),
    "from" => $from, "size" => $productPerPage,
    'body'  => [
        "query" => [
           "bool" => [
              "must" => [
                 [
                      "multi_match" => [
                          "fields" => ["prod_name", "prod_seo_name"],
                          "type" => "phrase_prefix",
                          "query" => 'samsung'
                      ]
                 ],
              ]
           ]
        ]
    ],
];
if ($manufId > 0) {
  $params['body']['query']['bool']['must'][] = [
      'term' => ['manuf_id' => $manufId],
  ];
}