# Java API - strange behavior on first time index creation

**URL:** https://discuss.elastic.co/t/java-api-strange-behavior-on-first-time-index-creation/4502
**Category:** Elasticsearch
**Created:** [May 30, 2011, 10:12pm UTC](https://discuss.elastic.co/t/java-api-strange-behavior-on-first-time-index-creation/4502 "2011-05-30T22:12:14Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Slava\_G](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@Slava\_G](https://discuss.elastic.co/u/Slava_G)
#### Post date: [May 30, 2011, 10:12pm UTC](https://discuss.elastic.co/t/java-api-strange-behavior-on-first-time-index-creation/4502/1 "2011-05-30T22:12:14Z")

</div>

Hi,  
Tried to create index on clean elasticsearch 0.16.1 and got exception:

The code :  
IndexResponse indexRes = client.prepareIndex("test", "contacts",  
"1")  
.setSource(jsonBuilder()  
.startObject()  
.field("Name", "Test")  
.field("BirthDate", new Date())  
.field("Email", "test@test.com")  
.endObject()  
).execute()  
.actionGet();

Exception in thread "main"  
org.elasticsearch.indices.IndexMissingException: [test] missing  
at  
org.elasticsearch.cluster.metadata.MetaData.concreteIndex(MetaData.java:  
211)  
at  
org.elasticsearch.action.index.TransportIndexAction.innerExecute(TransportIndexAction.java:  
123)  
at org.elasticsearch.action.index.TransportIndexAction.access  
$000(TransportIndexAction.java:70)  
at org.elasticsearch.action.index.TransportIndexAction  
$1.onResponse(TransportIndexAction.java:104)  
at org.elasticsearch.action.index.TransportIndexAction  
$1.onResponse(TransportIndexAction.java:102)  
at org.elasticsearch.action.support.BaseAction$ThreadedActionListener  
$1.run(BaseAction.java:84)  
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:662)

Next run of the same code - works fine.

Another issue that I can't exactly isolate the behavior is I have code  
that creates indexes with long and complex index name and id = 1  
then after index is created , the search across \_all doesn't return  
any hits. Same code, with id = null works file and search returns  
hits.

Same code with simple index name :  
IndexResponse indexRes = client.prepareIndex("test", "contacts",  
"1")  
.setSource(jsonBuilder()  
.startObject()  
.field("Name", "Test")  
.field("BirthDate", new Date())  
.field("Email", "test@test.com")  
.endObject()  
).execute()  
.actionGet();

even with id = 1 makes next search positive. I really tried to find  
what I'm doing wrong, but same code, differences only index name and  
id.

Clean installation of elasticsearch.

Thank You and Best Regards/

---

<div class="post-metadata">

### Author: ![Christopher\_Burkey](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@Christopher\_Burkey](https://discuss.elastic.co/u/Christopher_Burkey)
#### Post date: [June 1, 2011, 1:12am UTC](https://discuss.elastic.co/t/java-api-strange-behavior-on-first-time-index-creation/4502/2 "2011-06-01T01:12:48Z")

</div>

I just started looking at Elasticsearch today and noticed that index  
updates in general do not seem to save right away. I guess it is the  
nature of being distributed?

```
	IndexRequestBuilder builder = client.prepareIndex("test", "asset",

```

"103");  
IndexResponse response =  
builder.setSource(XContentFactory.jsonBuilder()  
.startObject()  
.field("user", "kimchy")  
.field("postDate", new Date())  
.field("message", "trying out Elastic Search")  
.endObject()  
)  
.execute().actionGet();

```
	SearchResponse results = client.prepareSearch("test")
	        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
	        .setQuery(QueryBuilders.termQuery("user", "kimchy"))
	        .setFrom(0).setSize(60).setExplain(true)
	        .execute()
	        .actionGet();

	log("Found " + results.getHits().totalHits() );

```

This main method will find 0 results. Next time I run it it will find  
1

On May 30, 6:12 pm, slavag [slav...@gmail.com](mailto:slav...@gmail.com) wrote:

