use of 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());
}
}
use of 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());
}
use of 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);
}
use of 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);
}
use of org.elasticsearch.index.query.QueryBuilder in project sonarqube by SonarSource.
the class ProjectMeasuresIndex method search.
public SearchIdResult<String> search(ProjectMeasuresQuery query, SearchOptions searchOptions) {
SearchRequestBuilder requestBuilder = getClient().prepareSearch(INDEX_TYPE_PROJECT_MEASURES).setFetchSource(false).setFrom(searchOptions.getOffset()).setSize(searchOptions.getLimit());
BoolQueryBuilder esFilter = boolQuery();
Map<String, QueryBuilder> filters = createFilters(query);
filters.values().forEach(esFilter::must);
requestBuilder.setQuery(esFilter);
addFacets(requestBuilder, searchOptions, filters, query);
addSort(query, requestBuilder);
return new SearchIdResult<>(requestBuilder.get(), id -> id);
}
Aggregations