# Creating Indicies - Sometimes necessary, other times not?

**URL:** <https://discuss.elastic.co/t/creating-indicies-sometimes-necessary-other-times-not/4260>\
**Category:** Elasticsearch\
**Created:** [April 19, 2011, 11:14pm UTC](https://discuss.elastic.co/t/creating-indicies-sometimes-necessary-other-times-not/4260 "2011-04-19T23:14:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Cook](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@James\_Cook](https://discuss.elastic.co/u/James_Cook)\
**Post date:** [April 19, 2011, 11:14pm UTC](https://discuss.elastic.co/t/creating-indicies-sometimes-necessary-other-times-not/4260/1 "2011-04-19T23:14:46Z")

</div>

After using ES for over a year, I'm now trying to understand a long standing  
problem we experience. 🙂

Assume I have no gateway configured and I have started ES 0.15.2 in an  
embedded manner using Spring inside of a web application. I am using the  
Java API to communicate with ES. There is only one instance of ES on my  
system, so no clustering.

I have a single index (ppkc) with multiple types. The type I am interested  
in is sysinfo, and I want to use dynamic mappings so I have not created an  
explicit mapping file.

## Scenario #1

If I attempt to index a document, I can turn on debug log statements and see  
the index module attempt to locate mappings in the data config path, and I  
assume since none are found the default dynamic mapping kicks in.

Any subsequent queries on the 'sysinfo' type return as expected.

## Scenario #2

However, if I attempt to query for a document _before_ indexing, I get a  
MissingIndexException.

I don't understand why an index succeeds when I perform a query before  
indexing a document. If I do the index first then a query, all is well.

So, thinking that I can catch the exception and handle it by  
programmatically forcing an index I wrote this function:

```
private void checkForIndex(Client client, String esIndex) {
    final ClusterAdminClient cluster = client.admin().cluster();

```

cluster.prepareHealth().setWaitForYellowStatus().execute().actionGet();

```
    ClusterStateResponse response =

```

cluster.prepareState().execute().actionGet();  
boolean hasIndex = response.getState().metaData().hasIndex(esIndex);

```
    if(!hasIndex) {
        LOG.info("Index Not Found, creating index: " + esIndex);

```

client.admin().indices().create(createIndexRequest(esIndex)).actionGet();  
}  
}

However, this doesn't work either. The index is created without issue, but  
my query results in this exception:

Caused by: org.elasticsearch.indices.TypeMissingException: [ppkc]  
type[sysinfo] missing  
at  
org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:95)  
at  
org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:62)  
at  
org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:250)  
at  
org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:239)  
at  
org.elasticsearch.transport.netty.MessageChannelHandler$RequestHandler.run(MessageChannelHandler.java:238)  
at  
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
at  
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
at java.lang.Thread.run(Thread.java:680)

Any thoughts?

---

<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:** [April 19, 2011, 11:23pm UTC](https://discuss.elastic.co/t/creating-indicies-sometimes-necessary-other-times-not/4260/2 "2011-04-19T23:23:44Z")

</div>

Heya,

An index operation will cause an index to be automatically created, a search on a specific index will not. Same applies to type, when indexing, the type is created, but, if you search/get a type before it is created, you will get TypeMissing failure (actually, in master, it will only happen for get, not for search).

Why not just create the index with empty mappings when the application starts up, and if its already there, just ignore the IndexAlreadyExists failure?

-shay.banon  
On Wednesday, April 20, 2011 at 2:14 AM, James Cook wrote:

> After using ES for over a year, I'm now trying to understand a long standing problem we experience. 🙂
> 
> Assume I have no gateway configured and I have started ES 0.15.2 in an embedded manner using Spring inside of a web application. I am using the Java API to communicate with ES. There is only one instance of ES on my system, so no clustering.
> 
> I have a single index (ppkc) with multiple types. The type I am interested in is sysinfo, and I want to use dynamic mappings so I have not created an explicit mapping file.
> 
> ## Scenario #1
> 
> If I attempt to index a document, I can turn on debug log statements and see the index module attempt to locate mappings in the data config path, and I assume since none are found the default dynamic mapping kicks in.
> 
> Any subsequent queries on the 'sysinfo' type return as expected.
> 
> ## Scenario #2
> 
> However, if I attempt to query for a document _before_ indexing, I get a MissingIndexException.
> 
> I don't understand why an index succeeds when I perform a query before indexing a document. If I do the index first then a query, all is well.
> 
> So, thinking that I can catch the exception and handle it by programmatically forcing an index I wrote this function:
> 
> private void checkForIndex(Client client, String esIndex) {  
> final ClusterAdminClient cluster = client.admin().cluster();  
> cluster.prepareHealth().setWaitForYellowStatus().execute().actionGet();
> 
> ClusterStateResponse response = cluster.prepareState().execute().actionGet();  
> boolean hasIndex = response.getState().metaData().hasIndex(esIndex);
> 
> if(!hasIndex) {  
> LOG.info("Index Not Found, creating index: " + esIndex);  
> client.admin().indices().create(createIndexRequest(esIndex)).actionGet();  
> }  
> }
> 
> However, this doesn't work either. The index is created without issue, but my query results in this exception:
> 
> Caused by: org.elasticsearch.indices.TypeMissingException: [ppkc] type[sysinfo] missing  
> at org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:95)  
> at org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:62)  
> at org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:250)  
> at org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:239)  
> at org.elasticsearch.transport.netty.MessageChannelHandler$RequestHandler.run(MessageChannelHandler.java:238)  
> at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
> at java.lang.Thread.run(Thread.java:680)
> 
> Any thoughts?

