Hi,
i am trying out elasticsearch with the java api, but i dont get it to work. I can index documents, but searching for them does not work. I tried termQuery(), termsQuery() and matchQuery(). But documents are never returned.
What is wrong in the code below?
Kind regards
David
final Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
client.admin().indices().create(new CreateIndexRequest("myindex")).actionGet();
final XContentBuilder product1 = XContentFactory.jsonBuilder().prettyPrint()
.startObject()
.field("id", "prod1")
.field("name", "prod1")
.field("productUrl", "man-prod1")
.field("price", "9.99")
.endObject();
final XContentBuilder product2 = XContentFactory.jsonBuilder().prettyPrint()
.startObject()
.field("id", "prod2")
.field("name", "prod2")
.field("productUrl", "man-prod2")
.field("price", "19.99")
.endObject();
IndexResponse response1 = client.prepareIndex("myindex", "products", "prod1")
.setSource(product1).get();
System.out.println(response1.isCreated()); // gives true
IndexResponse response2 = client.prepareIndex("myindex", "products", "prod2")
.setSource(product2).get();
System.out.println(response2.isCreated()); // gives true
final SearchResponse searchResponse = client.prepareSearch("myindex")
.setTypes("products")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.termQuery("productUrl", "man-prod2"))
.execute()
.actionGet();
final SearchHit[] searchHits = searchResponse.getHits().getHits();