# Java API: How to detect just submitted docs are indexed or not?

**URL:** <https://discuss.elastic.co/t/java-api-how-to-detect-just-submitted-docs-are-indexed-or-not/7394>\
**Category:** Elasticsearch\
**Created:** [April 19, 2012, 9:53am UTC](https://discuss.elastic.co/t/java-api-how-to-detect-just-submitted-docs-are-indexed-or-not/7394 "2012-04-19T09:53:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diomas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/diomas/32/2910_2.png) [@Diomas](https://discuss.elastic.co/u/Diomas)\
**Post date:** [April 19, 2012, 9:53am UTC](https://discuss.elastic.co/t/java-api-how-to-detect-just-submitted-docs-are-indexed-or-not/7394/1 "2012-04-19T09:53:19Z")

</div>

Hey, I'm just started play with ES and want to try it in a desktop app  
using java API.

I noticed that if I submit a set of documents with  
client.prepareIndex(indexName, indexType).setSource( /\* jsonBuilder stuff  
here \*/ ).execute().actionGet();

then if I try to search right after I submited my docs:  
SearchResponse response =  
client.prepareSearch(indexName).setQuery(queryString("google")).execute().actionGet();  
I get usually no hits, but sometimes I get one hit. Anyway this is not that  
I expect: I have 12 documents matching the query.

But if I put this line before the search statement:  
Thread.sleep(1000);  
I get all my 12 hits.

So the question is how can I know exactly if the index is ready to be  
searched?

---

<div class="post-metadata">

**Author:** ![Ariel\_Mendoza](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@Ariel\_Mendoza](https://discuss.elastic.co/u/Ariel_Mendoza)\
**Post date:** [April 19, 2012, 10:12am UTC](https://discuss.elastic.co/t/java-api-how-to-detect-just-submitted-docs-are-indexed-or-not/7394/2 "2012-04-19T10:12:14Z")

</div>

On development mode, try the code below, but I'm not sure if its advisable  
in prod since server(fast) should be avail to make your data available in  
(near) real time.

client.admin().indices().refresh(new RefreshRequest(indexName)).actionGet();

On Thu, Apr 19, 2012 at 5:53 PM, Diomas [dmitriy.skopa@gmail.com](mailto:dmitriy.skopa@gmail.com) wrote:

> Hey, I'm just started play with ES and want to try it in a desktop app  
> using java API.
> 
> I noticed that if I submit a set of documents with  
> client.prepareIndex(indexName, indexType).setSource( /\* jsonBuilder stuff  
> here \*/ ).execute().actionGet();
> 
> then if I try to search right after I submited my docs:  
> SearchResponse response =  
> client.prepareSearch(indexName).setQuery(queryString("google")).execute().actionGet();  
> I get usually no hits, but sometimes I get one hit. Anyway this is not  
> that I expect: I have 12 documents matching the query.
> 
> But if I put this line before the search statement:  
> Thread.sleep(1000);  
> I get all my 12 hits.
> 
> So the question is how can I know exactly if the index is ready to be  
> searched?

---

<div class="post-metadata">

**Author:** ![Diomas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/diomas/32/2910_2.png) [@Diomas](https://discuss.elastic.co/u/Diomas)\
**Post date:** [April 19, 2012, 10:54am UTC](https://discuss.elastic.co/t/java-api-how-to-detect-just-submitted-docs-are-indexed-or-not/7394/3 "2012-04-19T10:54:08Z")

</div>

That works well. Thanks! I'm working on a desktop tool (single node, no  
shards or replicas) - is it normal for this case?

On Thursday, April 19, 2012 2:12:14 PM UTC+4, Ariel Mendoza wrote:

> On development mode, try the code below, but I'm not sure if its advisable  
> in prod since server(fast) should be avail to make your data available in  
> (near) real time.
> 
> client.admin().indices().refresh(new RefreshRequest(indexName  
> )).actionGet();
> 
> On Thu, Apr 19, 2012 at 5:53 PM, Diomas wrote:
> 
> > Hey, I'm just started play with ES and want to try it in a desktop app  
> > using java API.
> > 
> > I noticed that if I submit a set of documents with  
> > client.prepareIndex(indexName, indexType).setSource( /\* jsonBuilder stuff  
> > here \*/ ).execute().actionGet();
> > 
> > then if I try to search right after I submited my docs:  
> > SearchResponse response =  
> > client.prepareSearch(indexName).setQuery(queryString("google")).execute().actionGet();  
> > I get usually no hits, but sometimes I get one hit. Anyway this is not  
> > that I expect: I have 12 documents matching the query.
> > 
> > But if I put this line before the search statement:  
> > Thread.sleep(1000);  
> > I get all my 12 hits.
> > 
> > So the question is how can I know exactly if the index is ready to be  
> > searched?

On Thursday, April 19, 2012 2:12:14 PM UTC+4, Ariel Mendoza wrote:

> On development mode, try the code below, but I'm not sure if its advisable  
> in prod since server(fast) should be avail to make your data available in  
> (near) real time.
> 
> client.admin().indices().refresh(new RefreshRequest(indexName  
> )).actionGet();
> 
> On Thu, Apr 19, 2012 at 5:53 PM, Diomas wrote:
> 
> > Hey, I'm just started play with ES and want to try it in a desktop app  
> > using java API.
> > 
> > I noticed that if I submit a set of documents with  
> > client.prepareIndex(indexName, indexType).setSource( /\* jsonBuilder stuff  
> > here \*/ ).execute().actionGet();
> > 
> > then if I try to search right after I submited my docs:  
> > SearchResponse response =  
> > client.prepareSearch(indexName).setQuery(queryString("google")).execute().actionGet();  
> > I get usually no hits, but sometimes I get one hit. Anyway this is not  
> > that I expect: I have 12 documents matching the query.
> > 
> > But if I put this line before the search statement:  
> > Thread.sleep(1000);  
> > I get all my 12 hits.
> > 
> > So the question is how can I know exactly if the index is ready to be  
> > searched?

---

<div class="post-metadata">

**Author:** ![Ariel\_Mendoza](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@Ariel\_Mendoza](https://discuss.elastic.co/u/Ariel_Mendoza)\
**Post date:** [April 19, 2012, 11:40am UTC](https://discuss.elastic.co/t/java-api-how-to-detect-just-submitted-docs-are-indexed-or-not/7394/4 "2012-04-19T11:40:54Z")

</div>

I guess your using Node client. You can shift to transport and set the  
number of shards and replicas that your desktop can handle.

1. In {ES\_HOME}/config/elasticsearch.yml edit the property below and leave  
everything else unchanged. By default it has 5 shards and 1 replica each.  
cluster.name: MyIndex

2. Java API Transport Client:  
Settings settings = ImmutableSettings.settingsBuilder()  
.put("cluster.name", "MyIndex")  
.put("client.transport.sniff", true)  
.build();

InetSocketTransportAddress("localhost", 9300));  
client.addTransportAddress(new  
InetSocketTransportAddress("localhost", 9301));

1. You can have multiple es instance in one machine. Open a two(2) command  
line(windows) and execute {ES\_HOME}/bin/elasticsearch.bat in each. This  
should the run transports addresses; localhost:9300 and localhost:9301.  
Same is true if you have multiple machine.

2. Download elasticsearch-head to ease up your monitoring of indexed data  
and/or debugging.

Enjoy!

On Thu, Apr 19, 2012 at 6:54 PM, Diomas [dmitriy.skopa@gmail.com](mailto:dmitriy.skopa@gmail.com) wrote:

> That works well. Thanks! I'm working on a desktop tool (single node, no  
> shards or replicas) - is it normal for this case?
> 
> On Thursday, April 19, 2012 2:12:14 PM UTC+4, Ariel Mendoza wrote:
> 
> > On development mode, try the code below, but I'm not sure if its  
> > advisable in prod since server(fast) should be avail to make your data  
> > available in (near) real time.
> > 
> > client.admin().indices().refresh(new RefreshRequest(indexName  
> > )).actionGet();
> > 
> > On Thu, Apr 19, 2012 at 5:53 PM, Diomas wrote:
> > 
> > > Hey, I'm just started play with ES and want to try it in a desktop app  
> > > using java API.
> > > 
> > > I noticed that if I submit a set of documents with  
> > > client.prepareIndex(indexName, indexType).setSource( /\* jsonBuilder  
> > > stuff here \*/ ).execute().actionGet();
> > > 
> > > then if I try to search right after I submited my docs:  
> > > SearchResponse response =  
> > > client.prepareSearch(indexName).setQuery(queryString("google")).execute().actionGet();  
> > > I get usually no hits, but sometimes I get one hit. Anyway this is not  
> > > that I expect: I have 12 documents matching the query.
> > > 
> > > But if I put this line before the search statement:  
> > > Thread.sleep(1000);  
> > > I get all my 12 hits.
> > > 
> > > So the question is how can I know exactly if the index is ready to be  
> > > searched?
> 
> On Thursday, April 19, 2012 2:12:14 PM UTC+4, Ariel Mendoza wrote:
> 
> > On development mode, try the code below, but I'm not sure if its  
> > advisable in prod since server(fast) should be avail to make your data  
> > available in (near) real time.
> > 
> > client.admin().indices().refresh(new RefreshRequest(indexName  
> > )).actionGet();
> > 
> > On Thu, Apr 19, 2012 at 5:53 PM, Diomas wrote:
> > 
> > > Hey, I'm just started play with ES and want to try it in a desktop app  
> > > using java API.
> > > 
> > > I noticed that if I submit a set of documents with  
> > > client.prepareIndex(indexName, indexType).setSource( /\* jsonBuilder  
> > > stuff here \*/ ).execute().actionGet();
> > > 
> > > then if I try to search right after I submited my docs:  
> > > SearchResponse response =  
> > > client.prepareSearch(indexName).setQuery(queryString("google")).execute().actionGet();  
> > > I get usually no hits, but sometimes I get one hit. Anyway this is not  
> > > that I expect: I have 12 documents matching the query.
> > > 
> > > But if I put this line before the search statement:  
> > > Thread.sleep(1000);  
> > > I get all my 12 hits.
> > > 
> > > So the question is how can I know exactly if the index is ready to be  
> > > searched?

---

<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 21, 2012, 11:19am UTC](https://discuss.elastic.co/t/java-api-how-to-detect-just-submitted-docs-are-indexed-or-not/7394/5 "2012-04-21T11:19:39Z")

</div>

When you index a document, its indexed, but, it won't be immediately  
available to search (thats the near real time aspect). by default, there is  
a refresh\_interval of 1 second where a the index will be refreshed every  
second. You can issue a refresh API call yourself, but its not recommended  
to do it after each index request.

On Thu, Apr 19, 2012 at 1:54 PM, Diomas [dmitriy.skopa@gmail.com](mailto:dmitriy.skopa@gmail.com) wrote:

> That works well. Thanks! I'm working on a desktop tool (single node, no  
> shards or replicas) - is it normal for this case?
> 
> On Thursday, April 19, 2012 2:12:14 PM UTC+4, Ariel Mendoza wrote:
> 
> > On development mode, try the code below, but I'm not sure if its  
> > advisable in prod since server(fast) should be avail to make your data  
> > available in (near) real time.
> > 
> > client.admin().indices().refresh(new RefreshRequest(indexName  
> > )).actionGet();
> > 
> > On Thu, Apr 19, 2012 at 5:53 PM, Diomas wrote:
> > 
> > > Hey, I'm just started play with ES and want to try it in a desktop app  
> > > using java API.
> > > 
> > > I noticed that if I submit a set of documents with  
> > > client.prepareIndex(indexName, indexType).setSource( /\* jsonBuilder  
> > > stuff here \*/ ).execute().actionGet();
> > > 
> > > then if I try to search right after I submited my docs:  
> > > SearchResponse response =  
> > > client.prepareSearch(indexName).setQuery(queryString("google")).execute().actionGet();  
> > > I get usually no hits, but sometimes I get one hit. Anyway this is not  
> > > that I expect: I have 12 documents matching the query.
> > > 
> > > But if I put this line before the search statement:  
> > > Thread.sleep(1000);  
> > > I get all my 12 hits.
> > > 
> > > So the question is how can I know exactly if the index is ready to be  
> > > searched?
> 
> On Thursday, April 19, 2012 2:12:14 PM UTC+4, Ariel Mendoza wrote:
> 
> > On development mode, try the code below, but I'm not sure if its  
> > advisable in prod since server(fast) should be avail to make your data  
> > available in (near) real time.
> > 
> > client.admin().indices().refresh(new RefreshRequest(indexName  
> > )).actionGet();
> > 
> > On Thu, Apr 19, 2012 at 5:53 PM, Diomas wrote:
> > 
> > > Hey, I'm just started play with ES and want to try it in a desktop app  
> > > using java API.
> > > 
> > > I noticed that if I submit a set of documents with  
> > > client.prepareIndex(indexName, indexType).setSource( /\* jsonBuilder  
> > > stuff here \*/ ).execute().actionGet();
> > > 
> > > then if I try to search right after I submited my docs:  
> > > SearchResponse response =  
> > > client.prepareSearch(indexName).setQuery(queryString("google")).execute().actionGet();  
> > > I get usually no hits, but sometimes I get one hit. Anyway this is not  
> > > that I expect: I have 12 documents matching the query.
> > > 
> > > But if I put this line before the search statement:  
> > > Thread.sleep(1000);  
> > > I get all my 12 hits.
> > > 
> > > So the question is how can I know exactly if the index is ready to be  
> > > searched?

---

<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, 3:31am UTC](https://discuss.elastic.co/t/java-api-how-to-detect-just-submitted-docs-are-indexed-or-not/7394/6 "2017-07-06T03:31:33Z")

</div>