---

<div class="post-metadata">

**Author:** ![James\_Cook](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@James\_Cook](https://discuss.elastic.co/u/James_Cook)\
**Post date:** [April 19, 2011, 11:34pm UTC](https://discuss.elastic.co/t/creating-indicies-sometimes-necessary-other-times-not/4260/3 "2011-04-19T23:34:56Z")

</div>

Thanks Shay. Is there a tech reason preventing the automatic creation of an  
index whether it is accessed for read _or_ write?

I have other types which use mapping files. This sysinfo type is the first  
one I am using as a simple bucket for unstructured data and I want to use  
dynamic mapping for it. I worked around my problem by creating a  
sysinfo.json mapping file that looks like this:

```
{ "sysinfo" : { } }

```

Thanks,  
jim

On Tue, Apr 19, 2011 at 7:23 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:

> Heya,
> 
> An index operation will cause an index to be automatically created, a  
> search on a specific index will not. Same applies to type, when indexing,  
> the type is created, but, if you search/get a type before it is created, you  
> will get TypeMissing failure (actually, in master, it will only happen for  
> get, not for search).
> 
> Why not just create the index with empty mappings when the application  
> starts up, and if its already there, just ignore the IndexAlreadyExists  
> failure?
> 
> -shay.banon
> 
> On Wednesday, April 20, 2011 at 2:14 AM, James Cook wrote:
> 
> After using ES for over a year, I'm now trying to understand a long  
> standing problem we experience. 🙂
> 
> Assume I have no gateway configured and I have started ES 0.15.2 in an  
> embedded manner using Spring inside of a web application. I am using the  
> Java API to communicate with ES. There is only one instance of ES on my  
> system, so no clustering.
> 
> I have a single index (ppkc) with multiple types. The type I am interested  
> in is sysinfo, and I want to use dynamic mappings so I have not created an  
> explicit mapping file.
> 
> ## Scenario #1
> 
> If I attempt to index a document, I can turn on debug log statements and  
> see the index module attempt to locate mappings in the data config path, and  
> I assume since none are found the default dynamic mapping kicks in.
> 
> Any subsequent queries on the 'sysinfo' type return as expected.
> 
> ## Scenario #2
> 
> However, if I attempt to query for a document _before_ indexing, I get a  
> MissingIndexException.
> 
> I don't understand why an index succeeds when I perform a query before  
> indexing a document. If I do the index first then a query, all is well.
> 
> So, thinking that I can catch the exception and handle it by  
> programmatically forcing an index I wrote this function:
> 
> ```
> private void checkForIndex(Client client, String esIndex) {
> final ClusterAdminClient cluster = client.admin().cluster();
> 
> ```
> 
> cluster.prepareHealth().setWaitForYellowStatus().execute().actionGet();
> 
> ```
> ClusterStateResponse response =
> 
> ```
> 
> cluster.prepareState().execute().actionGet();  
> boolean hasIndex =  
> response.getState().metaData().hasIndex(esIndex);
> 
> ```
> if(!hasIndex) {
> LOG.info("Index Not Found, creating index: " + esIndex);
> 
> ```
> 
> client.admin().indices().create(createIndexRequest(esIndex)).actionGet();  
> }  
> }
> 
> However, this doesn't work either. The index is created without issue, but  
> my query results in this exception:
> 
> Caused by: org.elasticsearch.indices.TypeMissingException: [ppkc]  
> type[sysinfo] missing  
> at  
> org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:95)  
> at  
> org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:62)  
> at  
> org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:250)  
> at  
> org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:239)  
> at  
> org.elasticsearch.transport.netty.MessageChannelHandler$RequestHandler.run(MessageChannelHandler.java:238)  
> at  
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
> at  
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
> at java.lang.Thread.run(Thread.java:680)
> 
> Any thoughts?

---

