Hey, new to using Dato and need some help getting filtering to work using a regex - trying to pull back matching whole words in single / multi line string fields. Documentation on the subject is a bit thin and canât find any threads which cover it but apologies up front if Iâve missed an article. Running the search from the API Explorer and using {someTextField: {matches: {pattern: "someValidRegex", caseSensitive: false}}}
doesnât give any results when the clause should return a number of records. Does pattern take a straight regex or does it need additional formatting ? Thanks in advance.
Hello @jonathan.allen
Could you send us the exact query and the project you are using it on at support@datocms.com so that we can take a closer look?
Thanks!
Hey @m.finamor , thanks for the quick reply.
The project is 48590, running against develop
The query is:
query AllAdverts { allAdverts(first: 100, filter: {OR: [ {offerPromotionalText: {matches: {pattern: "\bhat\b", caseSensitive: false}}}, {advertiserAboutUs: {matches: {pattern: "\bhat\b", caseSensitive: false}}}, {advertiserName: {matches: {pattern: "\bhat\b", caseSensitive: false}}},], approvalStatus: {eq: "Approved"}}) { advertiserName advertiserAboutUs offerPromotionalText } }
The actual query is constructed in a web app based on user search terms so need to match whole words only. Tried a lot of variations but no joy. The expression is fine running it on regex101 with appropriate samples.
Thanks in advance.
Hello @jonathan.allen
For word boundaries we should use \y instead of \b, and we should use double backslashes to escape the character.
However, there is no record that matches the word âhatâ in your project, there is one that has the word âthatâ in one of the âofferPromotionalTextâ
So the query
query AllAdverts {
allAdverts(first: 100, filter: {OR: [{offerPromotionalText: {matches: {pattern: "\\ythat\\y", caseSensitive: false}}}, {advertiserAboutUs: {matches: {pattern: "\\ythat\\y", caseSensitive: false}}}, {advertiserName: {matches: {pattern: "\\ythat\\y", caseSensitive: false}}}], approvalStatus: {eq: "Approved"}}) {
advertiserName
advertiserAboutUs
offerPromotionalText
}
}
Returns that record
Thanks @m.finamor, that solves it. There are 2 records on the develop
environment which have âhatâ in their advertiserName
field and they pull back fine and those which have âthatâ are not returned.
Interesting that \b
is not supported. Is there any documentation to cover what is supported and what isnât ?
Thanks.
Hello, I have similar problem with filtering posts (allProducts in my case)
I need to get lists of products that cointains all of these words "bmw, F20, red, stripe
with this query ?
query MyQuery {
allProducts(filter: {name: {matches: {pattern: "
^(?=.*\bbmw\b)(?=.*\bf20\b)(?=.*\bred\b)(?=.*\bstripe\b).+
"}}}) {
name
}
}
can somebody help me ???
Hello @krzysztof.bialek1 sorry for the delay on this
It seems like the regex you are looking for is
^(?=.*bmw)(?=.*F20)(?=.*Red)(?=.*Stripe).*
Using positive lookahead to search for the 4 words âbmwâ âF20â, "Red, and âStripeâ
yes! thatâs a good answer thank you !