Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run an autocomplete $search on multiple fields?

The documentation states that the path needs to be a string and not an array of strings. I just want to confirm that that is in fact the only possibility, and if, in either case, there is a recommended way to do this.

e.g. I want to search (with autocompletion) my movies for the text "hammer" on both the title and the plot

In the current scenario I can implement a search by either title or plot with ease. But If I try to do it for both, making path an array of strings, which is acceptable on other operators, I get an error

like image 797
Rodrigo Sasaki Avatar asked Jul 09 '20 22:07

Rodrigo Sasaki


1 Answers

If you are trying to match on title and plot, use should: in compound operator

create autocomplete search index on both fields : title and plot

$search: {
    compound: {
        should: [
            {
                autocomplete: {
                    query:'hammer',
                    path: 'title',
                },
            },
            {
                autocomplete: {
                    query:'hammer',
                    path: 'plot',
                },
            },
        ],
    },
},
like image 111
Jitendra Patel Avatar answered Oct 23 '22 06:10

Jitendra Patel