Error in syntax with multi fields search

Hello!
I look at "Elasticsearch Do's, Don'ts and Pro-Tips - Itamar Syn Hershko" video tutorial at
https://www.youtube.com/watch?v=c9O5_a50aOQ I see condition on several
fiedls https://imgur.com/a/17zAZ4w and try to make it in my Laravel 5.7 app(with elasticsearch/elasticsearch plugin) as :

        $elasticQuery = [
            "bool" => [
                "must"   => [
                    "multi_match" => [
                        "query"  => $text,
                        "fields" => ["name^4", "description"]
                    ],
                ],
                "should" => [
                    "term" => [
                        "category_id" => 1,
                    ]
                ]
            ]
        ];

But I got error :

{"error":{"root_cause":[{"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":133}],"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":133},"status":400}

Can you say why error and which syntax is valid?

When I use simple condition:

                $elasticQuery = [
                    'multi_match' => [
                        'query' => $text,
                        'fields' => ['name^4', 'description'],
                    ],
                ];
I got valid results as I expected...

Thanks!

Making some search I found working next syntax :

        $elasticQuery = [
            "bool" => [
                'should' => [


                        [
                                "multi_match" => [
                                    "query"  => $text,
                                    "type"   => "cross_fields",
                                    "fields" => [
                                        "name^4",
                                        "description"
                                    ]
                                ]
                        ],

                        [
                            'match' => [
                                'category_id' => [
                                    'query' => 1,
                                ]
                            ]
                        ],

                        [
                            'match' => [
                                'category_id' => [
                                    'query' => 3,
                                ]
                            ]
                        ],
                    
                ]
            ]
        ];

when I need to make search by text fields and array of category (1 and 3) in example above.

It works, but looks like it works like "OR" condition, but I need to make restriction as "AND" if to use sql terminology...

Which way is correct to make restriction as "AND" ?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.