Search API for custom records using "contains" | Community
Skip to main content

Search API for custom records using "contains"

  • March 28, 2022
  • 4 replies
  • 0 views

Hi,

I have an API call to the search endpoint of the custom objects API (https://developer.zendesk.com/api-reference/custom-data/custom-objects-api/search/). I want to return records using a "like" sentence and my understanding from documentation is that you must use "contains" for that (https://developer.zendesk.com/api-reference/custom-data/custom-objects-api/search/#comparison-object). But in my case, it works as "eq". Example: The field to search is an string that can contains numbers so I want to return all records that have a number 2 in that string (e.g. AB2, 2AB...). If I use "$contains": "2", it returns no records. If I use "$contains": "AB2" or "$contains": "2AB", it returns one record.

Is that the expected behavior? If so, is there any way to get records back using a "like" instruction in a field that just contains one string?

Thanks.

Regards,

Lydia 

4 replies

  • March 30, 2022
Hi Lydia,
 
Have you tried searching for 2 without the quotes? I believe that when you place the query value in quotes it treats it as a phrase search instead of a prefix search. It ends up looking for a phrase where the full value is 2 instead of just part of it. 
 
Hope this helps!
 
Best,
 
Erica 

  • Author
  • March 31, 2022

Hi Erica,

The field that I'm using for querying is a string so if I remove the quotes I get an error:

{
    "errors": [
        {
            "code": "BadRequest",
            "status": "400",
            "title": "Bad Request",
            "detail": "query $and: ref_number: $contains: Cannot pass NUMBER type as a value with '$contains' operator"
        }
    ]
}

 

Regards,

Lydia


  • Author
  • April 1, 2022

Update: That's the intended behavior as explained here: https://developer.zendesk.com/documentation/custom-data/custom-objects/searching-object-records/#building-a-basic-query

For more information about the operators, see Comparison Object in the API reference. For example, despite its name, the $contains operator looks for whole word matches. If you search for "sig", the response will not contain any instances of "sign".


  • April 4, 2022
Hi Lydia!
 
Thank you for clarifying and following up. Appreciate you sharing your findings and providing that documentation link. Glad you were able to get that clarity!
 
Best, 
 
Erica