Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loopback search on all fields

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

like image 273
Marc Rasmussen Avatar asked Jun 29 '18 12:06

Marc Rasmussen


3 Answers

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 ...

like image 143
Pierre Clocher Avatar answered Oct 20 '22 17:10

Pierre Clocher


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

like image 4
Ajaykkumar R Avatar answered Oct 20 '22 17:10

Ajaykkumar R


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.

like image 1
antzshrek Avatar answered Oct 20 '22 15:10

antzshrek