Java client questions


(otnateos) #1

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/.
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?


(Berkay Mollamustafaoglu-2) #2

I think you've run into "near realtime" aspect of ElasticSearch. Some info
here:
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 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/
.
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?


(Shay Banon) #3

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.comwrote:

I think you've run into "near realtime" aspect of ElasticSearch. Some info
here:
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 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/.
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?


(system) #4