<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:** [April 19, 2011, 11:37pm UTC](https://discuss.elastic.co/t/creating-indicies-sometimes-necessary-other-times-not/4260/4 "2011-04-19T23:37:03Z")

</div>

It just does not seem correct to create an index / type when searching.  
On Wednesday, April 20, 2011 at 2:34 AM, James Cook wrote:

> Thanks Shay. Is there a tech reason preventing the automatic creation of an index whether it is accessed for read _or_ write?
> 
> I have other types which use mapping files. This sysinfo type is the first one I am using as a simple bucket for unstructured data and I want to use dynamic mapping for it. I worked around my problem by creating a sysinfo.json mapping file that looks like this:
> 
> { "sysinfo" : { } }
> 
> Thanks,  
> jim
> 
> On Tue, Apr 19, 2011 at 7:23 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> 
> > Heya,
> > 
> > An index operation will cause an index to be automatically created, a search on a specific index will not. Same applies to type, when indexing, the type is created, but, if you search/get a type before it is created, you will get TypeMissing failure (actually, in master, it will only happen for get, not for search).
> > 
> > Why not just create the index with empty mappings when the application starts up, and if its already there, just ignore the IndexAlreadyExists failure?
> > 
> > -shay.banon  
> > On Wednesday, April 20, 2011 at 2:14 AM, James Cook wrote:
> > 
> > > After using ES for over a year, I'm now trying to understand a long standing problem we experience. 🙂
> > > 
> > > Assume I have no gateway configured and I have started ES 0.15.2 in an embedded manner using Spring inside of a web application. I am using the Java API to communicate with ES. There is only one instance of ES on my system, so no clustering.
> > > 
> > > I have a single index (ppkc) with multiple types. The type I am interested in is sysinfo, and I want to use dynamic mappings so I have not created an explicit mapping file.
> > > 
> > > ## Scenario #1
> > > 
> > > If I attempt to index a document, I can turn on debug log statements and see the index module attempt to locate mappings in the data config path, and I assume since none are found the default dynamic mapping kicks in.
> > > 
> > > Any subsequent queries on the 'sysinfo' type return as expected.
> > > 
> > > ## Scenario #2
> > > 
> > > However, if I attempt to query for a document _before_ indexing, I get a MissingIndexException.
> > > 
> > > I don't understand why an index succeeds when I perform a query before indexing a document. If I do the index first then a query, all is well.
> > > 
> > > So, thinking that I can catch the exception and handle it by programmatically forcing an index I wrote this function:
> > > 
> > > private void checkForIndex(Client client, String esIndex) {  
> > > final ClusterAdminClient cluster = client.admin().cluster();  
> > > cluster.prepareHealth().setWaitForYellowStatus().execute().actionGet();
> > > 
> > > ClusterStateResponse response = cluster.prepareState().execute().actionGet();  
> > > boolean hasIndex = response.getState().metaData().hasIndex(esIndex);
> > > 
> > > if(!hasIndex) {  
> > > LOG.info("Index Not Found, creating index: " + esIndex);  
> > > client.admin().indices().create(createIndexRequest(esIndex)).actionGet();  
> > > }  
> > > }
> > > 
> > > However, this doesn't work either. The index is created without issue, but my query results in this exception:
> > > 
> > > Caused by: org.elasticsearch.indices.TypeMissingException: [ppkc] type[sysinfo] missing  
> > > at org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:95)  
> > > at org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:62)  
> > > at org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:250)  
> > > at org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:239)  
> > > at org.elasticsearch.transport.netty.MessageChannelHandler$RequestHandler.run(MessageChannelHandler.java:238)  
> > > at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
> > > at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
> > > at java.lang.Thread.run(Thread.java:680)
> > > 
> > > Any thoughts?

---

<div class="post-metadata">

**Author:** ![James\_Cook](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@James\_Cook](https://discuss.elastic.co/u/James_Cook)\
**Post date:** [April 20, 2011, 1:16am UTC](https://discuss.elastic.co/t/creating-indicies-sometimes-necessary-other-times-not/4260/5 "2011-04-20T01:16:06Z")

</div>

OK, but this "inconsistency" was hard for me to understand as a new user  
(and a year later). 🙂

Works:  
index(document)

Fails:  
query(pred);

Works:  
index(dummy);  
query(pred);

-- jim

On Tue, Apr 19, 2011 at 7:37 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:

> It just does not seem correct to create an index / type when searching.
> 
> On Wednesday, April 20, 2011 at 2:34 AM, James Cook wrote:
> 
> Thanks Shay. Is there a tech reason preventing the automatic creation of an  
> index whether it is accessed for read _or_ write?
> 
> I have other types which use mapping files. This sysinfo type is the first  
> one I am using as a simple bucket for unstructured data and I want to use  
> dynamic mapping for it. I worked around my problem by creating a  
> sysinfo.json mapping file that looks like this:
> 
> ```
> { "sysinfo" : { } }
> 
> ```
> 
> Thanks,  
> jim
> 
> On Tue, Apr 19, 2011 at 7:23 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:
> 
> Heya,
> 
> An index operation will cause an index to be automatically created, a  
> search on a specific index will not. Same applies to type, when indexing,  
> the type is created, but, if you search/get a type before it is created, you  
> will get TypeMissing failure (actually, in master, it will only happen for  
> get, not for search).
> 
> Why not just create the index with empty mappings when the application  
> starts up, and if its already there, just ignore the IndexAlreadyExists  
> failure?
> 
> -shay.banon
> 
> On Wednesday, April 20, 2011 at 2:14 AM, James Cook wrote:
> 
> After using ES for over a year, I'm now trying to understand a long  
> standing problem we experience. 🙂
> 
> Assume I have no gateway configured and I have started ES 0.15.2 in an  
> embedded manner using Spring inside of a web application. I am using the  
> Java API to communicate with ES. There is only one instance of ES on my  
> system, so no clustering.
> 
> I have a single index (ppkc) with multiple types. The type I am interested  
> in is sysinfo, and I want to use dynamic mappings so I have not created an  
> explicit mapping file.
> 
> ## Scenario #1
> 
> If I attempt to index a document, I can turn on debug log statements and  
> see the index module attempt to locate mappings in the data config path, and  
> I assume since none are found the default dynamic mapping kicks in.
> 
> Any subsequent queries on the 'sysinfo' type return as expected.
> 
> ## Scenario #2
> 
> However, if I attempt to query for a document _before_ indexing, I get a  
> MissingIndexException.
> 
> I don't understand why an index succeeds when I perform a query before  
> indexing a document. If I do the index first then a query, all is well.
> 
> So, thinking that I can catch the exception and handle it by  
> programmatically forcing an index I wrote this function:
> 
> ```
> private void checkForIndex(Client client, String esIndex) {
> final ClusterAdminClient cluster = client.admin().cluster();
> 
> ```
> 
> cluster.prepareHealth().setWaitForYellowStatus().execute().actionGet();
> 
> ```
> ClusterStateResponse response =
> 
> ```
> 
> cluster.prepareState().execute().actionGet();  
> boolean hasIndex =  
> response.getState().metaData().hasIndex(esIndex);
> 
> ```
> if(!hasIndex) {
> LOG.info("Index Not Found, creating index: " + esIndex);
> 
> ```
> 
> client.admin().indices().create(createIndexRequest(esIndex)).actionGet();  
> }  
> }
> 
> However, this doesn't work either. The index is created without issue, but  
> my query results in this exception:
> 
> Caused by: org.elasticsearch.indices.TypeMissingException: [ppkc]  
> type[sysinfo] missing  
> at  
> org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:95)  
> at  
> org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:62)  
> at  
> org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:250)  
> at  
> org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:239)  
> at  
> org.elasticsearch.transport.netty.MessageChannelHandler$RequestHandler.run(MessageChannelHandler.java:238)  
> at  
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
> at  
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
> at java.lang.Thread.run(Thread.java:680)
> 
> Any thoughts?

