import com.datacloudsec.analysis.rule.commons.es.JestService;
import com.datacloudsec.analysis.rule.commons.event.model.EventLinkObject;
import io.searchbox.annotations.JestId;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.search.aggregation.*;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.*;
import java.io.IOException;
import java.util.*;
import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
public class TestEsQuery {
private JestService jestService;
private JestClient client;
private String indexName = "event";
private String typeName = "event_log";
@Before
public void setUp() throws Exception {
jestService = new JestService();
client = jestService.getJestClient();
}
@After
public void tearDown() throws Exception {
jestService.closeJestClient(client);
}
@Test
public void createIndex() throws Exception {
boolean result = jestService.createIndex(client, indexName);
System.out.println(result);
}
@Test
public void testGetTermsAggregation()
throws Exception {
createIndex();
String source = "{\"" + typeName + "\":{\"properties\":{" +
"\"gender\":{\"store\":true,\"type\":\"keyword\"}" +
",\"age\":{\"store\":true,\"type\":\"keyword\"}" +
"}}}";
System.out.println(source);
boolean result1 = jestService.createIndexMapping(client, indexName, typeName, source);
System.out.println(result1);
index(indexName, typeName, null, "{\"gender\":\"male\",\"gender11\":\"male11\"}");
index(indexName, typeName, null, "{\"gender\":\"male\",\"gender11\":\"male11\"}");
index(indexName, typeName, null, "{\"gender\":\"female\",\"gender11\":\"male11\"}");
String query = "{\n" +
" \"query\" : {\n" +
" \"match_all\" : {}\n" +
" },\n" +
" \"aggs\" : {\n" +
" \"terms1\" : {\n" +
" \"terms\" : {\n" +
" \"field\" : \"gender\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
Search search = new Search.Builder(query)
.addIndex(indexName)
.addType(typeName)
.build();
SearchResult result = client.execute(search);
assertTrue(result.getErrorMessage(), result.isSucceeded());
}
@Test
public void createIndexMapping() throws Exception {
createIndex();
String source = "{\"" + typeName + "\":{\"properties\":{"
+ "\"act\":{\"store\":true,\"type\":\"keyword\"}"
+ ",\"ip\":{\"store\":true,\"type\":\"keyword\"}"
+ ",\"value\":{\"store\":true,\"type\":\"keyword\"}"
+ ",\"url\":{\"store\":true,\"type\":\"keyword\"}"
+ "}}}";
System.out.println(source);
boolean result = jestService.createIndexMapping(client, indexName, typeName, source);
// boolean result = jestService.createIndexMapping(client, indexName, typeName, source);
System.out.println(result);
index(indexName, typeName, null, "{\"hour\":\"2013-02-01\",\"value\":\"1\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-02\",\"value\":\"1\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-03\",\"value\":\"1\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-04\",\"value\":\"1\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-05\",\"value\":\"1\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-06\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-06\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-06\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-06\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-06\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-07\",\"value\":\"1\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-08\",\"value\":\"1\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-09\",\"value\":\"1\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-10\",\"value\":\"1\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-11\",\"value\":\"1\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-12\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-12\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-12\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-12\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-12\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-12\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-13\",\"value\":\"1\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-14\",\"value\":\"1\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-15\",\"value\":\"1\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-16\",\"value\":\"1\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-17\",\"value\":\"1\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-18\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-18\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-18\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-18\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-18\",\"value\":\"5\",\"act\":\"1\"}");
index(indexName, typeName, null, "{\"hour\":\"2013-02-18\",\"value\":\"5\",\"act\":\"1\"}");
}
@Test
public void queryTest() throws IOException {
String query = "{\n" +
" \"aggs\": {\n" +
" \"queries\": {\n" +
" \"terms\": {\n" +
" \"field\": \"act\",\n" +
" \"size\": 500\n" +
" },\n" +
" \"aggs\": {\n" +
" \"series\": {\n" +
" \"date_histogram\": {\n" +
" \"field\": \"hour\",\n" +
" \"interval\": \"day\"\n" +
" },\n" +
" \"aggs\": {\n" +
" \"avg\": {\n" +
" \"value_count\": {\n" +
" \"field\": \"value\"\n" +
" }\n" +
" },\n" +
" \"movavg\": {\n" +
" \"moving_avg\": {\n" +
" \"buckets_path\": \"avg\",\n" +
" \"window\": 24,\n" +
" \"model\": \"simple\"\n" +
" }\n" +
" },\n" +
" \"surprise\": {\n" +
" \"bucket_script\": {\n" +
" \"buckets_path\": {\n" +
" \"avg\": \"avg\",\n" +
" \"movavg\": \"movavg\"\n" +
" },\n" +
" \"script\": \"Math.abs(params.avg - params.movavg)\"\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"largest_surprise\": {\n" +
" \"max_bucket\": {\n" +
" \"buckets_path\": \"series.surprise\"\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"ninetieth_surprise\": {\n" +
" \"percentiles_bucket\": {\n" +
" \"buckets_path\": \"queries>largest_surprise\",\n" +
" \"percents\": [\n" +
" 90.0\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
Search search = new Search.Builder(query)
.addIndex(indexName)
.addType(typeName)
.build();
SearchResult result = client.execute(search);
assertTrue(result.getErrorMessage(), result.isSucceeded());
TermsAggregation terms_name = result.getAggregations().getTermsAggregation("terms_name");
}
@Test
public void getIndexMapping() throws Exception {
String result = jestService.getIndexMapping(client, "event_20180326", "event");
// String result = jestService.getIndexMapping(client, indexName, typeName);
System.out.println(result);
}
@Test
public void index_1() throws Exception {
List<Object> objs = new ArrayList<Object>();
objs.add(new User(1, "T:o\"m-", new Date()));
objs.add(new User(2, "J,e{r}r;y:", new Date()));
boolean result = jestService.index(client, indexName, typeName, objs);
System.out.println(result);
}
@Test
public void termQuery() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders
.termQuery("name", "T:o\"m-");//单值完全匹配查询
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.size(10);
searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
SearchResult result = jestService.search(client, indexName, typeName, query);
List<SearchResult.Hit<User, Void>> hits = result.getHits(User.class);
System.out.println("Size:" + hits.size());
for (SearchResult.Hit<User, Void> hit : hits) {
User user = hit.source;
System.out.println(user.toString());
}
}
@Test
public void termQuery1() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders
// .termQuery("user", "admin");//单值完全匹配查询
.matchAllQuery();
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.size(1000);
searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
ArrayList arrayList = new ArrayList();
// arrayList.add("event_20180327");
// arrayList.add("event_20180326");
// arrayList.add("event_20180325");
// arrayList.add("event_20180324");
// arrayList.add("event_20180323");
// arrayList.add("event_20180322");
// arrayList.add("event_20180321");
// arrayList.add("event_20180320");
arrayList.add("event");
for (int i = 0; i < arrayList.size(); i++) {
if (!jestService.indicesExists(client, (String) arrayList.get(i))) {
arrayList.remove(i);
}
}
SearchResult result = jestService.search(client, arrayList, typeName, query);
// SearchResult result = jestService.search(client, indexName, typeName, query);
assertTrue(result.getErrorMessage(), result.isSucceeded());
List<SearchResult.Hit<EventLinkObject, Void>> hits = result.getHits(EventLinkObject.class);
System.out.println("Size:" + hits.size());
for (SearchResult.Hit<EventLinkObject, Void> hit : hits) {
EventLinkObject user = hit.source;
System.out.println(user);
}
}
@Test
public void termsQuery() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders
.termsQuery("name", new String[]{"T:o\"m-", "J,e{r}r;y:"});//多值完全匹配查询
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.size(10);
searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
SearchResult result = jestService.search(client, indexName, typeName, query);
List<SearchResult.Hit<User, Void>> hits = result.getHits(User.class);
System.out.println("Size:" + hits.size());
for (SearchResult.Hit<User, Void> hit : hits) {
User user = hit.source;
System.out.println(user.toString());
}
}
@Test
public void wildcardQuery() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders
.wildcardQuery("name", "*:*");//通配符和正则表达式查询
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.size(10);
searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
SearchResult result = jestService.search(client, indexName, typeName, query);
List<SearchResult.Hit<User, Void>> hits = result.getHits(User.class);
System.out.println("Size:" + hits.size());
for (SearchResult.Hit<User, Void> hit : hits) {
User user = hit.source;
System.out.println(user.toString());
}
}
@Test
public void prefixQuery() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders
.prefixQuery("name", "T:o");//前缀查询
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.size(10);
searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
SearchResult result = jestService.search(client, indexName, typeName, query);
List<SearchResult.Hit<User, Void>> hits = result.getHits(User.class);
System.out.println("Size:" + hits.size());
for (SearchResult.Hit<User, Void> hit : hits) {
User user = hit.source;
System.out.println(user.toString());
}
}
@Test
public void rangeQuery() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders
.rangeQuery("occur_time")
.gte("0")
.lte("11111111111111")
.includeLower(true)
.includeUpper(true);//区间查询
searchSourceBuilder.query(queryBuilder);
// searchSourceBuilder.size(10);
// searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
SearchResult result = jestService.search(client, indexName, typeName, query);
List<SearchResult.Hit<EventLinkObject, Void>> hits = result.getHits(EventLinkObject.class);
System.out.println("Size:" + hits.size());
for (SearchResult.Hit<EventLinkObject, Void> hit : hits) {
EventLinkObject user = hit.source;
System.out.println(user.toString());
}
}
@Test
public void queryString() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders
.queryString(QueryParser.escape("T:o\""));//文本检索,应该是将查询的词先分成词库中存在的词,然后分别去检索,存在任一存在的词即返回,查询词分词后是OR的关系。需要转义特殊字符
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.size(10);
searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
SearchResult result = jestService.search(client, indexName, typeName, query);
List<SearchResult.Hit<User, Void>> hits = result.getHits(User.class);
System.out.println("Size:" + hits.size());
for (SearchResult.Hit<User, Void> hit : hits) {
User user = hit.source;
System.out.println(user.toString());
}
}
@Test
public void count() throws Exception {
String[] name = new String[]{"T:o\"m-", "Jerry"};
String from = "2016-09-01T00:00:00";
String to = "2016-10-01T00:00:00";
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.termsQuery("name", name))
.must(QueryBuilders.rangeQuery("birth").gte(from).lte(to));
searchSourceBuilder.query(queryBuilder);
String query = searchSourceBuilder.toString();
System.out.println(query);
Double count = jestService.count(client, indexName, typeName, query);
System.out.println("Count:" + count);
}
@Test
public void count1() throws Exception {
String[] name = new String[]{"adm"};
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.termsQuery("user", name));
searchSourceBuilder.query(queryBuilder);
String query = searchSourceBuilder.toString();
System.out.println(query);
Double count = jestService.count(client, indexName, typeName, query);
System.out.println("Count:" + count);
}
@Test
public void get() throws Exception {
String id = "2";
JestResult result = jestService.get(client, indexName, typeName, id);
if (result.isSucceeded()) {
User user = result.getSourceAsObject(User.class);
System.out.println(user.toString());
}
}
@Test
public void deleteIndexDocument() throws Exception {
String id = "2";
boolean result = jestService.delete(client, indexName, typeName, id);
System.out.println(result);
}
@Test
public void deleteIndex() throws Exception {
ArrayList<String> arrayList = new ArrayList();
// arrayList.add("event_20180324");
// arrayList.add("event_20180323");
// arrayList.add("event_20180322");
// arrayList.add("event_20180321");
// arrayList.add("event_20180320");
// arrayList.add("event_20180319");
// arrayList.add("event_20180318");
// arrayList.add("event_20180317");
// arrayList.add("event_20180326");
// arrayList.add("event_20180325");
arrayList.add("event");
arrayList.forEach(index -> {
boolean result = false;
try {
result = jestService.delete(client, index);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(result);
});
}
@Test
public void testGetValueCountAggregation1() throws Exception {
ArrayList<String> arrayList = new ArrayList();
arrayList.add("event_20180324");
arrayList.add("event_20180323");
arrayList.add("event_20180322");
arrayList.add("event_20180321");
arrayList.add("event_20180326");
arrayList.add("event_20180325");
arrayList.add("event_20180320");
arrayList.add("event_20180319");
arrayList.add("event_20180318");
// arrayList.add("event");
for (int i = 0; i < arrayList.size(); i++) {
if (!jestService.indicesExists(client, arrayList.get(i))) {
arrayList.remove(i);
}
}
String query = "{\n" +
" \"aggs\": {\n" +
" \"metrics\": {\n" +
" \"terms\": {\n" +
" \"field\": \"user\",\n" +
" \"size\": 5\n" +
" },\n" +
" \"aggs\": {\n" +
" \"queries\": {\n" +
" \"terms\": {\n" +
" \"field\": \"url\",\n" +
" \"size\": 500\n" +
" },\n" +
" \"aggs\": {\n" +
" \"series\": {\n" +
" \"date_histogram\": {\n" +
" \"field\": \"occur_time\",\n" +
" \"interval\": \"hour\",\n" +
" \"min_doc_count\": 1\n" +
" },\n" +
" \"aggs\": {\n" +
" \"avg\": {\n" +
" \"value_count\": {\n" +
" \"field\": \"url\"\n" +
" }\n" +
" },\n" +
" \"movavg\": {\n" +
" \"moving_avg\": {\n" +
" \"buckets_path\": \"avg\",\n" +
" \"window\": 24,\n" +
" \"model\": \"simple\"\n" +
" }\n" +
" },\n" +
" \"surprise\": {\n" +
" \"bucket_script\": {\n" +
" \"buckets_path\": {\n" +
" \"avg\": \"avg\",\n" +
" \"movavg\": \"movavg\"\n" +
" },\n" +
" \"script\": \"Math.abs(params.avg - params.movavg)\"\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"largest_surprise\": {\n" +
" \"max_bucket\": {\n" +
" \"buckets_path\": \"series.surprise\"\n" +
" }\n" +
" }\n" +
" }\n" +
/* " },\n" +
" \"ninetieth_surprise\": {\n" +
" \"percentiles_bucket\": {\n" +
" \"buckets_path\": \"queries>largest_surprise\",\n" +
" \"percents\": [\n" +
" 90.0\n" +
" ]\n" +
" }\n" +*/
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
/* String query = "{\n" +
" \"size\": 0,\n" +
" \"aggs\": {\n" +
" \"sales_per_month\": {\n" +
" \"date_histogram\": {\n" +
" \"field\": \"occur_time\",\n" +
" \"interval\": \"month\"\n" +
" },\n" +
" \"aggs\": {\n" +
" \"total_sales\": {\n" +
" \"value_count\": {\n" +
" \"field\": \"act\"\n" +
" }\n" +
" },\n" +
" \"t-shirt-percentage\": {\n" +
" \"bucket_script\": {\n" +
" \"buckets_path\": {\n" +
" \"tShirtSales\": \"total_sales\",\n" +
" \"totalSales\": \"total_sales\"\n" +
" },\n" +
" \"script\": \"params.tShirtSales / params.totalSales * 100\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";*/
Search search = new Search.Builder(query)
.addIndex(arrayList)
.addType("event")
.build();
SearchResult result = client.execute(search);
assertTrue(result.getErrorMessage(), result.isSucceeded());
TermsAggregation terms_name = result.getAggregations().getTermsAggregation("terms_name");
List<TermsAggregation.Entry> nameBuckets = terms_name.getBuckets();
}
@Test
public void testGetValueCountAggregation() throws IOException {
String query = "{\n" +
" \"aggs\": {\n" +
" \"terms_name\": {\n" +
" \"terms\": {\n" +
" \"field\": \"user\",\n" +
" \"size\": 1000 ,\n" +
" \"collect_mode\" : \"breadth_first\"" +
" },\n" +
" \"aggs\": {\n" +
" \"terms_attr\": {\n" +
" \"terms\": {\n" +
" \"field\": \"url\",\n" +
" \"size\": 1000 ,\n" +
" \"collect_mode\" : \"breadth_first\"" +
" },\n" +
" \"aggs\": {\n" +
" \"hour_sum\": {\n" +
" \"date_histogram\": {\n" +
" \"field\": \"occur_time\",\n" +
" \"interval\": \"hour\",\n" +
" \"format\": \"yyyy-MM-dd HH\",\n" +
" \"min_doc_count\": 1\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
Search search = new Search.Builder(query)
.addIndex("event_20180323")
.addType("event")
.build();
SearchResult result = client.execute(search);
assertTrue(result.getErrorMessage(), result.isSucceeded());
TermsAggregation terms_name = result.getAggregations().getTermsAggregation("terms_name");
List<TermsAggregation.Entry> nameBuckets = terms_name.getBuckets();
for (int i = 0; i < nameBuckets.size(); i++) {
TermsAggregation.Entry entry = nameBuckets.get(i);
TermsAggregation termsAttr = entry.getTermsAggregation("terms_attr");
List<TermsAggregation.Entry> attrBuckets = termsAttr.getBuckets();
attrBuckets.forEach(t -> {
TermsAggregation hourSum = t.getTermsAggregation("hour_sum");
List<TermsAggregation.Entry> hourSumBuckets = hourSum.getBuckets();
hourSumBuckets.forEach(h -> {
// System.out.println(entry.getKey() + ":" + t.getKey() + ":" + h.getKey() + ":" + h.getCount());
System.out.println(new Date(Long.parseLong(h.getKey())) + ":" + h.getCount());
});
});
}
}
@Test
public void testGetSignificantTermsAggregation()
throws Exception {
String TYPE = typeName;
String INDEX = indexName;
/*"range" : {
"occur_time" : {
"from" : "0",
"to" : "11111111111111",*/
String query = "{\n" +
" \"query\": {\n" +
" \"range\": {\n" +
" \"occur_time\": {\n" +
" \"from\": \"0\",\n" +
" \"to\": \"11111111111111\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"aggs\" : {\n" +
" \"significant_terms1\" : {\n" +
" \"significant_terms\" : {\n" +
" \"field\" : \"url\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
Search search = new Search.Builder(query)
.addIndex(INDEX)
.addType(TYPE)
.build();
SearchResult result = client.execute(search);
assertTrue(result.getErrorMessage(), result.isSucceeded());
SignificantTermsAggregation significantTerms = result.getAggregations().getSignificantTermsAggregation("significant_terms1");
Long totalCount = significantTerms.getTotalCount();
System.out.println(totalCount);
List<SignificantTermsAggregation.SignificantTerm> buckets = significantTerms.getBuckets();
for (int i = 0; i < buckets.size(); i++) {
SignificantTermsAggregation.SignificantTerm significantTerm = buckets.get(i);
System.out.println(significantTerm.getBackgroundCount());
System.out.println(significantTerm.getKey());
System.out.println(significantTerm.getScore());
System.out.println(significantTerm.getCount());
System.out.println(significantTerm.getName());
}
}
@Test
public void testGetDateHistogramAggregation()
throws IOException {
String TYPE = typeName;
String INDEX = indexName;
String query = "{\n" +
" \"query\" : {\n" +
" \"match_all\" : {}\n" +
" },\n" +
" \"aggs\" : {\n" +
" \"histo1\" : {\n" +
" \"date_histogram\" : {\n" +
" \"field\" : \"occur_time\",\n" +
" \"interval\" : \"day\",\n" +
" \"min_doc_count\": 1\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
Search search = new Search.Builder(query)
.addIndex(INDEX)
.addType(TYPE)
.build();
SearchResult result = client.execute(search);
assertTrue(result.getErrorMessage(), result.isSucceeded());
DateHistogramAggregation dateHistogram = result.getAggregations().getDateHistogramAggregation("histo1");
assertEquals("histo1", dateHistogram.getName());
assertEquals(3, dateHistogram.getBuckets().size());
assertTrue(1L == dateHistogram.getBuckets().get(0).getCount());
assertTrue(1359676800000L == dateHistogram.getBuckets().get(0).getTime());
assertTrue(1L == dateHistogram.getBuckets().get(1).getCount());
assertTrue(1359849600000L == dateHistogram.getBuckets().get(1).getTime());
assertTrue(2L == dateHistogram.getBuckets().get(2).getCount());
assertTrue(1359936000000L == dateHistogram.getBuckets().get(2).getTime());
Aggregation aggregation = result.getAggregations().getAggregation("histo1", DateHistogramAggregation.class);
assertTrue(aggregation instanceof DateHistogramAggregation);
DateHistogramAggregation dateHistogramByType = (DateHistogramAggregation) aggregation;
assertEquals(dateHistogram, dateHistogramByType);
Map<String, Class> nameToTypeMap = new HashMap<String, Class>();
nameToTypeMap.put("histo1", DateHistogramAggregation.class);
List<Aggregation> aggregations = result.getAggregations().getAggregations(nameToTypeMap);
assertEquals(1, aggregations.size());
assertTrue(aggregations.get(0) instanceof DateHistogramAggregation);
DateHistogramAggregation dateHistogramWithMap = (DateHistogramAggregation) aggregations.get(0);
assertEquals(dateHistogram, dateHistogramWithMap);
}
@Test
public void testGetExtendedStatsAggregation()
throws IOException {
String TYPE = typeName;
String INDEX = indexName;
String query = "{\n" +
" \"query\" : {\n" +
" \"match_all\" : {}\n" +
" },\n" +
" \"aggs\" : {\n" +
" \"extended_stats1\" : {\n" +
" \"extended_stats\" : {\n" +
" \"field\" : \"url\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
Search search = new Search.Builder(query)
.addIndex(INDEX)
.addType(TYPE)
.build();
SearchResult result = client.execute(search);
assertTrue(result.getErrorMessage(), result.isSucceeded());
ExtendedStatsAggregation extendedStats = result.getAggregations().getExtendedStatsAggregation("extended_stats1");
assertEquals("extended_stats1", extendedStats.getName());
assertEquals(new Double(2.5), extendedStats.getAvg());
assertTrue(2L == extendedStats.getCount());
assertEquals(new Double(3), extendedStats.getMax());
assertEquals(new Double(2), extendedStats.getMin());
assertEquals(new Double(5), extendedStats.getSum());
assertEquals(new Double(.5), extendedStats.getStdDeviation());
assertEquals(new Double(13), extendedStats.getSumOfSquares());
assertEquals(new Double(.25), extendedStats.getVariance());
Aggregation aggregation = result.getAggregations().getAggregation("extended_stats1", ExtendedStatsAggregation.class);
assertTrue(aggregation instanceof ExtendedStatsAggregation);
ExtendedStatsAggregation extendedStatsByType = (ExtendedStatsAggregation) aggregation;
assertEquals(extendedStats, extendedStatsByType);
Map<String, Class> nameToTypeMap = new HashMap<String, Class>();
nameToTypeMap.put("extended_stats1", ExtendedStatsAggregation.class);
List<Aggregation> aggregations = result.getAggregations().getAggregations(nameToTypeMap);
assertEquals(1, aggregations.size());
assertTrue(aggregations.get(0) instanceof ExtendedStatsAggregation);
ExtendedStatsAggregation extendedStatsWithMap = (ExtendedStatsAggregation) aggregations.get(0);
assertEquals(extendedStats, extendedStatsWithMap);
}
private void index(String index, String type, Object o, String source) throws IOException {
Index i = new Index.Builder(source).index(index).type(type).build();
DocumentResult execute = client.execute(i);
System.out.println(execute.isSucceeded());
}
}
class User {
@JestId
private Integer id;
private String name;
private Date birth;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public User() {
super();
// TODO Auto-generated constructor stub }
public User(Integer id, String name, Date birth) {
super();
this.id = id;
this.name = name;
this.birth = birth;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", birth=" + birth + "]";
}
}