Search in sources :

Example 1 with BoolQueryBuilder

use of org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.

the class AdjacencyMatrixIT method testWithSubAggregation.

public void testWithSubAggregation() throws Exception {
    BoolQueryBuilder boolQ = new BoolQueryBuilder();
    boolQ.must(termQuery("tag", "tag1"));
    boolQ.must(termQuery("tag", "tag2"));
    SearchResponse response = client().prepareSearch("idx").addAggregation(adjacencyMatrix("tags", newMap("tag1", termQuery("tag", "tag1")).add("tag2", termQuery("tag", "tag2")).add("both", boolQ)).subAggregation(avg("avg_value").field("value"))).execute().actionGet();
    assertSearchResponse(response);
    AdjacencyMatrix matrix = response.getAggregations().get("tags");
    assertThat(matrix, notNullValue());
    assertThat(matrix.getName(), equalTo("tags"));
    int expectedBuckets = 0;
    if (numTag1Docs > 0) {
        expectedBuckets++;
    }
    if (numTag2Docs > 0) {
        expectedBuckets++;
    }
    if (numMultiTagDocs > 0) {
        // both, both&tag1, both&tag2, tag1&tag2
        expectedBuckets += 4;
    }
    assertThat(matrix.getBuckets().size(), equalTo(expectedBuckets));
    assertThat(matrix.getProperty("_bucket_count"), equalTo(expectedBuckets));
    Object[] propertiesKeys = (Object[]) matrix.getProperty("_key");
    Object[] propertiesDocCounts = (Object[]) matrix.getProperty("_count");
    Object[] propertiesCounts = (Object[]) matrix.getProperty("avg_value.value");
    assertEquals(expectedBuckets, propertiesKeys.length);
    assertEquals(propertiesKeys.length, propertiesDocCounts.length);
    assertEquals(propertiesKeys.length, propertiesCounts.length);
    for (int i = 0; i < propertiesCounts.length; i++) {
        AdjacencyMatrix.Bucket bucket = matrix.getBucketByKey(propertiesKeys[i].toString());
        assertThat(bucket, Matchers.notNullValue());
        Avg avgValue = bucket.getAggregations().get("avg_value");
        assertThat(avgValue, notNullValue());
        assertThat((long) propertiesDocCounts[i], equalTo(bucket.getDocCount()));
        assertThat((double) propertiesCounts[i], equalTo(avgValue.getValue()));
    }
    AdjacencyMatrix.Bucket tag1Bucket = matrix.getBucketByKey("tag1");
    assertThat(tag1Bucket, Matchers.notNullValue());
    assertThat(tag1Bucket.getDocCount(), equalTo((long) numTag1Docs));
    long sum = 0;
    for (int i = 0; i < numSingleTag1Docs; i++) {
        sum += i + 1;
    }
    for (int i = numSingleTag1Docs + numSingleTag2Docs; i < numDocs; i++) {
        sum += i + 1;
    }
    assertThat(tag1Bucket.getAggregations().asList().isEmpty(), is(false));
    Avg avgBucket1Value = tag1Bucket.getAggregations().get("avg_value");
    assertThat(avgBucket1Value, notNullValue());
    assertThat(avgBucket1Value.getName(), equalTo("avg_value"));
    assertThat(avgBucket1Value.getValue(), equalTo((double) sum / numTag1Docs));
    Bucket tag2Bucket = matrix.getBucketByKey("tag2");
    assertThat(tag2Bucket, Matchers.notNullValue());
    assertThat(tag2Bucket.getDocCount(), equalTo((long) numTag2Docs));
    sum = 0;
    for (int i = numSingleTag1Docs; i < numDocs; i++) {
        sum += i + 1;
    }
    assertThat(tag2Bucket.getAggregations().asList().isEmpty(), is(false));
    Avg avgBucket2Value = tag2Bucket.getAggregations().get("avg_value");
    assertThat(avgBucket2Value, notNullValue());
    assertThat(avgBucket2Value.getName(), equalTo("avg_value"));
    assertThat(avgBucket2Value.getValue(), equalTo((double) sum / numTag2Docs));
    // Check intersection buckets are computed correctly by comparing with
    // ANDed query bucket results
    Bucket bucketBothQ = matrix.getBucketByKey("both");
    if (numMultiTagDocs == 0) {
        // Empty intersections are not returned.
        assertThat(bucketBothQ, Matchers.nullValue());
        Bucket bucketIntersectQ = matrix.getBucketByKey("tag1&tag2");
        assertThat(bucketIntersectQ, Matchers.nullValue());
        Bucket tag1Both = matrix.getBucketByKey("both&tag1");
        assertThat(tag1Both, Matchers.nullValue());
    } else {
        assertThat(bucketBothQ, Matchers.notNullValue());
        assertThat(bucketBothQ.getDocCount(), equalTo((long) numMultiTagDocs));
        Avg avgValueBothQ = bucketBothQ.getAggregations().get("avg_value");
        Bucket bucketIntersectQ = matrix.getBucketByKey("tag1&tag2");
        assertThat(bucketIntersectQ, Matchers.notNullValue());
        assertThat(bucketIntersectQ.getDocCount(), equalTo((long) numMultiTagDocs));
        Avg avgValueIntersectQ = bucketBothQ.getAggregations().get("avg_value");
        assertThat(avgValueIntersectQ.getValue(), equalTo(avgValueBothQ.getValue()));
        Bucket tag1Both = matrix.getBucketByKey("both&tag1");
        assertThat(tag1Both, Matchers.notNullValue());
        assertThat(tag1Both.getDocCount(), equalTo((long) numMultiTagDocs));
        Avg avgValueTag1BothIntersectQ = tag1Both.getAggregations().get("avg_value");
        assertThat(avgValueTag1BothIntersectQ.getValue(), equalTo(avgValueBothQ.getValue()));
    }
}
Also used : Avg(org.elasticsearch.search.aggregations.metrics.avg.Avg) Bucket(org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix.Bucket) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Bucket(org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix.Bucket) AdjacencyMatrix(org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with BoolQueryBuilder

use of org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.

the class AdjacencyMatrixIT method testEmptyFilterDeclarations.

// See NullPointer issue when filters are empty:
// https://github.com/elastic/elasticsearch/issues/8438
public void testEmptyFilterDeclarations() throws Exception {
    QueryBuilder emptyFilter = new BoolQueryBuilder();
    SearchResponse response = client().prepareSearch("idx").addAggregation(adjacencyMatrix("tags", newMap("all", emptyFilter).add("tag1", termQuery("tag", "tag1")))).execute().actionGet();
    assertSearchResponse(response);
    AdjacencyMatrix filters = response.getAggregations().get("tags");
    assertThat(filters, notNullValue());
    AdjacencyMatrix.Bucket allBucket = filters.getBucketByKey("all");
    assertThat(allBucket.getDocCount(), equalTo((long) numDocs));
    AdjacencyMatrix.Bucket bucket = filters.getBucketByKey("tag1");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix.Bucket) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) AdjacencyMatrix(org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 3 with BoolQueryBuilder

use of org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.

the class FiltersIT method testEmptyFilterDeclarations.

// See NullPointer issue when filters are empty:
// https://github.com/elastic/elasticsearch/issues/8438
public void testEmptyFilterDeclarations() throws Exception {
    QueryBuilder emptyFilter = new BoolQueryBuilder();
    SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("all", emptyFilter), new KeyedFilter("tag1", termQuery("tag", "tag1"))))).execute().actionGet();
    assertSearchResponse(response);
    Filters filters = response.getAggregations().get("tags");
    assertThat(filters, notNullValue());
    Filters.Bucket allBucket = filters.getBucketByKey("all");
    assertThat(allBucket.getDocCount(), equalTo((long) numDocs));
    Filters.Bucket bucket = filters.getBucketByKey("tag1");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
}
Also used : KeyedFilter(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter) Filters(org.elasticsearch.search.aggregations.bucket.filters.Filters) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 4 with BoolQueryBuilder

