Search in sources :

Example 21 with QueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.

the class AbstractQueryTestCase method testToQuery.

/**
     * Test creates the {@link Query} from the {@link QueryBuilder} under test and delegates the
     * assertions being made on the result to the implementing subclass.
     */
public void testToQuery() throws IOException {
    for (int runs = 0; runs < NUMBER_OF_TESTQUERIES; runs++) {
        QueryShardContext context = createShardContext();
        assert context.isCachable();
        context.setAllowUnmappedFields(true);
        QB firstQuery = createTestQueryBuilder();
        QB controlQuery = copyQuery(firstQuery);
        SearchContext searchContext = getSearchContext(randomTypes, context);
        /* we use a private rewrite context here since we want the most realistic way of asserting that we are cacheable or not.
             * We do it this way in SearchService where
             * we first rewrite the query with a private context, then reset the context and then build the actual lucene query*/
        QueryBuilder rewritten = rewriteQuery(firstQuery, new QueryShardContext(context));
        Query firstLuceneQuery = rewritten.toQuery(context);
        if (isCachable(firstQuery)) {
            assertTrue("query was marked as not cacheable in the context but this test indicates it should be cacheable: " + firstQuery.toString(), context.isCachable());
        } else {
            assertFalse("query was marked as cacheable in the context but this test indicates it should not be cacheable: " + firstQuery.toString(), context.isCachable());
        }
        assertNotNull("toQuery should not return null", firstLuceneQuery);
        assertLuceneQuery(firstQuery, firstLuceneQuery, searchContext);
        //remove after assertLuceneQuery since the assertLuceneQuery impl might access the context as well
        assertTrue("query is not equal to its copy after calling toQuery, firstQuery: " + firstQuery + ", secondQuery: " + controlQuery, firstQuery.equals(controlQuery));
        assertTrue("equals is not symmetric after calling toQuery, firstQuery: " + firstQuery + ", secondQuery: " + controlQuery, controlQuery.equals(firstQuery));
        assertThat("query copy's hashcode is different from original hashcode after calling toQuery, firstQuery: " + firstQuery + ", secondQuery: " + controlQuery, controlQuery.hashCode(), equalTo(firstQuery.hashCode()));
        QB secondQuery = copyQuery(firstQuery);
        // query _name never should affect the result of toQuery, we randomly set it to make sure
        if (randomBoolean()) {
            secondQuery.queryName(secondQuery.queryName() == null ? randomAsciiOfLengthBetween(1, 30) : secondQuery.queryName() + randomAsciiOfLengthBetween(1, 10));
        }
        searchContext = getSearchContext(randomTypes, context);
        Query secondLuceneQuery = rewriteQuery(secondQuery, context).toQuery(context);
        assertNotNull("toQuery should not return null", secondLuceneQuery);
        assertLuceneQuery(secondQuery, secondLuceneQuery, searchContext);
        if (builderGeneratesCacheableQueries()) {
            assertEquals("two equivalent query builders lead to different lucene queries", rewrite(secondLuceneQuery), rewrite(firstLuceneQuery));
        }
        if (supportsBoostAndQueryName()) {
            secondQuery.boost(firstQuery.boost() + 1f + randomFloat());
            Query thirdLuceneQuery = rewriteQuery(secondQuery, context).toQuery(context);
            assertNotEquals("modifying the boost doesn't affect the corresponding lucene query", rewrite(firstLuceneQuery), rewrite(thirdLuceneQuery));
        }
        // check that context#isFilter is not changed by invoking toQuery/rewrite
        boolean filterFlag = randomBoolean();
        context.setIsFilter(filterFlag);
        rewriteQuery(firstQuery, context).toQuery(context);
        assertEquals("isFilter should be unchanged", filterFlag, context.isFilter());
    }
}
Also used : Query(org.apache.lucene.search.Query) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermQuery(org.apache.lucene.search.TermQuery) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) SearchContext(org.elasticsearch.search.internal.SearchContext) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder)

Example 22 with QueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.

the class AbstractQueryTestCase method assertParsedQuery.

/**
     * Parses the query provided as bytes argument and compares it with the expected result provided as argument as a {@link QueryBuilder}
     */
private static void assertParsedQuery(XContentParser parser, QueryBuilder expectedQuery) throws IOException {
    QueryBuilder newQuery = parseQuery(parser);
    assertNotSame(newQuery, expectedQuery);
    assertEquals(expectedQuery, newQuery);
    assertEquals(expectedQuery.hashCode(), newQuery.hashCode());
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder)

Example 23 with QueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project sonarqube by SonarSource.

the class IssueIndex method createAssigneesFacet.

