MongoDB search text in specific language on item with multiple languages

Viewed 256

I have a collection which indexes combined data from two other collections.

{
    "type" : "page",
    "translation" : [
        {
            "language" : "dutch",
            "content" : {
                "title" : "Dutch appel peer banaan",
                "intro" : "Dutch appel peer banaan",
                "body" : "Dutch appel peer banaan"
            }
        },
        {
            "language" : "french",
            "content" : {
                "title" : "French pomme poire banane",
                "intro" : "French pomme poire banane",
                "body" : "French pomme poire banane"
            }
        },
        {
            "language" : "english",
            "content" : {
                "title" : "English apple pear banana",
                "intro" : "English apple pear banana",
                "body" : "English apple pear banana"
            }
        }
    ]
}

I would like to be able to search this collection for keywords in a specific language. Searching for 'pear' in English, for example, should return the item. Searching for that same keyword ('pear') in dutch should return nothing.

This is the query I am using to search my collection.

Collection.find({
    $text: {
        $search: req.params.input,
        $language: language, // language variable eg 'dutch'
    },
})

The indexes on the collection are created as follows (through the mongoose schema).

Schema.index({
    'translation.content.body': 'text',
    'translation.content.title': 'text',
    'translation.content.intro:': 'text',
}, {
    name: 'SearchIndex',
});

This search returns all items where the keyword exists on any language and not the language specified. Is there any way I can indicate I only want to search the items where "language" : "dutch" (or any other language passed as a parameter)?

0 Answers
Related