---

<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:** [April 20, 2011, 1:18am UTC](https://discuss.elastic.co/t/creating-indicies-sometimes-necessary-other-times-not/4260/6 "2011-04-20T01:18:08Z")

</div>

I don't think so, at least not for me (the difficult to understand part). If you want full consistency, you can disable index creation on index API, then it will be consistent. I don't think that an index / type should be created when searching.  
On Wednesday, April 20, 2011 at 4:16 AM, James Cook wrote:

> OK, but this "inconsistency" was hard for me to understand as a new user (and a year later). 🙂
> 
> Works:  
> index(document)
> 
> Fails:  
> query(pred);
> 
> Works:  
> index(dummy);  
> query(pred);
> 
> -- jim
> 
> On Tue, Apr 19, 2011 at 7:37 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> 
> > It just does not seem correct to create an index / type when searching.  
> > On Wednesday, April 20, 2011 at 2:34 AM, James Cook wrote:
> > 
> > > Thanks Shay. Is there a tech reason preventing the automatic creation of an index whether it is accessed for read _or_ write?
> > > 
> > > I have other types which use mapping files. This sysinfo type is the first one I am using as a simple bucket for unstructured data and I want to use dynamic mapping for it. I worked around my problem by creating a sysinfo.json mapping file that looks like this:
> > > 
> > > { "sysinfo" : { } }
> > > 
> > > Thanks,  
> > > jim
> > > 
> > > On Tue, Apr 19, 2011 at 7:23 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> > > 
> > > > Heya,
> > > > 
> > > > An index operation will cause an index to be automatically created, a search on a specific index will not. Same applies to type, when indexing, the type is created, but, if you search/get a type before it is created, you will get TypeMissing failure (actually, in master, it will only happen for get, not for search).
> > > > 
> > > > Why not just create the index with empty mappings when the application starts up, and if its already there, just ignore the IndexAlreadyExists failure?
> > > > 
> > > > -shay.banon  
> > > > On Wednesday, April 20, 2011 at 2:14 AM, James Cook wrote:
> > > > 
> > > > > After using ES for over a year, I'm now trying to understand a long standing problem we experience. 🙂
> > > > > 
> > > > > Assume I have no gateway configured and I have started ES 0.15.2 in an embedded manner using Spring inside of a web application. I am using the Java API to communicate with ES. There is only one instance of ES on my system, so no clustering.
> > > > > 
> > > > > I have a single index (ppkc) with multiple types. The type I am interested in is sysinfo, and I want to use dynamic mappings so I have not created an explicit mapping file.
> > > > > 
> > > > > ## Scenario #1
> > > > > 
> > > > > If I attempt to index a document, I can turn on debug log statements and see the index module attempt to locate mappings in the data config path, and I assume since none are found the default dynamic mapping kicks in.
> > > > > 
> > > > > Any subsequent queries on the 'sysinfo' type return as expected.
> > > > > 
> > > > > ## Scenario #2
> > > > > 
> > > > > However, if I attempt to query for a document _before_ indexing, I get a MissingIndexException.
> > > > > 
> > > > > I don't understand why an index succeeds when I perform a query before indexing a document. If I do the index first then a query, all is well.
> > > > > 
> > > > > So, thinking that I can catch the exception and handle it by programmatically forcing an index I wrote this function:
> > > > > 
> > > > > private void checkForIndex(Client client, String esIndex) {  
> > > > > final ClusterAdminClient cluster = client.admin().cluster();  
> > > > > cluster.prepareHealth().setWaitForYellowStatus().execute().actionGet();
> > > > > 
> > > > > ClusterStateResponse response = cluster.prepareState().execute().actionGet();  
> > > > > boolean hasIndex = response.getState().metaData().hasIndex(esIndex);
> > > > > 
> > > > > if(!hasIndex) {  
> > > > > LOG.info("Index Not Found, creating index: " + esIndex);  
> > > > > client.admin().indices().create(createIndexRequest(esIndex)).actionGet();  
> > > > > }  
> > > > > }
> > > > > 
> > > > > However, this doesn't work either. The index is created without issue, but my query results in this exception:
> > > > > 
> > > > > Caused by: org.elasticsearch.indices.TypeMissingException: [ppkc] type[sysinfo] missing  
> > > > > at org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:95)  
> > > > > at org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:62)  
> > > > > at org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:250)  
> > > > > at org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:239)  
> > > > > at org.elasticsearch.transport.netty.MessageChannelHandler$RequestHandler.run(MessageChannelHandler.java:238)  
> > > > > at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
> > > > > at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
> > > > > at java.lang.Thread.run(Thread.java:680)
> > > > > 
> > > > > Any thoughts?

---

<div class="post-metadata">

