9

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://i.sstatic.net/CGbCC.jpg

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!

1
  • Please, look at MODIFIED BLOCK # 2 Commented Nov 21, 2018 at 8:54

1 Answer 1

5
+100

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 ]
                ]
            ]
    ]
]
];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.