# Java client questions

**URL:** https://discuss.elastic.co/t/java-client-questions/3247
**Category:** Elasticsearch
**Created:** [August 24, 2010, 1:56am UTC](https://discuss.elastic.co/t/java-client-questions/3247 "2010-08-24T01:56:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![otnateos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/otnateos/32/3327_2.png) [@otnateos](https://discuss.elastic.co/u/otnateos)
#### Post date: [August 24, 2010, 1:56am UTC](https://discuss.elastic.co/t/java-client-questions/3247/1 "2010-08-24T01:56:48Z")

</div>

Hi,

I am just testing elasticsearch using java client. The examples are  
straight forward, unfortunately there is not much documentation going  
on, so I just throw the question here.

I followed [http://www.elasticsearch.com/docs/elasticsearch/java\_api/index/](http://www.elasticsearch.com/docs/elasticsearch/java_api/index/).  
After adding an index, I try to retrieve it by id

Node node = nodeBuilder().local(true).node();  
Client client = node.client();  
BinaryXContentBuilder builder = jsonBuilder()  
.startObject()  
.field("user","robert")  
.field("postDate",new Date())  
.field("message", "test trying out Elastic Search")  
.endObject();  
System.out.println("json: "+builder.string());  
IndexResponse idxResponse= client.prepareIndex(TWITTER,  
TWEET,"1")  
.setSource(builder)  
.execute()  
.actionGet();

GetResponse getRsp= client.prepareGet(TWITTER, TWEET,  
String.valueOf(id))  
.execute()  
.actionGet();  
boolean exist = getRsp.exists();  
System.out.println("get exist? "+exist);

```
        if(exist) {
            System.out.println("get result:

```

"+getRsp.field("user"));  
}

by the end of execution, the getRsp always return "not exist".

So anything else I am missing to make this piece of code works?

---

<div class="post-metadata">

### Author: ![Berkay\_Mollamustafao](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@Berkay\_Mollamustafao](https://discuss.elastic.co/u/Berkay_Mollamustafao)
#### Post date: [August 24, 2010, 2:39am UTC](https://discuss.elastic.co/t/java-client-questions/3247/2 "2010-08-24T02:39:55Z")

</div>

I think you've run into "near realtime" aspect of Elasticsearch. Some info  
here:  
[www.elasticsearch.com/docs/elasticsearch/index\_modules/engine/robin/](http://www.elasticsearch.com/docs/elasticsearch/index_modules/engine/robin/)

Basically, by default, index is refresh every 1 sec, hence if you want to  
retrieve it right away after you write, you either have to call refresh via  
API or wait for 1 sec.  
You can find some of the earlier discussions in the list, if you search for  
near real time.

Regards,  
Berkay Mollamustafaoglu  
mberkay on yahoo, google and skype

On Mon, Aug 23, 2010 at 9:56 PM, otnateos [otnateos@gmail.com](mailto:otnateos@gmail.com) wrote:

> Hi,
> 
> I am just testing elasticsearch using java client. The examples are  
> straight forward, unfortunately there is not much documentation going  
> on, so I just throw the question here.
> 
> I followed [http://www.elasticsearch.com/docs/elasticsearch/java\_api/index/](http://www.elasticsearch.com/docs/elasticsearch/java_api/index/)  
> .  
> After adding an index, I try to retrieve it by id
> 
> Node node = nodeBuilder().local(true).node();  
> Client client = node.client();  
> BinaryXContentBuilder builder = jsonBuilder()  
> .startObject()  
> .field("user","robert")  
> .field("postDate",new Date())  
> .field("message", "test trying out Elastic Search")  
> .endObject();  
> System.out.println("json: "+builder.string());  
> IndexResponse idxResponse= client.prepareIndex(TWITTER,  
> TWEET,"1")  
> .setSource(builder)  
> .execute()  
> .actionGet();
> 
> GetResponse getRsp= client.prepareGet(TWITTER, TWEET,  
> String.valueOf(id))  
> .execute()  
> .actionGet();  
> boolean exist = getRsp.exists();  
> System.out.println("get exist? "+exist);
> 
> ```
> if(exist) {
> System.out.println("get result:
> 
> ```
> 
> "+getRsp.field("user"));  
> }
> 
> by the end of execution, the getRsp always return "not exist".
> 
> So anything else I am missing to make this piece of code works?

---

<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: [August 24, 2010, 11:01am UTC](https://discuss.elastic.co/t/java-client-questions/3247/3 "2010-08-24T11:01:58Z")

</div>

Yep, this is the problem. You can explicitly call the refresh API in your  
_test_ using client.admin().indices().prepareRefresh()... .

-shay.banon

On Tue, Aug 24, 2010 at 5:39 AM, Berkay Mollamustafaoglu  
[mberkay@gmail.com](mailto:mberkay@gmail.com)wrote:

> I think you've run into "near realtime" aspect of Elasticsearch. Some info  
> here:  
> [www.elasticsearch.com/docs/elasticsearch/index\_modules/engine/robin/](http://www.elasticsearch.com/docs/elasticsearch/index_modules/engine/robin/)
> 
> Basically, by default, index is refresh every 1 sec, hence if you want to  
> retrieve it right away after you write, you either have to call refresh via  
> API or wait for 1 sec.  
> You can find some of the earlier discussions in the list, if you search for  
> near real time.
> 
> Regards,  
> Berkay Mollamustafaoglu  
> mberkay on yahoo, google and skype
> 
> On Mon, Aug 23, 2010 at 9:56 PM, otnateos [otnateos@gmail.com](mailto:otnateos@gmail.com) wrote:
> 
> > Hi,
> > 
> > I am just testing elasticsearch using java client. The examples are  
> > straight forward, unfortunately there is not much documentation going  
> > on, so I just throw the question here.
> > 
> > I followed  
> > [http://www.elasticsearch.com/docs/elasticsearch/java\_api/index/](http://www.elasticsearch.com/docs/elasticsearch/java_api/index/).  
> > After adding an index, I try to retrieve it by id
> > 
> > Node node = nodeBuilder().local(true).node();  
> > Client client = node.client();  
> > BinaryXContentBuilder builder = jsonBuilder()  
> > .startObject()  
> > .field("user","robert")  
> > .field("postDate",new Date())  
> > .field("message", "test trying out Elastic Search")  
> > .endObject();  
> > System.out.println("json: "+builder.string());  
> > IndexResponse idxResponse= client.prepareIndex(TWITTER,  
> > TWEET,"1")  
> > .setSource(builder)  
> > .execute()  
> > .actionGet();
> > 
> > GetResponse getRsp= client.prepareGet(TWITTER, TWEET,  
> > String.valueOf(id))  
> > .execute()  
> > .actionGet();  
> > boolean exist = getRsp.exists();  
> > System.out.println("get exist? "+exist);
> > 
> > ```
> > if(exist) {
> > System.out.println("get result:
> > 
> > ```
> > 
> > "+getRsp.field("user"));  
> > }
> > 
> > by the end of execution, the getRsp always return "not exist".
> > 
> > So anything else I am missing to make this piece of code works?

---

<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:20am UTC](https://discuss.elastic.co/t/java-client-questions/3247/4 "2017-07-06T04:20:25Z")

</div>


