<logback.version>1.1.7</logback.version>
<slf4j.version>1.7.21</slf4j.version>
<tomcat.version>8.5.16</tomcat.version>
- <elasticsearch.version>2.4.4</elasticsearch.version>
+ <elasticsearch.version>5.0.0</elasticsearch.version>
<orchestrator.version>3.15.0.1090</orchestrator.version>
<okhttp.version>3.7.0</okhttp.version>
<jackson.version>2.6.6</jackson.version>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-api</artifactId>
+ <version>2.7</version>
+ </dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-api</artifactId>
+ <version>2.6.2</version>
</dependency>
<dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-classic</artifactId>
- </dependency>
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-core</artifactId>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ <version>2.6.2</version>
</dependency>
<!-- testing -->
*/
package org.sonar.search;
+import java.io.IOException;
import org.apache.lucene.util.StringHelper;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.node.Node;
-import org.elasticsearch.node.NodeBuilder;
+import org.elasticsearch.node.NodeValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.process.Jmx;
if (esSettings.getAsInt(MIMINUM_MASTER_NODES, 1) >= 2) {
LOGGER.info("Elasticsearch is waiting {} for {} node(s) to be up to start.",
esSettings.get(INITIAL_STATE_TIMEOUT),
- esSettings.get(MIMINUM_MASTER_NODES)
- );
+ esSettings.get(MIMINUM_MASTER_NODES));
+ }
+ node = new Node(settings.build());
+ try {
+ node.start();
+ } catch (NodeValidationException e) {
+ throw new RuntimeException("Failed to start ES", e);
}
- node = NodeBuilder.nodeBuilder().settings(esSettings).build();
- node.start();
}
// copied from https://github.com/elastic/elasticsearch/blob/v2.3.3/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java
@Override
public Status getStatus() {
boolean esStatus = node != null && node.client().admin().cluster().prepareHealth()
- .setWaitForYellowStatus()
- .setTimeout(TimeValue.timeValueSeconds(30L))
- .get()
- .getStatus() != ClusterHealthStatus.RED;
+ .setWaitForYellowStatus()
+ .setTimeout(TimeValue.timeValueSeconds(30L))
+ .get()
+ .getStatus() != ClusterHealthStatus.RED;
if (esStatus) {
return Status.OPERATIONAL;
}
@Override
public void stop() {
if (node != null && !node.isClosed()) {
- node.close();
+ try {
+ node.close();
+ } catch (IOException e) {
+ LOGGER.error("Failed to stop ES cleanly", e);
+ }
}
Jmx.unregister(EsSettingsMBean.OBJECT_NAME);
}
import java.io.IOException;
import java.net.InetAddress;
+import java.util.Collection;
+import java.util.Collections;
import java.util.Properties;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.NoNodeAvailableException;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
+import org.elasticsearch.plugins.Plugin;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
Logger logger = mock(Logger.class);
underTest.LOGGER = logger;
underTest.start();
+ System.out.println(org.apache.logging.log4j.Logger.class);
verify(logger).info(eq("Elasticsearch is waiting {} for {} node(s) to be up to start."), eq("1s"), eq("2"));
}
assertThat(underTest.getStatus()).isEqualTo(Monitored.Status.OPERATIONAL);
Settings settings = Settings.builder().put("cluster.name", A_CLUSTER_NAME).build();
- client = TransportClient.builder().settings(settings).build()
- .addTransportAddress(new InetSocketTransportAddress(InetAddress.getLoopbackAddress(), port));
+ Collection<Class<? extends Plugin>> plugins = Collections.emptyList();
+ client = new TransportClient(settings, plugins) {
+
+ }.addTransportAddress(new InetSocketTransportAddress(InetAddress.getLoopbackAddress(), port));
assertThat(client.admin().cluster().prepareClusterStats().get().getStatus()).isEqualTo(ClusterHealthStatus.GREEN);
underTest.stop();
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-api</artifactId>
+ </dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<artifactId>elasticsearch</artifactId>
</dependency>
<dependency>
- <groupId>org.elasticsearch</groupId>
- <artifactId>elasticsearch</artifactId>
+ <groupId>org.elasticsearch.test</groupId>
+ <artifactId>framework</artifactId>
<version>${elasticsearch.version}</version>
<scope>test</scope>
- <type>test-jar</type>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.filters.InternalFilters;
-import org.elasticsearch.search.aggregations.bucket.filters.InternalFilters.Bucket;
+import org.elasticsearch.search.aggregations.bucket.filters.InternalFilters.InternalBucket;
import org.elasticsearch.search.aggregations.metrics.tophits.InternalTopHits;
-import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder;
-import org.elasticsearch.search.highlight.HighlightBuilder;
+import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsAggregationBuilder;
+import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.ScoreSortBuilder;
import org.sonar.server.es.EsClient;
this.authorizationTypeSupport = authorizationTypeSupport;
}
- private static HighlightBuilder.Field createHighlighter() {
+ private static HighlightBuilder.Field createHighlighterField() {
HighlightBuilder.Field field = new HighlightBuilder.Field(FIELD_NAME);
field.highlighterType("fvh");
field.matchedFields(
}
private static FiltersAggregationBuilder createAggregation(ComponentIndexQuery query) {
- FiltersAggregationBuilder filtersAggregation = AggregationBuilders.filters(FILTERS_AGGREGATION_NAME)
+ return AggregationBuilders.filters(
+ FILTERS_AGGREGATION_NAME,
+ query.getQualifiers().stream().map(q -> termQuery(FIELD_QUALIFIER, q)).toArray(QueryBuilder[]::new))
.subAggregation(createSubAggregation(query));
- query.getQualifiers().forEach(q -> filtersAggregation.filter(q, termQuery(FIELD_QUALIFIER, q)));
- return filtersAggregation;
}
- private static TopHitsBuilder createSubAggregation(ComponentIndexQuery query) {
- TopHitsBuilder sub = AggregationBuilders.topHits(DOCS_AGGREGATION_NAME)
- .setHighlighterEncoder("html")
- .setHighlighterPreTags("<mark>")
- .setHighlighterPostTags("</mark>")
- .addHighlightedField(createHighlighter())
- .setFrom(query.getSkip())
- .setSize(query.getLimit())
- .addSort(new ScoreSortBuilder())
- .addSort(new FieldSortBuilder(ComponentIndexDefinition.FIELD_NAME));
- return sub.setFetchSource(false);
+ private static TopHitsAggregationBuilder createSubAggregation(ComponentIndexQuery query) {
+ return AggregationBuilders.topHits(DOCS_AGGREGATION_NAME)
+ .highlighter(new HighlightBuilder()
+ .encoder("html")
+ .preTags("<mark>")
+ .postTags("</mark>")
+ .field(createHighlighterField())
+ )
+ .from(query.getSkip())
+ .size(query.getLimit())
+ .sort(new ScoreSortBuilder())
+ .sort(new FieldSortBuilder(ComponentIndexDefinition.FIELD_NAME))
+ .fetchSource(false);
}
private QueryBuilder createQuery(ComponentIndexQuery query, ComponentTextSearchFeature... features) {
private static ComponentIndexResults aggregationsToQualifiers(SearchResponse response) {
InternalFilters filtersAgg = response.getAggregations().get(FILTERS_AGGREGATION_NAME);
- List<Bucket> buckets = filtersAgg.getBuckets();
+ List<InternalBucket> buckets = filtersAgg.getBuckets();
return ComponentIndexResults.newBuilder()
.setQualifiers(
buckets.stream().map(ComponentIndex::bucketToQualifier))
.build();
}
- private static ComponentHitsPerQualifier bucketToQualifier(Bucket bucket) {
+ private static ComponentHitsPerQualifier bucketToQualifier(InternalBucket bucket) {
InternalTopHits docs = bucket.getAggregations().get(DOCS_AGGREGATION_NAME);
SearchHits hitList = docs.getHits();
package org.sonar.server.es;
import java.util.Arrays;
+import org.elasticsearch.action.support.WriteRequest.RefreshPolicy;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.settings.Settings;
public static final String TYPE = "type";
public static final String INDEX = "index";
- public static final String INDEX_SEARCHABLE_FOR_TEXT = "analyzed"; // ES 5: remove this constant and replace usages with INDEX_SEARCHABLE
- public static final String INDEX_SEARCHABLE_FOR_KEYWORD = "not_analyzed"; // ES 5: change to true and rename to INDEX_SEARCHABLE
- public static final String INDEX_NOT_SEARCHABLE = "no"; // ES 5: change to false
- public static final String FIELD_TYPE_TEXT = "string"; // ES 5: change to text
- public static final String FIELD_TYPE_KEYWORD = "string"; // ES 5: change to keyword
+ public static final String INDEX_SEARCHABLE = "true";
+ public static final String INDEX_NOT_SEARCHABLE = "false";
+ public static final String FIELD_TYPE_TEXT = "text";
+ public static final String FIELD_TYPE_KEYWORD = "keyword";
public static final String STANDARD = "standard";
public static final String PATTERN = "pattern";
public static final String CUSTOM = "custom";
public static final String KEYWORD = "keyword";
public static final String CLASSIC = "classic";
- public static final boolean REFRESH_IMMEDIATE = true; // ES 5: replace with org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE
- public static final boolean REFRESH_NONE = false; // ES 5: replace with org.elasticsearch.action.support.WriteRequest.RefreshPolicy.NONE
+ public static final RefreshPolicy REFRESH_IMMEDIATE = RefreshPolicy.IMMEDIATE;
+ public static final RefreshPolicy REFRESH_NONE = RefreshPolicy.NONE;
public static final String TRUNCATE = "truncate";
import static org.sonar.server.es.DefaultIndexSettings.FIELD_TYPE_TEXT;
import static org.sonar.server.es.DefaultIndexSettings.FILTER;
import static org.sonar.server.es.DefaultIndexSettings.INDEX;
-import static org.sonar.server.es.DefaultIndexSettings.INDEX_SEARCHABLE_FOR_TEXT;
+import static org.sonar.server.es.DefaultIndexSettings.INDEX_SEARCHABLE;
import static org.sonar.server.es.DefaultIndexSettings.KEYWORD;
import static org.sonar.server.es.DefaultIndexSettings.LOWERCASE;
import static org.sonar.server.es.DefaultIndexSettings.MAXIMUM_NGRAM_LENGTH;
public SortedMap<String, String> fieldMapping() {
return ImmutableSortedMap.of(
TYPE, FIELD_TYPE_TEXT,
- INDEX, INDEX_SEARCHABLE_FOR_TEXT,
+ INDEX, INDEX_SEARCHABLE,
ANALYZER, getName());
}
},
public SortedMap<String, String> fieldMapping() {
return ImmutableSortedMap.of(
TYPE, FIELD_TYPE_TEXT,
- INDEX, INDEX_SEARCHABLE_FOR_TEXT,
+ INDEX, INDEX_SEARCHABLE,
ANALYZER, INDEX_GRAMS_ANALYZER.getName(),
SEARCH_ANALYZER, getName());
}
public SortedMap<String, String> fieldMapping() {
return ImmutableSortedMap.of(
TYPE, FIELD_TYPE_TEXT,
- INDEX, INDEX_SEARCHABLE_FOR_TEXT,
+ INDEX, INDEX_SEARCHABLE,
ANALYZER, INDEX_PREFIX_ANALYZER.getName(),
SEARCH_ANALYZER, getName());
}
public SortedMap<String, String> fieldMapping() {
return ImmutableSortedMap.of(
TYPE, FIELD_TYPE_TEXT,
- INDEX, INDEX_SEARCHABLE_FOR_TEXT,
+ INDEX, INDEX_SEARCHABLE,
ANALYZER, INDEX_PREFIX_CASE_INSENSITIVE_ANALYZER.getName(),
SEARCH_ANALYZER, getName());
}
public SortedMap<String, String> fieldMapping() {
return ImmutableSortedMap.of(
TYPE, FIELD_TYPE_TEXT,
- INDEX, INDEX_SEARCHABLE_FOR_TEXT,
+ INDEX, INDEX_SEARCHABLE,
ANALYZER, USER_INDEX_GRAMS_ANALYZER.getName(),
SEARCH_ANALYZER, getName());
}
public SortedMap<String, String> fieldMapping() {
return ImmutableSortedMap.of(
TYPE, FIELD_TYPE_TEXT,
- INDEX, INDEX_SEARCHABLE_FOR_TEXT,
+ INDEX, INDEX_SEARCHABLE,
ANALYZER, INDEX_WORDS_ANALYZER.getName(),
SEARCH_ANALYZER, getName());
}
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
+import java.util.Collections;
import java.util.stream.Collectors;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
boolean clusterEnabled = config.getBoolean(ProcessProperties.CLUSTER_ENABLED).orElse(false);
if (clusterEnabled && config.getBoolean(ProcessProperties.CLUSTER_SEARCH_DISABLED).orElse(false)) {
esSettings.put("client.transport.sniff", true);
- nativeClient = TransportClient.builder().settings(esSettings).build();
+ nativeClient = new TransportClient(esSettings.build(), Collections.emptyList()) {};
Arrays.stream(config.getStringArray(ProcessProperties.CLUSTER_SEARCH_HOSTS))
.map(HostAndPort::fromString)
.forEach(h -> addHostToClient(h, nativeClient));
LOGGER.info("Connected to remote Elasticsearch: [{}]", displayedAddresses(nativeClient));
} else {
- nativeClient = TransportClient.builder().settings(esSettings).build();
+ nativeClient = new TransportClient(esSettings.build(), Collections.emptyList()) {};
HostAndPort host = HostAndPort.fromParts(config.get(ProcessProperties.SEARCH_HOST).get(), config.getInt(ProcessProperties.SEARCH_PORT).get());
addHostToClient(host, nativeClient);
LOGGER.info("Connected to local Elasticsearch: [{}]", displayedAddresses(nativeClient));
import static org.sonar.server.es.DefaultIndexSettings.FIELD_TYPE_TEXT;
import static org.sonar.server.es.DefaultIndexSettings.INDEX;
import static org.sonar.server.es.DefaultIndexSettings.INDEX_NOT_SEARCHABLE;
-import static org.sonar.server.es.DefaultIndexSettings.INDEX_SEARCHABLE_FOR_KEYWORD;
-import static org.sonar.server.es.DefaultIndexSettings.INDEX_SEARCHABLE_FOR_TEXT;
+import static org.sonar.server.es.DefaultIndexSettings.INDEX_SEARCHABLE;
import static org.sonar.server.es.DefaultIndexSettings.TYPE;
import static org.sonar.server.es.DefaultIndexSettingsElement.UUID_MODULE_ANALYZER;
public NewIndexType createUuidPathField(String fieldName) {
return setProperty(fieldName, ImmutableSortedMap.of(
TYPE, FIELD_TYPE_TEXT,
- INDEX, INDEX_SEARCHABLE_FOR_TEXT,
+ INDEX, DefaultIndexSettings.INDEX_SEARCHABLE,
ANALYZER, UUID_MODULE_ANALYZER.getName()));
}
}
/**
- * "index: no" -> Don’t index this field at all. This field will not be searchable.
- * By default field is "not_analyzed": it is searchable, but index the value exactly
- * as specified.
- */
- // ES 5: update javadoc to:
- /*
* "index: false" -> Make this field not searchable.
* By default field is "true": it is searchable, but index the value exactly
* as specified.
if (subFields.isEmpty()) {
hash.putAll(ImmutableMap.of(
"type", FIELD_TYPE_KEYWORD,
- "index", disableSearch ? INDEX_NOT_SEARCHABLE : INDEX_SEARCHABLE_FOR_KEYWORD,
- "norms",
- ImmutableMap.of("enabled", String.valueOf(!disableNorms)) // ES 5: replace with String.valueOf(!disableNorms)
- ));
+ "index", disableSearch ? INDEX_NOT_SEARCHABLE : INDEX_SEARCHABLE,
+ "norms", String.valueOf(!disableNorms)));
} else {
hash.put("type", "multi_field");
multiFields.put(fieldName, ImmutableMap.of(
"type", FIELD_TYPE_KEYWORD,
- "index", INDEX_SEARCHABLE_FOR_KEYWORD,
+ "index", INDEX_SEARCHABLE,
"term_vector", termVectorWithPositionOffsets ? "with_positions_offsets" : "no",
"norms",
- ImmutableMap.of("enabled", "false") // ES 5: replace with "false"
+ "false"
));
hash.put("fields", multiFields);
public NestedFieldBuilder addKeywordField(String fieldName) {
return setProperty(fieldName, ImmutableMap.of(
"type", FIELD_TYPE_KEYWORD,
- "index", INDEX_SEARCHABLE_FOR_KEYWORD));
+ "index", INDEX_SEARCHABLE));
}
public NestedFieldBuilder addDoubleField(String fieldName) {
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import java.util.List;
-import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
+import org.sonar.core.util.stream.MoreCollectors;
import static org.sonar.server.ws.WsUtils.checkRequest;
return fields.get(name);
}
- public void fill(SearchRequestBuilder request, String name, boolean asc) {
+ public List<FieldSortBuilder> fill(String name, boolean asc) {
List<Field> list = fields.get(name);
checkRequest(!list.isEmpty(), "Bad sort field: %s", name);
- doFill(request, list, asc);
+ return doFill(list, asc);
}
- public void fillDefault(SearchRequestBuilder request) {
- doFill(request, defaultFields, true);
+ public List<FieldSortBuilder> fillDefault() {
+ return doFill(defaultFields, true);
}
- private static void doFill(SearchRequestBuilder request, List<Field> fields, boolean asc) {
- for (Field field : fields) {
+ private static List<FieldSortBuilder> doFill(List<Field> fields, boolean asc) {
+ return fields.stream().map(field -> {
FieldSortBuilder sortBuilder = SortBuilders.fieldSort(field.name);
boolean effectiveAsc = asc != field.reverse;
sortBuilder.order(effectiveAsc ? SortOrder.ASC : SortOrder.DESC);
boolean effectiveMissingLast = asc == field.missingLast;
sortBuilder.missing(effectiveMissingLast ? "_last" : "_first");
- request.addSort(sortBuilder);
- }
+ return sortBuilder;
+ }).collect(MoreCollectors.toList(fields.size()));
}
public static class Field {
import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Order;
-import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
+import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
+import org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
* @param selected the terms, that the user already has selected
* @return the (global) aggregation, that can be added on top level of the elasticsearch request
*/
- public AggregationBuilder buildStickyFacet(String fieldName, String facetName, Function<TermsBuilder, AggregationBuilder<?>> additionalAggregationFilter, Object... selected) {
+ public AggregationBuilder buildStickyFacet(String fieldName, String facetName, Function<TermsAggregationBuilder, AggregationBuilder> additionalAggregationFilter, Object... selected) {
return buildStickyFacet(fieldName, facetName, FACET_DEFAULT_SIZE, additionalAggregationFilter, selected);
}
return buildStickyFacet(fieldName, facetName, size, t -> t, selected);
}
- private AggregationBuilder buildStickyFacet(String fieldName, String facetName, int size, Function<TermsBuilder, AggregationBuilder<?>> additionalAggregationFilter,
+ private AggregationBuilder buildStickyFacet(String fieldName, String facetName, int size, Function<TermsAggregationBuilder, AggregationBuilder> additionalAggregationFilter,
Object... selected) {
BoolQueryBuilder facetFilter = getStickyFacetFilter(fieldName);
FilterAggregationBuilder facetTopAggregation = buildTopFacetAggregation(fieldName, facetName, facetFilter, size, additionalAggregationFilter);
}
private FilterAggregationBuilder buildTopFacetAggregation(String fieldName, String facetName, BoolQueryBuilder facetFilter, int size,
- Function<TermsBuilder, AggregationBuilder<?>> additionalAggregationFilter) {
- TermsBuilder termsAggregation = buildTermsFacetAggregation(fieldName, facetName, size);
- AggregationBuilder<?> improvedAggregation = additionalAggregationFilter.apply(termsAggregation);
+ Function<TermsAggregationBuilder, AggregationBuilder> additionalAggregationFilter) {
+ TermsAggregationBuilder termsAggregation = buildTermsFacetAggregation(fieldName, facetName, size);
+ AggregationBuilder improvedAggregation = additionalAggregationFilter.apply(termsAggregation);
return AggregationBuilders
- .filter(facetName + "_filter")
- .filter(facetFilter)
+ .filter(facetName + "_filter", facetFilter)
.subAggregation(improvedAggregation);
}
- private TermsBuilder buildTermsFacetAggregation(String fieldName, String facetName, int size) {
- TermsBuilder termsAggregation = AggregationBuilders.terms(facetName)
+ private TermsAggregationBuilder buildTermsFacetAggregation(String fieldName, String facetName, int size) {
+ TermsAggregationBuilder termsAggregation = AggregationBuilders.terms(facetName)
.field(fieldName)
.order(order)
.size(size)
}
public FilterAggregationBuilder addSelectedItemsToFacet(String fieldName, String facetName, FilterAggregationBuilder facetTopAggregation,
- Function<TermsBuilder, AggregationBuilder<?>> additionalAggregationFilter, Object... selected) {
+ Function<TermsAggregationBuilder, AggregationBuilder> additionalAggregationFilter, Object... selected) {
if (selected.length <= 0) {
return facetTopAggregation;
}
.map(s -> EsUtils.escapeSpecialRegexChars(s.toString()))
.collect(PIPE_JOINER);
- TermsBuilder selectedTerms = AggregationBuilders.terms(facetName + "_selected")
+ TermsAggregationBuilder selectedTerms = AggregationBuilders.terms(facetName + "_selected")
.field(fieldName)
- .include(includes);
+ .includeExclude(new IncludeExclude(includes, null));
if (subAggregation != null) {
selectedTerms = selectedTerms.subAggregation(subAggregation);
}
- AggregationBuilder<?> improvedAggregation = additionalAggregationFilter.apply(selectedTerms);
+ AggregationBuilder improvedAggregation = additionalAggregationFilter.apply(selectedTerms);
facetTopAggregation.subAggregation(improvedAggregation);
return facetTopAggregation;
}
}
private Optional<String> getMetadata(String id) {
- GetRequestBuilder request = esClient.prepareGet(MetadataIndexDefinition.INDEX_TYPE_METADATA, id).setFields(MetadataIndexDefinition.FIELD_VALUE);
+ GetRequestBuilder request = esClient.prepareGet(MetadataIndexDefinition.INDEX_TYPE_METADATA, id)
+ .setStoredFields(MetadataIndexDefinition.FIELD_VALUE);
GetResponse response = request.get();
if (response.isExists()) {
GetField field = response.getField(MetadataIndexDefinition.FIELD_VALUE);
esClient.prepareIndex(MetadataIndexDefinition.INDEX_TYPE_METADATA)
.setId(id)
.setSource(MetadataIndexDefinition.FIELD_VALUE, hash)
- .setRefresh(REFRESH_IMMEDIATE) // ES 5: change to setRefreshPolicy
+ .setRefreshPolicy(REFRESH_IMMEDIATE)
.get();
}
}
*/
package org.sonar.server.es.request;
-import java.io.IOException;
import java.util.Arrays;
import org.elasticsearch.action.ListenableActionFuture;
import org.elasticsearch.action.search.SearchAction;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.unit.TimeValue;
-import org.elasticsearch.common.xcontent.ToXContent;
-import org.elasticsearch.common.xcontent.XContentBuilder;
-import org.elasticsearch.common.xcontent.XContentFactory;
import org.sonar.api.utils.log.Profiler;
import org.sonar.server.es.EsClient;
@Override
public String toString() {
StringBuilder message = new StringBuilder();
- message.append(String.format("ES search request '%s'", xContentToString(super.internalBuilder())));
+ message.append(String.format("ES search request '%s'", super.request));
if (request.indices().length > 0) {
message.append(String.format(" on indices '%s'", Arrays.toString(request.indices())));
}
}
return message.toString();
}
-
- private static String xContentToString(ToXContent toXContent) {
- try {
- XContentBuilder builder = XContentFactory.jsonBuilder();
- toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS);
- return builder.string();
- } catch (IOException e) {
- throw new IllegalStateException("Fail to convert request to string", e);
- }
- }
-
}
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
+import org.elasticsearch.indices.TermsLookup;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
+import org.elasticsearch.search.aggregations.bucket.histogram.ExtendedBounds;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Order;
-import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
+import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
+import org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude;
import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
import org.elasticsearch.search.aggregations.metrics.min.Min;
-import org.elasticsearch.search.aggregations.metrics.sum.SumBuilder;
+import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
+import org.elasticsearch.search.sort.FieldSortBuilder;
+import org.joda.time.DateTimeZone;
import org.elasticsearch.search.aggregations.metrics.valuecount.InternalValueCount;
import org.joda.time.Duration;
import org.sonar.api.utils.DateUtils;
// TODO move to Facets ?
private static final String FACET_SUFFIX_MISSING = "_missing";
private static final String IS_ASSIGNED_FILTER = "__isAssigned";
- private static final SumBuilder EFFORT_AGGREGATION = AggregationBuilders.sum(FACET_MODE_EFFORT).field(IssueIndexDefinition.FIELD_ISSUE_EFFORT);
+ private static final SumAggregationBuilder EFFORT_AGGREGATION = AggregationBuilders.sum(FACET_MODE_EFFORT).field(IssueIndexDefinition.FIELD_ISSUE_EFFORT);
private static final Order EFFORT_AGGREGATION_ORDER = Order.aggregation(FACET_MODE_EFFORT, false);
private static final int DEFAULT_FACET_SIZE = 15;
private static final Duration TWENTY_DAYS = Duration.standardDays(20L);
BoolQueryBuilder viewsFilter = boolQuery();
for (String viewUuid : viewUuids) {
- viewsFilter.should(QueryBuilders.termsLookupQuery(IssueIndexDefinition.FIELD_ISSUE_PROJECT_UUID)
- .lookupIndex(ViewIndexDefinition.INDEX_TYPE_VIEW.getIndex())
- .lookupType(ViewIndexDefinition.INDEX_TYPE_VIEW.getType())
- .lookupId(viewUuid)
- .lookupPath(ViewIndexDefinition.FIELD_PROJECTS));
+ viewsFilter.should(QueryBuilders.termsLookupQuery(IssueIndexDefinition.FIELD_ISSUE_PROJECT_UUID,
+ new TermsLookup(
+ ViewIndexDefinition.INDEX_TYPE_VIEW.getIndex(),
+ ViewIndexDefinition.INDEX_TYPE_VIEW.getType(),
+ viewUuid,
+ ViewIndexDefinition.FIELD_PROJECTS)));
}
return viewsFilter;
}
}
private void configureSorting(IssueQuery query, SearchRequestBuilder esRequest) {
+ createSortBuilders(query).forEach(esRequest::addSort);
+ }
+
+ private List<FieldSortBuilder> createSortBuilders(IssueQuery query) {
String sortField = query.sort();
if (sortField != null) {
boolean asc = BooleanUtils.isTrue(query.asc());
- sorting.fill(esRequest, sortField, asc);
- } else {
- sorting.fillDefault(esRequest);
+ return sorting.fill(sortField, asc);
}
+ return sorting.fillDefault();
}
private Map<String, QueryBuilder> createFilters(IssueQuery query) {
AggregationBuilder dateHistogram = AggregationBuilders.dateHistogram(PARAM_CREATED_AT)
.field(IssueIndexDefinition.FIELD_ISSUE_FUNC_CREATED_AT)
- .interval(bucketSize)
+ .dateHistogramInterval(bucketSize)
.minDocCount(0L)
.format(DateUtils.DATETIME_FORMAT)
- .timeZone(TimeZone.getTimeZone("GMT").getID())
+ .timeZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT")))
.offset(offsetInSeconds + "s")
// ES dateHistogram bounds are inclusive while createdBefore parameter is exclusive
- .extendedBounds(startTime, endTime - 1_000L);
+ .extendedBounds(new ExtendedBounds(startTime, endTime - 1_000L));
dateHistogram = addEffortAggregationIfNeeded(query, dateHistogram);
return Optional.of(dateHistogram);
}
BoolQueryBuilder facetFilter = assignedToMeFacetBuilder.getStickyFacetFilter(IS_ASSIGNED_FILTER, fieldName);
FilterAggregationBuilder facetTopAggregation = AggregationBuilders
- .filter(facetName + "__filter")
- .filter(facetFilter)
+ .filter(facetName + "__filter", facetFilter)
.subAggregation(addEffortAggregationIfNeeded(query, AggregationBuilders.terms(facetName + "__terms")
.field(fieldName)
- .include(escapeSpecialRegexChars(login))));
+ .includeExclude(new IncludeExclude(escapeSpecialRegexChars(login), null))));
builder.addAggregation(
AggregationBuilders.global(facetName)
.filter(termQuery(FIELD_ISSUE_ORGANIZATION_UUID, organization.getUuid())))
.setSize(0);
- TermsBuilder termsAggregation = AggregationBuilders.terms(AGGREGATION_NAME_FOR_TAGS)
+ TermsAggregationBuilder termsAggregation = AggregationBuilders.terms(AGGREGATION_NAME_FOR_TAGS)
.field(IssueIndexDefinition.FIELD_ISSUE_TAGS)
.size(size)
.order(Terms.Order.term(true))
.minDocCount(1L);
if (textQuery != null) {
String escapedTextQuery = escapeSpecialRegexChars(textQuery);
- termsAggregation.include(format(SUBSTRING_MATCH_REGEXP, escapedTextQuery));
+ termsAggregation.includeExclude(new IncludeExclude(format(SUBSTRING_MATCH_REGEXP, escapedTextQuery), null));
}
requestBuilder.addAggregation(termsAggregation);
requestBuilder.setQuery(boolQuery().must(QueryBuilders.matchAllQuery()).filter(createBoolFilter(query)));
- TermsBuilder aggreg = AggregationBuilders.terms("_ref")
+ TermsAggregationBuilder aggreg = AggregationBuilders.terms("_ref")
.field(fieldName)
.size(maxNumberOfTags)
.order(termsOrder)
.minDocCount(1L);
if (textQuery != null) {
- aggreg.include(format(SUBSTRING_MATCH_REGEXP, escapeSpecialRegexChars(textQuery)));
+ aggreg.includeExclude(new IncludeExclude(format(SUBSTRING_MATCH_REGEXP, escapeSpecialRegexChars(textQuery)), null));
}
SearchResponse searchResponse = requestBuilder.addAggregation(aggreg).get();
long from = froms.get(i);
request
.addAggregation(AggregationBuilders
- .filter(projectUuid)
- .filter(boolQuery()
+ .filter(projectUuid, boolQuery()
.filter(termQuery(IssueIndexDefinition.FIELD_ISSUE_PROJECT_UUID, projectUuid))
.filter(rangeQuery(IssueIndexDefinition.FIELD_ISSUE_FUNC_CREATED_AT).gte(new Date(from)))
)
import java.util.Set;
import java.util.stream.IntStream;
import javax.annotation.Nullable;
+import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
-import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder;
-import org.elasticsearch.search.aggregations.bucket.range.RangeBuilder;
+import org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
-import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
+import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
+import org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.server.es.DefaultIndexSettingsElement;
private static void addRangeFacetIncludingNoData(SearchRequestBuilder esSearch, String metricKey, StickyFacetBuilder facetBuilder, Double... thresholds) {
esSearch.addAggregation(createStickyFacet(metricKey, facetBuilder,
- AggregationBuilders.filter("combined_" + metricKey)
- .filter(matchAllQuery())
+ AggregationBuilders.filter("combined_" + metricKey, matchAllQuery())
.subAggregation(createRangeFacet(metricKey, thresholds))
.subAggregation(createNoDataFacet(metricKey))));
}
BoolQueryBuilder facetFilter = facetBuilder.getStickyFacetFilter(facetKey);
return AggregationBuilders
.global(facetKey)
- .subAggregation(AggregationBuilders.filter("facet_filter_" + facetKey)
- .filter(facetFilter)
- .subAggregation(aggregationBuilder));
+ .subAggregation(
+ AggregationBuilders
+ .filter("facet_filter_" + facetKey, facetFilter)
+ .subAggregation(aggregationBuilder));
}
private static AbstractAggregationBuilder createRangeFacet(String metricKey, Double... thresholds) {
- RangeBuilder rangeAgg = AggregationBuilders.range(metricKey)
+ RangeAggregationBuilder rangeAgg = AggregationBuilders.range(metricKey)
.field(FIELD_MEASURES_VALUE);
final int lastIndex = thresholds.length - 1;
IntStream.range(0, thresholds.length)
}
});
- return AggregationBuilders.nested("nested_" + metricKey)
- .path(FIELD_MEASURES)
+ return AggregationBuilders.nested("nested_" + metricKey, FIELD_MEASURES)
.subAggregation(
- AggregationBuilders.filter("filter_" + metricKey)
- .filter(termsQuery(FIELD_MEASURES_KEY, metricKey))
+ AggregationBuilders.filter("filter_" + metricKey, termsQuery(FIELD_MEASURES_KEY, metricKey))
.subAggregation(rangeAgg));
}
private static AbstractAggregationBuilder createNoDataFacet(String metricKey) {
- return AggregationBuilders.filter("no_data_" + metricKey)
- .filter(boolQuery()
- .mustNot(nestedQuery(FIELD_MEASURES, termQuery(FIELD_MEASURES_KEY, metricKey))));
+ return AggregationBuilders.filter(
+ "no_data_" + metricKey,
+ boolQuery().mustNot(nestedQuery(FIELD_MEASURES, termQuery(FIELD_MEASURES_KEY, metricKey), ScoreMode.Avg)));
}
private static AbstractAggregationBuilder createRatingFacet(String metricKey) {
- return AggregationBuilders.nested("nested_" + metricKey)
- .path(FIELD_MEASURES)
+ return AggregationBuilders.nested("nested_" + metricKey, FIELD_MEASURES)
.subAggregation(
- AggregationBuilders.filter("filter_" + metricKey)
- .filter(termsQuery(FIELD_MEASURES_KEY, metricKey))
- .subAggregation(filters(metricKey)
- .filter("1", termQuery(FIELD_MEASURES_VALUE, 1d))
- .filter("2", termQuery(FIELD_MEASURES_VALUE, 2d))
- .filter("3", termQuery(FIELD_MEASURES_VALUE, 3d))
- .filter("4", termQuery(FIELD_MEASURES_VALUE, 4d))
- .filter("5", termQuery(FIELD_MEASURES_VALUE, 5d))));
+ AggregationBuilders.filter("filter_" + metricKey, termsQuery(FIELD_MEASURES_KEY, metricKey))
+ .subAggregation(filters(metricKey,
+ termQuery(FIELD_MEASURES_VALUE, 1d),
+ termQuery(FIELD_MEASURES_VALUE, 2d),
+ termQuery(FIELD_MEASURES_VALUE, 3d),
+ termQuery(FIELD_MEASURES_VALUE, 4d),
+ termQuery(FIELD_MEASURES_VALUE, 5d))));
}
private static AbstractAggregationBuilder createQualityGateFacet() {
- FiltersAggregationBuilder qualityGateStatusFilter = AggregationBuilders.filters(ALERT_STATUS_KEY);
- QUALITY_GATE_STATUS.entrySet().forEach(entry -> qualityGateStatusFilter.filter(entry.getKey(), termQuery(FIELD_QUALITY_GATE_STATUS, entry.getValue())));
- return qualityGateStatusFilter;
+ return AggregationBuilders.filters(
+ ALERT_STATUS_KEY,
+ QUALITY_GATE_STATUS.entrySet().stream()
+ .map(entry -> termQuery(FIELD_QUALITY_GATE_STATUS, entry.getValue()))
+ .toArray(QueryBuilder[]::new));
}
private Map<String, QueryBuilder> createFilters(ProjectMeasuresQuery query) {
private static QueryBuilder toQuery(MetricCriterion criterion) {
if (criterion.isNoData()) {
- return boolQuery().mustNot(nestedQuery(FIELD_MEASURES, termQuery(FIELD_MEASURES_KEY, criterion.getMetricKey())));
+ return boolQuery().mustNot(
+ nestedQuery(
+ FIELD_MEASURES,
+ termQuery(FIELD_MEASURES_KEY, criterion.getMetricKey()),
+ ScoreMode.Avg));
}
- return nestedQuery(FIELD_MEASURES, boolQuery()
- .filter(termQuery(FIELD_MEASURES_KEY, criterion.getMetricKey()))
- .filter(toValueQuery(criterion)));
+ return nestedQuery(
+ FIELD_MEASURES,
+ boolQuery()
+ .filter(termQuery(FIELD_MEASURES_KEY, criterion.getMetricKey()))
+ .filter(toValueQuery(criterion)),
+ ScoreMode.Avg);
}
private static QueryBuilder toValueQuery(MetricCriterion criterion) {
return emptyList();
}
- TermsBuilder tagFacet = AggregationBuilders.terms(FIELD_TAGS)
+ TermsAggregationBuilder tagFacet = AggregationBuilders.terms(FIELD_TAGS)
.field(FIELD_TAGS)
.size(size)
.minDocCount(1)
.order(Terms.Order.term(true));
if (textQuery != null) {
- tagFacet.include(".*" + escapeSpecialRegexChars(textQuery) + ".*");
+ tagFacet.includeExclude(new IncludeExclude(".*" + escapeSpecialRegexChars(textQuery) + ".*", null));
}
SearchRequestBuilder searchQuery = client
.map(GroupDto::getId)
.forEach(groupId -> filter.should(termQuery(FIELD_GROUP_IDS, groupId)));
- return QueryBuilders.hasParentQuery(TYPE_AUTHORIZATION,
- QueryBuilders.boolQuery().filter(filter));
+ return QueryBuilders.hasParentQuery(
+ TYPE_AUTHORIZATION,
+ QueryBuilders.boolQuery().filter(filter),
+ false);
}
}
try {
esClient.prepareClearCache().get();
- for (String index : esClient.prepareState().get().getState().getMetaData().concreteAllIndices()) {
+ for (String index : esClient.prepareState().get().getState().getMetaData().getConcreteAllIndices()) {
clearIndex(new IndexType(index, index));
}
} catch (Exception e) {
nodes.put(node.getName(), nodeAttributes);
nodeAttributes.put("Address", node.getAddress().toString());
nodeAttributes.put("Type", node.isMasterNode() ? "Master" : "Slave");
- nodeAttributes.put("Disk Available", byteCountToDisplaySize(stats.getFs().getTotal().getAvailable().bytes()));
+ nodeAttributes.put("Disk Available", byteCountToDisplaySize(stats.getFs().getTotal().getAvailable().getBytes()));
nodeAttributes.put("Store Size", byteCountToDisplaySize(stats.getIndices().getStore().getSizeInBytes()));
nodeAttributes.put("Open Files", stats.getProcess().getOpenFileDescriptors());
nodeAttributes.put("JVM Heap Usage", formatPercent(stats.getJvm().getMem().getHeapUsedPercent()));
- nodeAttributes.put("JVM Heap Used", byteCountToDisplaySize(stats.getJvm().getMem().getHeapUsed().bytes()));
- nodeAttributes.put("JVM Heap Max", byteCountToDisplaySize(stats.getJvm().getMem().getHeapMax().bytes()));
- nodeAttributes.put("JVM Non Heap Used", byteCountToDisplaySize(stats.getJvm().getMem().getNonHeapUsed().bytes()));
+ nodeAttributes.put("JVM Heap Used", byteCountToDisplaySize(stats.getJvm().getMem().getHeapUsed().getBytes()));
+ nodeAttributes.put("JVM Heap Max", byteCountToDisplaySize(stats.getJvm().getMem().getHeapMax().getBytes()));
+ nodeAttributes.put("JVM Non Heap Used", byteCountToDisplaySize(stats.getJvm().getMem().getNonHeapUsed().getBytes()));
nodeAttributes.put("JVM Threads", stats.getJvm().getThreads().getCount());
nodeAttributes.put("Field Data Memory", byteCountToDisplaySize(stats.getIndices().getFieldData().getMemorySizeInBytes()));
nodeAttributes.put("Field Data Circuit Breaker Limit", byteCountToDisplaySize(stats.getBreaker().getStats(CircuitBreaker.FIELDDATA).getLimit()));
import java.util.function.Function;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
+import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.HasParentQueryBuilder;
-import org.elasticsearch.index.query.MatchQueryBuilder;
+import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
-import org.elasticsearch.index.query.SimpleQueryStringBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
-import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
-import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
+import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
+import org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
Map<String, QueryBuilder> filters = buildFilters(query);
if (!options.getFacets().isEmpty()) {
- for (AbstractAggregationBuilder aggregation : getFacets(query, options, qb, filters).values()) {
+ for (AggregationBuilder aggregation : getFacets(query, options, qb, filters).values()) {
esSearch.addAggregation(aggregation);
}
}
qb.should(simpleQueryStringQuery(query.getQueryText())
.field(SEARCH_WORDS_ANALYZER.subField(FIELD_RULE_NAME), 20f)
.field(FIELD_RULE_HTML_DESCRIPTION, 3f)
- .defaultOperator(SimpleQueryStringBuilder.Operator.AND)).boost(20f);
+ .defaultOperator(Operator.AND)).boost(20f);
// Match and partial Match queries
// Search by key uses the "sortable" sub-field as it requires to be case-insensitive (lower-case filtering)
- qb.should(matchQuery(SORTABLE_ANALYZER.subField(FIELD_RULE_KEY), queryString).operator(MatchQueryBuilder.Operator.AND).boost(30f));
- qb.should(matchQuery(SORTABLE_ANALYZER.subField(FIELD_RULE_RULE_KEY), queryString).operator(MatchQueryBuilder.Operator.AND).boost(15f));
+ qb.should(matchQuery(SORTABLE_ANALYZER.subField(FIELD_RULE_KEY), queryString).operator(Operator.AND).boost(30f));
+ qb.should(matchQuery(SORTABLE_ANALYZER.subField(FIELD_RULE_RULE_KEY), queryString).operator(Operator.AND).boost(15f));
qb.should(termQuery(FIELD_RULE_LANGUAGE, queryString, 3f));
return qb;
}
private static QueryBuilder termQuery(String field, String query, float boost) {
return QueryBuilders.multiMatchQuery(query,
field, SEARCH_WORDS_ANALYZER.subField(field))
- .operator(MatchQueryBuilder.Operator.AND)
+ .operator(Operator.AND)
.boost(boost);
}
.map(tag -> boolQuery()
.filter(QueryBuilders.termQuery(FIELD_RULE_EXTENSION_TAGS, tag))
.filter(termsQuery(FIELD_RULE_EXTENSION_SCOPE, RuleExtensionScope.system().getScope(), RuleExtensionScope.organization(query.getOrganization()).getScope())))
- .map(childQuery -> QueryBuilders.hasChildQuery(INDEX_TYPE_RULE_EXTENSION.getType(), childQuery))
+ .map(childQuery -> QueryBuilders.hasChildQuery(INDEX_TYPE_RULE_EXTENSION.getType(), childQuery, ScoreMode.None))
.forEach(q::should);
filters.put(FIELD_RULE_EXTENSION_TAGS, q);
}
if (TRUE.equals(query.getActivation())) {
filters.put("activation",
QueryBuilders.hasChildQuery(INDEX_TYPE_ACTIVE_RULE.getType(),
- childQuery));
+ childQuery, ScoreMode.None));
} else if (FALSE.equals(query.getActivation())) {
filters.put("activation",
boolQuery().mustNot(
QueryBuilders.hasChildQuery(INDEX_TYPE_ACTIVE_RULE.getType(),
- childQuery)));
+ childQuery, ScoreMode.None)));
}
QProfileDto compareToQProfile = query.getCompareToQProfile();
if (compareToQProfile != null) {
filters.put("comparison",
QueryBuilders.hasChildQuery(INDEX_TYPE_ACTIVE_RULE.getType(),
- boolQuery().must(QueryBuilders.termQuery(FIELD_ACTIVE_RULE_PROFILE_UUID, compareToQProfile.getRulesProfileUuid()))));
+ boolQuery().must(QueryBuilders.termQuery(FIELD_ACTIVE_RULE_PROFILE_UUID, compareToQProfile.getRulesProfileUuid())),
+ ScoreMode.None));
}
}
return filter;
}
- private static Map<String, AbstractAggregationBuilder> getFacets(RuleQuery query, SearchOptions options, QueryBuilder queryBuilder, Map<String, QueryBuilder> filters) {
- Map<String, AbstractAggregationBuilder> aggregations = new HashMap<>();
+ private static Map<String, AggregationBuilder> getFacets(RuleQuery query, SearchOptions options, QueryBuilder queryBuilder, Map<String, QueryBuilder> filters) {
+ Map<String, AggregationBuilder> aggregations = new HashMap<>();
StickyFacetBuilder stickyFacetBuilder = stickyFacetBuilder(queryBuilder, filters);
addDefaultFacets(query, options, aggregations, stickyFacetBuilder);
return aggregations;
}
- private static void addDefaultFacets(RuleQuery query, SearchOptions options, Map<String, AbstractAggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
+ private static void addDefaultFacets(RuleQuery query, SearchOptions options, Map<String, AggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
if (options.getFacets().contains(FACET_LANGUAGES) || options.getFacets().contains(FACET_OLD_DEFAULT)) {
Collection<String> languages = query.getLanguages();
aggregations.put(FACET_LANGUAGES,
Collection<String> tags = query.getTags();
checkArgument(query.getOrganization() != null, "Cannot use tags facet, if no organization is specified.", query.getTags());
- Function<TermsBuilder, AggregationBuilder<?>> childFeature = termsAggregation -> {
+ Function<TermsAggregationBuilder, AggregationBuilder> childFeature = termsAggregation -> {
- FilterAggregationBuilder scopeAggregation = AggregationBuilders.filter("scope_filter_for_" + FACET_TAGS).filter(
+ FilterAggregationBuilder scopeAggregation = AggregationBuilders.filter(
+ "scope_filter_for_" + FACET_TAGS,
termsQuery(FIELD_RULE_EXTENSION_SCOPE,
RuleExtensionScope.system().getScope(),
RuleExtensionScope.organization(query.getOrganization()).getScope()))
.subAggregation(termsAggregation);
- return AggregationBuilders.children("children_for_" + termsAggregation.getName())
- .childType(INDEX_TYPE_RULE_EXTENSION.getType())
+ return AggregationBuilders.children("children_for_" + termsAggregation.getName(), INDEX_TYPE_RULE_EXTENSION.getType())
.subAggregation(scopeAggregation);
};
}
}
- private static void addStatusFacetIfNeeded(SearchOptions options, Map<String, AbstractAggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
+ private static void addStatusFacetIfNeeded(SearchOptions options, Map<String, AggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
if (options.getFacets().contains(FACET_STATUSES)) {
BoolQueryBuilder facetFilter = stickyFacetBuilder.getStickyFacetFilter(FIELD_RULE_STATUS);
- AbstractAggregationBuilder statuses = AggregationBuilders.filter(FACET_STATUSES + "_filter")
- .filter(facetFilter)
+ AggregationBuilder statuses = AggregationBuilders.filter(FACET_STATUSES + "_filter", facetFilter)
.subAggregation(
AggregationBuilders
.terms(FACET_STATUSES)
.field(FIELD_RULE_STATUS)
- .include(Joiner.on('|').join(ALL_STATUSES_EXCEPT_REMOVED))
- .exclude(RuleStatus.REMOVED.toString())
+ .includeExclude(new IncludeExclude(Joiner.on('|').join(ALL_STATUSES_EXCEPT_REMOVED), RuleStatus.REMOVED.toString()))
.size(ALL_STATUSES_EXCEPT_REMOVED.size()));
aggregations.put(FACET_STATUSES, AggregationBuilders.global(FACET_STATUSES).subAggregation(statuses));
}
}
- private static void addActiveSeverityFacetIfNeeded(RuleQuery query, SearchOptions options, Map<String, AbstractAggregationBuilder> aggregations,
+ private static void addActiveSeverityFacetIfNeeded(RuleQuery query, SearchOptions options, Map<String, AggregationBuilder> aggregations,
StickyFacetBuilder stickyFacetBuilder) {
QProfileDto profile = query.getQProfile();
if (options.getFacets().contains(FACET_ACTIVE_SEVERITIES) && profile != null) {
// from which we remove filters that concern active rules ("activation")
HasParentQueryBuilder ruleFilter = QueryBuilders.hasParentQuery(
INDEX_TYPE_RULE.getType(),
- stickyFacetBuilder.getStickyFacetFilter("activation"));
+ stickyFacetBuilder.getStickyFacetFilter("activation"),
+ false);
// Rebuilding the active rule filter without severities
BoolQueryBuilder childrenFilter = boolQuery();
RuleIndex.addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_INHERITANCE, query.getInheritance());
QueryBuilder activeRuleFilter = childrenFilter.must(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())));
+ AggregationBuilder activeSeverities = AggregationBuilders.children(FACET_ACTIVE_SEVERITIES + "_children", INDEX_TYPE_ACTIVE_RULE.getType())
+ .subAggregation(
+ AggregationBuilders.filter(FACET_ACTIVE_SEVERITIES + "_filter", activeRuleFilter)
+ .subAggregation(
+ AggregationBuilders
+ .terms(FACET_ACTIVE_SEVERITIES)
+ .field(FIELD_ACTIVE_RULE_SEVERITY)
+ .includeExclude(new IncludeExclude(Joiner.on('|').join(Severity.ALL), null))
+ .size(Severity.ALL.size())));
aggregations.put(FACET_ACTIVE_SEVERITIES, AggregationBuilders.global(FACET_ACTIVE_SEVERITIES).subAggregation(activeSeverities));
}
}
public List<String> terms(String fields, @Nullable String query, int size) {
- TermsBuilder termsAggregation = AggregationBuilders.terms(AGGREGATION_NAME)
+ TermsAggregationBuilder termsAggregation = AggregationBuilders.terms(AGGREGATION_NAME)
.field(fields)
.size(size)
.minDocCount(1);
if (query != null) {
- termsAggregation.include(".*" + escapeSpecialRegexChars(query) + ".*");
+ termsAggregation.includeExclude(new IncludeExclude(".*" + escapeSpecialRegexChars(query) + ".*", null));
}
SearchRequestBuilder request = client
.prepareSearch(INDEX_TYPE_RULE, INDEX_TYPE_ACTIVE_RULE)
RuleExtensionScope.system().getScope(),
RuleExtensionScope.organization(organization).getScope());
- TermsBuilder termsAggregation = AggregationBuilders.terms(AGGREGATION_NAME_FOR_TAGS)
+ TermsAggregationBuilder termsAggregation = AggregationBuilders.terms(AGGREGATION_NAME_FOR_TAGS)
.field(FIELD_RULE_EXTENSION_TAGS)
.size(size)
.order(Terms.Order.term(true))
ofNullable(query)
.map(EsUtils::escapeSpecialRegexChars)
.map(queryString -> ".*" + queryString + ".*")
- .ifPresent(termsAggregation::include);
+ .map(s -> new IncludeExclude(s, null))
+ .ifPresent(termsAggregation::includeExclude);
SearchRequestBuilder request = client
.prepareSearch(INDEX_TYPE_RULE_EXTENSION)
ruleMapping.keywordFieldBuilder(FIELD_RULE_NAME).addSubFields(SORTABLE_ANALYZER, SEARCH_WORDS_ANALYZER).build();
ruleMapping.setProperty(FIELD_RULE_HTML_DESCRIPTION, ImmutableSortedMap.of(
DefaultIndexSettings.TYPE, DefaultIndexSettings.FIELD_TYPE_TEXT,
- DefaultIndexSettings.INDEX, DefaultIndexSettings.INDEX_SEARCHABLE_FOR_TEXT,
+ DefaultIndexSettings.INDEX, DefaultIndexSettings.INDEX_SEARCHABLE,
DefaultIndexSettings.ANALYZER, ENGLISH_HTML_ANALYZER.getName(),
DefaultIndexSettings.SEARCH_ANALYZER, ENGLISH_HTML_ANALYZER.getName()));
ruleMapping.keywordFieldBuilder(FIELD_RULE_SEVERITY).disableNorms().build();
import com.google.common.base.Optional;
import java.util.ArrayList;
import java.util.List;
+import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.search.SearchHit;
import org.sonar.server.es.EsClient;
SearchRequestBuilder searchRequest = client.prepareSearch(TestIndexDefinition.INDEX_TYPE_TEST)
.setSize(searchOptions.getLimit())
.setFrom(searchOptions.getOffset())
- .setQuery(nestedQuery(FIELD_COVERED_FILES, boolQuery()
- .must(termQuery(FIELD_COVERED_FILES + "." + FIELD_COVERED_FILE_UUID, sourceFileUuid))
- .must(termQuery(FIELD_COVERED_FILES + "." + FIELD_COVERED_FILE_LINES, lineNumber))));
+ .setQuery(nestedQuery(
+ FIELD_COVERED_FILES,
+ boolQuery()
+ .must(termQuery(FIELD_COVERED_FILES + "." + FIELD_COVERED_FILE_UUID, sourceFileUuid))
+ .must(termQuery(FIELD_COVERED_FILES + "." + FIELD_COVERED_FILE_LINES, lineNumber)),
+ ScoreMode.Avg));
return new SearchResult<>(searchRequest.get(), TestDoc::new);
}
import org.sonar.server.es.NewIndex;
import static org.sonar.server.es.DefaultIndexSettings.FIELD_TYPE_KEYWORD;
-import static org.sonar.server.es.DefaultIndexSettings.INDEX_SEARCHABLE_FOR_KEYWORD;
+import static org.sonar.server.es.DefaultIndexSettings.INDEX_SEARCHABLE;
public class TestIndexDefinition implements IndexDefinition {
mapping.keywordFieldBuilder(FIELD_MESSAGE).disableNorms().disableSearch().build();
mapping.keywordFieldBuilder(FIELD_STACKTRACE).disableNorms().disableSearch().build();
mapping.setProperty(FIELD_COVERED_FILES, ImmutableMap.of("type", "nested", "properties", ImmutableMap.of(
- FIELD_COVERED_FILE_UUID, ImmutableMap.of("type", FIELD_TYPE_KEYWORD, "index", INDEX_SEARCHABLE_FOR_KEYWORD),
+ FIELD_COVERED_FILE_UUID, ImmutableMap.of("type", FIELD_TYPE_KEYWORD, "index", INDEX_SEARCHABLE),
FIELD_COVERED_FILE_LINES, ImmutableMap.of("type", "integer"))));
mapping.createDateTimeField(FIELD_UPDATED_AT);
}
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
-import org.elasticsearch.index.query.MatchQueryBuilder;
+import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
USER_SEARCH_GRAMS_ANALYZER.subField(FIELD_NAME),
FIELD_EMAIL,
USER_SEARCH_GRAMS_ANALYZER.subField(FIELD_EMAIL))
- .operator(MatchQueryBuilder.Operator.AND);
+ .operator(Operator.AND);
}
request.setQuery(boolQuery().must(esQuery).filter(filter));
import java.io.IOException;
import java.net.InetAddress;
import java.nio.file.Files;
+import java.util.Collections;
import java.util.Properties;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.client.transport.TransportClient;
}
private void reset() {
- TransportClient client = TransportClient.builder().settings(Settings.builder()
+ TransportClient client = new TransportClient(Settings.builder()
.put("network.bind_host", "localhost")
.put("cluster.name", clusterName)
- .build()).build();
+ .build(), Collections.emptyList()) {};
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getLoopbackAddress(), port));
// wait for node to be ready
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.cluster.metadata.IndexMetaData;
-import org.elasticsearch.cluster.node.DiscoveryNode;
+import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
-import org.elasticsearch.node.NodeBuilder;
+import org.elasticsearch.node.NodeValidationException;
import org.elasticsearch.search.SearchHit;
import org.junit.rules.ExternalResource;
import org.sonar.api.config.internal.MapSettings;
public void putDocuments(IndexType indexType, BaseDoc... docs) {
try {
BulkRequestBuilder bulk = client.prepareBulk()
- .setRefresh(REFRESH_IMMEDIATE); // ES 5: change to setRefreshPolicy
+ .setRefreshPolicy(REFRESH_IMMEDIATE);
for (BaseDoc doc : docs) {
bulk.add(new IndexRequest(indexType.getIndex(), indexType.getType(), doc.getId())
.parent(doc.getParent())
private final Node node;
private NodeHolder() {
+ String nodeName = "tmp-es-" + RandomUtils.nextInt();
+ Path tmpDir;
try {
- String nodeName = "tmp-es-" + RandomUtils.nextInt();
- Path tmpDir = Files.createTempDirectory("tmp-es");
- tmpDir.toFile().deleteOnExit();
-
- node = NodeBuilder.nodeBuilder().local(true).data(true).settings(org.elasticsearch.common.settings.Settings.builder()
- .put("cluster.name", nodeName)
- .put("node.name", nodeName)
- // the two following properties are probably not used because they are
- // declared on indices too
- .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
- .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
- // limit the number of threads created (see org.elasticsearch.common.util.concurrent.EsExecutors)
- .put("processors", 1)
- .put("http.enabled", false)
- .put("config.ignore_system_properties", true)
- .put("action.auto_create_index", false)
- .put("path.home", tmpDir))
- .build();
- node.start();
- checkState(DiscoveryNode.localNode(node.settings()));
- checkState(!node.isClosed());
+ tmpDir = Files.createTempDirectory("tmp-es");
+ } catch (IOException e) {
+ throw new RuntimeException("Cannot create elasticsearch temporary directory", e);
+ }
- // wait for node to be ready
- node.client().admin().cluster().prepareHealth().setWaitForGreenStatus().get();
+ tmpDir.toFile().deleteOnExit();
- // delete the indices (should not exist)
- DeleteIndexResponse response = node.client().admin().indices().prepareDelete("_all").get();
- checkState(response.isAcknowledged());
- } catch (IOException e) {
- throw Throwables.propagate(e);
+ Settings.Builder settings = Settings.builder()
+ .put("transport.type", "local")
+ .put("node.data", true)
+ .put("cluster.name", nodeName)
+ .put("node.name", nodeName)
+ // the two following properties are probably not used because they are
+ // declared on indices too
+ .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
+ .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
+ // limit the number of threads created (see org.elasticsearch.common.util.concurrent.EsExecutors)
+ .put("processors", 1)
+ .put("http.enabled", false)
+ .put("config.ignore_system_properties", true)
+ .put("action.auto_create_index", false)
+ .put("path.home", tmpDir);
+ node = new Node(settings.build());
+ try {
+ node.start();
+ } catch (NodeValidationException e) {
+ throw new RuntimeException("Cannot start Elasticsearch node", e);
}
+ checkState(!node.isClosed());
+
+ // wait for node to be ready
+ node.client().admin().cluster().prepareHealth().setWaitForGreenStatus().get();
+
+ // delete the indices (should not exist)
+ DeleteIndexResponse response = node.client().admin().indices().prepareDelete("_all").get();
+ checkState(response.isAcknowledged());
}
}
import org.sonar.server.es.metadata.MetadataIndexDefinition;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
IndexType fakeIndexType = new IndexType("fakes", "fake");
String id = "1";
- es.client().prepareIndex(fakeIndexType).setId(id).setSource(new FakeDoc().getFields()).setRefresh(true).get();
+ es.client().prepareIndex(fakeIndexType).setId(id).setSource(new FakeDoc().getFields()).setRefreshPolicy(IMMEDIATE).get();
assertThat(es.client().prepareGet(fakeIndexType, id).get().isExists()).isTrue();
// v1
NewIndex index = new NewIndex("issues");
NewIndex.NewIndexType mapping = index.createType("issue");
mapping.setAttribute("dynamic", "true");
- mapping.setProperty("foo_field", ImmutableMap.of("type", "string"));// ES 5: change to keyword
+ mapping.setProperty("foo_field", ImmutableMap.of("type", "keyword"));
mapping.createBooleanField("boolean_field");
mapping.createByteField("byte_field");
mapping.createDateTimeField("dt_field");
assertThat(mapping).isNotNull();
assertThat(mapping.getAttributes().get("dynamic")).isEqualTo("true");
assertThat(mapping.getProperty("foo_field")).isInstanceOf(Map.class);
- assertThat((Map) mapping.getProperty("foo_field")).containsEntry("type", "string");// ES 5: change to keyword
+ assertThat((Map) mapping.getProperty("foo_field")).containsEntry("type", "keyword");
assertThat((Map) mapping.getProperty("byte_field")).isNotEmpty();
assertThat((Map) mapping.getProperty("double_field")).isNotEmpty();
assertThat((Map) mapping.getProperty("dt_field")).isNotEmpty();
.build();
Map<String, Object> props = (Map) mapping.getProperty("basic_field");
- assertThat(props.get("type")).isEqualTo("string");// ES 5: change to keyword
- assertThat(props.get("index")).isEqualTo("not_analyzed");// ES 5: change to true
+ assertThat(props.get("type")).isEqualTo("keyword");
+ assertThat(props.get("index")).isEqualTo("true");
assertThat(props.get("fields")).isNull();
props = (Map) mapping.getProperty("not_searchable_field");
- assertThat(props.get("type")).isEqualTo("string");// ES 5: change to keyword
- assertThat(props.get("index")).isEqualTo("no");// ES 5: change to false
+ assertThat(props.get("type")).isEqualTo("keyword");
+ assertThat(props.get("index")).isEqualTo("false");
assertThat(props.get("fields")).isNull();
props = (Map) mapping.getProperty("all_capabilities_field");
assertThat(result.get("type")).isEqualTo("nested");
Map<String, Map<String, Object>> subProperties = (Map) result.get("properties");
- assertThat(subProperties.get("key").get("type")).isEqualTo("string"); // ES 5: change to "keyword
+ assertThat(subProperties.get("key").get("type")).isEqualTo("keyword");
assertThat(subProperties.get("value").get("type")).isEqualTo("double");
}
mapping.keywordFieldBuilder("the_doc_value").build();
Map<String, Object> props = (Map) mapping.getProperty("the_doc_value");
- assertThat(props.get("type")).isEqualTo("string");// ES 5: change to keyword
+ assertThat(props.get("type")).isEqualTo("keyword");
assertThat(props.get("doc_values")).isNull();
}
package org.sonar.server.es;
import java.util.List;
-import org.apache.commons.lang.reflect.FieldUtils;
-import org.elasticsearch.action.search.SearchAction;
-import org.elasticsearch.action.search.SearchRequestBuilder;
-import org.elasticsearch.client.Client;
-import org.elasticsearch.search.builder.SearchSourceBuilder;
-import org.elasticsearch.search.sort.SortBuilder;
+import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.sonar.server.exceptions.BadRequestException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
-import static org.mockito.Mockito.mock;
public class SortingTest {
Sorting sorting = new Sorting();
sorting.add("updatedAt");
- SearchRequestBuilder request = new SearchRequestBuilder(mock(Client.class), SearchAction.INSTANCE);
- sorting.fill(request, "updatedAt", true);
- List<SortBuilder> fields = fields(request);
+ List<FieldSortBuilder> fields = sorting.fill("updatedAt", true);
assertThat(fields).hasSize(1);
expectField(fields.get(0), "updatedAt", "_first", SortOrder.ASC);
}
Sorting sorting = new Sorting();
sorting.add("updatedAt");
- SearchRequestBuilder request = new SearchRequestBuilder(mock(Client.class), SearchAction.INSTANCE);
- sorting.fill(request, "updatedAt", false);
- List<SortBuilder> fields = fields(request);
+ List<FieldSortBuilder> fields = sorting.fill("updatedAt", false);
assertThat(fields).hasSize(1);
expectField(fields.get(0), "updatedAt", "_last", SortOrder.DESC);
}
Sorting sorting = new Sorting();
sorting.add("updatedAt").missingLast();
- SearchRequestBuilder request = new SearchRequestBuilder(mock(Client.class), SearchAction.INSTANCE);
- sorting.fill(request, "updatedAt", true);
- List<SortBuilder> fields = fields(request);
+ List<FieldSortBuilder> fields = sorting.fill("updatedAt", true);
assertThat(fields).hasSize(1);
expectField(fields.get(0), "updatedAt", "_last", SortOrder.ASC);
}
Sorting sorting = new Sorting();
sorting.add("updatedAt").missingLast();
- SearchRequestBuilder request = new SearchRequestBuilder(mock(Client.class), SearchAction.INSTANCE);
- sorting.fill(request, "updatedAt", false);
- List<SortBuilder> fields = fields(request);
+ List<FieldSortBuilder> fields = sorting.fill("updatedAt", false);
assertThat(fields).hasSize(1);
expectField(fields.get(0), "updatedAt", "_first", SortOrder.DESC);
}
sorting.add("fileLine", "severity").reverse();
sorting.add("fileLine", "key").missingLast();
- SearchRequestBuilder request = new SearchRequestBuilder(mock(Client.class), SearchAction.INSTANCE);
- sorting.fill(request, "fileLine", true);
- List<SortBuilder> fields = fields(request);
+ List<FieldSortBuilder> fields = sorting.fill("fileLine", true);
assertThat(fields).hasSize(4);
expectField(fields.get(0), "file", "_first", SortOrder.ASC);
expectField(fields.get(1), "line", "_first", SortOrder.ASC);
sorting.add("file");
try {
- sorting.fill(new SearchRequestBuilder(mock(Client.class), SearchAction.INSTANCE), "unknown", true);
+ sorting.fill("unknown", true);
fail();
} catch (BadRequestException e) {
assertThat(e.getMessage()).isEqualTo("Bad sort field: unknown");
Sorting sorting = new Sorting();
sorting.addDefault("file");
- SearchRequestBuilder request = new SearchRequestBuilder(mock(Client.class), SearchAction.INSTANCE);
- sorting.fillDefault(request);
- List<SortBuilder> fields = fields(request);
+ List<FieldSortBuilder> fields = sorting.fillDefault();
assertThat(fields).hasSize(1);
}
- private void expectField(SortBuilder field, String expectedField, String expectedMissing, SortOrder expectedSort) throws IllegalAccessException {
- assertThat(fieldName(field)).isEqualTo(expectedField);
- assertThat(missing(field)).isEqualTo(expectedMissing);
- assertThat(order(field)).isEqualTo(expectedSort);
- }
-
- private static List<SortBuilder> fields(SearchRequestBuilder request) throws IllegalAccessException {
- SearchSourceBuilder source = request.internalBuilder();
- return (List<SortBuilder>) FieldUtils.readField(source, "sorts", true);
- }
-
- private static String fieldName(SortBuilder sortBuilder) throws IllegalAccessException {
- return (String) FieldUtils.readField(sortBuilder, "fieldName", true);
- }
-
- private static String missing(SortBuilder sortBuilder) throws IllegalAccessException {
- return (String) FieldUtils.readField(sortBuilder, "missing", true);
- }
-
- private static SortOrder order(SortBuilder sortBuilder) throws IllegalAccessException {
- return (SortOrder) FieldUtils.readField(sortBuilder, "order", true);
+ private void expectField(FieldSortBuilder field, String expectedField, String expectedMissing, SortOrder expectedSort) throws IllegalAccessException {
+ assertThat(field.getFieldName()).isEqualTo(expectedField);
+ assertThat(field.missing()).isEqualTo(expectedMissing);
+ assertThat(field.order()).isEqualTo(expectedSort);
}
}
*/
package org.sonar.server.es.request;
+import org.elasticsearch.action.DocWriteResponse.Result;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.common.unit.TimeValue;
IndexResponse response = esTester.client().prepareIndex(FakeIndexDefinition.INDEX_TYPE_FAKE)
.setSource(FakeIndexDefinition.newDoc(42).getFields())
.get();
- assertThat(response.isCreated()).isTrue();
+ assertThat(response.getResult()).isSameAs(Result.CREATED);
}
@Test
IndexResponse response = esTester.client().prepareIndex(FakeIndexDefinition.INDEX_TYPE_FAKE)
.setSource(FakeIndexDefinition.newDoc(42).getFields())
.get();
- assertThat(response.isCreated()).isTrue();
+ assertThat(response.getResult()).isSameAs(Result.CREATED);
assertThat(logTester.logs(LoggerLevel.TRACE)).hasSize(1);
}
import org.elasticsearch.action.get.MultiGetRequest;
import org.elasticsearch.action.get.MultiGetRequestBuilder;
import org.elasticsearch.common.unit.TimeValue;
-import org.elasticsearch.search.fetch.source.FetchSourceContext;
+import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.utils.log.LogTester;
@Test
public void fail_to_search_bad_query() {
try {
- esTester.client().prepareSearch(FakeIndexDefinition.INDEX).setQuery("bad query").get();
+ esTester.client().prepareSearch("non-existing-index").get();
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalStateException.class);
.setId(uuid)
.setRouting(projectUuid)
.setSource(IOUtils.toString(getClass().getResource(format("%s/%s_%s_%s.json", getClass().getSimpleName(), projectUuid, fileUuid, testName))))
- .setRefresh(REFRESH_IMMEDIATE) // ES 5: change to setRefreshPolicy
+ .setRefreshPolicy(REFRESH_IMMEDIATE)
.get();
}