Azure Search - StartsWith, Contains, and EndsWith Queries
- Estimated read time: 5 min read
- Written by Chad Campbell on Jul 3rd 2016
Azure Search supports two types of queries: simple and full. Simple queries use a lightweight query syntax for document-wide keywords searches. Full queries rely on the Lucene Query Syntax to provide richer, more specific kinds of queries.
Sometimes you may want to find documents that have terms that begin with a string. In C#, you might do this with the
startsWith. But, when writing a query in Azure Search, you have to go another route.
If I wanted to find terms in a document that begin with the string "Mega", I could run a query that looks like this:
The above is a simple query that examines all
searchable fields in a document for terms that begin with "Mega". To search for terms in specific fields that begin with a string, you need to use a full query. For example, if I wanted to look in a field called
myField for terms that begin with "Mega", you could run a query like this:
In my Azure Search training course on Pluralsight, I go into depth about the difference between simple and full queries. Still, for your immediate needs, you might want to find terms that contain another string.
Sometimes you might need to get documents that have terms that have one or more characters in them. In C#, you could do this with the
includes method. In the search world, these are viewed as "infix" queries. To run an infix query in Azure Search, you have to use a full query.
You cannot do an infix query in simple queries. The reason why is because the simple query language restricts the
* usage to suffix'. The
* has broader usage in full queries though. For example, if I wanted to find terms that contained the string "alo" (for whatever reason), you could run a query like this:
Notice that I searched for
/.*alo.*/ in this query. This relies on full queries support for regular expressions to do some pattern matching. This is necessary because, at the time of writing, there wasn't any build in string function to search for terms that contained a string. This approach is also necessary if you want to find terms that end with a specific string.
Sometimes, you might want to find documents that have terms that end with some string. In Azure Search, this is known as a suffix query. In C#, you could use the
endsWith method. In Azure Search, you have to once again rely on a regular expression. For example, if I wanted to find documents that had terms that ended with
myField, I could do something like this:
Once again, this type of query is only available in full queries.
At this point, you've seen how to use prefix, infix, and suffix queries. Notably, each query works at the term level. They do not "work" at the field value level. For example, imagine my documents had the following values in
|Happier than a clam|
|Today will be sunnier|
Well, both of these would match the query used above. The term "Happier" in the first row matches our regular expression. In addition, the term "sunnier" in the second row matches the regular expression. If you need to find documents with a field value that ends with a string, you'll have to build a custom analyzer.
I hope this post helped you. If you'd like to learn more about Azure Search, I hope you'll check out my Azure Search training course on Pluralsight. If you don't have a subscription, I'd like to offer you a free trial. The course goes into detail about Azure Search so you can learn how to do more with it.
If there are topics you're curious about, please feel free to mention them in the comments below. Thanks for reading.