本文整理汇总了Java中org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket方法的典型用法代码示例。如果您正苦于以下问题:Java Terms.Bucket方法的具体用法?Java Terms.Bucket怎么用?Java Terms.Bucket使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.elasticsearch.search.aggregations.bucket.terms.Terms
的用法示例。
在下文中一共展示了Terms.Bucket方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkByRateInSequential
import org.elasticsearch.search.aggregations.bucket.terms.Terms; //导入方法依赖的package包/类
/**
* Check crawler by request sending rate, which is read from configruation
* file
*
* @throws InterruptedException InterruptedException
* @throws IOException IOException
*/
public void checkByRateInSequential() throws InterruptedException, IOException {
es.createBulkProcessor();
int rate = Integer.parseInt(props.getProperty("sendingrate"));
Terms users = this.getUserTerms(this.httpType);
LOG.info("Original User count: {}", Integer.toString(users.getBuckets().size()));
int userCount = 0;
for (Terms.Bucket entry : users.getBuckets()) {
String user = entry.getKey().toString();
int count = checkByRate(es, user);
userCount += count;
}
es.destroyBulkProcessor();
LOG.info("User count: {}", Integer.toString(userCount));
}
示例2: parseAggregationResult
import org.elasticsearch.search.aggregations.bucket.terms.Terms; //导入方法依赖的package包/类
private void parseAggregationResult (SearchResponse response, String[] aggreFields, JSONObject resObj) {
List<Map<String, Object>> aggrs = new ArrayList<Map<String, Object>>();
for (String aggreField : aggreFields) {
Terms terms = response.getAggregations().get(aggreField);
if (terms != null) {
Map<String, Object> maps = new HashMap<String, Object>();
for (Terms.Bucket bucket : terms.getBuckets()) {
maps.put((String)bucket.getKey(), bucket.getDocCount());
}
JSONObject jo = new JSONObject();
jo.put(aggreField, maps);
aggrs.add(jo);
}
}
resObj.put("aggregations", aggrs);
}
示例3: relatedInfo
import org.elasticsearch.search.aggregations.bucket.terms.Terms; //导入方法依赖的package包/类
/**
* 获取query的关联信息
*
* @param query 查询queryString
* @param size 结果集数量
* @return 相关信息
*/
public Pair<Map<String, List<Terms.Bucket>>, List<Webpage>> relatedInfo(String query, int size) {
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME)
.setTypes(TYPE_NAME)
.setQuery(QueryBuilders.queryStringQuery(query))
.addSort("gatherTime", SortOrder.DESC)
.addAggregation(AggregationBuilders.terms("relatedPeople").field("namedEntity.nr"))
.addAggregation(AggregationBuilders.terms("relatedLocation").field("namedEntity.ns"))
.addAggregation(AggregationBuilders.terms("relatedInstitution").field("namedEntity.nt"))
.addAggregation(AggregationBuilders.terms("relatedKeywords").field("keywords"))
.setSize(size);
SearchResponse response = searchRequestBuilder.execute().actionGet();
Map<String, List<Terms.Bucket>> info = Maps.newHashMap();
info.put("relatedPeople", ((Terms) response.getAggregations().get("relatedPeople")).getBuckets());
info.put("relatedLocation", ((Terms) response.getAggregations().get("relatedLocation")).getBuckets());
info.put("relatedInstitution", ((Terms) response.getAggregations().get("relatedInstitution")).getBuckets());
info.put("relatedKeywords", ((Terms) response.getAggregations().get("relatedKeywords")).getBuckets());
return Pair.of(info, warpHits2List(response.getHits()));
}
示例4: testScriptScore
import org.elasticsearch.search.aggregations.bucket.terms.Terms; //导入方法依赖的package包/类
public void testScriptScore() throws ExecutionException, InterruptedException, IOException {
indexRandomFrequencies01(randomBoolean() ? "text" : "long");
ScriptHeuristic scriptHeuristic = getScriptSignificanceHeuristic();
SearchResponse response = client().prepareSearch(INDEX_NAME)
.addAggregation(terms("class").field(CLASS_FIELD)
.subAggregation(significantTerms("mySignificantTerms")
.field(TEXT_FIELD)
.executionHint(randomExecutionHint())
.significanceHeuristic(scriptHeuristic)
.minDocCount(1).shardSize(2).size(2)))
.execute()
.actionGet();
assertSearchResponse(response);
for (Terms.Bucket classBucket : ((Terms) response.getAggregations().get("class")).getBuckets()) {
SignificantTerms sigTerms = classBucket.getAggregations().get("mySignificantTerms");
for (SignificantTerms.Bucket bucket : sigTerms.getBuckets()) {
assertThat(bucket.getSignificanceScore(),
is((double) bucket.getSubsetDf() + bucket.getSubsetSize() + bucket.getSupersetDf() + bucket.getSupersetSize()));
}
}
}
示例5: reverseToRootGroupByOnNestedFieldWithFilterTestWithReverseNestedNoPath
import org.elasticsearch.search.aggregations.bucket.terms.Terms; //导入方法依赖的package包/类
@Test
public void reverseToRootGroupByOnNestedFieldWithFilterTestWithReverseNestedNoPath() throws Exception {
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a'),reverse_nested(someField)", TEST_INDEX));
InternalNested nested = result.get("[email protected]");
InternalFilter filter = nested.getAggregations().get("[email protected]");
Terms infos = filter.getAggregations().get("message.info");
Assert.assertEquals(1,infos.getBuckets().size());
for(Terms.Bucket bucket : infos.getBuckets()) {
InternalReverseNested reverseNested = bucket.getAggregations().get("[email protected]");
Terms terms = reverseNested.getAggregations().get("someField");
Terms.Bucket internalBucket = terms.getBuckets().get(0);
long count = ((ValueCount) internalBucket.getAggregations().get("COUNT(*)")).getValue();
String key = internalBucket.getKey().toString();
if(key.equalsIgnoreCase("b")) {
Assert.assertEquals(2, count);
}
else {
throw new Exception(String.format("Unexpected key. expected: only a . found: %s", key));
}
}
}
示例6: assertNoDocCountError
import org.elasticsearch.search.aggregations.bucket.terms.Terms; //导入方法依赖的package包/类
private void assertNoDocCountError(int size, SearchResponse accurateResponse, SearchResponse testResponse) {
Terms accurateTerms = accurateResponse.getAggregations().get("terms");
assertThat(accurateTerms, notNullValue());
assertThat(accurateTerms.getName(), equalTo("terms"));
assertThat(accurateTerms.getDocCountError(), equalTo(0L));
Terms testTerms = testResponse.getAggregations().get("terms");
assertThat(testTerms, notNullValue());
assertThat(testTerms.getName(), equalTo("terms"));
assertThat(testTerms.getDocCountError(), equalTo(0L));
Collection<Bucket> testBuckets = testTerms.getBuckets();
assertThat(testBuckets.size(), lessThanOrEqualTo(size));
assertThat(accurateTerms.getBuckets().size(), greaterThanOrEqualTo(testBuckets.size()));
for (Terms.Bucket testBucket : testBuckets) {
assertThat(testBucket, notNullValue());
Terms.Bucket accurateBucket = accurateTerms.getBucketByKey(testBucket.getKeyAsString());
assertThat(accurateBucket, notNullValue());
assertThat(accurateBucket.getDocCountError(), equalTo(0L));
assertThat(testBucket.getDocCountError(), equalTo(0L));
}
}
示例7: groupByTest
import org.elasticsearch.search.aggregations.bucket.terms.Terms; //导入方法依赖的package包/类
@Test
public void groupByTest() throws Exception {
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender", TEST_INDEX));
Terms gender = result.get("gender");
for(Terms.Bucket bucket : gender.getBuckets()) {
String key = bucket.getKey().toString();
long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
if(key.equalsIgnoreCase("m")) {
Assert.assertEquals(507, count);
}
else if(key.equalsIgnoreCase("f")) {
Assert.assertEquals(493, count);
}
else {
throw new Exception(String.format("Unexpected key. expected: m OR f. found: %s", key));
}
}
}
示例8: groupByOnNestedFieldWithFilterTest
import org.elasticsearch.search.aggregations.bucket.terms.Terms; //导入方法依赖的package包/类
@Test
public void groupByOnNestedFieldWithFilterTest() throws Exception {
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a')", TEST_INDEX));
InternalNested nested = result.get("[email protected]");
InternalFilter filter = nested.getAggregations().get("[email protected]");
Terms infos = filter.getAggregations().get("message.info");
Assert.assertEquals(1,infos.getBuckets().size());
for(Terms.Bucket bucket : infos.getBuckets()) {
String key = bucket.getKey().toString();
long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
if(key.equalsIgnoreCase("a")) {
Assert.assertEquals(2, count);
}
else {
throw new Exception(String.format("Unexpected key. expected: only a . found: %s", key));
}
}
}
示例9: testNestedDiversity
import org.elasticsearch.search.aggregations.bucket.terms.Terms; //导入方法依赖的package包/类
public void testNestedDiversity() throws Exception {
// Test multiple samples gathered under buckets made by a parent agg
int MAX_DOCS_PER_AUTHOR = 1;
TermsAggregationBuilder rootTerms = terms("genres").field("genre");
DiversifiedAggregationBuilder sampleAgg = new DiversifiedAggregationBuilder("sample").shardSize(100);
sampleAgg.field("author").maxDocsPerValue(MAX_DOCS_PER_AUTHOR).executionHint(randomExecutionHint());
sampleAgg.subAggregation(terms("authors").field("author"));
rootTerms.subAggregation(sampleAgg);
SearchResponse response = client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH)
.addAggregation(rootTerms).execute().actionGet();
assertSearchResponse(response);
Terms genres = response.getAggregations().get("genres");
Collection<Bucket> genreBuckets = genres.getBuckets();
for (Terms.Bucket genreBucket : genreBuckets) {
Sampler sample = genreBucket.getAggregations().get("sample");
Terms authors = sample.getAggregations().get("authors");
Collection<Bucket> testBuckets = authors.getBuckets();
for (Terms.Bucket testBucket : testBuckets) {
assertThat(testBucket.getDocCount(), lessThanOrEqualTo((long) NUM_SHARDS * MAX_DOCS_PER_AUTHOR));
}
}
}
示例10: groupByOnNestedFieldTest
import org.elasticsearch.search.aggregations.bucket.terms.Terms; //导入方法依赖的package包/类
@Test
public void groupByOnNestedFieldTest() throws Exception {
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info)", TEST_INDEX));
InternalNested nested = result.get("[email protected]");
Terms infos = nested.getAggregations().get("message.info");
Assert.assertEquals(3,infos.getBuckets().size());
for(Terms.Bucket bucket : infos.getBuckets()) {
String key = bucket.getKey().toString();
long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
if(key.equalsIgnoreCase("a")) {
Assert.assertEquals(2, count);
}
else if(key.equalsIgnoreCase("c")) {
Assert.assertEquals(2, count);
}
else if(key.equalsIgnoreCase("b")) {
Assert.assertEquals(1, count);
}
else {
throw new Exception(String.format("Unexpected key. expected: a OR b OR c . found: %s", key));
}
}
}
示例11: assertEquals
import org.elasticsearch.search.aggregations.bucket.terms.Terms; //导入方法依赖的package包/类
private void assertEquals(Terms t1, Terms t2) {
List<Terms.Bucket> t1Buckets = t1.getBuckets();
List<Terms.Bucket> t2Buckets = t1.getBuckets();
assertEquals(t1Buckets.size(), t2Buckets.size());
for (Iterator<Terms.Bucket> it1 = t1Buckets.iterator(), it2 = t2Buckets.iterator(); it1.hasNext(); ) {
final Terms.Bucket b1 = it1.next();
final Terms.Bucket b2 = it2.next();
assertEquals(b1.getDocCount(), b2.getDocCount());
assertEquals(b1.getKey(), b2.getKey());
}
}
示例12: assertNoDocCountErrorSingleResponse
import org.elasticsearch.search.aggregations.bucket.terms.Terms; //导入方法依赖的package包/类
private void assertNoDocCountErrorSingleResponse(int size, SearchResponse testResponse) {
Terms testTerms = testResponse.getAggregations().get("terms");
assertThat(testTerms, notNullValue());
assertThat(testTerms.getName(), equalTo("terms"));
assertThat(testTerms.getDocCountError(), equalTo(0L));
Collection<Bucket> testBuckets = testTerms.getBuckets();
assertThat(testBuckets.size(), lessThanOrEqualTo(size));
for (Terms.Bucket testBucket : testBuckets) {
assertThat(testBucket, notNullValue());
assertThat(testBucket.getDocCountError(), equalTo(0L));
}
}
示例13: standardTriples
import org.elasticsearch.search.aggregations.bucket.terms.Terms; //导入方法依赖的package包/类
public static void standardTriples(ESDriver es, String index, String type) throws IOException {
es.createBulkProcessor();
SearchResponse sr = es.getClient().prepareSearch(index).setTypes(type).setQuery(QueryBuilders.matchAllQuery()).setSize(0)
.addAggregation(AggregationBuilders.terms("concepts").field("concept_A").size(0)).execute().actionGet();
Terms concepts = sr.getAggregations().get("concepts");
for (Terms.Bucket entry : concepts.getBuckets()) {
String concept = (String) entry.getKey();
double maxSim = LinkageTriple.getMaxSimilarity(es, index, type, concept);
if (maxSim == 1.0) {
continue;
}
SearchResponse scrollResp = es.getClient().prepareSearch(index).setTypes(type).setScroll(new TimeValue(60000)).setQuery(QueryBuilders.termQuery("concept_A", concept))
.addSort("weight", SortOrder.DESC).setSize(100).execute().actionGet();
while (true) {
for (SearchHit hit : scrollResp.getHits().getHits()) {
Map<String, Object> metadata = hit.getSource();
double sim = (double) metadata.get("weight");
double newSim = sim / maxSim;
UpdateRequest ur = es.generateUpdateRequest(index, type, hit.getId(), "weight", Double.parseDouble(df.format(newSim)));
es.getBulkProcessor().add(ur);
}
scrollResp = es.getClient().prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
if (scrollResp.getHits().getHits().length == 0) {
break;
}
}
}
es.destroyBulkProcessor();
}
示例14: testSubAggregations
import org.elasticsearch.search.aggregations.bucket.terms.Terms; //导入方法依赖的package包/类
@Test
public void testSubAggregations() throws Exception {
Set expectedAges = new HashSet<>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers()));
final String query = String.format("SELECT /*! DOCS_WITH_AGGREGATION(10) */" +
" * FROM %s/account GROUP BY (gender, terms('field'='age','size'=200,'alias'='age')), (state) LIMIT 200,200", TEST_INDEX);
Map<String, Set<Integer>> buckets = new HashMap<>();
SqlElasticSearchRequestBuilder select = getSearchRequestBuilder(query);
SearchResponse response = (SearchResponse) select.get();
Aggregations result = response.getAggregations();
Terms gender = result.get("gender");
for(Terms.Bucket genderBucket : gender.getBuckets()) {
String genderKey = genderBucket.getKey().toString();
buckets.put(genderKey, new HashSet<Integer>());
Terms ageBuckets = (Terms) genderBucket.getAggregations().get("age");
for(Terms.Bucket ageBucket : ageBuckets.getBuckets()) {
buckets.get(genderKey).add(Integer.parseInt(ageBucket.getKey().toString()));
}
}
Assert.assertEquals(2, buckets.keySet().size());
Assert.assertEquals(expectedAges, buckets.get("m"));
Assert.assertEquals(expectedAges, buckets.get("f"));
Terms state = result.get("state");
for(Terms.Bucket stateBucket : state.getBuckets()) {
if(stateBucket.getKey().toString().equalsIgnoreCase("ak")) {
Assert.assertTrue("There are 22 entries for state ak", stateBucket.getDocCount() == 22);
}
}
Assert.assertEquals(response.getHits().totalHits(), 1000);
Assert.assertEquals(response.getHits().hits().length, 10);
}
示例15: genSessionByRefererInSequential
import org.elasticsearch.search.aggregations.bucket.terms.Terms; //导入方法依赖的package包/类
/**
* Method to generate session by time threshold and referrer
*
* @param timeThres value of time threshold (s)
* @throws ElasticsearchException ElasticsearchException
* @throws IOException IOException
*/
public void genSessionByRefererInSequential(int timeThres) throws ElasticsearchException, IOException {
Terms users = this.getUserTerms(this.cleanupType);
int sessionCount = 0;
for (Terms.Bucket entry : users.getBuckets()) {
String user = (String) entry.getKey();
Integer sessionNum = genSessionByReferer(es, user, timeThres);
sessionCount += sessionNum;
}
LOG.info("Initial session count: {}", Integer.toString(sessionCount));
}