Hi,
I am trying to use JSON builder to get some documents from elasticsearch db.
IndexResponse response = client.prepareIndex("asset_index", "Asset"
).setSource(
*jsonBuilder*().startObject().field("name", "FCC_SRV02"
).endObject()).execute().actionGet();
I am getting IndexResponse.
How can I convert IndexResponse to SearchResponse so I retrieve SearchHit []
Regards,
Janusz
--
dadoonet
(David Pilato)
January 9, 2013, 10:08am
2
Hi Janusz,
Indexing is a different operation than searching.
So, after indexing a doc, you have to GET it or SEARCH it.
Have a look here:
// indexing document
ir = node.client().prepareIndex("meal", "beer").setSource(jsonString)
.execute().actionGet();
Assert.assertNotNull(ir);
Assert.assertNotNull(ir.getId());
GetResponse gr = node.client().prepareGet("meal", "beer", ir.getId())
.execute().actionGet();
Assert.assertNotNull(gr);
Assert.assertNotNull(gr.getId());
Assert.assertEquals(ir.getId(), gr.getId());
Beer indexedBeer = mapper.readValue(gr.getSourceAsBytes(), Beer.class);
Assert.assertNotNull(indexedBeer);
Assert.assertEquals(beer, indexedBeer);
https://github.com/elasticsearchfr/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex1/IndexTest.java#L69
SearchResponse sr = node.client().prepareSearch().setQuery(qb)
.execute().actionGet();
Assert.assertNotNull(sr);
Assert.assertNotNull(sr.getHits());
Assert.assertTrue(sr.getHits().getTotalHits() > 0);
logger.info("We found {} beers", sr.getHits().totalHits());
for (SearchHit hit : sr.getHits()) {
Beer beer = BeerHelper.toBeer(hit.getSourceAsString());
Assert.assertEquals("Heineken", beer.getBrand());
Assert.assertTrue(beer.getPrice()>5 && beer.getPrice()<10);
}
}
/**
* We want to build a complex Query based on brand name and price fields and we want to filter results
* to have only 1L or more beers.
* <br>We must find Heineken beers with price between 5 and 10 and size more than 1.
* <br>We should have some results (or we are really unlucky!).
https://github.com/elasticsearchfr/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex2/SearchTest.java#L268
https://github.com/elasticsearchfr/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex2/SearchTest.java
HTH
David.
Le 9 janvier 2013 à 10:56, JD jdalecki@tycoint.com a écrit :
Hi,
I am trying to use JSON builder to get some documents from elasticsearch db.
IndexResponse response = client.prepareIndex("asset_index",
"Asset").setSource(
jsonBuilder().startObject().field("name",
"FCC_SRV02").endObject()).execute().actionGet();
I am getting IndexResponse.
How can I convert IndexResponse to SearchResponse so I retrieve SearchHit
Regards,
Janusz
--
--
David Pilato
http://www.scrutmydocs.org/
http://dev.david.pilato.fr/
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
--
Sorry David. I feel a bit embarrassed by this question – you are obviously
right - I just did not click that this code is actually creating index and
not searching.
Thanks for the reply and clarification.
Regards,
janusz
On Wednesday, January 9, 2013 8:56:45 PM UTC+11, JD wrote:
Hi,
I am trying to use JSON builder to get some documents from elasticsearch
db.
IndexResponse response = client.prepareIndex("asset_index", "Asset"
).setSource(
*jsonBuilder*().startObject().field("name", "FCC_SRV02"
).endObject()).execute().actionGet();
I am getting IndexResponse.
How can I convert IndexResponse to SearchResponse so I retrieve *SearchHit
Regards,
Janusz
--
dadoonet
(David Pilato)
January 10, 2013, 2:53am
4
Just a new clarification about the terms you use.
This command does not create an index but index a document .
Cheers
David
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
Le 10 janv. 2013 à 03:49, JD jdalecki@tycoint.com a écrit :
Sorry David. I feel a bit embarrassed by this question – you are obviously right - I just did not click that this code is actually creating index and not searching.
Thanks for the reply and clarification.
Regards,
janusz
On Wednesday, January 9, 2013 8:56:45 PM UTC+11, JD wrote:
Hi,
I am trying to use JSON builder to get some documents from elasticsearch db.
IndexResponse response = client.prepareIndex("asset_index", "Asset").setSource(
jsonBuilder().startObject().field("name", "FCC_SRV02").endObject()).execute().actionGet();
I am getting IndexResponse.
How can I convert IndexResponse to SearchResponse so I retrieve SearchHit
Regards,
Janusz
--
--
Yep - correct.
Thanks David for all your replies.
Regards,
Janusz
On Wednesday, January 9, 2013 8:56:45 PM UTC+11, JD wrote:
Hi,
I am trying to use JSON builder to get some documents from elasticsearch
db.
IndexResponse response = client.prepareIndex("asset_index", "Asset"
).setSource(
*jsonBuilder*().startObject().field("name", "FCC_SRV02"
).endObject()).execute().actionGet();
I am getting IndexResponse.
How can I convert IndexResponse to SearchResponse so I retrieve *SearchHit
Regards,
Janusz
--