1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
| package com.demo;
import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.metrics.avg.Avg; import org.elasticsearch.search.aggregations.metrics.max.Max; import org.elasticsearch.search.aggregations.metrics.min.Min; import org.elasticsearch.search.aggregations.metrics.sum.Sum; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.Before; import org.junit.Test;
import java.net.InetAddress; import java.util.Map;
@Slf4j public class QueryTest {
TransportClient transportClient = null;
private final static String hostName = "localhost"; private final static Integer port = 9300; private final static Integer poolSize = 5;
private final static String index = "db-1"; private final static String type = "tab-1";
@Before public void init() { try { Settings esSetting = Settings.builder() .put("cluster.name", "docker-cluster")
.put("thread_pool.search.size", poolSize) .build(); transportClient = new PreBuiltTransportClient(esSetting); TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), port); transportClient.addTransportAddresses(transportAddress); } catch (Exception e) { log.error("elasticsearch TransportClient create error!!", e); } }
@Test public void matchQuery() {
QueryBuilder fieldQuery = QueryBuilders.matchQuery("age", 98); log.info("对 age = 28 匹配"); mathquery(fieldQuery); log.info("查询所有"); QueryBuilder allQuery = QueryBuilders.matchAllQuery(); mathquery(allQuery); log.info("matchPhrase查询"); log.info("词语匹配短语 : 内容 -->"); QueryBuilder matchQuery = QueryBuilders.matchPhraseQuery("content", "内容 -->1"); mathquery(matchQuery);
}
@Test public void termTest() {
QueryBuilder oneTermBuilder = QueryBuilders.termQuery("content", "90"); mathquery(oneTermBuilder);
log.info("---------");
QueryBuilder termsBuilder = QueryBuilders.termsQuery("content", "内容1", "97"); mathquery(termsBuilder);
}
@Test public void jhTest() {
String maxName = "ageMax"; AggregationBuilder max = AggregationBuilders.max(maxName).field("age"); log.info(" query DSL : {}", max.toString()); SearchResponse sr = transportClient.prepareSearch(index).addAggregation(max).get(); Max maxValue = sr.getAggregations().get(maxName);
log.info(" data : {}", maxValue.toString()); log.info(" max value : {}", maxValue.getValue());
String minName = "ageMin"; AggregationBuilder min = AggregationBuilders.min(minName).field("age"); log.info(" query DSL : {}", min.toString()); SearchResponse mrep = transportClient.prepareSearch(index).addAggregation(min).get(); Min minValue = mrep.getAggregations().get(minName);
log.info(" data : {}", minValue.toString()); log.info(" min value : {}", minValue.getValue());
String avgName = "avgName"; AggregationBuilder avg = AggregationBuilders.avg(avgName).field("age"); log.info(" query DSL : {}", avg.toString()); SearchResponse avgrep = transportClient.prepareSearch(index).addAggregation(avg).get(); Avg avgValue = avgrep.getAggregations().get(avgName);
log.info(" data : {}", avgValue.toString()); log.info(" avg value : {}", avgValue.getValue());
String sumName = "avgName"; AggregationBuilder sum = AggregationBuilders.sum(sumName).field("age"); log.info(" query DSL : {}", sum.toString()); SearchResponse sumrep = transportClient.prepareSearch(index).addAggregation(sum).get(); Sum sumValue = sumrep.getAggregations().get(sumName);
log.info(" data : {}", sumValue.toString()); log.info(" avg value : {}", sumValue.getValue());
}
private void mathquery(QueryBuilder fieldQuery) { log.error(" query 语句 : {}", fieldQuery.toString()); SearchResponse response = transportClient.prepareSearch(index).setQuery(fieldQuery).get(); for(SearchHit hit:response.getHits()) { Map<String, Object> map = hit.getSourceAsMap(); log.info("query data : {}", JSON.toJSONString(map)); } }
}
|