M
M
Mikhailo Poberezhny2016-08-22 18:53:54
MongoDB
Mikhailo Poberezhny, 2016-08-22 18:53:54

How to search for an incomplete match in 'mongoose-text-search'?

//event schema
var mongoose = require('mongoose');
var textSearch = require('mongoose-text-search');

var eventSchema = new mongoose.Schema({
    description: String,
    address: String
}, { collection: 'events' });

eventSchema.plugin(textSearch);

eventSchema.index(
    {
        description: 'text',
        address: 'text'
    },
    {
        name: 'textScore',
        weights: {
            description: 1,
            address: 2
        }
    }
);
module.exports = mongoose.model('Event', eventSchema);

//Events.js file
app.get('/test/:text', function(req,res) {
        var Event = require('../models/Event');
        Event
            .find(
                { $text : { $search :  req.params.text} },
                { score : { $meta: "textScore" } }
            )
            .sort({ score : { $meta : 'textScore' } })
            .exec(function(err, results) {
                res.send(results)
                // Находит только если "req.params.text" полностью совпадает с текстом в базе пробовал через регулярку аля
                // new RegExp(req.params.text, 'i'), в самом mongoose она работает а в этом плагине нет... помогите
            });
    })

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
serious911, 2016-08-25
@serious911

At one time, I implemented this task using $ regex.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question