3

I have one problem I wanted to implement full text search for mongodb, so i got stuck becouse in database created just one index for text search and i want to have multiple fields for search .. exmaple of schema :

name: {
    type: String,
    required: true,
    trim: true,
    index: 'text'
  },
  sub_name: {
    type: String,
    trim: true

  },
  location: [{
      geo: {
        type: [Number],
        index: '2d'
      },
      description: {
        type: String,
        trim: true,
        index: 'text'
      },
      address: {

        city: {
          type: String,
          index: 'text'
        },

        street: {
          type: String,
          index: 'text'
        },
        state: {
          type: String,
          index: 'text'
        }
      },

so what is the trick to do that ? how can i apply mutliple fields for full text search ? thx for anwser ...

Maco
  • 113
  • 6
  • 12

2 Answers2

7

Try something like that:

var schema= new Schema({
  name: String,
  sub_name: String,
  tags: { type: [String], index: true } // field level
});

schema.index({ name: 'text', sub_name: 'text' }); // schema level
marcinn
  • 1,786
  • 11
  • 13
  • and how do i apply index in description ? schema.index({ 'location.description': 'text', type: 'text' }); ? – Maco Jan 19 '15 at 12:35
  • I've never created index on nested fields in mongoose, but since that is the way you create index in mogno shell(by specifying the path) i guess thats how it should work in mongoose as well. schemaObj.index({ 'location.description': 'text', type: 'text' }); – marcinn Jan 19 '15 at 12:50
0

Create Multiple Text Indexes use this. this will help for full text search

  1. FIND THE INDEXES : db.collectionName.getIndexes();

  2. Than Drop index Name: db.campaigns.dropIndex('name_text');

  3. Create Indexes on Multiple Key: db.campaigns.ensureIndex({ name: 'text' , description: 'text', tags : 'text' });

Armali
  • 18,255
  • 14
  • 57
  • 171
Vijay Prajapati
  • 208
  • 1
  • 5
  • 19