API - Search for items containing text

hello again guys,

I would like to know if there is a way for me to do a search in a given collection, but not using an EXACT term, but that it CONTAINS a certain term in some part of the text.

Example:
POST: api/collections/get/articles
Body: { "limit": 10, "filter": { "content" : "%phone%" }, "sort": {"_created":-1} }

Note that in this example I use ‘%’ as I would in a database query using ‘LIKE’.

And then, in a given entry that has a CONTENT field filled in with ‘My beautiful phone’ it would be one of the entries I would list, as it has the text ‘phone’ inside the CONTENT field.

Does Cockpit provide any ‘native’ features for this?
Thank you very much to everyone in advance!

Yes, there is such a thing, the $regex filter.

POST:
api/collections/get/articles
Body:

{     
  "limit": 10,     
  "filter": {   
    "content": {      
      "$regex" : "(phone|tablet)" // a valid PHP regular expression without slashes
    }
  },     
  "sort": { 
    "_created" : -1
  } 
}

rel Search itens by term - #4 by artur

2 Likes

Dude, thanks a lot!

I had viewed this topic but hadn’t seen it that way, I thought it was something else. Thank you very much for the explanation, it was very specific!