private static AggregationBuilder createAssigneesFacet(IssueQuery query, Map<String, QueryBuilder> filters, QueryBuilder queryBuilder) {
    String fieldName = IssueIndexDefinition.FIELD_ISSUE_ASSIGNEE;
    String facetName = PARAM_ASSIGNEES;
    // Same as in super.stickyFacetBuilder
    Map<String, QueryBuilder> assigneeFilters = Maps.newHashMap(filters);
    assigneeFilters.remove(IS_ASSIGNED_FILTER);
    assigneeFilters.remove(fieldName);
    StickyFacetBuilder assigneeFacetBuilder = newStickyFacetBuilder(query, assigneeFilters, queryBuilder);
    BoolQueryBuilder facetFilter = assigneeFacetBuilder.getStickyFacetFilter(fieldName);
    FilterAggregationBuilder facetTopAggregation = assigneeFacetBuilder.buildTopFacetAggregation(fieldName, facetName, facetFilter, DEFAULT_FACET_SIZE);
    Collection<String> assigneesEscaped = escapeValuesForFacetInclusion(query.assignees());
    if (!assigneesEscaped.isEmpty()) {
        facetTopAggregation = assigneeFacetBuilder.addSelectedItemsToFacet(fieldName, facetName, facetTopAggregation, assigneesEscaped.toArray());
    }
    // Add missing facet for unassigned issues
    facetTopAggregation.subAggregation(addEffortAggregationIfNeeded(query, AggregationBuilders.missing(facetName + FACET_SUFFIX_MISSING).field(fieldName)));
    return AggregationBuilders.global(facetName).subAggregation(facetTopAggregation);
}
Also used : FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) StickyFacetBuilder(org.sonar.server.es.StickyFacetBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder)

Example 24 with QueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project sonarqube by SonarSource.

the class IssueIndex method createResolutionFacet.

private static AggregationBuilder createResolutionFacet(IssueQuery query, Map<String, QueryBuilder> filters, QueryBuilder esQuery) {
    String fieldName = IssueIndexDefinition.FIELD_ISSUE_RESOLUTION;
    String facetName = PARAM_RESOLUTIONS;
    // Same as in super.stickyFacetBuilder
    Map<String, QueryBuilder> resolutionFilters = Maps.newHashMap(filters);
    resolutionFilters.remove("__isResolved");
    resolutionFilters.remove(fieldName);
    StickyFacetBuilder assigneeFacetBuilder = newStickyFacetBuilder(query, resolutionFilters, esQuery);
    BoolQueryBuilder facetFilter = assigneeFacetBuilder.getStickyFacetFilter(fieldName);
    FilterAggregationBuilder facetTopAggregation = assigneeFacetBuilder.buildTopFacetAggregation(fieldName, facetName, facetFilter, DEFAULT_FACET_SIZE);
    facetTopAggregation = assigneeFacetBuilder.addSelectedItemsToFacet(fieldName, facetName, facetTopAggregation);
    // Add missing facet for unresolved issues
    facetTopAggregation.subAggregation(addEffortAggregationIfNeeded(query, AggregationBuilders.missing(facetName + FACET_SUFFIX_MISSING).field(fieldName)));
    return AggregationBuilders.global(facetName).subAggregation(facetTopAggregation);
}
Also used : FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) StickyFacetBuilder(org.sonar.server.es.StickyFacetBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder)

Example 25 with QueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project sonarqube by SonarSource.

the class RuleIndex method addActiveSeverityFacetIfNeeded.

private static void addActiveSeverityFacetIfNeeded(RuleQuery query, SearchOptions options, Map<String, AbstractAggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
    if (options.getFacets().contains(FACET_ACTIVE_SEVERITIES)) {
        // We are building a children aggregation on active rules
        // so the rule filter has to be used as parent filter for active rules
        // from which we remove filters that concern active rules ("activation")
        HasParentQueryBuilder ruleFilter = QueryBuilders.hasParentQuery(INDEX_TYPE_RULE.getType(), stickyFacetBuilder.getStickyFacetFilter("activation"));
        // Rebuilding the active rule filter without severities
        BoolQueryBuilder childrenFilter = boolQuery();
        addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_PROFILE_KEY, query.getQProfileKey());
        RuleIndex.addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_INHERITANCE, query.getInheritance());
        QueryBuilder activeRuleFilter;
        if (childrenFilter.hasClauses()) {
            activeRuleFilter = childrenFilter.must(ruleFilter);
        } else {
            activeRuleFilter = ruleFilter;
        }
        AbstractAggregationBuilder activeSeverities = AggregationBuilders.children(FACET_ACTIVE_SEVERITIES + "_children").childType(INDEX_TYPE_ACTIVE_RULE.getType()).subAggregation(AggregationBuilders.filter(FACET_ACTIVE_SEVERITIES + "_filter").filter(activeRuleFilter).subAggregation(AggregationBuilders.terms(FACET_ACTIVE_SEVERITIES).field(FIELD_ACTIVE_RULE_SEVERITY).include(Joiner.on('|').join(Severity.ALL)).size(Severity.ALL.size())));
        aggregations.put(FACET_ACTIVE_SEVERITIES, AggregationBuilders.global(FACET_ACTIVE_SEVERITIES).subAggregation(activeSeverities));
    }
}
Also used : AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder)

Aggregations

QueryBuilder (org.elasticsearch.index.query.QueryBuilder)371 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)245 Test (org.testng.annotations.Test)156 DynamicEntity (org.molgenis.data.support.DynamicEntity)137 SearchResponse (org.elasticsearch.action.search.SearchResponse)58 QueryImpl (org.molgenis.data.support.QueryImpl)36 Map (java.util.Map)32 Test (org.junit.Test)31 ArrayList (java.util.ArrayList)30 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)29 HashMap (java.util.HashMap)27 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)27 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)27 IOException (java.io.IOException)23 SearchHit (org.elasticsearch.search.SearchHit)22 AbstractQueryBuilder (org.elasticsearch.index.query.AbstractQueryBuilder)19 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)19 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)17 List (java.util.List)16 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)15