**Author:** ![James\_Cook](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@James\_Cook](https://discuss.elastic.co/u/James_Cook)\
**Post date:** [April 20, 2011, 1:23am UTC](https://discuss.elastic.co/t/creating-indicies-sometimes-necessary-other-times-not/4260/7 "2011-04-20T01:23:14Z")

</div>

I'm starting to feel you really don't think it is a good idea to create an  
index when searching. 😃

What would you think about adding a configuration option for a comma  
separated list of indexes that should be created at startup?

-- jim

On Tue, Apr 19, 2011 at 9:18 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:

> I don't think so, at least not for me (the difficult to understand part).  
> If you want full consistency, you can disable index creation on index API,  
> then it will be consistent. I don't think that an index / type should be  
> created when searching.
> 
> On Wednesday, April 20, 2011 at 4:16 AM, James Cook wrote:
> 
> OK, but this "inconsistency" was hard for me to understand as a new user  
> (and a year later). 🙂
> 
> Works:  
> index(document)
> 
> Fails:  
> query(pred);
> 
> Works:  
> index(dummy);  
> query(pred);
> 
> -- jim
> 
> On Tue, Apr 19, 2011 at 7:37 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:
> 
> It just does not seem correct to create an index / type when searching.
> 
> On Wednesday, April 20, 2011 at 2:34 AM, James Cook wrote:
> 
> Thanks Shay. Is there a tech reason preventing the automatic creation of an  
> index whether it is accessed for read _or_ write?
> 
> I have other types which use mapping files. This sysinfo type is the first  
> one I am using as a simple bucket for unstructured data and I want to use  
> dynamic mapping for it. I worked around my problem by creating a  
> sysinfo.json mapping file that looks like this:
> 
> ```
> { "sysinfo" : { } }
> 
> ```
> 
> Thanks,  
> jim
> 
> On Tue, Apr 19, 2011 at 7:23 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:
> 
> Heya,
> 
> An index operation will cause an index to be automatically created, a  
> search on a specific index will not. Same applies to type, when indexing,  
> the type is created, but, if you search/get a type before it is created, you  
> will get TypeMissing failure (actually, in master, it will only happen for  
> get, not for search).
> 
> Why not just create the index with empty mappings when the application  
> starts up, and if its already there, just ignore the IndexAlreadyExists  
> failure?
> 
> -shay.banon
> 
> On Wednesday, April 20, 2011 at 2:14 AM, James Cook wrote:
> 
> After using ES for over a year, I'm now trying to understand a long  
> standing problem we experience. 🙂
> 
> Assume I have no gateway configured and I have started ES 0.15.2 in an  
> embedded manner using Spring inside of a web application. I am using the  
> Java API to communicate with ES. There is only one instance of ES on my  
> system, so no clustering.
> 
> I have a single index (ppkc) with multiple types. The type I am interested  
> in is sysinfo, and I want to use dynamic mappings so I have not created an  
> explicit mapping file.
> 
> ## Scenario #1
> 
> If I attempt to index a document, I can turn on debug log statements and  
> see the index module attempt to locate mappings in the data config path, and  
> I assume since none are found the default dynamic mapping kicks in.
> 
> Any subsequent queries on the 'sysinfo' type return as expected.
> 
> ## Scenario #2
> 
> However, if I attempt to query for a document _before_ indexing, I get a  
> MissingIndexException.
> 
> I don't understand why an index succeeds when I perform a query before  
> indexing a document. If I do the index first then a query, all is well.
> 
> So, thinking that I can catch the exception and handle it by  
> programmatically forcing an index I wrote this function:
> 
> ```
> private void checkForIndex(Client client, String esIndex) {
> final ClusterAdminClient cluster = client.admin().cluster();
> 
> ```
> 
> cluster.prepareHealth().setWaitForYellowStatus().execute().actionGet();
> 
> ```
> ClusterStateResponse response =
> 
> ```
> 
> cluster.prepareState().execute().actionGet();  
> boolean hasIndex =  
> response.getState().metaData().hasIndex(esIndex);
> 
> ```
> if(!hasIndex) {
> LOG.info("Index Not Found, creating index: " + esIndex);
> 
> ```
> 
> client.admin().indices().create(createIndexRequest(esIndex)).actionGet();  
> }  
> }
> 
> However, this doesn't work either. The index is created without issue, but  
> my query results in this exception:
> 
> Caused by: org.elasticsearch.indices.TypeMissingException: [ppkc]  
> type[sysinfo] missing  
> at  
> org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:95)  
> at  
> org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:62)  
> at  
> org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:250)  
> at  
> org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:239)  
> at  
> org.elasticsearch.transport.netty.MessageChannelHandler$RequestHandler.run(MessageChannelHandler.java:238)  
> at  
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
> at  
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
> at java.lang.Thread.run(Thread.java:680)
> 
> Any thoughts?

---

