Loopback Mongo Fulltext Search

538 views
Skip to first unread message

ma...@ideajunction.uk

unread,
Jun 27, 2016, 6:00:45 AM6/27/16
to loopb...@googlegroups.com
Hi, 

I am looking into using Loopback as a new backend for out AngularJS application and was looking into full text search, we are looking to migrate to a MongoDB and I noticed that you can create text indexes, but I cannot seem to create them in the model.json in loopback. Do I need to create the index in a bootscript, if so how can I access the raw mongo connector to modify run the Model.createIndex() function?

I tried adding this to the models property 
"index": true

But I am still getting this when running filer[where][$text][search] = 'time'
{
  "error": {
    "name": "MongoError",
    "status": 500,
    "message": "Unable to execute query: error processing query: ns=db.model limit=1000 skip=0\nTree: TEXT : query=space, language=, tag=NULL\nSort: { _id: 1 }\nProj: {}\n planner returned error: need exactly one text index for $text query",
    "$err": "Unable to execute query: error processing query: ns=db.model limit=1000 skip=0\nTree: TEXT : query=space, language=, tag=NULL\nSort: { _id: 1 }\nProj: {}\n planner returned error: need exactly one text index for $text query",
    "code": 17007
  }

Many thanks in advance 

Mark Johnson

unread,
Jul 18, 2016, 8:56:32 PM7/18/16
to LoopbackJS
I got around this by using the raw connector, like this:
    app.datasources.users.connector.connect(function(err, db) {
      db.collection('Member').createIndex({ firstName: "text", lastName: "text" }, { name: "fulltext" }, function(err) {
      });
    })

DigitalDiva

unread,
Nov 22, 2016, 3:35:20 AM11/22/16
to LoopbackJS
Add this to your model.json:

"indexes": {
"keywordFulltext" : { "term" : "text" }
}

In your model.json the "indexes" property is a sibling to 

"relations": {
}

OR/AND

"properties": {
"term": {
"type": "string",
"required": true,
"index": {"unique": true}
}
}

You need to run auto-update to apply new indexes to previously-defined models. See https://groups.google.com/forum/#!topic/loopbackjs/9-3EP53qJVM

- Tami
Reply all
Reply to author
Forward
0 new messages