API data query filtering

Hey!

I would like to write a query for for my endpoint with these conditions:
My data is localized:
https://cdn.builder.io/api/v2/content/help-data?apiKey=32f148657e2646be8562eb4e6ebfa190

Here is an example result for it:
image

Example: query string= ‘question’, locale= 'en’
My conditions:
I would like to search in my specific locale
AND
Search in the title OR the description property (case insensitive and search or description includes my query string)

I tried something, but it is clearly not working:

const searchResult = await builder.getAll('help-data', {
      options: {
        noTargeting: true,
        query: {
          'data.helplist.[locale].title.$options=i.$regex': searchQuery,
        },
      },
    })

How can I pass my locale to the query string, and meet all my conditions above, please help me, I am not very used with this complex mongo syntax.

Thanks in advanced!

Hey @radikris! Since you’re looking for a regex inside of an array you’ll want to use the $regex and $elemMatch operators. Then your query would look like this to search for something in the title:

query: {
          'data.helplist.[locale].$elemMatch': { title: { $regex: searchQuery } }
        }

Thanks, thats one more step closer to the solution:

  1. How can I pass the locale dynamically to this query, because this list is localized with builder, but I only want to search in the list[locale]. (see my example above)

  2. How can I search for ‘title’ OR ‘description’?

  1. You’re on the right track! Javascript does not allow template strings for property names. You can wrap it in brackets instead to make it a computed property.
    [`data.helplist.${locale}.$elemMatch`]

  2. You can use the $or operator in the value:

{
   $or: [
      { description: { $regex: searchQuery } },
      { title: { $regex: searchQuery } }
   ]
}
1 Like

Hi @ancheetah !
Everything works now like a charm, except the case insensitive part:

Here is what I have:

 const searchResult = await builder.getAll('help-data', {
      options: {
        noTargeting: true,
        query: {
          [`data.helplist.${locale}.$elemMatch`]: {
            $or: [
              { description: { $regex: searchQuery, $options: 'i' } },
              { title: { $regex: searchQuery, $options: 'i' } },
            ],
          },
        },
      },
    })

I followed the official mongodb docs:

But let me show you an example:
Search for ‘masodik’ or ‘Masodik’ are not the same:
“masodik”:

“Masodik”:

I don’t know what could cause the issue, can you please check this out?
Thanks

@radikris I wonder if the [data.helplist.${locale}.$elemMatch] isnt being calculated to what you expect? I would console log to make sure that is what you want it to be

I was able to get results correctly in the Builder.io: Drag and drop page builder and CMS with the query object set to:

{
    data: {
        helplist: {
            en: {
                $elemMatch: {
                    $or: [{
                            description: {
                                $regex: 'vegyes',
                                $options: 'i'
                            }
                        },
                        {
                            title: {
                                $regex: 'vegyes',
                                $options: 'i'
                            }
                        }
                    ]
                }
            }
        }
    }
}

or

{"data":{"helplist":{"en":{"$elemMatch":{"$or":[{"description":{"$regex":"vegyes","$options":"i"}},{"title":{"$regex":"vegyes","$options":"i"}}]}}}}}

And it was giving what appeared to be case-insensitive results, check it out yourself and see if that helps you troubleshoot more

1 Like

okey, thanks for pointing this out for me, I realized it was not the issue with my querying, I had an extra filtering local, where I did not add the toLowerCase() :man_facepalming:
Thank you, sorry for this unneccessary question :smiley: but hope someone else can learn from it

1 Like