Loopback search on all fields
Asked Answered
B

4

7

I am using loopback as a backend now I wish to be able to search across all fields in the table

For instance, take the following table fields:

id, name, title, created, updated

Now say I want to search across the table using the following string "nomad"

However, I am unsure how to structure the query

I have attempted:

{"where": {"keywords": {"inq": ["nomad"]}}}

However, this just returns all results

So how do I do this? :)

If it matters my database is a postgresql

Bannerman answered 29/6, 2018 at 12:41 Comment(3)
can you tell us more about the app? is it angular? there are some differences if soNagey
@JordanHendrix Hello yes the app is an angular app however i am not using loopback angular. Loopback is used as my backend api so the calls to it should be fairly "normal"Bannerman
Any luck so far?Spellman
W
5

Loopback is not where you need to look at. It's just a framework with universal ORM / ODM to connect to your DB and expose is with a query language over rest. You probably need in your case to add a text index in your postgresql database. By indexing all your desired properties into a text index, you'll be able to perform search in your DB.

Here is the documentation. https://www.postgresql.org/docs/9.5/static/textsearch.html

You can still achieve your goal using loopback ORM with something like

{"where": {"prop1": {"regex": "nomad"}, "prop2": {"regex": "nomad"}}}

but your DB will die in few queries ...

Witham answered 5/7, 2018 at 12:21 Comment(0)
S
4

You can use loopback's case insensitive regex to search the database for the value.

Example:

Fields: [id, name, title, created, updated]

let whereCondition = {id: {regexp: '/nomad/i'}, name: {regexp: '/nomad/i'}}

app.models.ModelName.find({
  where: whereCondition
});

Refer the loopback documentation url below: https://loopback.io/doc/en/lb2/Where-filter.html#regular-expressions

Schoenburg answered 7/7, 2018 at 7:14 Comment(0)
T
1

You can try using RegExp :

 let searchField = "nomad";
 var search = new RegExp(searchField, 'i');
{"where": {"id":search,"name" : search}

I have use Aggregate in LookUp to Search .

Example :

 let searchField = "anyname";
     var searchCond = [];
         if (searchField) {
                        var search = new RegExp(searchField, 'i');
                        searchCond.push({ "id": search },{ "name": search });
                    }

//Depending on your other condition you add at aggregate :

    var modelCollection = Model.getDataSource().connector.collection(Model.modelName);
    modelCollection.aggregate([{
                        $match: {
                            $or: searchCond
                        }
                    }], function(err, data) {
    if (err) return callback(err);
    return callback(null, data);
  });
Trounce answered 2/7, 2018 at 12:0 Comment(0)
S
1

You will need to get a querying function that will transverse through your DB and return back your data from the table.

Eg:

Account.find({where: {name: 'John'}, limit: 3}, function(err, accounts) { /* ... */ });

Or

{where: {property: value}} 

You can get to discover more from Loopback Querying data, Loopback Where filter and postgresql RETURN QUERY.

Spellman answered 8/7, 2018 at 23:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.