I'm using nodeJs Mongoose to perform text search;
var mongoose = require('mongoose');
var config = require('../config');
var mongoosePaginate = require('mongoose-paginate');
var poiSchema = mongoose.Schema({
city:String,
cap:String,
country:String,
address: String,
description: String,
latitude: Number,
longitude: Number,
title: String,
url: String,
images:Array,
freeText:String,
owner:String,
});
poiSchema.index({'$**': 'text'});
poiSchema.plugin(mongoosePaginate);
mongoose.Promise = global.Promise;
mongoose.connect(config.database);
module.exports = mongoose.model('Poi', poiSchema);
As you can see here
poiSchema.index({'$**': 'text'});
I create a text index on every field inside my schema.
When I try to perform a text search, I develop this code:
var term = "a search term";
var query = {'$text':{'$search': term}};
Poi.paginate(query, {}, function(err, pois) {
if(!pois){
pois = {
docs:[],
total:0
};
}
res.json({search:pois.docs,total:pois.total});
});
Unfortunately, when I use whitespace inside term search, it will fetch all documents inside the collection that matches every single field inside term search split by whitespace.
I imagine that text index has as tokenizer whitespace;
I need to know how to escape whitespace in order to search every field that has the entire term search without splitting it.
I tried replacing whitespace with \\
but nothing changes.
Could please someone help me?