<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:** [April 20, 2011, 1:25am UTC](https://discuss.elastic.co/t/creating-indicies-sometimes-necessary-other-times-not/4260/8 "2011-04-20T01:25:24Z")

</div>

The problem with startup parameters is that they don't lend themselves well to distributed systems. A more proper solution is to just use the create index API when starting the node, making sure things are there. Its pretty simple to do, no?  
On Wednesday, April 20, 2011 at 4:23 AM, James Cook wrote:

> I'm starting to feel you really don't think it is a good idea to create an index when searching. 😃
> 
> What would you think about adding a configuration option for a comma separated list of indexes that should be created at startup?
> 
> -- jim
> 
> On Tue, Apr 19, 2011 at 9:18 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> 
> > I don't think so, at least not for me (the difficult to understand part). If you want full consistency, you can disable index creation on index API, then it will be consistent. I don't think that an index / type should be created when searching.  
> > On Wednesday, April 20, 2011 at 4:16 AM, James Cook wrote:
> > 
> > > OK, but this "inconsistency" was hard for me to understand as a new user (and a year later). 🙂
> > > 
> > > Works:  
> > > index(document)
> > > 
> > > Fails:  
> > > query(pred);
> > > 
> > > Works:  
> > > index(dummy);  
> > > query(pred);
> > > 
> > > -- jim
> > > 
> > > On Tue, Apr 19, 2011 at 7:37 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> > > 
> > > > It just does not seem correct to create an index / type when searching.  
> > > > On Wednesday, April 20, 2011 at 2:34 AM, James Cook wrote:
> > > > 
> > > > > Thanks Shay. Is there a tech reason preventing the automatic creation of an index whether it is accessed for read _or_ write?
> > > > > 
> > > > > I have other types which use mapping files. This sysinfo type is the first one I am using as a simple bucket for unstructured data and I want to use dynamic mapping for it. I worked around my problem by creating a sysinfo.json mapping file that looks like this:
> > > > > 
> > > > > { "sysinfo" : { } }
> > > > > 
> > > > > Thanks,  
> > > > > jim
> > > > > 
> > > > > On Tue, Apr 19, 2011 at 7:23 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> > > > > 
> > > > > > Heya,
> > > > > > 
> > > > > > An index operation will cause an index to be automatically created, a search on a specific index will not. Same applies to type, when indexing, the type is created, but, if you search/get a type before it is created, you will get TypeMissing failure (actually, in master, it will only happen for get, not for search).
> > > > > > 
> > > > > > Why not just create the index with empty mappings when the application starts up, and if its already there, just ignore the IndexAlreadyExists failure?
> > > > > > 
> > > > > > -shay.banon  
> > > > > > On Wednesday, April 20, 2011 at 2:14 AM, James Cook wrote:
> > > > > > 
> > > > > > > After using ES for over a year, I'm now trying to understand a long standing problem we experience. 🙂
> > > > > > > 
> > > > > > > Assume I have no gateway configured and I have started ES 0.15.2 in an embedded manner using Spring inside of a web application. I am using the Java API to communicate with ES. There is only one instance of ES on my system, so no clustering.
> > > > > > > 
> > > > > > > I have a single index (ppkc) with multiple types. The type I am interested in is sysinfo, and I want to use dynamic mappings so I have not created an explicit mapping file.
> > > > > > > 
> > > > > > > ## Scenario #1
> > > > > > > 
> > > > > > > If I attempt to index a document, I can turn on debug log statements and see the index module attempt to locate mappings in the data config path, and I assume since none are found the default dynamic mapping kicks in.
> > > > > > > 
> > > > > > > Any subsequent queries on the 'sysinfo' type return as expected.
> > > > > > > 
> > > > > > > ## Scenario #2
> > > > > > > 
> > > > > > > However, if I attempt to query for a document _before_ indexing, I get a MissingIndexException.
> > > > > > > 
> > > > > > > I don't understand why an index succeeds when I perform a query before indexing a document. If I do the index first then a query, all is well.
> > > > > > > 
> > > > > > > So, thinking that I can catch the exception and handle it by programmatically forcing an index I wrote this function:
> > > > > > > 
> > > > > > > private void checkForIndex(Client client, String esIndex) {  
> > > > > > > final ClusterAdminClient cluster = client.admin().cluster();  
> > > > > > > cluster.prepareHealth().setWaitForYellowStatus().execute().actionGet();
> > > > > > > 
> > > > > > > ClusterStateResponse response = cluster.prepareState().execute().actionGet();  
> > > > > > > boolean hasIndex = response.getState().metaData().hasIndex(esIndex);
> > > > > > > 
> > > > > > > if(!hasIndex) {  
> > > > > > > LOG.info("Index Not Found, creating index: " + esIndex);  
> > > > > > > client.admin().indices().create(createIndexRequest(esIndex)).actionGet();  
> > > > > > > }  
> > > > > > > }
> > > > > > > 
> > > > > > > However, this doesn't work either. The index is created without issue, but my query results in this exception:
> > > > > > > 
> > > > > > > Caused by: org.elasticsearch.indices.TypeMissingException: [ppkc] type[sysinfo] missing  
> > > > > > > at org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:95)  
> > > > > > > at org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:62)  
> > > > > > > at org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:250)  
> > > > > > > at org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:239)  
> > > > > > > at org.elasticsearch.transport.netty.MessageChannelHandler$RequestHandler.run(MessageChannelHandler.java:238)  
> > > > > > > at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
> > > > > > > at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
> > > > > > > at java.lang.Thread.run(Thread.java:680)
> > > > > > > 
> > > > > > > Any thoughts?

---

<div class="post-metadata">

