Fulltext search in custom fields

It would be great to have possibility to search in custom fields and page content, eg. to provide website’s content search.
I looked into content panel, and tried similar query - sth like:

query.data.myCustomField.$regex='*searchText*'

but with no luck. (results empty)

Hey @aubergine - try leaving out the *'s, e.g.

query.data.myCustomField.$regex="searchText"

E.g. you can query all of our docs that have the text “Builder” in them with this

https://cdn.builder.io/api/v2/content/docs-content?apiKey=YJIGb4i01jvw0SRdL5Bt&query.data.title.$regex="Builder"&fields=name

You can make this a little nicer too by making the search case insensitive by also adding query.data.myCustomField.$options=i

https://cdn.builder.io/api/v2/content/docs-content?apiKey=YJIGb4i01jvw0SRdL5Bt&query.data.title.$regex="Builder"&fields=name&query.data.title.$options=i

You can find more info about mongodb’s regex operator here

Steve

1 Like

Thanks!
Right this case is resolved, but what if I’d like to search in multiple fields?

Based on builder admin query:
query.$or[0].data.title.$regex: .*hubbahubba.*, query.$or[0].data.title.$options: i, query.$or[1].data.description.short.$regex: .*hubbahubba.*, query.$or[1].data.description.short.$options: i,

gives me nothing :frowning: ; while:

query.data.title.$regex: .*hubbahubba.*,
query.data.title.$options: i,

and
query.data.description.short.$regex: .*hubbahubba.*, query.data.description.short.$options: i,

used separately - work.

At the moment I use $or, query stops working

Similarly - if I’d like to search eg. events that have date from and to specified:
query.data.date.from.$gte: 1597425047724 AND query.data.date.to.$lte: 1597425047724

it works with one condition used, but I don’t know how to do logical AND. $and[0]? Doesn’t work :slight_smile:

Last thing: If I’d like to search within page contents I have to query: query.data.blocks.$regex etc?

Hey @aubergine - for complex queries like with $or try using full objects as values (we parse all values with JSON5). E.g.:

https://cdn.builder.io/api/v2/content/docs-content?apiKey=YJIGb4i01jvw0SRdL5Bt&fields=name&query.$or=[{‘data.title’:{$regex:‘Shopify’}},{‘data.description’:{$regex:‘Shopify’}}]

The above should work for $and as well. If it doesn’t - can you send a full example URL you are using so we can help you more specifically debug?

For a search of all content, you can use our (beta) search API that searches the full contents with elasticsearch. Note that elasticsearch doesn’t support partial word matching, so you must search full words. For example, you can search all of our docs that mention “Shopify” with this

https://cdn.builder.io/api/v1/search/docs-content?apiKey=YJIGb4i01jvw0SRdL5Bt&search=shopify&fields=name

Thank you! I’ll try that and let you know! :slight_smile:

Thank you again. It works as needed :slight_smile:

@steve How do I search for case-sensitive string?

@manuel you can do the above, just leave out the $options=i part (the i option for regex makes it case insensitive)