elasticsearch入门(二 基础api实例)
本人目前使用的版本是2.0.0
client
基础代码如下
Settings settings = Settings.settingsBuilder().put("cluster.name","jiangnan_cluster").put("client.transport.sniff", true).build(); try { ElasticClient client = TransportClient.builder().settings(settings).build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.24.3.95"), 9300)) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.24.3.96"), 9300)) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.24.3.97"), 9300)); } catch (UnknownHostException e) { e.printStackTrace(); }
官网demo
https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.0/transport-client.html
官网给的demo 并没有设置setting 的方式,使用后会有
None of the configured nodes are available 异常
create index
基础代码如下
Map<String, Object> json = new HashMap<String, Object>(); json.put("user","jn"); json.put("postDate",new Date()); json.put("message", "trying out Elasticsearch"); IndexResponse response = client.prepareIndex("index", “type”, ”1") .setSource(json) .get();
如果 此处 id 不自己定义, es 会创建一个默认的随机码(还未细看具体实现)似的编码 如:AVRCKiIlXmm1GX2hHZvw
官网demo
https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.0/java-docs-index.html
get index
基础代码如下
GetResponse response = client.prepareGet("index", "type", "1") .setOperationThreaded(false) .get(); System.out.println(response.getSourceAsString());
注:setOperationThreaded
operationthreaded 默认为 true,此处显示的设置为false 代表 非多线程模式
官网demo
https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.0/java-docs-get.html
delete index 同 get index
官网demo
https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.0/java-docs-delete.html
update index
基础代码如下
Map<String, Object> json = new HashMap<String, Object>(); json.put("id","ss"); json.put("user","jbbcn"); json.put("postDate",new Date()); json.put("message", "trying out Elasticsearch"); try { UpdateRequest request = new UpdateRequest("index","type","1") .doc(jsonBuilder().startObject().field("user", "zy").endObject()); UpdateResponse response = client.update(request).get(); System.out.println(response.getVersion()); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); }
官网demo
https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.0/java-docs-update.html
bulk
基础代码如下
BulkRequestBuilder request = client.prepareBulk(); BulkResponse response = null; try { response = request .add(client.prepareIndex("index", "type", "2") .setSource(jsonBuilder().startObject().field("user", "jn5").endObject())) .add(client.prepareIndex("index", "type", "3") .setSource(jsonBuilder().startObject().field("user", "jn3").endObject())) .add(client.prepareDelete("index","type","3")) .get(); } catch (IOException e) { e.printStackTrace(); } System.out.println(response.toString());
此处作用是将多个操作合并为一个request 操作
官网demo
https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.0/java-docs-bulk.html
search index
基础代码如下
SearchResponse response = client.prepareSearch("index") .setTypes("type") .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery(QueryBuilders.termQuery("message","out")) .setPostFilter(QueryBuilders.existsQuery("id”))//只获取包含 id field 的 结果 .setFrom(0).setSize(10).setExplain(true) .execute() .actionGet(); SearchHits searchHits = response.getHits(); System.out.println(searchHits.hits().length); for(SearchHit hitFields:searchHits.getHits()){ System.out.println(hitFields.getSource().toString()); } System.out.println(response.getHits().toString()); // 获取所有数据 // SearchResponse response = client.prepareSearch().execute().actionGet();
官网demo
https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.0/java-search.html