**Author:** ![James\_Cook](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@James\_Cook](https://discuss.elastic.co/u/James_Cook)\
**Post date:** [April 20, 2011, 1:33am UTC](https://discuss.elastic.co/t/creating-indicies-sometimes-necessary-other-times-not/4260/9 "2011-04-20T01:33:43Z")

</div>

Sure, no problem. Thanks for the quick responses.

-- jim

On Tue, Apr 19, 2011 at 9:25 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:

> The problem with startup parameters is that they don't lend themselves  
> well to distributed systems. A more proper solution is to just use the  
> create index API when starting the node, making sure things are there. Its  
> pretty simple to do, no?
> 
> On Wednesday, April 20, 2011 at 4:23 AM, James Cook wrote:
> 
> I'm starting to feel you really don't think it is a good idea to create an  
> index when searching. 😃
> 
> What would you think about adding a configuration option for a comma  
> separated list of indexes that should be created at startup?
> 
> -- jim
> 
> On Tue, Apr 19, 2011 at 9:18 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:
> 
> I don't think so, at least not for me (the difficult to understand  
> part). If you want full consistency, you can disable index creation on index  
> API, then it will be consistent. I don't think that an index / type should  
> be created when searching.
> 
> On Wednesday, April 20, 2011 at 4:16 AM, James Cook wrote:
> 
> OK, but this "inconsistency" was hard for me to understand as a new user  
> (and a year later). 🙂
> 
> Works:  
> index(document)
> 
> Fails:  
> query(pred);
> 
> Works:  
> index(dummy);  
> query(pred);
> 
> -- jim
> 
> On Tue, Apr 19, 2011 at 7:37 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:
> 
> It just does not seem correct to create an index / type when searching.
> 
> On Wednesday, April 20, 2011 at 2:34 AM, James Cook wrote:
> 
> Thanks Shay. Is there a tech reason preventing the automatic creation of an  
> index whether it is accessed for read _or_ write?
> 
> I have other types which use mapping files. This sysinfo type is the first  
> one I am using as a simple bucket for unstructured data and I want to use  
> dynamic mapping for it. I worked around my problem by creating a  
> sysinfo.json mapping file that looks like this:
> 
> ```
> { "sysinfo" : { } }
> 
> ```
> 
> Thanks,  
> jim
> 
> On Tue, Apr 19, 2011 at 7:23 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:
> 
> Heya,
> 
> An index operation will cause an index to be automatically created, a  
> search on a specific index will not. Same applies to type, when indexing,  
> the type is created, but, if you search/get a type before it is created, you  
> will get TypeMissing failure (actually, in master, it will only happen for  
> get, not for search).
> 
> Why not just create the index with empty mappings when the application  
> starts up, and if its already there, just ignore the IndexAlreadyExists  
> failure?
> 
> -shay.banon
> 
> On Wednesday, April 20, 2011 at 2:14 AM, James Cook wrote:
> 
> After using ES for over a year, I'm now trying to understand a long  
> standing problem we experience. 🙂
> 
> Assume I have no gateway configured and I have started ES 0.15.2 in an  
> embedded manner using Spring inside of a web application. I am using the  
> Java API to communicate with ES. There is only one instance of ES on my  
> system, so no clustering.
> 
> I have a single index (ppkc) with multiple types. The type I am interested  
> in is sysinfo, and I want to use dynamic mappings so I have not created an  
> explicit mapping file.
> 
> ## Scenario #1
> 
> If I attempt to index a document, I can turn on debug log statements and  
> see the index module attempt to locate mappings in the data config path, and  
> I assume since none are found the default dynamic mapping kicks in.
> 
> Any subsequent queries on the 'sysinfo' type return as expected.
> 
> ## Scenario #2
> 
> However, if I attempt to query for a document _before_ indexing, I get a  
> MissingIndexException.
> 
> I don't understand why an index succeeds when I perform a query before  
> indexing a document. If I do the index first then a query, all is well.
> 
> So, thinking that I can catch the exception and handle it by  
> programmatically forcing an index I wrote this function:
> 
> ```
> private void checkForIndex(Client client, String esIndex) {
> final ClusterAdminClient cluster = client.admin().cluster();
> 
> ```
> 
> cluster.prepareHealth().setWaitForYellowStatus().execute().actionGet();
> 
> ```
> ClusterStateResponse response =
> 
> ```
> 
> cluster.prepareState().execute().actionGet();  
> boolean hasIndex =  
> response.getState().metaData().hasIndex(esIndex);
> 
> ```
> if(!hasIndex) {
> LOG.info("Index Not Found, creating index: " + esIndex);
> 
> ```
> 
> client.admin().indices().create(createIndexRequest(esIndex)).actionGet();  
> }  
> }
> 
> However, this doesn't work either. The index is created without issue, but  
> my query results in this exception:
> 
> Caused by: org.elasticsearch.indices.TypeMissingException: [ppkc]  
> type[sysinfo] missing  
> at  
> org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:95)  
> at  
> org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:62)  
> at  
> org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:250)  
> at  
> org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:239)  
> at  
> org.elasticsearch.transport.netty.MessageChannelHandler$RequestHandler.run(MessageChannelHandler.java:238)  
> at  
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
> at  
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
> at java.lang.Thread.run(Thread.java:680)
> 
> Any thoughts?

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [April 20, 2011, 9:21am UTC](https://discuss.elastic.co/t/creating-indicies-sometimes-necessary-other-times-not/4260/10 "2011-04-20T09:21:41Z")

</div>

You can always do this by hand. Check if an index exist, if not create  
it. Or like so in Java:

```
public void saveCreateIndex(String indexName) {
    try {
      client.admin().indices().create(new

```

CreateIndexRequest(indexName)).actionGet();  
} catch (Exception ex) {  
// already exists  
}  
}

Regards,  
Peter.

On Apr 20, 3:33 am, James Cook [jc...@tracermedia.com](mailto:jc...@tracermedia.com) wrote:

