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

Viewed 1919

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

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',
                },
            },
        ],
    },
},
Related