use of org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.

the class InnerHitsIT method testRandomParentChild.

public void testRandomParentChild() throws Exception {
    assertAcked(prepareCreate("idx").addMapping("parent").addMapping("child1", "_parent", "type=parent").addMapping("child2", "_parent", "type=parent"));
    int numDocs = scaledRandomIntBetween(5, 50);
    List<IndexRequestBuilder> requestBuilders = new ArrayList<>();
    int child1 = 0;
    int child2 = 0;
    int[] child1InnerObjects = new int[numDocs];
    int[] child2InnerObjects = new int[numDocs];
    for (int parent = 0; parent < numDocs; parent++) {
        String parentId = String.format(Locale.ENGLISH, "%03d", parent);
        requestBuilders.add(client().prepareIndex("idx", "parent", parentId).setSource("{}", XContentType.JSON));
        int numChildDocs = child1InnerObjects[parent] = scaledRandomIntBetween(1, numDocs);
        int limit = child1 + numChildDocs;
        for (; child1 < limit; child1++) {
            requestBuilders.add(client().prepareIndex("idx", "child1", String.format(Locale.ENGLISH, "%04d", child1)).setParent(parentId).setSource("{}", XContentType.JSON));
        }
        numChildDocs = child2InnerObjects[parent] = scaledRandomIntBetween(1, numDocs);
        limit = child2 + numChildDocs;
        for (; child2 < limit; child2++) {
            requestBuilders.add(client().prepareIndex("idx", "child2", String.format(Locale.ENGLISH, "%04d", child2)).setParent(parentId).setSource("{}", XContentType.JSON));
        }
    }
    indexRandom(true, requestBuilders);
    int size = randomIntBetween(0, numDocs);
    BoolQueryBuilder boolQuery = new BoolQueryBuilder();
    boolQuery.should(constantScoreQuery(hasChildQuery("child1", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("a").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size), false)));
    boolQuery.should(constantScoreQuery(hasChildQuery("child2", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size), false)));
    SearchResponse searchResponse = client().prepareSearch("idx").setSize(numDocs).setTypes("parent").addSort("_uid", SortOrder.ASC).setQuery(boolQuery).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, numDocs);
    assertThat(searchResponse.getHits().getHits().length, equalTo(numDocs));
    int offset1 = 0;
    int offset2 = 0;
    for (int parent = 0; parent < numDocs; parent++) {
        SearchHit searchHit = searchResponse.getHits().getAt(parent);
        assertThat(searchHit.getType(), equalTo("parent"));
        assertThat(searchHit.getId(), equalTo(String.format(Locale.ENGLISH, "%03d", parent)));
        assertThat(searchHit.getShard(), notNullValue());
        SearchHits inner = searchHit.getInnerHits().get("a");
        assertThat(inner.getTotalHits(), equalTo((long) child1InnerObjects[parent]));
        for (int child = 0; child < child1InnerObjects[parent] && child < size; child++) {
            SearchHit innerHit = inner.getAt(child);
            assertThat(innerHit.getType(), equalTo("child1"));
            String childId = String.format(Locale.ENGLISH, "%04d", offset1 + child);
            assertThat(innerHit.getId(), equalTo(childId));
            assertThat(innerHit.getNestedIdentity(), nullValue());
        }
        offset1 += child1InnerObjects[parent];
        inner = searchHit.getInnerHits().get("b");
        assertThat(inner.getTotalHits(), equalTo((long) child2InnerObjects[parent]));
        for (int child = 0; child < child2InnerObjects[parent] && child < size; child++) {
            SearchHit innerHit = inner.getAt(child);
            assertThat(innerHit.getType(), equalTo("child2"));
            String childId = String.format(Locale.ENGLISH, "%04d", offset2 + child);
            assertThat(innerHit.getId(), equalTo(childId));
            assertThat(innerHit.getNestedIdentity(), nullValue());
        }
        offset2 += child2InnerObjects[parent];
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ElasticsearchAssertions.assertSearchHit(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 5 with BoolQueryBuilder

use of org.elasticsearch.index.query.BoolQueryBuilder in project elasticsearch by elastic.

the class InnerHitsIT method testRandomNested.

public void testRandomNested() throws Exception {
    assertAcked(prepareCreate("idx").addMapping("type", "field1", "type=nested", "field2", "type=nested"));
    int numDocs = scaledRandomIntBetween(25, 100);
    List<IndexRequestBuilder> requestBuilders = new ArrayList<>();
    int[] field1InnerObjects = new int[numDocs];
    int[] field2InnerObjects = new int[numDocs];
    for (int i = 0; i < numDocs; i++) {
        int numInnerObjects = field1InnerObjects[i] = scaledRandomIntBetween(1, numDocs);
        XContentBuilder source = jsonBuilder().startObject().startArray("field1");
        for (int j = 0; j < numInnerObjects; j++) {
            source.startObject().field("x", "y").endObject();
        }
        numInnerObjects = field2InnerObjects[i] = scaledRandomIntBetween(1, numDocs);
        source.endArray().startArray("field2");
        for (int j = 0; j < numInnerObjects; j++) {
            source.startObject().field("x", "y").endObject();
        }
        source.endArray().endObject();
        requestBuilders.add(client().prepareIndex("idx", "type", String.format(Locale.ENGLISH, "%03d", i)).setSource(source));
    }
    indexRandom(true, requestBuilders);
    int size = randomIntBetween(0, numDocs);
    BoolQueryBuilder boolQuery = new BoolQueryBuilder();
    boolQuery.should(nestedQuery("field1", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("a").setSize(size).addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)), false));
    boolQuery.should(nestedQuery("field2", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)).setSize(size), false));
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(boolQuery).setSize(numDocs).addSort("_uid", SortOrder.ASC).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, numDocs);
    assertThat(searchResponse.getHits().getHits().length, equalTo(numDocs));
    for (int i = 0; i < numDocs; i++) {
        SearchHit searchHit = searchResponse.getHits().getAt(i);
        assertThat(searchHit.getShard(), notNullValue());
        SearchHits inner = searchHit.getInnerHits().get("a");
        assertThat(inner.getTotalHits(), equalTo((long) field1InnerObjects[i]));
        for (int j = 0; j < field1InnerObjects[i] && j < size; j++) {
            SearchHit innerHit = inner.getAt(j);
            assertThat(innerHit.getNestedIdentity().getField().string(), equalTo("field1"));
            assertThat(innerHit.getNestedIdentity().getOffset(), equalTo(j));
            assertThat(innerHit.getNestedIdentity().getChild(), nullValue());
        }
        inner = searchHit.getInnerHits().get("b");
        assertThat(inner.getTotalHits(), equalTo((long) field2InnerObjects[i]));
        for (int j = 0; j < field2InnerObjects[i] && j < size; j++) {
            SearchHit innerHit = inner.getAt(j);
            assertThat(innerHit.getNestedIdentity().getField().string(), equalTo("field2"));
            assertThat(innerHit.getNestedIdentity().getOffset(), equalTo(j));
            assertThat(innerHit.getNestedIdentity().getChild(), nullValue());
        }
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ElasticsearchAssertions.assertSearchHit(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SearchHits(org.elasticsearch.search.SearchHits) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)98 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)25 SearchResponse (org.elasticsearch.action.search.SearchResponse)15 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)9 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)9 HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)8 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)5 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)5 Map (java.util.Map)4 AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)4 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)4 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 StickyFacetBuilder (org.sonar.server.es.StickyFacetBuilder)4 HasChildQueryBuilder (org.elasticsearch.index.query.HasChildQueryBuilder)3 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)3