简体   繁体   English

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

[英]How to pass null filter in elastic search + PHP

I want to filter search result. 我想过滤搜索结果。 I want to search that if 'manuf_id' is not specified than is should return all records. 我想搜索如果未指定'manuf_id'则应返回所有记录。 below is my search query in elastic. 以下是我的弹性搜索查询。

$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]
                     ]
                  ]
               ]
            ]
            ],
       ];

Above query is not running. 以上查询未运行。 is there something I am missing? 有什么我想念的吗? ANy help would be great. 任何帮助都会很大。

You can pass this condition only in case when it necessary, like this: 仅在必要时才可以通过此条件,例如:

$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],
  ];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM