package com.agileeagle.gf.dw.test; import com.agileeagle.gf.dw.service.DwPublicOptionsService; import com.agileeagle.gf.dw.service.impl.DwPublicOptionsServiceImpl; import com.agileeagle.gf.dw.util.DateUtils; import com.agileeagle.gf.dw.util.Delimiter; import com.agileeagle.gf.dw.util.client.ESClient; import com.google.common.collect.Lists; import org.apache.zookeeper.proto.ExistsRequest; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.client.Client; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.sort.SortOrder; import org.junit.Test; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; public class ESTest { ESClient esClient = new ESClient(); @org.junit.Test public void testQuery() { Client client = esClient.getTransportClient(); String index = "group-message-2016.09"; String type = "group-message"; MatchAllQueryBuilder sbuilder = QueryBuilders.matchAllQuery(); SearchRequestBuilder srq = client.prepareSearch(index) .setTypes(type) .setSearchType(SearchType.DFS_QUERY_AND_FETCH) .addSort("@timestamp", SortOrder.DESC).setQuery(sbuilder); SearchResponse response = srq.setFrom(0).setSize(60).setExplain(true).execute().actionGet(); SearchHits hits = response.getHits(); System.out.println(hits.totalHits()); // for (int i = 0; i < hits.getHits().length; i++) { // SearchHit hit = hits.getHits()[i]; // System.out.println(hit.getSourceAsString()); // // Map<String, SearchHitField> hitField = hit.getFields(); // // Set<Map.Entry<String, SearchHitField>> hitFieldEntrys = hitField.entrySet(); // for (Map.Entry<String, SearchHitField> entry : hitFieldEntrys) { // System.out.println(entry.getKey() + "------" + entry.getValue().getValue().toString()); // } // } // // System.out.println("--------------------------------------------"); List<Map<String,Object>> mapList = Lists.newArrayList(); for (int i = 0; i < hits.getHits().length; i++) { SearchHit hit = hits.getHits()[i]; String id = hit.getId(); String hitIndex = hit.getIndex(); Map<String, Object> val = hit.getSource(); mapList.add(val); } System.out.println(mapList.toString()); } @Test public void testString() { String[] indexName = "a_b_c".split(Delimiter.UNDERLINE.getName()); StringBuffer sb = new StringBuffer(); for (int i=0; i<indexName.length; i++) { String name = indexName[i]; if (true) { sb.append(name); } else { break; } if (i!=indexName.length-1) { sb.append(Delimiter.UNDERLINE.getName()); } } System.out.println( sb.toString().split(Delimiter.UNDERLINE.getName()) ); } @org.junit.Test public void testTermQuery() { Client client = esClient.getTransportClient(); String index = "group-message-2016.09"; String type = "group-message"; MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("s_content", "d"); // MatchQueryBuilder queryBuilder2 = QueryBuilders.matchQuery("n_id", "216"); SearchRequestBuilder srq = client.prepareSearch(index) .setTypes(type) .setQuery(queryBuilder) .setSearchType(SearchType.DFS_QUERY_AND_FETCH) .setPostFilter(QueryBuilders.rangeQuery("d_send") .from("2016-09-01 00:00:00") .to("2016-09-17 23:59:59")) .addSort("d_send", SortOrder.DESC); // srq.setQuery(queryBuilder2); // SearchRequestBuilder srq = client.prepareSearch(index).setTypes(type) // .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) // .setQuery(QueryBuilders.termQuery("s_content", "广告")) // Query // .addSort("@timestamp", SortOrder.DESC) // .setFrom(0).setSize(20).setExplain(true); SearchResponse response = srq.setFrom(0).execute().actionGet(); SearchHits hits = response.getHits(); System.out.println(response.getHits().getTotalHits()); List<Map<String,Object>> mapList = Lists.newArrayList(); for (int i = 0; i < hits.getHits().length; i++) { SearchHit hit = hits.getHits()[i]; String id = hit.getId(); String hitIndex = hit.getIndex(); Map<String, Object> val = hit.getSource(); System.out.println(val.toString()); mapList.add(val); } System.out.println(mapList.size()); } @Test public void testIsExit() { Client client = esClient.getTransportClient(); IndicesExistsResponse response = client.admin().indices() .exists(new IndicesExistsRequest(new String[] { "my" })) .actionGet(); if (response.isExists()) { System.out.println("存在的哟"); } // return false; System.out.println("不存在哟"); } DateUtils dateUtils = new DateUtils(); @Test public void testDate() { String startDate = "2016-07-07 09:05:44"; String endDate = "2016-07-27 09:05:44"; List<String> list = dateUtils.getListDateString(startDate, endDate); System.out.println(list); } @Test public void testAddMonth() { // String strDate = "2016-10-01 00:00:00"; // strDate = dateUtils.addMonth(strDate, 1); // System.out.println(strDate); String startDate = "2016-10-01 00:00:00"; String endDate = "2016-10-27 23:59:59"; double count = dateUtils.getMonthMargin(endDate, startDate); System.out.println(count); } @Test public void testService() { DwPublicOptionsService dwPublicOptionsService = new DwPublicOptionsServiceImpl(); List<String> torres = Lists.newArrayList(); torres.add("d"); String startDate = "2016-10-01 00:00:00"; String endDate = "2016-10-27 23:59:59"; List<Map<String,Object>> mapList = dwPublicOptionsService.getPublicOptionsTermQuery(torres, startDate, endDate); System.out.println(mapList); } @Test public void testZhiDing() throws Exception { // String index = "group-message-2016.07.07"; // String type = "group-message"; String index = "quan-dynamic-reply-2016.10"; String index2 = "quan-dynamic-info-2016.10"; String index3 = "quan-dynamic-comments-2016.10"; String type = "quan-dynamic-reply"; String type2 = "quan-dynamic-info"; String type3 = "quan-dynamic-comments"; String keyWord = "1"; Client client = esClient.getTransportClient(); // String index = "crm-chat-record-2016.10"; // String type = "crm-chat-record"; // String keyWord = "address"; // Client client = esClient.getTransportClient(); System.out.println(keyWord); SearchRequestBuilder sbuilder = client.prepareSearch(index, index2, index3).setTypes(type, type2, type3) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery(QueryBuilders.termQuery("s_content", keyWord)) // Query // .setPostFilter(QueryBuilders.rangeQuery("n_type").from(6).to(9)) // Filter // .setPostFilter(QueryBuilders.rangeQuery("@timestamp") // .from(dateUtils.getUTCTimeZoneTime("2016-10-17 00:00:00").replace("+0800", "Z")) // .to(dateUtils.getUTCTimeZoneTime("2016-10-17 23:59:59").replace("+0800", "Z"))) // Filter .addSort("@timestamp", SortOrder.DESC) .setFrom(0).setExplain(true); // System.out.println(sbuilder.toString()); System.out.println("---------------------------------"); SearchResponse response = sbuilder.execute().actionGet(); System.out.println(response.getHits().getTotalHits()); System.out.println("---------------------------------"); for (SearchHit searchHit : response.getHits()) { Map<String, Object> map = searchHit.sourceAsMap(); System.out.println(map.toString()); } } @Test public void testTimeZone() throws Exception{ DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); Date date = dateUtils.convertStringToDate("yyyy-MM-dd HH:mm:ss", "2016-07-07 00:00:00"); TimeZone srcTimeZone = TimeZone.getTimeZone("GMT+8"); TimeZone destTimeZone = TimeZone.getTimeZone("UTC"); System.out.println(dateTransformBetweenTimeZone(date, formatter, srcTimeZone, destTimeZone)); } public String dateTransformBetweenTimeZone(Date sourceDate, DateFormat formatter, TimeZone sourceTimeZone, TimeZone targetTimeZone) { Long targetTime = sourceDate.getTime() - sourceTimeZone.getRawOffset() + targetTimeZone.getRawOffset(); return getTime(new Date(targetTime), formatter); } public String getTime(Date date, DateFormat formatter){ return formatter.format(date); } @Test public void testMsgMonitor() { DwPublicOptionsService dwPublicOptionsService = new DwPublicOptionsServiceImpl(); int info = 1; String startDate = "2016-09-01 00:00:00"; String endDate = "2016-10-27 23:59:59"; String torres = "c"; String uid = ""; String groupId = ""; List<Map<String,Object>> mapList = dwPublicOptionsService.getMessageMonitorTermQuery(info, startDate, endDate, torres, uid, groupId); for (Map<String,Object> map : mapList) { System.out.println(map); } } @Test public void testScroll() { System.out.println("Scroll 启动"); Client client = esClient.getTransportClient(); MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("s_content", "c"); SearchResponse scrollResponse = client.prepareSearch("group-message-2016.09").setTypes("group-message") .setSearchType(SearchType.DFS_QUERY_AND_FETCH) .setSize(1000).setScroll(TimeValue.timeValueMillis(1)).setQuery(matchQueryBuilder) .execute().actionGet(); long count = scrollResponse.getHits().getTotalHits(); int sum = 0; for (int i=0; i<count; i++) { scrollResponse = client.prepareSearchScroll(scrollResponse.getScrollId()) .setScroll(TimeValue.timeValueMillis(8)) .execute().actionGet(); sum += scrollResponse.getHits().hits().length; System.out.println("总量:"+count + "已经查到-》"+sum); } } }
ES测试代码
最新推荐文章于 2024-05-22 14:15:35 发布