mayur
mayur

Reputation: 131

Multi match query with AND condition in elastic search php

I want to search on multiple field with AND condition. I have tried many ways but I am not able to generate result.

below is my code.

   "query" =>[
                    "multi_match" => [
                    "fields" => ["prod_name", "prod_seo_name"],
                    "type" => "phrase_prefix",
                    "query" => $query
                    ],
                     "bool" => [
                        "must" => [
                        "term" => [ "cat_type_id" => 1 ],

                     ],
                ],
            ],

I want to search with product name whose cat_type_id is 1.

I have tried following too.

"query" =>[
                            "multi_match" => [
                            "fields" => ["prod_name", "prod_seo_name"],
                            "type" => "phrase_prefix",
                            "query" => $query
                    ],
                     "filter" => [
                        "term"=> ["cat_type_id"=>1]
                    ]

Any help or reference would be great.

Upvotes: 2

Views: 1635

Answers (1)

Val
Val

Reputation: 217344

The correct way to do it is like this:

"query" => [
   "bool" => [
      "must" => [
         [
              "multi_match" => [
                  "fields" => ["prod_name", "prod_seo_name"],
                  "type" => "phrase_prefix",
                  "query" => $query
              ]
         ],
         [
              "term"=> ["cat_type_id"=>1]
         ]
      ]
   ]
]

Upvotes: 2

Related Questions