1

This question refers to: Diacritic Case-Insensitive search Loopback

I tried to add the following indexes - firstname and lastname of my NsUser model like @Markus_W_Mahlberg suggested - the loopback way.

    {
      "name": "NsUser",
      "base": "User",
      "idInjection": true,
      "options": {
        "validateUpsert": true
      },
      "indexes": {
        "firstname": "text" ,
        "lastname": "text"
      },
      "properties": {
        "firstname": {
          "type": "string"
        },
       "lastname": {
          "type": "string"
        }
       …
     }
     …
    }

I also use an auto-update script in my server.js to make sure indexes are working like suggested here: https://github.com/strongloop/loopback-connector-mongodb/issues/103

My MongoDB shell version is: 3.2.3

Still it is not working. Any ideas?

EDIT: To answer to Pawan - When I display my Indexes in Mongo using:

> db.NsUser.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "needsporty_DB.NsUser"
    },
    {
        "v" : 1,
        "key" : {
            "text" : 1
        },
        "name" : "firstname",
        "ns" : "needsporty_DB.NsUser"
    },
    {
        "v" : 1,
        "key" : {
            "_fts" : "text",
            "_ftsx" : 1
        },
        "name" : "diatrics_insensitive_keys",
        "ns" : "needsporty_DB.NsUser",
        "weights" : {
            "firstname" : 1,
            "lastname" : 1
        },
        "default_language" : "english",
        "language_override" : "language",
        "textIndexVersion" : 3
    }
]
Community
  • 1
  • 1
F3L1X79
  • 2,575
  • 2
  • 29
  • 45

1 Answers1

2

The Syntax of declaring index in model.json file is

"indexes": {
     //A composite index two keys: key1 in ascending order and key2 in descending order
     "<indexName>": { "<key1>": 1, "<key2>" : -1 }
    //single field index
    "<indexName>": { "<key1>" : 1 }
   // for text indexes
   "<indexName>": { "<key1>" : "text" }   
}

check the docs

So change your indexes object accordingly, As a collection can have at most one text index, so here you need to create a compound text index with firstname and lastname i.e

"indexes": {
    "textSearchName" : { "firstname" : "text", "lastname" : "text" }
}
RootHacker
  • 1,109
  • 8
  • 12
  • Thank you! It seems to be working but now I get this error: "Unhandled rejection MongoError: Index with pattern: { _fts: "text", _ftsx: 1 } already exists with different options". It's strange because it's the only index I've been creating. Any thoughts on this? – F3L1X79 Sep 20 '16 at 07:19
  • 1
    can you confirm indexes by calling db.collection.getIndexes() ? – RootHacker Sep 20 '16 at 09:23
  • > db.collection.getIndexes() : [ ]. Seems like it returns an empty array. – F3L1X79 Sep 20 '16 at 10:49
  • 1
    Have you replaced collection with name of your collection while getting indexes(may be you had,just confirming) i.e db.yourcollection.getIndexes(), then try . what's it returning after this ? – RootHacker Sep 20 '16 at 14:11
  • Shame on me... I updated the answer, if you might want to help. Thank you again! – F3L1X79 Sep 20 '16 at 14:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123798/discussion-between-f3l1x79-and-pawan). – F3L1X79 Sep 20 '16 at 14:36
  • 1
    you getting this error bcz you were trying to create two different text indexes on one collection, you can have only one text index docs.mongodb.com/manual/core/index-text/#create-text-index , to solve this create a composite text index on firstname and lastname instead of creating individual index. check edited answer . – RootHacker Sep 20 '16 at 18:14
  • Well indexes finally worked, but they're still diacritics sensitive with LoopbackJs' queries. Thank you anyway. – F3L1X79 Sep 21 '16 at 11:39