Query All Records in an Azure Search Index

  • Estimated read time: 3 min read
  • Written by Chad Campbell on Jun 11th 2016

Records in Azure Search are called "documents". Documents are stored in Azure Search indexes. In some ways, an index is like a table in a relational database. For that reason, sometimes you may want to replicate the behavior of something like SELECT * FROM MyIndex. However, you'll quickly discover that you cannot do this directly in Azure Search. This post will show you how to query all records, or documents, in an Azure Search Index.

The first thing to be aware of is that, by default, Azure Search returns 50 documents at a time. This value is so small because, the goal of a search should be to get the most relevant results. Not necessarily all matching documents. However, if you're reading this post, I'm assuming that you do want all matching documents. If you need to get all documents, you can rely on the $top and $skip parameters.

The $top parameter

The $top parameter lets you change the 50 document limit set by default. For example, here's a query that returns 100 documents.

https://[my-service-name].search.windows.net/indexes/my-search-index/docs?search=*&$top=100&api-version=[my-api-version]

The $top parameter let's you tell Azure Search how many results you want to return. With the $top parameter, you can use any number from 1 to 1,000. Anything over 1,000 will be truncated down to 1,000. This means at most, you can return 1,000 documents at a time. If your search index has more than 1,000 documents though, this parameter is only half of the story.

If you need to get more than 1,000 documents from a search index, you'll have to employ paging. I discuss paging in depth in my Azure Search course on Pluralsight (free trial). However, I still want to show you how to query all records in this post.

The $skip parameter

To page through the documents in an Azure Search index, you can use the $skip parameter, with the $top parameter. I like to think of the $top parameter as my page size. I then use the $skip parameter to jump to a page. For example, let's pretend there are 500 documents in my search index and I wanted to jump to page #3. Well, I could write a query like this:

https://[my-service-name].search.windows.net/indexes/my-search-index/docs?search=*&$top=100,$skip=200&api-version=[my-api-version]

This request to the Azure Search Service would basically ignore the first 200 matching search documents. It would then begin building a collection of documents starting with document #201. It would then return the next 100 documents. Or, put another way, results #201 through #300 would be returned.

Remember, Azure Search will only return at most 1,000 documents at a time. For that reason, if you want to get all documents in a search index, you're responsible for looping through the pages and building the collection yourself.

I hope this post answered your question. If you'd like to learn more about Azure Search, I hope you'll check out my Azure Search training course. This course is available 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