简体   繁体   中英

Combine range and must filter in elasticsearch

i have some problems with ElasticSearch. I am a newbie, so that is normal. My need is to configure the search on my website. I have some videos and there are some filters to decrease the shown results. I have exact match filters, but also range filters.

So my problem is to combine the search keyword with those filters, based on what the user is searching for.

I have made so far to combine the searched keyword with the range filters, but if there is set an exact match filter i have no idea how to do it.

Here is how the parameters go now to Elasticsearch:

[body] => Array
        (
            [query] => Array
                (
                    [filtered] => Array
                        (
                            [query] => Array
                                (
                                    [multi_match] => Array
                                        (
                                            [query] => messi
                                            [fields] => Array
                                                (
                                                    [0] => title
                                                    [1] => duration
                                                    [2] => id_category
                                                    [3] => id_tag
                                                )

                                            [fuzziness] => 0.5
                                        )

                                )

                            [filter] => Array
                                (
                                    [range] => Array
                                        (
                                            [duration] => Array
                                                (
                                                    [gte] => 0
                                                    [lte] => 300
                                                )

                                        )

                                )

                        )

                )

        )

How should it look like if i have filter to show me only the videos, matching the keyword "messi" with duration between 0 and 300 seconds in some category ?

Thnks in advice !

Have a look at the Bool Query . I would suggest you put your multi match query in the must part and move your filters to the (quite obvious) filter part. Because of that, ES will calculate a relevance score based on the multi match but combine it with cacheability of the filters. A short example:

<?php

$query = [
    'body' => [
        'query' => [
            'bool' => [
                'must' => [
                    [
                        'multi_match' => []
                    ],
                    ...
                ],
                'filter' => [
                    [
                        'range' => []
                    ],
                    ...
                ]
            ]
        ]
    ]
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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