Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch syntax error with multi fields search

I watched Elasticsearch Do's, Don'ts and Pro-Tips by Itamar Syn Hershko

https://www.youtube.com/watch?v=c9O5_a50aOQ

I see multiple conditions in several fields in the following image:

https://imgur.com/a/17zAZ4w

I tried to make it in my Laravel 5.7 app (with the elasticsearch/elasticsearch plugin) as seen in the following code:

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

but I got the error :

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

But when I use a simple condition:

        $elasticQuery = [
            'multi_match' => [
                'query' => $text,
                'fields' => ['name^4', 'description'],
            ],
        ];

I got a valid result:

[hits] => Array
    (
        [total] => 1
        [max_score] => 7.4126062
        [hits] => Array
            (
                [0] => Array
                    (
                        [_index] => select_vote
                        [_type] => vote
                        [_id] => 16
                        [_score] => 7.4126062
                        [_source] => Array
                            (
                                [id] => 16
                                [slug] => in-the-film-babe-what-type-of-animal-was-babe
                                [name] => In the film Babe, what type of animal was Babe?
                                [description] => Babe is a 1995 A...
                                [created_at] => 2018-11-10 09:14:15
                                [category_id] => 2
                                [category] => Array
                                    (
                                        [name] => Movie&Cartoons
                                        [slug] => movie-cartoons
                                        [created_at] => 2018-11-10 09:14:12
                                    )

                            )

                    )

            )

    )

is this a valid format for multi-request?

MODIFIED BLOCK # 2: Making some search I found work:

$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 a search by text fields and the array of category (1 and 3) in the example above it works but looks like it works like an "OR" condition, but I need to make a restriction like "AND" using SQL terminology...

Which way is correct in order to make a restriction as like "AND"?

Thanks!

like image 518
mstdmstd Avatar asked Nov 17 '18 08:11

mstdmstd


1 Answers

If you just change should to must it is not going to work because category_id cannot have two values at the same time (unless it's an array, but it's not).

You need to use the following query instead:

$elasticQuery = [
"bool" => [
    'must' => [
            [
                    "multi_match" => [
                        "query"  => $text,
                        "type"   => "cross_fields",
                        "fields" => [
                            "name^4",
                            "description"
                        ]
                    ]
            ],
    ],
    'filter' => [
            [
                'terms' => [
                    'category_id' => [ 1, 3 ]
                ]
            ]
    ]
]
];
like image 120
Val Avatar answered Sep 21 '22 12:09

Val