use of org.elasticsearch.search.aggregations.bucket.terms.StringTerms in project elasticsearch by elastic.
the class SignificantTermsSignificanceScoreIT method testScoresEqualForPositiveAndNegative.
public void testScoresEqualForPositiveAndNegative(SignificanceHeuristic heuristic) throws Exception {
//check that results for both classes are the same with exclude negatives = false and classes are routing ids
SearchResponse response = client().prepareSearch("test").addAggregation(terms("class").field("class").subAggregation(significantTerms("mySignificantTerms").field("text").executionHint(randomExecutionHint()).significanceHeuristic(heuristic).minDocCount(1).shardSize(1000).size(1000))).execute().actionGet();
assertSearchResponse(response);
StringTerms classes = response.getAggregations().get("class");
assertThat(classes.getBuckets().size(), equalTo(2));
Iterator<Terms.Bucket> classBuckets = classes.getBuckets().iterator();
Aggregations aggregations = classBuckets.next().getAggregations();
SignificantTerms sigTerms = aggregations.get("mySignificantTerms");
Collection<SignificantTerms.Bucket> classA = sigTerms.getBuckets();
Iterator<SignificantTerms.Bucket> classBBucketIterator = sigTerms.getBuckets().iterator();
assertThat(classA.size(), greaterThan(0));
for (SignificantTerms.Bucket classABucket : classA) {
SignificantTerms.Bucket classBBucket = classBBucketIterator.next();
assertThat(classABucket.getKey(), equalTo(classBBucket.getKey()));
assertThat(classABucket.getSignificanceScore(), closeTo(classBBucket.getSignificanceScore(), 1.e-5));
}
}
use of org.elasticsearch.search.aggregations.bucket.terms.StringTerms in project tutorials by eugenp.
the class ElasticSearchQueryIntegrationTest method givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually.
@Test
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() {
final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags").order(Terms.Order.aggregation("_count", false));
final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet();
final Map<String, Aggregation> results = response.getAggregations().asMap();
final StringTerms topTags = (StringTerms) results.get("top_tags");
final List<String> keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString).collect(toList());
assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys);
}
use of org.elasticsearch.search.aggregations.bucket.terms.StringTerms in project elasticsearch by elastic.
the class NestedIT method testNestNestedAggs.
public void testNestNestedAggs() throws Exception {
SearchResponse response = client().prepareSearch("idx_nested_nested_aggs").addAggregation(nested("level1", "nested1").subAggregation(terms("a").field("nested1.a.keyword").collectMode(aggCollectionMode).subAggregation(nested("level2", "nested1.nested2").subAggregation(sum("sum").field("nested1.nested2.b"))))).get();
assertSearchResponse(response);
Nested level1 = response.getAggregations().get("level1");
assertThat(level1, notNullValue());
assertThat(level1.getName(), equalTo("level1"));
assertThat(level1.getDocCount(), equalTo(2L));
StringTerms a = level1.getAggregations().get("a");
Terms.Bucket bBucket = a.getBucketByKey("a");
assertThat(bBucket.getDocCount(), equalTo(1L));
Nested level2 = bBucket.getAggregations().get("level2");
assertThat(level2.getDocCount(), equalTo(1L));
Sum sum = level2.getAggregations().get("sum");
assertThat(sum.getValue(), equalTo(2d));
a = level1.getAggregations().get("a");
bBucket = a.getBucketByKey("b");
assertThat(bBucket.getDocCount(), equalTo(1L));
level2 = bBucket.getAggregations().get("level2");
assertThat(level2.getDocCount(), equalTo(1L));
sum = level2.getAggregations().get("sum");
assertThat(sum.getValue(), equalTo(2d));
}
use of org.elasticsearch.search.aggregations.bucket.terms.StringTerms in project elasticsearch by elastic.
the class SignificantTermsSignificanceScoreIT method testXContentResponse.
public void testXContentResponse() throws Exception {
String type = randomBoolean() ? "text" : "long";
String settings = "{\"index.number_of_shards\": 1, \"index.number_of_replicas\": 0}";
SharedSignificantTermsTestMethods.index01Docs(type, settings, this);
SearchResponse response = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE).addAggregation(terms("class").field(CLASS_FIELD).subAggregation(significantTerms("sig_terms").field(TEXT_FIELD))).execute().actionGet();
assertSearchResponse(response);
StringTerms classes = response.getAggregations().get("class");
assertThat(classes.getBuckets().size(), equalTo(2));
for (Terms.Bucket classBucket : classes.getBuckets()) {
Map<String, Aggregation> aggs = classBucket.getAggregations().asMap();
assertTrue(aggs.containsKey("sig_terms"));
SignificantTerms agg = (SignificantTerms) aggs.get("sig_terms");
assertThat(agg.getBuckets().size(), equalTo(1));
String term = agg.iterator().next().getKeyAsString();
String classTerm = classBucket.getKeyAsString();
assertTrue(term.equals(classTerm));
}
XContentBuilder responseBuilder = XContentFactory.jsonBuilder();
responseBuilder.startObject();
classes.toXContent(responseBuilder, ToXContent.EMPTY_PARAMS);
responseBuilder.endObject();
String result = "{\"class\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0," + "\"buckets\":[" + "{" + "\"key\":\"0\"," + "\"doc_count\":4," + "\"sig_terms\":{" + "\"doc_count\":4," + "\"buckets\":[" + "{" + "\"key\":" + (type.equals("long") ? "0," : "\"0\",") + "\"doc_count\":4," + "\"score\":0.39999999999999997," + "\"bg_count\":5" + "}" + "]" + "}" + "}," + "{" + "\"key\":\"1\"," + "\"doc_count\":3," + "\"sig_terms\":{" + "\"doc_count\":3," + "\"buckets\":[" + "{" + "\"key\":" + (type.equals("long") ? "1," : "\"1\",") + "\"doc_count\":3," + "\"score\":0.75," + "\"bg_count\":4" + "}]}}]}}";
assertThat(responseBuilder.string(), equalTo(result));
}
use of org.elasticsearch.search.aggregations.bucket.terms.StringTerms in project crate by crate.
the class FulltextIntegrationTest method testCopyValuesFromStringArrayToIndex.
@Test
public void testCopyValuesFromStringArrayToIndex() throws Exception {
execute("CREATE TABLE t_string_array (" + " id INTEGER PRIMARY KEY," + " keywords ARRAY(STRING) INDEX USING FULLTEXT," + " INDEX keywords_ft USING FULLTEXT(keywords)" + ")");
execute("INSERT INTO t_string_array (id, keywords) VALUES (1, ['foo', 'bar', 'foo bar'])");
ensureYellow();
refresh();
execute("SELECT id, keywords FROM t_string_array WHERE match(keywords_ft, 'foo')");
assertThat(response.rows().length, is(1));
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("t_string_array");
// we need to use ES search request to aggregate on terms
String q = "{\n" + " \"from\": 0,\n" + " \"query\": {\n" + " \"match_all\": {}\n" + " },\n" + " \"aggregations\": {\n" + " \"kw\": {\n" + " \"terms\": {\"field\": \"keywords_ft\"}\n" + " }\n" + " },\n" + " \"size\": 0\n" + "}";
BytesReference bytesRef = new BytesArray(q);
searchRequest.source(bytesRef);
SearchResponse searchResponse = client().search(searchRequest).get();
StringTerms terms = searchResponse.getAggregations().get("kw");
List<String> termValues = new ArrayList<>(terms.getBuckets().size());
terms.getBuckets().forEach(b -> termValues.add(b.getKeyAsString()));
assertThat(termValues, containsInAnyOrder("foo", "bar"));
}
Aggregations