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

1

12

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

Fledgling answered 9/7, 2020 at 22:16 Comment(1)
#63187315 <-- check out that answer. It's the same problem in solution with more data. It should solve your problem..Herminiahermione
A
16

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',
                },
            },
        ],
    },
},
Aileenailene answered 12/5, 2021 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.