# Searching an empty index

**URL:** <https://discuss.elastic.co/t/searching-an-empty-index/4159>\
**Category:** Elasticsearch\
**Created:** [March 25, 2011, 10:22pm UTC](https://discuss.elastic.co/t/searching-an-empty-index/4159 "2011-03-25T22:22:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Curtis\_Caravone](https://avatars.discourse-cdn.com/v4/letter/c/e47c2d/32.png) [@Curtis\_Caravone](https://discuss.elastic.co/u/Curtis_Caravone)\
**Post date:** [March 25, 2011, 10:22pm UTC](https://discuss.elastic.co/t/searching-an-empty-index/4159/1 "2011-03-25T22:22:30Z")

</div>

I know this is an edge case, but...

I have a search with a field sort (using Java API) that throws an exception  
if the index is empty.

I believe this is because my empty index has no mappings defined, and the  
sort cannot figure out the data type of the field in question (even though  
there are no results to sort). What is the best way to deal with this case?  
I can think of a couple of ways:

1. Insert a dummy record so ES can determine the mappings (kind of hacky)
2. Define explicit mappings when I create the index (a little annoying,  
since I find the default mappings useful and would prefer to use them)
3. Do an additional index status request first to see if the document count  
is zero (has to be done on every request)
4. Catch the exception (seems dangerous because I don't think I get the  
real cause (missing mapping) except as part of the message text)
5. other ideas?

thanks,

Curtis

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 26, 2011, 4:44pm UTC](https://discuss.elastic.co/t/searching-an-empty-index/4159/2 "2011-03-26T16:44:39Z")

</div>

The best way is to define the mapping just for the relevant field you plan to sort on. There is different support for sorting based on the field type derived/defined, thats why its required.  
On Saturday, March 26, 2011 at 12:22 AM, Curtis Caravone wrote:

> I know this is an edge case, but...
> 
> I have a search with a field sort (using Java API) that throws an exception if the index is empty.
> 
> I believe this is because my empty index has no mappings defined, and the sort cannot figure out the data type of the field in question (even though there are no results to sort). What is the best way to deal with this case? I can think of a couple of ways:
> 
> 1. Insert a dummy record so ES can determine the mappings (kind of hacky)
> 2. Define explicit mappings when I create the index (a little annoying, since I find the default mappings useful and would prefer to use them)
> 3. Do an additional index status request first to see if the document count is zero (has to be done on every request)
> 4. Catch the exception (seems dangerous because I don't think I get the real cause (missing mapping) except as part of the message text)
> 5. other ideas?
> 
> thanks,
> 
> Curtis

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 4:09am UTC](https://discuss.elastic.co/t/searching-an-empty-index/4159/3 "2017-07-06T04:09:12Z")

</div>