> Sure, no problem. Thanks for the quick responses.
> 
> -- jim
> 
> On Tue, Apr 19, 2011 at 9:25 PM, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com)wrote:
> 
> > The problem with startup parameters is that they don't lend themselves  
> > well to distributed systems. A more proper solution is to just use the  
> > create index API when starting the node, making sure things are there. Its  
> > pretty simple to do, no?
> 
> > On Wednesday, April 20, 2011 at 4:23 AM, James Cook wrote:
> 
> > I'm starting to feel you really don't think it is a good idea to create an  
> > index when searching. 😃
> 
> > What would you think about adding a configuration option for a comma  
> > separated list of indexes that should be created at startup?
> 
> > -- jim
> 
> > On Tue, Apr 19, 2011 at 9:18 PM, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com)wrote:
> 
> > I don't think so, at least not for me (the difficult to understand  
> > part). If you want full consistency, you can disable index creation on index  
> > API, then it will be consistent. I don't think that an index / type should  
> > be created when searching.
> 
> > On Wednesday, April 20, 2011 at 4:16 AM, James Cook wrote:
> 
> > OK, but this "inconsistency" was hard for me to understand as a new user  
> > (and a year later). 🙂
> 
> > Works:  
> > index(document)
> 
> > Fails:  
> > query(pred);
> 
> > Works:  
> > index(dummy);  
> > query(pred);
> 
> > -- jim
> 
> > On Tue, Apr 19, 2011 at 7:37 PM, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com)wrote:
> 
> > It just does not seem correct to create an index / type when searching.
> 
> > On Wednesday, April 20, 2011 at 2:34 AM, James Cook wrote:
> 
> > Thanks Shay. Is there a tech reason preventing the automatic creation of an  
> > index whether it is accessed for read _or_ write?
> 
> > I have other types which use mapping files. This sysinfo type is the first  
> > one I am using as a simple bucket for unstructured data and I want to use  
> > dynamic mapping for it. I worked around my problem by creating a  
> > sysinfo.json mapping file that looks like this:
> 
> > ```
> > { "sysinfo" : { } }
> > 
> > ```
> 
> > Thanks,  
> > jim
> 
> > On Tue, Apr 19, 2011 at 7:23 PM, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com)wrote:
> 
> > Heya,
> 
> > An index operation will cause an index to be automatically created, a  
> > search on a specific index will not. Same applies to type, when indexing,  
> > the type is created, but, if you search/get a type before it is created, you  
> > will get TypeMissing failure (actually, in master, it will only happen for  
> > get, not for search).
> 
> > Why not just create the index with empty mappings when the application  
> > starts up, and if its already there, just ignore the IndexAlreadyExists  
> > failure?
> 
> > -shay.banon
> 
> > On Wednesday, April 20, 2011 at 2:14 AM, James Cook wrote:
> 
> > After using ES for over a year, I'm now trying to understand a long  
> > standing problem we experience. 🙂
> 
> > Assume I have no gateway configured and I have started ES 0.15.2 in an  
> > embedded manner using Spring inside of a web application. I am using the  
> > Java API to communicate with ES. There is only one instance of ES on my  
> > system, so no clustering.
> 
> > I have a single index (ppkc) with multiple types. The type I am interested  
> > in is sysinfo, and I want to use dynamic mappings so I have not created an  
> > explicit mapping file.
> 
> > ## Scenario #1
> 
> > If I attempt to index a document, I can turn on debug log statements and  
> > see the index module attempt to locate mappings in the data config path, and  
> > I assume since none are found the default dynamic mapping kicks in.
> 
> > Any subsequent queries on the 'sysinfo' type return as expected.
> 
> > ## Scenario #2
> 
> > However, if I attempt to query for a document _before_ indexing, I get a  
> > MissingIndexException.
> 
> > I don't understand why an index succeeds when I perform a query before  
> > indexing a document. If I do the index first then a query, all is well.
> 
> > So, thinking that I can catch the exception and handle it by  
> > programmatically forcing an index I wrote this function:
> 
> > ```
> > private void checkForIndex(Client client, String esIndex) {
> > final ClusterAdminClient cluster = client.admin().cluster();
> > 
> > ```
> 
> > cluster.prepareHealth().setWaitForYellowStatus().execute().actionGet();
> 
> > ```
> > ClusterStateResponse response =
> > 
> > ```
> > 
> > cluster.prepareState().execute().actionGet();  
> > boolean hasIndex =  
> > response.getState().metaData().hasIndex(esIndex);
> 
> > ```
> > if(!hasIndex) {
> > LOG.info("Index Not Found, creating index: " + esIndex);
> > 
> > ```
> 
> > client.admin().indices().create(createIndexRequest(esIndex)).actionGet();  
> > }  
> > }
> 
> > However, this doesn't work either. The index is created without issue, but  
> > my query results in this exception:
> 
> > Caused by: org.elasticsearch.indices.TypeMissingException: [ppkc]  
> > type[sysinfo] missing  
> > at  
> > org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:95)  
> > at  
> > org.elasticsearch.action.get.TransportGetAction.shardOperation(TransportGetAction.java:62)  
> > at  
> > org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:250)  
> > at  
> > org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$ShardTransportHandler.messageReceived(TransportShardSingleOperationAction.java:239)  
> > at  
> > org.elasticsearch.transport.netty.MessageChannelHandler$RequestHandler.run(MessageChannelHandler.java:238)  
> > at  
> > java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
> > at  
> > java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
> > at java.lang.Thread.run(Thread.java:680)
> 
> > Any thoughts?

---

<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:08am UTC](https://discuss.elastic.co/t/creating-indicies-sometimes-necessary-other-times-not/4260/11 "2017-07-06T04:08:01Z")

</div>


