use of org.elasticsearch.search.builder.SearchSourceBuilder in project engine by craftercms.
the class ContentTypeBasedDataFetcher method doGet.
/**
* {@inheritDoc}
*/
@Override
public Object doGet(final DataFetchingEnvironment env) {
Field field = env.getMergedField().getSingleField();
String fieldName = field.getName();
// Get arguments for pagination & sorting
int offset = Optional.ofNullable(env.<Integer>getArgument(ARG_NAME_OFFSET)).orElse(0);
int limit = Optional.ofNullable(env.<Integer>getArgument(ARG_NAME_LIMIT)).orElse(defaultLimit);
String sortBy = Optional.ofNullable(env.<String>getArgument(ARG_NAME_SORT_BY)).orElse(defaultSortField);
String sortOrder = Optional.ofNullable(env.<String>getArgument(ARG_NAME_SORT_ORDER)).orElse(defaultSortOrder);
List<String> queryFieldIncludes = new LinkedList<>();
// Add content-type to includes, we might need it for a GraphQL TypeResolver
queryFieldIncludes.add(QUERY_FIELD_NAME_CONTENT_TYPE);
List<Map<String, Object>> items = new LinkedList<>();
Map<String, Object> result = new HashMap<>(2);
result.put(FIELD_NAME_ITEMS, items);
// Setup the ES query
SearchSourceBuilder source = new SearchSourceBuilder();
BoolQueryBuilder query = boolQuery();
source.query(query).from(offset).size(limit).sort(sortBy, SortOrder.fromString(sortOrder));
StopWatch watch = new StopWatch(field.getName() + " - " + field.getAlias());
watch.start("build filters");
// Filter by the content-type
switch(fieldName) {
case FIELD_NAME_CONTENT_ITEMS:
query.filter(existsQuery(QUERY_FIELD_NAME_CONTENT_TYPE));
break;
case FIELD_NAME_PAGES:
query.filter(regexpQuery(QUERY_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE_REGEX_PAGE));
break;
case FIELD_NAME_COMPONENTS:
query.filter(regexpQuery(QUERY_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE_REGEX_COMPONENT));
break;
default:
// Get the content-type name from the field name
query.filter(termQuery(QUERY_FIELD_NAME_CONTENT_TYPE, getOriginalName(fieldName)));
break;
}
// Check the selected fields to build the ES query
Optional<Field> itemsField = field.getSelectionSet().getSelections().stream().map(f -> (Field) f).filter(f -> f.getName().equals(FIELD_NAME_ITEMS)).findFirst();
if (itemsField.isPresent()) {
List<Selection> selections = itemsField.get().getSelectionSet().getSelections();
selections.forEach(selection -> processSelection(StringUtils.EMPTY, selection, query, queryFieldIncludes, env));
}
// Only fetch the selected fields for better performance
source.fetchSource(queryFieldIncludes.toArray(new String[0]), new String[0]);
watch.stop();
logger.debug("Executing query: {}", source);
watch.start("searching items");
SearchResponse response = elasticsearch.search(new SearchRequest().source(source));
watch.stop();
watch.start("processing items");
result.put(FIELD_NAME_TOTAL, response.getHits().totalHits);
if (response.getHits().totalHits > 0) {
for (SearchHit hit : response.getHits().getHits()) {
items.add(fixItems(hit.getSourceAsMap()));
}
}
watch.stop();
if (logger.isTraceEnabled()) {
logger.trace(watch.prettyPrint());
}
return result;
}
use of org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.
the class ProjectMeasuresIndexerTest method assertThatProjectHasTag.
private void assertThatProjectHasTag(ComponentDto project, String expectedTag) {
SearchRequest request = prepareSearch(TYPE_PROJECT_MEASURES.getMainType()).source(new SearchSourceBuilder().query(boolQuery().filter(termQuery(FIELD_INDEX_TYPE, TYPE_PROJECT_MEASURES.getName())).filter(termQuery(FIELD_TAGS, expectedTag))));
assertThat(es.client().search(request).getHits().getHits()).extracting(SearchHit::getId).contains(project.uuid());
}
use of org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.
the class ProjectMeasuresIndexerTest method assertThatQualifierIs.
private void assertThatQualifierIs(String qualifier, String... componentsUuid) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(boolQuery().filter(termQuery(FIELD_INDEX_TYPE, TYPE_PROJECT_MEASURES.getName())).filter(termQuery(FIELD_QUALIFIER, qualifier)).filter(termsQuery(FIELD_UUID, componentsUuid)));
SearchRequest request = prepareSearch(TYPE_PROJECT_MEASURES.getMainType()).source(searchSourceBuilder);
assertThat(es.client().search(request).getHits().getHits()).extracting(SearchHit::getId).containsExactlyInAnyOrder(componentsUuid);
}
use of org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.
the class RuleIndex method listTags.
public List<String> listTags(@Nullable String query, int size) {
int maxPageSize = 500;
checkArgument(size <= maxPageSize, "Page size must be lower than or equals to " + maxPageSize);
if (size <= 0) {
return emptyList();
}
TermsAggregationBuilder termsAggregation = AggregationBuilders.terms(AGGREGATION_NAME_FOR_TAGS).field(FIELD_RULE_TAGS).size(size).order(BucketOrder.key(true)).minDocCount(1);
ofNullable(query).map(EsUtils::escapeSpecialRegexChars).map(queryString -> ".*" + queryString + ".*").map(s -> new IncludeExclude(s, null)).ifPresent(termsAggregation::includeExclude);
SearchRequest request = EsClient.prepareSearch(TYPE_RULE.getMainType()).source(new SearchSourceBuilder().query(matchAllQuery()).size(0).aggregation(termsAggregation));
SearchResponse esResponse = client.search(request);
return EsUtils.termsKeys(esResponse.getAggregations().get(AGGREGATION_NAME_FOR_TAGS));
}
use of org.elasticsearch.search.builder.SearchSourceBuilder in project sonarqube by SonarSource.
the class BulkIndexerTest method bulk_delete.
@Test
public void bulk_delete() {
int max = 500;
int removeFrom = 200;
FakeDoc[] docs = new FakeDoc[max];
for (int i = 0; i < max; i++) {
docs[i] = FakeIndexDefinition.newDoc(i);
}
es.putDocuments(TYPE_FAKE, docs);
assertThat(count()).isEqualTo(max);
SearchRequest req = EsClient.prepareSearch(TYPE_FAKE).source(new SearchSourceBuilder().query(QueryBuilders.rangeQuery(FakeIndexDefinition.INT_FIELD).gte(removeFrom)));
BulkIndexer.delete(es.client(), TYPE_FAKE, req);
assertThat(count()).isEqualTo(removeFrom);
}
Aggregations