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.

Regardless of the type of query you're using, sometimes you want to find terms in a way you're familiar with. In C# or JavaScript, you may have searched for strings that started with, contained, or ended with another string. In this post, I'll show you how to emulate these types of methods in an Azure Search query.

A StartsWith Query

Sometimes you may want to find documents that have terms that begin with a string. In C#, you might do this with the StartsWith method. In JavaScript, you might do this with a method of the same name, just different casing: 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:

https://[myService].search.windows.net/indexes/[myIndex]/docs?api-version=2015-02-28&search=mega*

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:

https://[myService].search.windows.net/indexes/[myIndex]/docs?api-version=2015-02-28&queryType=full&search=myField:mega*

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.

A Contains Query

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 Contains method. In JavaScript, 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:

https://[myService].search.windows.net/indexes/[myIndex]/docs?api-version=2015-02-28&queryType=full&search=myField:/.*alo.*/

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.

An EndsWith Query

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 JavaScript, you could do this with 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 er in myField, I could do something like this:

https://[myService].search.windows.net/indexes/[myIndex]/docs?api-version=2015-02-28&queryType=full&search=myField:/.*er/

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 myField:

myField
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.


Comments

comments powered by Disqus

Chad Campbell
Chad Campbell

Chad is an independent software professional. He has been named a Microsoft MVP five times. His books have been translated into multiple languages and distributed worldwide. He holds a computer science degree from Purdue University, where he also studied psychology.

Chad has built sites, apps, frameworks, libraries, and platforms using Java, .NET, and Node. He's ran his own startups and has created software for Fortune 100 companies. In short, Chad knows how to create software. From ideation to delivery. From start-to-finish.



Follow Chad Online