> Hi,  
> Tried to create index on clean elasticsearch 0.16.1 and got exception:
> 
> The code :  
> IndexResponse indexRes = client.prepareIndex("test", "contacts",  
> "1")  
> .setSource(jsonBuilder()  
> .startObject()  
> .field("Name", "Test")  
> .field("BirthDate", new Date())  
> .field("Email", "[t...@test.com](mailto:t...@test.com)")  
> .endObject()  
> ).execute()  
> .actionGet();
> 
> Exception in thread "main"  
> org.elasticsearch.indices.IndexMissingException: [test] missing  
> at  
> org.elasticsearch.cluster.metadata.MetaData.concreteIndex(MetaData.java:  
> 211)  
> at  
> org.elasticsearch.action.index.TransportIndexAction.innerExecute(TransportIndexAction.java:  
> 123)  
> at org.elasticsearch.action.index.TransportIndexAction.access  
> $000(TransportIndexAction.java:70)  
> at org.elasticsearch.action.index.TransportIndexAction  
> $1.onResponse(TransportIndexAction.java:104)  
> at org.elasticsearch.action.index.TransportIndexAction  
> $1.onResponse(TransportIndexAction.java:102)  
> at org.elasticsearch.action.support.BaseAction$ThreadedActionListener  
> $1.run(BaseAction.java:84)  
> 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:662)
> 
> Next run of the same code - works fine.
> 
> Another issue that I can't exactly isolate the behavior is I have code  
> that creates indexes with long and complex index name and id = 1  
> then after index is created , the search across \_all doesn't return  
> any hits. Same code, with id = null works file and search returns  
> hits.
> 
> Same code with simple index name :  
> IndexResponse indexRes = client.prepareIndex("test", "contacts",  
> "1")  
> .setSource(jsonBuilder()  
> .startObject()  
> .field("Name", "Test")  
> .field("BirthDate", new Date())  
> .field("Email", "[t...@test.com](mailto:t...@test.com)")  
> .endObject()  
> ).execute()  
> .actionGet();
> 
> even with id = 1 makes next search positive. I really tried to find  
> what I'm doing wrong, but same code, differences only index name and  
> id.
> 
> Clean installation of elasticsearch.
> 
> Thank You and Best Regards/

---

<div class="post-metadata">

### Author: ![danoyoung](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@danoyoung](https://discuss.elastic.co/u/danoyoung)
#### Post date: [June 1, 2011, 1:23am UTC](https://discuss.elastic.co/t/java-api-strange-behavior-on-first-time-index-creation/4502/3 "2011-06-01T01:23:12Z")

</div>

The document is saved, but not immediately avail. for search. If you need  
to save it and then search on it immediately, try calling the \_refresh  
method after the save.

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

On Tue, May 31, 2011 at 7:12 PM, Christopher Burkey \<  
[christopher.burkey@gmail.com](mailto:christopher.burkey@gmail.com)\> wrote:

> I just started looking at Elasticsearch today and noticed that index  
> updates in general do not seem to save right away. I guess it is the  
> nature of being distributed?
> 
> ```
> IndexRequestBuilder builder = client.prepareIndex("test",
> 
> ```
> 
> "asset",  
> "103");  
> IndexResponse response =  
> builder.setSource(XContentFactory.jsonBuilder()  
> .startObject()  
> .field("user", "kimchy")  
> .field("postDate", new Date())  
> .field("message", "trying out Elastic Search")  
> .endObject()  
> )  
> .execute().actionGet();
> 
> ```
> SearchResponse results = client.prepareSearch("test")
> .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
> .setQuery(QueryBuilders.termQuery("user", "kimchy"))
> .setFrom(0).setSize(60).setExplain(true)
> .execute()
> .actionGet();
> 
> log("Found " + results.getHits().totalHits() );
> 
> ```
> 
> This main method will find 0 results. Next time I run it it will find  
> 1
> 
> On May 30, 6:12 pm, slavag [slav...@gmail.com](mailto:slav...@gmail.com) wrote:
> 
> > Hi,  
> > Tried to create index on clean elasticsearch 0.16.1 and got exception:
> > 
> > The code :  
> > IndexResponse indexRes =  
> > client.prepareIndex("test", "contacts",  
> > "1")  
> > .setSource(jsonBuilder()  
> > .startObject()  
> > .field("Name", "Test")  
> > .field("BirthDate", new Date())  
> > .field("Email", "[t...@test.com](mailto:t...@test.com)")  
> > .endObject()  
> > ).execute()  
> > .actionGet();
> > 
> > Exception in thread "main"  
> > org.elasticsearch.indices.IndexMissingException: [test] missing  
> > at  
> > org.elasticsearch.cluster.metadata.MetaData.concreteIndex(MetaData.java:  
> > 211)  
> > at
> 
> org.elasticsearch.action.index.TransportIndexAction.innerExecute(TransportIndexAction.java:
> 
> > 1. at org.elasticsearch.action.index.TransportIndexAction.access  
> > $000(TransportIndexAction.java:70)  
> > at org.elasticsearch.action.index.TransportIndexAction  
> > $1.onResponse(TransportIndexAction.java:104)  
> > at org.elasticsearch.action.index.TransportIndexAction  
> > $1.onResponse(TransportIndexAction.java:102)  
> > at  
> > org.elasticsearch.action.support.BaseAction$ThreadedActionListener  
> > $1.run(BaseAction.java:84)  
> > 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:662)
> > 
> > Next run of the same code - works fine.
> > 
> > Another issue that I can't exactly isolate the behavior is I have code  
> > that creates indexes with long and complex index name and id = 1  
> > then after index is created , the search across \_all doesn't return  
> > any hits. Same code, with id = null works file and search returns  
> > hits.
> > 
> > Same code with simple index name :  
> > IndexResponse indexRes =  
> > client.prepareIndex("test", "contacts",  
> > "1")  
> > .setSource(jsonBuilder()  
> > .startObject()  
> > .field("Name", "Test")  
> > .field("BirthDate", new Date())  
> > .field("Email", "[t...@test.com](mailto:t...@test.com)")  
> > .endObject()  
> > ).execute()  
> > .actionGet();
> > 
> > even with id = 1 makes next search positive. I really tried to find  
> > what I'm doing wrong, but same code, differences only index name and  
> > id.
> > 
> > Clean installation of elasticsearch.
> > 
> > Thank You and Best Regards/

---

