Skip to content

Instantly share code, notes, and snippets.

@ledangtuanbk
Created February 11, 2019 09:26
Show Gist options
  • Save ledangtuanbk/8076ad2ba6980292ca2b745e947dd63f to your computer and use it in GitHub Desktop.
Save ledangtuanbk/8076ad2ba6980292ca2b745e947dd63f to your computer and use it in GitHub Desktop.
public static void main(String[] args) {
try {
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
String jsonObject = "{\"age\":14,\"dateOfBirth\":1471466076564,"
+"\"fullName\":\"John Doe\"}";
IndexResponse response = client.prepareIndex("people", "Doe")
.setSource(jsonObject, XContentType.JSON).get();
String id = response.getId();
String index = response.getIndex();
String type = response.getType();
long version = response.getVersion();
System.out.println(id);
System.out.println(index);
System.out.println(type);
System.out.println(version);
System.out.println(response.getResult());
SearchResponse searchResponse = client.prepareSearch()
.setTypes()
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setPostFilter(QueryBuilders.rangeQuery("age").from(10).to(15))
.execute()
.actionGet();
List<SearchHit> searchHits = Arrays.asList(searchResponse.getHits().getHits());
searchHits.forEach(
hit -> System.out.println(hit.getSourceAsString()));
}catch (Exception ex){
ex.printStackTrace();
}
}
@ledangtuanbk
Copy link
Author

org.elasticsearch.client transport 6.6.0 org.elasticsearch elasticsearch 6.6.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment