<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
import com.fasterxml.jackson.databind.ObjectMapper; import org.elasticsearch.action.bulk.BulkProcessor; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.get.MultiGetItemResponse; import org.elasticsearch.action.get.MultiGetResponse; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.reindex.BulkIndexByScrollResponse; import org.elasticsearch.index.reindex.DeleteByQueryAction; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.metrics.sum.Sum; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.Test; import java.net.InetAddress; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; /** * Package: Description: (java 操作es) Created by byte-zbs on * 2017 05 2017/5/4 17:16. * * @version:1.0.0 */ public class ESDemo { /** * 插入操作 */ @Test public void test1() { // 集群设置 Settings settings = Settings.builder().put("cluster.name", "mytest") .build(); try { TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("192.168.0.213"), 9300)); // 构建json XContentBuilder builder = XContentFactory.jsonBuilder() .startObject().field("user", "国") .field("postDate", new Date()) .field("age",23) .field("message", "trying out Elastics").endObject(); // client.prepareIndex().setSource() IndexResponse indexResponse = client.prepareIndex("test", "test") .setSource(builder).get(); } catch (Exception e) { e.printStackTrace(); } } /** * 根据id查询 */ @Test public void test2() { Settings settings = Settings.builder().put("cluster.name", "mytest") .build(); try { TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("192.168.0.213"), 9300)); GetResponse getResponse = client .prepareGet("test", "testxx", "AVvSzcIDCfxdfRyu9_r3") .setOperationThreaded(false).get(); Map<String, Object> map = getResponse.getSource(); for (String string : map.keySet()) { System.out.println(map.get(string)); } } catch (Exception e) { e.printStackTrace(); } } /** * 删除操作 */ @Test public void test3() { Settings settings = Settings.builder().put("cluster.name", "mytest") .build(); try { TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("192.168.0.213"), 9300)); BulkIndexByScrollResponse response = DeleteByQueryAction.INSTANCE.newRequestBuilder(client).filter( QueryBuilders.matchQuery("user","更新名字")).source("test").get(); long l = response.getDeleted(); System.out.println(l); } catch (Exception e) { e.printStackTrace(); } } /** * 更新操作 */ @Test public void test4() { Settings settings = Settings.builder().put("cluster.name", "mytest") .build(); try { TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("192.168.0.213"), 9300)); UpdateRequest updateRequest = new UpdateRequest(); updateRequest.index("test"); updateRequest.type("test"); updateRequest.id("AVvS9BWwCfxdfRyu9_sB"); updateRequest.doc(XContentFactory.jsonBuilder().startObject().field("user","更新名字").endObject()); UpdateResponse response = client.update(updateRequest).get(); } catch (Exception e) { e.printStackTrace(); } } @Test public void test5() { Settings settings = Settings.builder().put("cluster.name", "mytest") .build(); try { TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("192.168.0.213"), 9300)); MultiGetResponse responses = client.prepareMultiGet().add("test","test","AVvS8-mRCfxdfRyu9_sA","AVvS9BWwCfxdfRyu9_sB").get(); for (MultiGetItemResponse multiGetItemResponses:responses) { GetResponse getResponse = multiGetItemResponses.getResponse(); if (getResponse.isExists()) { System.out.println(getResponse.getSourceAsString()); } } } catch (Exception e) { e.printStackTrace(); } } .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("192.168.0.213"), 9300)); SearchResponse searchResponse = client.prepareSearch("test").setTypes("test") .setPostFilter(QueryBuilders.rangeQuery("age").from(23).to(30)) .addSort("age", SortOrder.ASC).execute().actionGet(); System.out.println(searchResponse.toString()); } catch (Exception e) { e.printStackTrace(); } } /** * 聚合 */ @Test public void test8() { Settings settings = Settings.builder().put("cluster.name", "mytest") .build(); try { TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("192.168.0.213"), 9300)); SearchResponse searchResponse = client.prepareSearch("test").setTypes("test") .setQuery(QueryBuilders.matchAllQuery()) .setSearchType(SearchType.QUERY_THEN_FETCH) .addAggregation(AggregationBuilders.terms("group_age") .field("age").subAggregation(AggregationBuilders.sum("sum_age").field("age")))//根据age分组,默认返回10,size(0)也是10 .get(); Terms terms = searchResponse.getAggregations().get("group_age"); List<Terms.Bucket> buckets = terms.getBuckets(); for(Terms.Bucket bt : buckets) { Sum sum = bt.getAggregations().get("sum_age"); System.out.println(sum.getValue() + "===" + sum.getName() ); } } catch (Exception e) { e.printStackTrace(); } } /** * 插入对象操作 * @param user */ public static void test9(User user) { Settings settings = Settings.builder().put("cluster.name", "mytest") .build(); try { TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("192.168.0.213"), 9300)); ObjectMapper mapper = new ObjectMapper(); byte[] bytes = mapper.writeValueAsBytes(user); IndexResponse indexResponse = client.prepareIndex("test","test").setSource(bytes).setId("1").execute().actionGet(); System.out.println(indexResponse.status()); //CREATED } catch (Exception e) { e.printStackTrace(); } } /** * 区间查询 * @param start * @param end * @return */ public static List<String> test10(int start,int end) { Settings settings = Settings.builder().put("cluster.name", "mytest") .build(); List<String> list = new ArrayList<>(); try { TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("192.168.0.213"), 9300)); SearchResponse searchResponse = client.prepareSearch("test").setTypes("test").setPostFilter(QueryBuilders.rangeQuery("age")).execute().actionGet(); SearchHits searchHits = searchResponse.getHits(); SearchHit[] searchHits1 = searchHits.hits(); for (SearchHit searchHitFields:searchHits1) { list.add(searchHitFields.getSourceAsString()); } } catch (Exception e) { e.printStackTrace(); } return list; } /** * 排序 * @param age */ public static void test11(String age) { Settings settings = Settings.builder().put("cluster.name", "mytest") .build(); List<String> list = new ArrayList<>(); try { TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("192.168.0.213"), 9300)); SearchResponse searchResponse = client.prepareSearch("test").setTypes("test").addSort("age",SortOrder.ASC).execute().actionGet(); SearchHits searchHits = searchResponse.getHits(); SearchHit[] searchHits1 = searchHits.hits(); for (SearchHit searchHitFields:searchHits1) { System.out.println(searchHitFields.getSourceAsString()); } } catch (Exception e) { e.printStackTrace(); } } @Test public void queryNewAnswerByhours(){ try { Settings settings = Settings.builder().put("cluster.name", "mytest") .build(); TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("192.168.0.213"), 9300)); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); SearchRequestBuilder searchRequestBuilder = client.prepareSearch("test"); searchRequestBuilder.setTypes("test"); String startTime ="2017-05-01 12:00:00"; String endTime = "2017-05-05 12:00:00"; Date date = TimeParse.stringToDate(startTime,"yyyy-MM-dd HH:mm:ss"); Date dateend = TimeParse.stringToDate(endTime,"yyyy-MM-dd HH:mm:ss"); boolQueryBuilder.must(QueryBuilders.rangeQuery("postDate").gt(date).lte(dateend)); searchRequestBuilder .setPostFilter(QueryBuilders.rangeQuery("postDate").gte(date).lte(dateend)); SearchResponse r = searchRequestBuilder.get(); SearchHits searchHits = r.getHits(); SearchHit[] SS = searchHits.hits(); for (SearchHit searchHitFields:SS) { System.out.println(searchHitFields.getSourceAsString()); } } catch (Exception e) { } /** * 分页查询 因为普通的分页仅仅是1W条数据 大量的数据获取需要scroll来获取相当于游标的方式 */ public static void queryNewAnswerByhours(TransportClient client){ SearchResponse scrollResponse = client.prepareSearch("test").setTypes("test") .setQuery(QueryBuilders.matchAllQuery()).setPostFilter(QueryBuilders.rangeQuery("time").gte("2017-05-01").lte("2017-05-07")) .setSize(10000).setScroll(TimeValue.timeValueMinutes(1)) .execute().actionGet(); long count = scrollResponse.getHits().getTotalHits(); for(int i=0,sum=0; sum<count; i++){ scrollResponse = client.prepareSearchScroll(scrollResponse.getScrollId()) .setScroll(TimeValue.timeValueMinutes(8)) .execute().actionGet(); sum += scrollResponse.getHits().hits().length; SearchHit[] SS = scrollResponse.getHits().hits(); for (SearchHit searchHitFields:SS) { System.out.println(searchHitFields.getId()); } System.out.println("总量"+count+" 已经查到"+sum); } } public static void main(String[] args) { User user = new User("虾米2-update-2",new Date(),40); test11("age"); } }