<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: [June 1, 2011, 2:09am UTC](https://discuss.elastic.co/t/java-api-strange-behavior-on-first-time-index-creation/4502/4 "2011-06-01T02:09:33Z")

</div>

Another option is to set the refresh flag when indexing. But, a note here, this is handy when writing tests and the like (calling refresh API, or setting the refresh flag), but should not be used in production. Refresh comes with an overhead, and refresh should be done using the automatic async refreshing process.

On Wednesday, June 1, 2011 at 4:23 AM, Dan Young wrote:

> The document is saved, but not immediately avail. for search. If you need to save it and then search on it immediately, try calling the \_refresh method after the save.
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/admin-indices-refresh.html)
> 
> On Tue, May 31, 2011 at 7:12 PM, Christopher Burkey \<[christopher.burkey@gmail.com](mailto:christopher.burkey@gmail.com) ([mailto:christopher.burkey@gmail.com](mailto:christopher.burkey@gmail.com))\> wrote:
> 
> > I just started looking at Elasticsearch today and noticed that index  
> > updates in general do not seem to save right away. I guess it is the  
> > nature of being distributed?
> > 
> > IndexRequestBuilder builder = client.prepareIndex("test", "asset",  
> > "103");  
> > IndexResponse response =  
> > builder.setSource(XContentFactory.jsonBuilder()  
> > .startObject()  
> > .field("user", "kimchy")  
> > .field("postDate", new Date())  
> > .field("message", "trying out Elastic Search")  
> > .endObject()  
> > )  
> > .execute().actionGet();
> > 
> > SearchResponse results = client.prepareSearch("test")  
> > .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> > .setQuery(QueryBuilders.termQuery("user", "kimchy"))  
> > .setFrom(0).setSize(60).setExplain(true)  
> > .execute()  
> > .actionGet();
> > 
> > log("Found " + results.getHits().totalHits() );
> > 
> > This main method will find 0 results. Next time I run it it will find  
> > 1
> > 
> > On May 30, 6:12 pm, slavag \<[slav...@gmail.com](mailto:slav...@gmail.com) ([mailto:slav...@gmail.com](mailto:slav...@gmail.com))\> wrote:
> > 
> > > Hi,  
> > > Tried to create index on clean elasticsearch 0.16.1 and got exception:
> > > 
> > > The code :  
> > > IndexResponse indexRes = client.prepareIndex("test", "contacts",  
> > > "1")  
> > > .setSource(jsonBuilder()  
> > > .startObject()  
> > > .field("Name", "Test")  
> > > .field("BirthDate", new Date())  
> > > .field("Email", "[t...@test.com](mailto:t...@test.com) ([mailto:t...@test.com](mailto:t...@test.com))")  
> > > .endObject()  
> > > ).execute()  
> > > .actionGet();
> > > 
> > > Exception in thread "main"  
> > > org.elasticsearch.indices.IndexMissingException: [test] missing  
> > > at  
> > > org.elasticsearch.cluster.metadata.MetaData.concreteIndex(MetaData.java:  
> > > 211)  
> > > at  
> > > org.elasticsearch.action.index.TransportIndexAction.innerExecute(TransportIndexAction.java:  
> > > 123)  
> > > at org.elasticsearch.action.index.TransportIndexAction.access  
> > > $000(TransportIndexAction.java:70)  
> > > at org.elasticsearch.action.index.TransportIndexAction  
> > > $1.onResponse(TransportIndexAction.java:104)  
> > > at org.elasticsearch.action.index.TransportIndexAction  
> > > $1.onResponse(TransportIndexAction.java:102)  
> > > at org.elasticsearch.action.support.BaseAction$ThreadedActionListener  
> > > $1.run(BaseAction.java:84)  
> > > 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:662)
> > > 
> > > Next run of the same code - works fine.
> > > 
> > > Another issue that I can't exactly isolate the behavior is I have code  
> > > that creates indexes with long and complex index name and id = 1  
> > > then after index is created , the search across \_all doesn't return  
> > > any hits. Same code, with id = null works file and search returns  
> > > hits.
> > > 
> > > Same code with simple index name :  
> > > IndexResponse indexRes = client.prepareIndex("test", "contacts",  
> > > "1")  
> > > .setSource(jsonBuilder()  
> > > .startObject()  
> > > .field("Name", "Test")  
> > > .field("BirthDate", new Date())  
> > > .field("Email", "[t...@test.com](mailto:t...@test.com) ([mailto:t...@test.com](mailto:t...@test.com))")  
> > > .endObject()  
> > > ).execute()  
> > > .actionGet();
> > > 
> > > even with id = 1 makes next search positive. I really tried to find  
> > > what I'm doing wrong, but same code, differences only index name and  
> > > id.
> > > 
> > > Clean installation of elasticsearch.
> > > 
> > > Thank You and Best Regards/

---

<div class="post-metadata">

### Author: ![Slava\_G](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@Slava\_G](https://discuss.elastic.co/u/Slava_G)
#### Post date: [June 1, 2011, 4:37am UTC](https://discuss.elastic.co/t/java-api-strange-behavior-on-first-time-index-creation/4502/5 "2011-06-01T04:37:53Z")

</div>

Hi,  
I tried to search 5 min after I tried to index the data , on the clean  
Elasticsearch installed on the same computer where my process is  
running.  
Should it take more then 5 min is such situation ?

What about first index exception ?

Best Regards.

On Jun 1, 5:09 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Another option is to set the refresh flag when indexing. But, a note here, this is handy when writing tests and the like (calling refresh API, or setting the refresh flag), but should not be used in production. Refresh comes with an overhead, and refresh should be done using the automatic async refreshing process.
> 
> On Wednesday, June 1, 2011 at 4:23 AM, Dan Young wrote:
> 
> > The document is saved, but not immediately avail. for search. If you need to save it and then search on it immediately, try calling the \_refresh method after the save.
> 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/admin-indices-refres)...
> 
> > On Tue, May 31, 2011 at 7:12 PM, Christopher Burkey \<[christopher.bur...@gmail.com](mailto:christopher.bur...@gmail.com) ([mailto:christopher.bur...@gmail.com](mailto:christopher.bur...@gmail.com))\> wrote:
> > 
> > > I just started looking at Elasticsearch today and noticed that index  
> > > updates in general do not seem to save right away. I guess it is the  
> > > nature of being distributed?
> 
> > > IndexRequestBuilder builder = client.prepareIndex("test", "asset",  
> > > "103");  
> > > IndexResponse response =  
> > > builder.setSource(XContentFactory.jsonBuilder()  
> > > .startObject()  
> > > .field("user", "kimchy")  
> > > .field("postDate", new Date())  
> > > .field("message", "trying out Elastic Search")  
> > > .endObject()  
> > > )  
> > > .execute().actionGet();
> 
> > > SearchResponse results = client.prepareSearch("test")  
> > > .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> > > .setQuery(QueryBuilders.termQuery("user", "kimchy"))  
> > > .setFrom(0).setSize(60).setExplain(true)  
> > > .execute()  
> > > .actionGet();
> 
> > > log("Found " + results.getHits().totalHits() );
> 
> > > This main method will find 0 results. Next time I run it it will find  
> > > 1
> 
> > > On May 30, 6:12 pm, slavag \<[slav...@gmail.com](mailto:slav...@gmail.com) ([mailto:slav...@gmail.com](mailto:slav...@gmail.com))\> wrote:
> > > 
> > > > Hi,  
> > > > Tried to create index on clean elasticsearch 0.16.1 and got exception:
> 
> > > > The code :  
> > > > IndexResponse indexRes = client.prepareIndex("test", "contacts",  
> > > > "1")  
> > > > .setSource(jsonBuilder()  
> > > > .startObject()  
> > > > .field("Name", "Test")  
> > > > .field("BirthDate", new Date())  
> > > > .field("Email", "[t...@test.com](mailto:t...@test.com) ([mailto:t...@test.com](mailto:t...@test.com))")  
> > > > .endObject()  
> > > > ).execute()  
> > > > .actionGet();
> 
> > > > Exception in thread "main"  
> > > > org.elasticsearch.indices.IndexMissingException: [test] missing  
> > > > at  
> > > > org.elasticsearch.cluster.metadata.MetaData.concreteIndex(MetaData.java:  
> > > > 211)  
> > > > at  
> > > > org.elasticsearch.action.index.TransportIndexAction.innerExecute(TransportI ndexAction.java:  
> > > > 123)  
> > > > at org.elasticsearch.action.index.TransportIndexAction.access  
> > > > $000(TransportIndexAction.java:70)  
> > > > at org.elasticsearch.action.index.TransportIndexAction  
> > > > $1.onResponse(TransportIndexAction.java:104)  
> > > > at org.elasticsearch.action.index.TransportIndexAction  
> > > > $1.onResponse(TransportIndexAction.java:102)  
> > > > at org.elasticsearch.action.support.BaseAction$ThreadedActionListener  
> > > > $1.run(BaseAction.java:84)  
> > > > 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:662)
> 
> > > > Next run of the same code - works fine.
> 
> > > > Another issue that I can't exactly isolate the behavior is I have code  
> > > > that creates indexes with long and complex index name and id = 1  
> > > > then after index is created , the search across \_all doesn't return  
> > > > any hits. Same code, with id = null works file and search returns  
> > > > hits.
> 
> > > > Same code with simple index name :  
> > > > IndexResponse indexRes = client.prepareIndex("test", "contacts",  
> > > > "1")  
> > > > .setSource(jsonBuilder()  
> > > > .startObject()  
> > > > .field("Name", "Test")  
> > > > .field("BirthDate", new Date())  
> > > > .field("Email", "[t...@test.com](mailto:t...@test.com) ([mailto:t...@test.com](mailto:t...@test.com))")  
> > > > .endObject()  
> > > > ).execute()  
> > > > .actionGet();
> 
> > > > even with id = 1 makes next search positive. I really tried to find  
> > > > what I'm doing wrong, but same code, differences only index name and  
> > > > id.
> 
> > > > Clean installation of elasticsearch.
> 
> > > > Thank You and Best Regards/

---

<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:05am UTC](https://discuss.elastic.co/t/java-api-strange-behavior-on-first-time-index-creation/4502/6 "2017-07-06T04:05:06Z")

</div>


