diff options
Diffstat (limited to 'core/src/test/java/org/elasticsearch/search')
48 files changed, 871 insertions, 453 deletions
diff --git a/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java b/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java index 849bec3d78..6c3e3f0bc4 100644 --- a/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java +++ b/core/src/test/java/org/elasticsearch/search/SearchModuleTests.java @@ -25,8 +25,8 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryParser; import org.elasticsearch.index.query.TermQueryBuilder; @@ -49,7 +49,6 @@ import org.elasticsearch.search.aggregations.pipeline.derivative.DerivativePipel import org.elasticsearch.search.aggregations.pipeline.derivative.InternalDerivative; import org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel; import org.elasticsearch.search.aggregations.pipeline.movavg.models.SimpleModel; -import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; @@ -61,6 +60,7 @@ import org.elasticsearch.search.fetch.subphase.highlight.FastVectorHighlighter; import org.elasticsearch.search.fetch.subphase.highlight.Highlighter; import org.elasticsearch.search.fetch.subphase.highlight.PlainHighlighter; import org.elasticsearch.search.fetch.subphase.highlight.PostingsHighlighter; +import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.search.suggest.CustomSuggester; import org.elasticsearch.search.suggest.Suggester; import org.elasticsearch.search.suggest.completion.CompletionSuggester; @@ -207,7 +207,7 @@ public class SearchModuleTests extends ModuleTestCase { assertThat(module.getQueryParserRegistry().getNames(), containsInAnyOrder(supportedQueries)); IndicesQueriesRegistry indicesQueriesRegistry = module.getQueryParserRegistry(); - XContentParser dummyParser = XContentHelper.createParser(new BytesArray("{}")); + XContentParser dummyParser = createParser(JsonXContent.jsonXContent, new BytesArray("{}")); for (String queryName : supportedQueries) { indicesQueriesRegistry.lookup(queryName, ParseFieldMatcher.EMPTY, dummyParser.getTokenLocation()); } @@ -318,7 +318,7 @@ public class SearchModuleTests extends ModuleTestCase { } @Override - protected ValuesSourceAggregatorFactory<ValuesSource, ?> innerBuild(AggregationContext context, + protected ValuesSourceAggregatorFactory<ValuesSource, ?> innerBuild(SearchContext context, ValuesSourceConfig<ValuesSource> config, AggregatorFactory<?> parent, Builder subFactoriesBuilder) throws IOException { return null; } diff --git a/core/src/test/java/org/elasticsearch/search/SearchServiceTests.java b/core/src/test/java/org/elasticsearch/search/SearchServiceTests.java index 13751ae453..0f62690360 100644 --- a/core/src/test/java/org/elasticsearch/search/SearchServiceTests.java +++ b/core/src/test/java/org/elasticsearch/search/SearchServiceTests.java @@ -61,6 +61,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import static java.util.Collections.singletonList; import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; +import static org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason.DELETED; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.Matchers.is; @@ -152,7 +153,7 @@ public class SearchServiceTests extends ESSingleNodeTestCase { public void run() { startGun.countDown(); while(running.get()) { - service.afterIndexDeleted(indexService.index(), indexService.getIndexSettings().getSettings()); + service.afterIndexRemoved(indexService.index(), indexService.getIndexSettings(), DELETED); if (randomBoolean()) { // here we trigger some refreshes to ensure the IR go out of scope such that we hit ACE if we access a search // context in a non-sane way. diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/AggregationCollectorTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/AggregationCollectorTests.java index 44f1adccc8..c81ec476b8 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/AggregationCollectorTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/AggregationCollectorTests.java @@ -26,7 +26,6 @@ import org.elasticsearch.index.IndexService; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.indices.query.IndicesQueriesRegistry; import org.elasticsearch.search.SearchRequestParsers; -import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.test.ESSingleNodeTestCase; @@ -63,12 +62,11 @@ public class AggregationCollectorTests extends ESSingleNodeTestCase { private boolean needsScores(IndexService index, String agg) throws IOException { AggregatorParsers parser = getInstanceFromNode(SearchRequestParsers.class).aggParsers; IndicesQueriesRegistry queriesRegistry = getInstanceFromNode(IndicesQueriesRegistry.class); - XContentParser aggParser = JsonXContent.jsonXContent.createParser(agg); + XContentParser aggParser = createParser(JsonXContent.jsonXContent, agg); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, aggParser, ParseFieldMatcher.STRICT); aggParser.nextToken(); - SearchContext searchContext = createSearchContext(index); - AggregationContext aggContext = new AggregationContext(searchContext); - final AggregatorFactories factories = parser.parseAggregators(parseContext).build(aggContext, null); + SearchContext context = createSearchContext(index); + final AggregatorFactories factories = parser.parseAggregators(parseContext).build(context, null); final Aggregator[] aggregators = factories.createTopLevelAggregators(); assertEquals(1, aggregators.length); return aggregators[0].needsScores(); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/AggregatorParsingTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/AggregatorParsingTests.java index 1f453aa40f..87ca8ffdd9 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/AggregatorParsingTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/AggregatorParsingTests.java @@ -19,58 +19,25 @@ package org.elasticsearch.search.aggregations; -import org.elasticsearch.Version; -import org.elasticsearch.cluster.ClusterState; -import org.elasticsearch.cluster.metadata.IndexMetaData; -import org.elasticsearch.cluster.metadata.MetaData; -import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.ParsingException; -import org.elasticsearch.common.inject.AbstractModule; -import org.elasticsearch.common.inject.Injector; -import org.elasticsearch.common.inject.ModulesBuilder; -import org.elasticsearch.common.io.stream.NamedWriteableRegistry; -import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.settings.SettingsModule; -import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.env.Environment; -import org.elasticsearch.index.Index; -import org.elasticsearch.search.SearchExtRegistry; -import org.elasticsearch.search.SearchRequestParsers; import org.elasticsearch.test.AbstractQueryTestCase; import org.elasticsearch.index.query.QueryParseContext; -import org.elasticsearch.indices.IndicesModule; -import org.elasticsearch.indices.breaker.CircuitBreakerService; -import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; import org.elasticsearch.indices.query.IndicesQueriesRegistry; -import org.elasticsearch.script.ScriptModule; import org.elasticsearch.script.ScriptService; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.IndexSettingsModule; -import org.elasticsearch.test.InternalSettingsPlugin; -import org.elasticsearch.test.VersionUtils; -import org.elasticsearch.threadpool.ThreadPool; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; import java.util.Random; -import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.stream.Collectors; -import java.util.stream.Stream; import static java.util.Collections.emptyList; -import static org.elasticsearch.test.ClusterServiceUtils.createClusterService; -import static org.elasticsearch.test.ClusterServiceUtils.setState; import static org.hamcrest.Matchers.containsString; public class AggregatorParsingTests extends ESTestCase { @@ -96,11 +63,7 @@ public class AggregatorParsingTests extends ESTestCase { Settings settings = Settings.builder().put("node.name", AbstractQueryTestCase.class.toString()) .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) .put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING.getKey(), false).build(); - IndicesModule indicesModule = new IndicesModule(Collections.emptyList()) ; SearchModule searchModule = new SearchModule(settings, false, emptyList()); - List<NamedWriteableRegistry.Entry> entries = new ArrayList<>(); - entries.addAll(indicesModule.getNamedWriteables()); - entries.addAll(searchModule.getNamedWriteables()); aggParsers = searchModule.getSearchRequestParsers().aggParsers; // create some random type with some default field, those types will // stick around for all of the subclasses @@ -114,23 +77,23 @@ public class AggregatorParsingTests extends ESTestCase { } public void testTwoTypes() throws Exception { - String source = JsonXContent.contentBuilder() + XContentBuilder source = JsonXContent.contentBuilder() .startObject() - .startObject("in_stock") - .startObject("filter") - .startObject("range") - .startObject("stock") - .field("gt", 0) - .endObject() - .endObject() - .endObject() - .startObject("terms") - .field("field", "stock") - .endObject() - .endObject() - .endObject().string(); + .startObject("in_stock") + .startObject("filter") + .startObject("range") + .startObject("stock") + .field("gt", 0) + .endObject() + .endObject() + .endObject() + .startObject("terms") + .field("field", "stock") + .endObject() + .endObject() + .endObject(); try { - XContentParser parser = XContentFactory.xContent(source).createParser(source); + XContentParser parser = createParser(source); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); aggParsers.parseAggregators(parseContext); @@ -141,7 +104,9 @@ public class AggregatorParsingTests extends ESTestCase { } public void testTwoAggs() throws Exception { - String source = JsonXContent.contentBuilder() + assumeFalse("Test only makes sense if JSON parser doesn't have strict duplicate checks enabled", + JsonXContent.isStrictDuplicateDetectionEnabled()); + XContentBuilder source = JsonXContent.contentBuilder() .startObject() .startObject("by_date") .startObject("date_histogram") @@ -163,9 +128,9 @@ public class AggregatorParsingTests extends ESTestCase { .endObject() .endObject() .endObject() - .endObject().string(); + .endObject(); try { - XContentParser parser = XContentFactory.xContent(source).createParser(source); + XContentParser parser = createParser(source); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); aggParsers.parseAggregators(parseContext); @@ -191,7 +156,7 @@ public class AggregatorParsingTests extends ESTestCase { } } - String source = JsonXContent.contentBuilder() + XContentBuilder source = JsonXContent.contentBuilder() .startObject() .startObject(name) .startObject("filter") @@ -202,9 +167,9 @@ public class AggregatorParsingTests extends ESTestCase { .endObject() .endObject() .endObject() - .endObject().string(); + .endObject(); try { - XContentParser parser = XContentFactory.xContent(source).createParser(source); + XContentParser parser = createParser(source); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); aggParsers.parseAggregators(parseContext); @@ -215,22 +180,24 @@ public class AggregatorParsingTests extends ESTestCase { } public void testSameAggregationName() throws Exception { + assumeFalse("Test only makes sense if JSON parser doesn't have strict duplicate checks enabled", + JsonXContent.isStrictDuplicateDetectionEnabled()); final String name = randomAsciiOfLengthBetween(1, 10); - String source = JsonXContent.contentBuilder() + XContentBuilder source = JsonXContent.contentBuilder() .startObject() - .startObject(name) - .startObject("terms") - .field("field", "a") - .endObject() - .endObject() - .startObject(name) - .startObject("terms") - .field("field", "b") - .endObject() - .endObject() - .endObject().string(); + .startObject(name) + .startObject("terms") + .field("field", "a") + .endObject() + .endObject() + .startObject(name) + .startObject("terms") + .field("field", "b") + .endObject() + .endObject() + .endObject(); try { - XContentParser parser = XContentFactory.xContent(source).createParser(source); + XContentParser parser = createParser(source); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); aggParsers.parseAggregators(parseContext); @@ -241,7 +208,7 @@ public class AggregatorParsingTests extends ESTestCase { } public void testMissingName() throws Exception { - String source = JsonXContent.contentBuilder() + XContentBuilder source = JsonXContent.contentBuilder() .startObject() .startObject("by_date") .startObject("date_histogram") @@ -257,9 +224,9 @@ public class AggregatorParsingTests extends ESTestCase { //.endObject() .endObject() .endObject() - .endObject().string(); + .endObject(); try { - XContentParser parser = XContentFactory.xContent(source).createParser(source); + XContentParser parser = createParser(source); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); aggParsers.parseAggregators(parseContext); @@ -270,7 +237,7 @@ public class AggregatorParsingTests extends ESTestCase { } public void testMissingType() throws Exception { - String source = JsonXContent.contentBuilder() + XContentBuilder source = JsonXContent.contentBuilder() .startObject() .startObject("by_date") .startObject("date_histogram") @@ -286,9 +253,9 @@ public class AggregatorParsingTests extends ESTestCase { .endObject() .endObject() .endObject() - .endObject().string(); + .endObject(); try { - XContentParser parser = XContentFactory.xContent(source).createParser(source); + XContentParser parser = createParser(source); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); aggParsers.parseAggregators(parseContext); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/BaseAggregationTestCase.java b/core/src/test/java/org/elasticsearch/search/aggregations/BaseAggregationTestCase.java index b20278895b..e8073bac35 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/BaseAggregationTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/BaseAggregationTestCase.java @@ -110,7 +110,7 @@ public abstract class BaseAggregationTestCase<AB extends AbstractAggregationBuil } factoriesBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS); XContentBuilder shuffled = shuffleXContent(builder); - XContentParser parser = XContentFactory.xContent(shuffled.bytes()).createParser(shuffled.bytes()); + XContentParser parser = createParser(shuffled); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); assertSame(XContentParser.Token.FIELD_NAME, parser.nextToken()); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/BasePipelineAggregationTestCase.java b/core/src/test/java/org/elasticsearch/search/aggregations/BasePipelineAggregationTestCase.java index f8681375e6..4e51ac13df 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/BasePipelineAggregationTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/BasePipelineAggregationTestCase.java @@ -112,7 +112,7 @@ public abstract class BasePipelineAggregationTestCase<AF extends AbstractPipelin } factoriesBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS); XContentBuilder shuffled = shuffleXContent(builder); - XContentParser parser = XContentFactory.xContent(shuffled.bytes()).createParser(shuffled.bytes()); + XContentParser parser = createParser(shuffled); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); String contentString = factoriesBuilder.toString(); logger.info("Content string: {}", contentString); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/FilterIT.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/FilterIT.java index 0c8b9a22c3..5e43ac675a 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/FilterIT.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/FilterIT.java @@ -21,17 +21,10 @@ package org.elasticsearch.search.aggregations.bucket; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryParseContext; -import org.elasticsearch.indices.query.IndicesQueriesRegistry; -import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.bucket.filter.Filter; -import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.Histogram; import org.elasticsearch.search.aggregations.metrics.avg.Avg; import org.elasticsearch.test.ESIntegTestCase; @@ -122,24 +115,6 @@ public class FilterIT extends ESIntegTestCase { assertThat(filter.getDocCount(), equalTo((long) numDocs)); } - /** - * test that "{ "filter" : {} }" is regarded as match_all when not parsing strict - */ - public void testEmptyFilter() throws Exception { - String emtpyFilterBody = "{ }"; - XContentParser parser = XContentFactory.xContent(emtpyFilterBody).createParser(emtpyFilterBody); - QueryParseContext parseContext = new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.EMPTY); - AggregationBuilder filterAgg = FilterAggregationBuilder.parse("tag1", parseContext); - - SearchResponse response = client().prepareSearch("idx").addAggregation(filterAgg).execute().actionGet(); - - assertSearchResponse(response); - - Filter filter = response.getAggregations().get("tag1"); - assertThat(filter, notNullValue()); - assertThat(filter.getDocCount(), equalTo((long) numDocs)); - } - public void testWithSubAggregation() throws Exception { SearchResponse response = client().prepareSearch("idx") .addAggregation(filter("tag1", termQuery("tag", "tag1")) diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/FiltersIT.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/FiltersIT.java index 619bba7da1..f42d23d9e7 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/FiltersIT.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/FiltersIT.java @@ -22,17 +22,10 @@ package org.elasticsearch.search.aggregations.bucket; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryParseContext; -import org.elasticsearch.indices.query.IndicesQueriesRegistry; -import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.bucket.filters.Filters; -import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter; import org.elasticsearch.search.aggregations.bucket.histogram.Histogram; import org.elasticsearch.search.aggregations.metrics.avg.Avg; @@ -205,45 +198,9 @@ public class FiltersIT extends ESIntegTestCase { assertThat(avgValue, notNullValue()); assertThat(avgValue.getName(), equalTo("avg_value")); assertThat(avgValue.getValue(), equalTo((double) sum / numTag2Docs)); - assertThat((String) propertiesKeys[1], equalTo("tag2")); - assertThat((long) propertiesDocCounts[1], equalTo((long) numTag2Docs)); - assertThat((double) propertiesCounts[1], equalTo((double) sum / numTag2Docs)); - } - - public void testEmptyFilter() throws Exception { - String emtpyFilterBody = "{ \"filters\" : [ {} ] }"; - XContentParser parser = XContentFactory.xContent(emtpyFilterBody).createParser(emtpyFilterBody); - parser.nextToken(); - QueryParseContext parseContext = new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.EMPTY); - AggregationBuilder filtersAgg = FiltersAggregationBuilder.parse("tag1", parseContext); - - SearchResponse response = client().prepareSearch("idx").addAggregation(filtersAgg).execute().actionGet(); - - assertSearchResponse(response); - - Filters filter = response.getAggregations().get("tag1"); - assertThat(filter, notNullValue()); - assertThat(filter.getBuckets().size(), equalTo(1)); - assertThat(filter.getBuckets().get(0).getDocCount(), equalTo((long) numDocs)); - } - - public void testEmptyKeyedFilter() throws Exception { - String emtpyFilterBody = "{ \"filters\" : {\"foo\" : {} } }"; - XContentParser parser = XContentFactory.xContent(emtpyFilterBody).createParser(emtpyFilterBody); - parser.nextToken(); - QueryParseContext parseContext = new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.EMPTY); - AggregationBuilder filtersAgg = FiltersAggregationBuilder.parse("tag1", parseContext); - - SearchResponse response = client().prepareSearch("idx").addAggregation(filtersAgg) - .execute().actionGet(); - - assertSearchResponse(response); - - Filters filter = response.getAggregations().get("tag1"); - assertThat(filter, notNullValue()); - assertThat(filter.getBuckets().size(), equalTo(1)); - assertThat(filter.getBuckets().get(0).getKey(), equalTo("foo")); - assertThat(filter.getBuckets().get(0).getDocCount(), equalTo((long) numDocs)); + assertThat(propertiesKeys[1], equalTo("tag2")); + assertThat(propertiesDocCounts[1], equalTo((long) numTag2Docs)); + assertThat(propertiesCounts[1], equalTo((double) sum / numTag2Docs)); } public void testAsSubAggregation() { @@ -444,9 +401,9 @@ public class FiltersIT extends ESIntegTestCase { assertThat(avgValue, notNullValue()); assertThat(avgValue.getName(), equalTo("avg_value")); assertThat(avgValue.getValue(), equalTo((double) sum / numTag1Docs)); - assertThat((String) propertiesKeys[0], equalTo("tag1")); - assertThat((long) propertiesDocCounts[0], equalTo((long) numTag1Docs)); - assertThat((double) propertiesCounts[0], equalTo((double) sum / numTag1Docs)); + assertThat(propertiesKeys[0], equalTo("tag1")); + assertThat(propertiesDocCounts[0], equalTo((long) numTag1Docs)); + assertThat(propertiesCounts[0], equalTo((double) sum / numTag1Docs)); bucket = filters.getBucketByKey("tag2"); assertThat(bucket, Matchers.notNullValue()); @@ -460,9 +417,9 @@ public class FiltersIT extends ESIntegTestCase { assertThat(avgValue, notNullValue()); assertThat(avgValue.getName(), equalTo("avg_value")); assertThat(avgValue.getValue(), equalTo((double) sum / numTag2Docs)); - assertThat((String) propertiesKeys[1], equalTo("tag2")); - assertThat((long) propertiesDocCounts[1], equalTo((long) numTag2Docs)); - assertThat((double) propertiesCounts[1], equalTo((double) sum / numTag2Docs)); + assertThat(propertiesKeys[1], equalTo("tag2")); + assertThat(propertiesDocCounts[1], equalTo((long) numTag2Docs)); + assertThat(propertiesCounts[1], equalTo((double) sum / numTag2Docs)); bucket = filters.getBucketByKey("_other_"); assertThat(bucket, Matchers.notNullValue()); @@ -476,9 +433,9 @@ public class FiltersIT extends ESIntegTestCase { assertThat(avgValue, notNullValue()); assertThat(avgValue.getName(), equalTo("avg_value")); assertThat(avgValue.getValue(), equalTo((double) sum / numOtherDocs)); - assertThat((String) propertiesKeys[2], equalTo("_other_")); - assertThat((long) propertiesDocCounts[2], equalTo((long) numOtherDocs)); - assertThat((double) propertiesCounts[2], equalTo((double) sum / numOtherDocs)); + assertThat(propertiesKeys[2], equalTo("_other_")); + assertThat(propertiesDocCounts[2], equalTo((long) numOtherDocs)); + assertThat(propertiesCounts[2], equalTo((double) sum / numOtherDocs)); } public void testEmptyAggregationWithOtherBucket() throws Exception { diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/LongTermsIT.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/LongTermsIT.java index 35905f91a9..a314bb2724 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/LongTermsIT.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/LongTermsIT.java @@ -305,41 +305,53 @@ public class LongTermsIT extends AbstractTermsTestCase { long includes[] = { 1, 2, 3, 98 }; long excludes[] = { -1, 2, 4 }; long empty[] = {}; - testIncludeExcludeResults(includes, empty, new long[] { 1, 2, 3 }); - testIncludeExcludeResults(includes, excludes, new long[] { 1, 3 }); - testIncludeExcludeResults(empty, excludes, new long[] { 0, 1, 3 }); + testIncludeExcludeResults(1, includes, empty, new long[] { 1, 2, 3 }, new long[0]); + testIncludeExcludeResults(1, includes, excludes, new long[] { 1, 3 }, new long[0]); + testIncludeExcludeResults(1, empty, excludes, new long[] { 0, 1, 3 }, new long[0]); + + testIncludeExcludeResults(0, includes, empty, new long[] { 1, 2, 3}, new long[] { 98 }); + testIncludeExcludeResults(0, includes, excludes, new long[] { 1, 3 }, new long[] { 98 }); + testIncludeExcludeResults(0, empty, excludes, new long[] { 0, 1, 3 }, new long[] {5, 6, 7, 8, 9, 10, 11}); } - private void testIncludeExcludeResults(long[] includes, long[] excludes, long[] expecteds) { + private void testIncludeExcludeResults(int minDocCount, long[] includes, long[] excludes, + long[] expectedWithCounts, long[] expectedZeroCounts) { SearchResponse response = client().prepareSearch("idx").setTypes("type") .addAggregation(terms("terms") .field(SINGLE_VALUED_FIELD_NAME) .includeExclude(new IncludeExclude(includes, excludes)) - .collectMode(randomFrom(SubAggCollectionMode.values()))) + .collectMode(randomFrom(SubAggCollectionMode.values())) + .minDocCount(minDocCount)) .execute().actionGet(); assertSearchResponse(response); Terms terms = response.getAggregations().get("terms"); assertThat(terms, notNullValue()); assertThat(terms.getName(), equalTo("terms")); - assertThat(terms.getBuckets().size(), equalTo(expecteds.length)); + assertThat(terms.getBuckets().size(), equalTo(expectedWithCounts.length + expectedZeroCounts.length)); - for (int i = 0; i < expecteds.length; i++) { - Terms.Bucket bucket = terms.getBucketByKey("" + expecteds[i]); + for (int i = 0; i < expectedWithCounts.length; i++) { + Terms.Bucket bucket = terms.getBucketByKey("" + expectedWithCounts[i]); assertThat(bucket, notNullValue()); assertThat(bucket.getDocCount(), equalTo(1L)); } + + for (int i = 0; i < expectedZeroCounts.length; i++) { + Terms.Bucket bucket = terms.getBucketByKey("" + expectedZeroCounts[i]); + assertThat(bucket, notNullValue()); + assertThat(bucket.getDocCount(), equalTo(0L)); + } } - - - + + + public void testSingleValueFieldWithPartitionedFiltering() throws Exception { runTestFieldWithPartitionedFiltering(SINGLE_VALUED_FIELD_NAME); } - + public void testMultiValueFieldWithPartitionedFiltering() throws Exception { runTestFieldWithPartitionedFiltering(MULTI_VALUED_FIELD_NAME); } - + private void runTestFieldWithPartitionedFiltering(String field) throws Exception { // Find total number of unique terms SearchResponse allResponse = client().prepareSearch("idx").setTypes("type") @@ -348,8 +360,8 @@ public class LongTermsIT extends AbstractTermsTestCase { Terms terms = allResponse.getAggregations().get("terms"); assertThat(terms, notNullValue()); assertThat(terms.getName(), equalTo("terms")); - int expectedCardinality = terms.getBuckets().size(); - + int expectedCardinality = terms.getBuckets().size(); + // Gather terms using partitioned aggregations final int numPartitions = randomIntBetween(2, 4); Set<Number> foundTerms = new HashSet<>(); @@ -368,9 +380,9 @@ public class LongTermsIT extends AbstractTermsTestCase { foundTerms.add(bucket.getKeyAsNumber()); } } - assertEquals(expectedCardinality, foundTerms.size()); + assertEquals(expectedCardinality, foundTerms.size()); } - + public void testSingleValueFieldWithMaxSize() throws Exception { SearchResponse response = client().prepareSearch("idx").setTypes("high_card_type") diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java index c75fac3606..48b393ef46 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java @@ -33,7 +33,7 @@ public class GeoHashGridParserTests extends ESTestCase { public void testParseValidFromInts() throws Exception { int precision = randomIntBetween(1, 12); - XContentParser stParser = JsonXContent.jsonXContent.createParser( + XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":" + precision + ", \"size\": 500, \"shard_size\": 550}"); QueryParseContext parseContext = new QueryParseContext(mockRegistry, stParser, ParseFieldMatcher.STRICT); @@ -45,7 +45,7 @@ public class GeoHashGridParserTests extends ESTestCase { public void testParseValidFromStrings() throws Exception { int precision = randomIntBetween(1, 12); - XContentParser stParser = JsonXContent.jsonXContent.createParser( + XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":\"" + precision + "\", \"size\": \"500\", \"shard_size\": \"550\"}"); QueryParseContext parseContext = new QueryParseContext(mockRegistry, stParser, ParseFieldMatcher.STRICT); XContentParser.Token token = stParser.nextToken(); @@ -55,7 +55,7 @@ public class GeoHashGridParserTests extends ESTestCase { } public void testParseErrorOnNonIntPrecision() throws Exception { - XContentParser stParser = JsonXContent.jsonXContent.createParser("{\"field\":\"my_loc\", \"precision\":\"2.0\"}"); + XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":\"2.0\"}"); QueryParseContext parseContext = new QueryParseContext(mockRegistry, stParser, ParseFieldMatcher.STRICT); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); @@ -69,7 +69,7 @@ public class GeoHashGridParserTests extends ESTestCase { } public void testParseErrorOnBooleanPrecision() throws Exception { - XContentParser stParser = JsonXContent.jsonXContent.createParser("{\"field\":\"my_loc\", \"precision\":false}"); + XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":false}"); QueryParseContext parseContext = new QueryParseContext(mockRegistry, stParser, ParseFieldMatcher.STRICT); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); @@ -82,7 +82,7 @@ public class GeoHashGridParserTests extends ESTestCase { } public void testParseErrorOnPrecisionOutOfRange() throws Exception { - XContentParser stParser = JsonXContent.jsonXContent.createParser("{\"field\":\"my_loc\", \"precision\":\"13\"}"); + XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":\"13\"}"); QueryParseContext parseContext = new QueryParseContext(mockRegistry, stParser, ParseFieldMatcher.STRICT); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/ExtendedBoundsTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/ExtendedBoundsTests.java index e2609f97ea..c232068fff 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/ExtendedBoundsTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/ExtendedBoundsTests.java @@ -36,7 +36,6 @@ import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.SearchParseException; -import org.elasticsearch.search.aggregations.bucket.histogram.ExtendedBounds; import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.test.ESTestCase; import org.joda.time.DateTimeZone; @@ -101,7 +100,7 @@ public class ExtendedBoundsTests extends ESTestCase { SearchContext context = mock(SearchContext.class); QueryShardContext qsc = new QueryShardContext(0, new IndexSettings(IndexMetaData.builder("foo").settings(indexSettings).build(), indexSettings), null, null, null, null, - null, null, null, null, null, () -> now); + null, null, null, null, () -> now); when(context.getQueryShardContext()).thenReturn(qsc); FormatDateTimeFormatter formatter = Joda.forPattern("dateOptionalTime"); DocValueFormat format = new DocValueFormat.DateTime(formatter, DateTimeZone.UTC); @@ -164,7 +163,7 @@ public class ExtendedBoundsTests extends ESTestCase { orig.toXContent(out, ToXContent.EMPTY_PARAMS); out.endObject(); - try (XContentParser in = JsonXContent.jsonXContent.createParser(out.bytes())) { + try (XContentParser in = createParser(JsonXContent.jsonXContent, out.bytes())) { XContentParser.Token token = in.currentToken(); assertNull(token); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregatorTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregatorTests.java index d658a36c07..7be372f328 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregatorTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregatorTests.java @@ -45,7 +45,6 @@ import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.BucketCollector; import org.elasticsearch.search.aggregations.SearchContextAggregations; -import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.test.ESSingleNodeTestCase; @@ -117,14 +116,13 @@ public class NestedAggregatorTests extends ESSingleNodeTestCase { IndexSearcher searcher = new IndexSearcher(directoryReader); indexService.mapperService().merge("test", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("test", "nested_field", "type=nested").string()), MapperService.MergeReason.MAPPING_UPDATE, false); - SearchContext searchContext = createSearchContext(indexService); - AggregationContext context = new AggregationContext(searchContext); + SearchContext context = createSearchContext(indexService); AggregatorFactories.Builder builder = AggregatorFactories.builder(); NestedAggregationBuilder factory = new NestedAggregationBuilder("test", "nested_field"); builder.addAggregator(factory); AggregatorFactories factories = builder.build(context, null); - searchContext.aggregations(new SearchContextAggregations(factories)); + context.aggregations(new SearchContextAggregations(factories)); Aggregator[] aggs = factories.createTopLevelAggregators(); BucketCollector collector = BucketCollector.wrap(Arrays.asList(aggs)); collector.preCollection(); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java index e46a61657d..655a03a75c 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java @@ -269,7 +269,7 @@ public class SignificanceHeuristicTests extends ESTestCase { IndicesQueriesRegistry registry = new IndicesQueriesRegistry(); try { - XContentParser stParser = JsonXContent.jsonXContent.createParser( + XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"text\", " + faultyHeuristicDefinition + ",\"min_doc_count\":200}"); QueryParseContext parseContext = new QueryParseContext(registry, stParser, ParseFieldMatcher.STRICT); stParser.nextToken(); @@ -286,7 +286,7 @@ public class SignificanceHeuristicTests extends ESTestCase { stBuilder.significanceHeuristic(significanceHeuristic).field("text").minDocCount(200); XContentBuilder stXContentBuilder = XContentFactory.jsonBuilder(); stBuilder.internalXContent(stXContentBuilder, null); - XContentParser stParser = JsonXContent.jsonXContent.createParser(stXContentBuilder.string()); + XContentParser stParser = createParser(JsonXContent.jsonXContent, stXContentBuilder.string()); return parseSignificanceHeuristic(significanceHeuristicParserRegistry, searchContext, stParser); } @@ -308,7 +308,7 @@ public class SignificanceHeuristicTests extends ESTestCase { protected SignificanceHeuristic parseFromString(ParseFieldRegistry<SignificanceHeuristicParser> significanceHeuristicParserRegistry, SearchContext searchContext, String heuristicString) throws IOException { - XContentParser stParser = JsonXContent.jsonXContent.createParser( + XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"text\", " + heuristicString + ", \"min_doc_count\":200}"); return parseSignificanceHeuristic(significanceHeuristicParserRegistry, searchContext, stParser); } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/FiltersTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/FiltersTests.java index a1fe372687..496e77f364 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/FiltersTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/FiltersTests.java @@ -22,7 +22,6 @@ package org.elasticsearch.search.aggregations.metrics; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.MatchNoneQueryBuilder; @@ -31,13 +30,11 @@ import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.indices.query.IndicesQueriesRegistry; import org.elasticsearch.search.aggregations.BaseAggregationTestCase; +import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter; -import org.elasticsearch.search.slice.SliceBuilder; import java.io.IOException; -import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder; - public class FiltersTests extends BaseAggregationTestCase<FiltersAggregationBuilder> { @Override @@ -90,7 +87,7 @@ public class FiltersTests extends BaseAggregationTestCase<FiltersAggregationBuil builder.startObject(); builder.startArray("filters").endArray(); builder.endObject(); - XContentParser parser = XContentHelper.createParser(shuffleXContent(builder).bytes()); + XContentParser parser = createParser(shuffleXContent(builder)); parser.nextToken(); QueryParseContext context = new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.STRICT); @@ -103,7 +100,7 @@ public class FiltersTests extends BaseAggregationTestCase<FiltersAggregationBuil builder.startArray("filters").endArray(); builder.field("other_bucket_key", "some_key"); builder.endObject(); - parser = XContentHelper.createParser(shuffleXContent(builder).bytes()); + parser = createParser(shuffleXContent(builder)); parser.nextToken(); context = new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.STRICT); filters = FiltersAggregationBuilder.parse("agg_name", context); @@ -116,7 +113,7 @@ public class FiltersTests extends BaseAggregationTestCase<FiltersAggregationBuil builder.field("other_bucket", false); builder.field("other_bucket_key", "some_key"); builder.endObject(); - parser = XContentHelper.createParser(shuffleXContent(builder).bytes()); + parser = createParser(shuffleXContent(builder)); parser.nextToken(); context = new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.STRICT); filters = FiltersAggregationBuilder.parse("agg_name", context); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsTests.java index 98e2833903..e076472a13 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsTests.java @@ -21,6 +21,7 @@ package org.elasticsearch.search.aggregations.metrics; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.AbstractQueryTestCase; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.script.Script; @@ -186,7 +187,7 @@ public class TopHitsTests extends BaseAggregationTestCase<TopHitsAggregationBuil " }\n" + "}"; try { - XContentParser parser = XContentFactory.xContent(source).createParser(source); + XContentParser parser = createParser(JsonXContent.jsonXContent, source); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); aggParsers.parseAggregators(parseContext); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/ExtendedStatsBucketTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/ExtendedStatsBucketTests.java index 390501d200..670ab0dabd 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/ExtendedStatsBucketTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/ExtendedStatsBucketTests.java @@ -21,6 +21,7 @@ package org.elasticsearch.search.aggregations.pipeline.bucketmetrics; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended.ExtendedStatsBucketPipelineAggregationBuilder; @@ -45,7 +46,7 @@ public class ExtendedStatsBucketTests extends AbstractBucketMetricsTestCase<Exte .endObject() .string(); - XContentParser parser = XContentFactory.xContent(content).createParser(content); + XContentParser parser = createParser(JsonXContent.jsonXContent, content); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); parser.nextToken(); // skip object start diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/PercentilesBucketTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/PercentilesBucketTests.java index 71bf937fbb..26ee43e7a1 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/PercentilesBucketTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/PercentilesBucketTests.java @@ -21,6 +21,7 @@ package org.elasticsearch.search.aggregations.pipeline.bucketmetrics; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile.PercentilesBucketPipelineAggregationBuilder; @@ -50,7 +51,7 @@ public class PercentilesBucketTests extends AbstractBucketMetricsTestCase<Percen .endObject() .string(); - XContentParser parser = XContentFactory.xContent(content).createParser(content); + XContentParser parser = createParser(JsonXContent.jsonXContent, content); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); parser.nextToken(); // skip object start diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/moving/avg/MovAvgTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/moving/avg/MovAvgTests.java index e34a56c496..7b29ae213f 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/moving/avg/MovAvgTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/moving/avg/MovAvgTests.java @@ -20,8 +20,8 @@ package org.elasticsearch.search.aggregations.pipeline.moving.avg; import org.elasticsearch.common.ParseFieldMatcher; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.search.aggregations.BasePipelineAggregationTestCase; import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; @@ -106,7 +106,7 @@ public class MovAvgTests extends BasePipelineAggregationTestCase<MovAvgPipelineA " }" + " }" + "}"; - XContentParser parser = XContentFactory.xContent(json).createParser(json); + XContentParser parser = createParser(JsonXContent.jsonXContent, json); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); assertSame(XContentParser.Token.FIELD_NAME, parser.nextToken()); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/support/IncludeExcludeTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/support/IncludeExcludeTests.java index 8929417c26..20f38297d9 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/support/IncludeExcludeTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/support/IncludeExcludeTests.java @@ -24,6 +24,15 @@ import org.apache.lucene.index.RandomAccessOrds; import org.apache.lucene.index.SortedSetDocValues; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.LongBitSet; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParseFieldMatcher; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.index.query.QueryParseContext; +import org.elasticsearch.indices.query.IndicesQueriesRegistry; import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude; import org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude.OrdinalsFilter; @@ -34,6 +43,9 @@ import java.util.Collections; import java.util.TreeSet; public class IncludeExcludeTests extends ESTestCase { + + private final ParseFieldMatcher parseFieldMatcher = ParseFieldMatcher.STRICT; + private final IndicesQueriesRegistry queriesRegistry = new IndicesQueriesRegistry(); public void testEmptyTermsWithOrds() throws IOException { IncludeExclude inexcl = new IncludeExclude( @@ -125,13 +137,168 @@ public class IncludeExcludeTests extends ESTestCase { assertFalse(acceptedOrds.get(0)); } - public void testEquals() { - assertEquals(new IncludeExclude(3, 20), new IncludeExclude(3, 20)); - assertEquals(new IncludeExclude(3, 20).hashCode(), new IncludeExclude(3, 20).hashCode()); - assertFalse(new IncludeExclude(3, 20).equals(new IncludeExclude(4, 20))); - assertTrue(new IncludeExclude(3, 20).hashCode() != new IncludeExclude(4, 20).hashCode()); - assertFalse(new IncludeExclude(3, 20).equals(new IncludeExclude(3, 21))); - assertTrue(new IncludeExclude(3, 20).hashCode() != new IncludeExclude(3, 21).hashCode()); + public void testPartitionedEquals() throws IOException { + IncludeExclude serialized = serialize(new IncludeExclude(3, 20), IncludeExclude.INCLUDE_FIELD); + assertFalse(serialized.isRegexBased()); + assertTrue(serialized.isPartitionBased()); + + IncludeExclude same = new IncludeExclude(3, 20); + assertEquals(serialized, same); + assertEquals(serialized.hashCode(), same.hashCode()); + + IncludeExclude differentParam1 = new IncludeExclude(4, 20); + assertFalse(serialized.equals(differentParam1)); + assertTrue(serialized.hashCode() != differentParam1.hashCode()); + + IncludeExclude differentParam2 = new IncludeExclude(3, 21); + assertFalse(serialized.equals(differentParam2)); + assertTrue(serialized.hashCode() != differentParam2.hashCode()); + } + + public void testExactIncludeValuesEquals() throws IOException { + String[] incValues = { "a", "b" }; + String[] differentIncValues = { "a", "c" }; + IncludeExclude serialized = serialize(new IncludeExclude(incValues, null), IncludeExclude.INCLUDE_FIELD); + assertFalse(serialized.isPartitionBased()); + assertFalse(serialized.isRegexBased()); + + IncludeExclude same = new IncludeExclude(incValues, null); + assertEquals(serialized, same); + assertEquals(serialized.hashCode(), same.hashCode()); + + IncludeExclude different = new IncludeExclude(differentIncValues, null); + assertFalse(serialized.equals(different)); + assertTrue(serialized.hashCode() != different.hashCode()); + } + + public void testExactExcludeValuesEquals() throws IOException { + String[] excValues = { "a", "b" }; + String[] differentExcValues = { "a", "c" }; + IncludeExclude serialized = serialize(new IncludeExclude(null, excValues), IncludeExclude.EXCLUDE_FIELD); + assertFalse(serialized.isPartitionBased()); + assertFalse(serialized.isRegexBased()); + + IncludeExclude same = new IncludeExclude(null, excValues); + assertEquals(serialized, same); + assertEquals(serialized.hashCode(), same.hashCode()); + + IncludeExclude different = new IncludeExclude(null, differentExcValues); + assertFalse(serialized.equals(different)); + assertTrue(serialized.hashCode() != different.hashCode()); + } + + public void testRegexInclude() throws IOException { + String incRegex = "foo.*"; + String differentRegex = "bar.*"; + IncludeExclude serialized = serialize(new IncludeExclude(incRegex, null), IncludeExclude.INCLUDE_FIELD); + assertFalse(serialized.isPartitionBased()); + assertTrue(serialized.isRegexBased()); + + IncludeExclude same = new IncludeExclude(incRegex, null); + assertEquals(serialized, same); + assertEquals(serialized.hashCode(), same.hashCode()); + + IncludeExclude different = new IncludeExclude(differentRegex, null); + assertFalse(serialized.equals(different)); + assertTrue(serialized.hashCode() != different.hashCode()); + } + + public void testRegexExclude() throws IOException { + String excRegex = "foo.*"; + String differentRegex = "bar.*"; + IncludeExclude serialized = serialize(new IncludeExclude(null, excRegex), IncludeExclude.EXCLUDE_FIELD); + assertFalse(serialized.isPartitionBased()); + assertTrue(serialized.isRegexBased()); + + IncludeExclude same = new IncludeExclude(null, excRegex); + assertEquals(serialized, same); + assertEquals(serialized.hashCode(), same.hashCode()); + + IncludeExclude different = new IncludeExclude(null, differentRegex); + assertFalse(serialized.equals(different)); + assertTrue(serialized.hashCode() != different.hashCode()); + } + + // Serializes/deserializes an IncludeExclude statement with a single clause + private IncludeExclude serialize(IncludeExclude incExc, ParseField field) throws IOException { + XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values())); + if (randomBoolean()) { + builder.prettyPrint(); + } + builder.startObject(); + incExc.toXContent(builder, ToXContent.EMPTY_PARAMS); + builder.endObject(); + + XContentParser parser = createParser(builder); + XContentParser.Token token = parser.nextToken(); + assertEquals(token, XContentParser.Token.START_OBJECT); + token = parser.nextToken(); + assertEquals(token, XContentParser.Token.FIELD_NAME); + assertEquals(field.getPreferredName(), parser.currentName()); + token = parser.nextToken(); + + QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, ParseFieldMatcher.STRICT); + if (field.getPreferredName().equalsIgnoreCase("include")) { + return IncludeExclude.parseInclude(parser, parseContext); + } else if (field.getPreferredName().equalsIgnoreCase("exclude")) { + return IncludeExclude.parseExclude(parser, parseContext); + } else { + throw new IllegalArgumentException( + "Unexpected field name serialized in test: " + field.getPreferredName()); + } + } + + public void testRegexIncludeAndExclude() throws IOException { + String incRegex = "foo.*"; + String excRegex = "football"; + String differentExcRegex = "foosball"; + IncludeExclude serialized = serializeMixedRegex(new IncludeExclude(incRegex, excRegex)); + assertFalse(serialized.isPartitionBased()); + assertTrue(serialized.isRegexBased()); + + IncludeExclude same = new IncludeExclude(incRegex, excRegex); + assertEquals(serialized, same); + assertEquals(serialized.hashCode(), same.hashCode()); + + IncludeExclude different = new IncludeExclude(incRegex, differentExcRegex); + assertFalse(serialized.equals(different)); + assertTrue(serialized.hashCode() != different.hashCode()); + } + + // Serializes/deserializes the IncludeExclude statement with include AND + // exclude clauses + private IncludeExclude serializeMixedRegex(IncludeExclude incExc) throws IOException { + XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values())); + if (randomBoolean()) { + builder.prettyPrint(); + } + builder.startObject(); + incExc.toXContent(builder, ToXContent.EMPTY_PARAMS); + builder.endObject(); + + XContentParser parser = createParser(builder); + QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); + XContentParser.Token token = parser.nextToken(); + assertEquals(token, XContentParser.Token.START_OBJECT); + + IncludeExclude inc = null; + IncludeExclude exc = null; + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { + assertEquals(XContentParser.Token.FIELD_NAME, token); + if (parseFieldMatcher.match(parser.currentName(), IncludeExclude.INCLUDE_FIELD)) { + token = parser.nextToken(); + inc = IncludeExclude.parseInclude(parser, parseContext); + } else if (parseFieldMatcher.match(parser.currentName(), IncludeExclude.EXCLUDE_FIELD)) { + token = parser.nextToken(); + exc = IncludeExclude.parseExclude(parser, parseContext); + } else { + throw new IllegalArgumentException("Unexpected field name serialized in test: " + parser.currentName()); + } + } + assertNotNull(inc); + assertNotNull(exc); + // Include and Exclude clauses are parsed independently and then merged + return IncludeExclude.merge(inc, exc); } } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/support/ValueTypeTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/support/ValueTypeTests.java new file mode 100644 index 0000000000..84995277fb --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/support/ValueTypeTests.java @@ -0,0 +1,49 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.support; + +import org.elasticsearch.test.ESTestCase; + +public class ValueTypeTests extends ESTestCase { + + public void testResolve() { + assertEquals(ValueType.STRING, ValueType.resolveForScript("string")); + assertEquals(ValueType.DOUBLE, ValueType.resolveForScript("float")); + assertEquals(ValueType.DOUBLE, ValueType.resolveForScript("double")); + assertEquals(ValueType.LONG, ValueType.resolveForScript("byte")); + assertEquals(ValueType.LONG, ValueType.resolveForScript("short")); + assertEquals(ValueType.LONG, ValueType.resolveForScript("integer")); + assertEquals(ValueType.LONG, ValueType.resolveForScript("long")); + assertEquals(ValueType.DATE, ValueType.resolveForScript("date")); + assertEquals(ValueType.IP, ValueType.resolveForScript("ip")); + assertEquals(ValueType.BOOLEAN, ValueType.resolveForScript("boolean")); + } + + public void testCompatibility() { + assertTrue(ValueType.DOUBLE.isA(ValueType.NUMERIC)); + assertTrue(ValueType.LONG.isA(ValueType.NUMERIC)); + assertTrue(ValueType.DATE.isA(ValueType.LONG)); + assertTrue(ValueType.NUMERIC.isA(ValueType.NUMBER)); + assertTrue(ValueType.BOOLEAN.isA(ValueType.NUMBER)); + assertFalse(ValueType.STRING.isA(ValueType.NUMBER)); + assertFalse(ValueType.DATE.isA(ValueType.IP)); + } + +} diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceConfigTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceConfigTests.java new file mode 100644 index 0000000000..5f8c658f23 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceConfigTests.java @@ -0,0 +1,264 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.support; + +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.SortedNumericDocValues; +import org.apache.lucene.util.BytesRef; +import org.elasticsearch.action.support.WriteRequest; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.IndexService; +import org.elasticsearch.index.engine.Engine.Searcher; +import org.elasticsearch.index.fielddata.SortedBinaryDocValues; +import org.elasticsearch.index.query.QueryShardContext; +import org.elasticsearch.test.ESSingleNodeTestCase; + +public class ValuesSourceConfigTests extends ESSingleNodeTestCase { + + public void testKeyword() throws Exception { + IndexService indexService = createIndex("index", Settings.EMPTY, "type", + "bytes", "type=keyword"); + client().prepareIndex("index", "type", "1") + .setSource("bytes", "abc") + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); + + try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { + QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L); + + ValuesSourceConfig<ValuesSource.Bytes> config = ValuesSourceConfig.resolve( + context, null, "bytes", null, null, null, null); + ValuesSource.Bytes valuesSource = config.toValuesSource(context); + LeafReaderContext ctx = searcher.reader().leaves().get(0); + SortedBinaryDocValues values = valuesSource.bytesValues(ctx); + values.setDocument(0); + assertEquals(1, values.count()); + assertEquals(new BytesRef("abc"), values.valueAt(0)); + } + } + + public void testEmptyKeyword() throws Exception { + IndexService indexService = createIndex("index", Settings.EMPTY, "type", + "bytes", "type=keyword"); + client().prepareIndex("index", "type", "1") + .setSource() + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); + + try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { + QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L); + + ValuesSourceConfig<ValuesSource.Bytes> config = ValuesSourceConfig.resolve( + context, null, "bytes", null, null, null, null); + ValuesSource.Bytes valuesSource = config.toValuesSource(context); + LeafReaderContext ctx = searcher.reader().leaves().get(0); + SortedBinaryDocValues values = valuesSource.bytesValues(ctx); + values.setDocument(0); + assertEquals(0, values.count()); + + config = ValuesSourceConfig.resolve( + context, null, "bytes", null, "abc", null, null); + valuesSource = config.toValuesSource(context); + values = valuesSource.bytesValues(ctx); + values.setDocument(0); + assertEquals(1, values.count()); + assertEquals(new BytesRef("abc"), values.valueAt(0)); + } + } + + public void testUnmappedKeyword() throws Exception { + IndexService indexService = createIndex("index", Settings.EMPTY, "type"); + client().prepareIndex("index", "type", "1") + .setSource() + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); + + try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { + QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L); + ValuesSourceConfig<ValuesSource.Bytes> config = ValuesSourceConfig.resolve( + context, ValueType.STRING, "bytes", null, null, null, null); + ValuesSource.Bytes valuesSource = config.toValuesSource(context); + assertNull(valuesSource); + + config = ValuesSourceConfig.resolve( + context, ValueType.STRING, "bytes", null, "abc", null, null); + valuesSource = config.toValuesSource(context); + LeafReaderContext ctx = searcher.reader().leaves().get(0); + SortedBinaryDocValues values = valuesSource.bytesValues(ctx); + values.setDocument(0); + assertEquals(1, values.count()); + assertEquals(new BytesRef("abc"), values.valueAt(0)); + } + } + + public void testLong() throws Exception { + IndexService indexService = createIndex("index", Settings.EMPTY, "type", + "long", "type=long"); + client().prepareIndex("index", "type", "1") + .setSource("long", 42) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); + + try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { + QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L); + + ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve( + context, null, "long", null, null, null, null); + ValuesSource.Numeric valuesSource = config.toValuesSource(context); + LeafReaderContext ctx = searcher.reader().leaves().get(0); + SortedNumericDocValues values = valuesSource.longValues(ctx); + values.setDocument(0); + assertEquals(1, values.count()); + assertEquals(42, values.valueAt(0)); + } + } + + public void testEmptyLong() throws Exception { + IndexService indexService = createIndex("index", Settings.EMPTY, "type", + "long", "type=long"); + client().prepareIndex("index", "type", "1") + .setSource() + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); + + try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { + QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L); + + ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve( + context, null, "long", null, null, null, null); + ValuesSource.Numeric valuesSource = config.toValuesSource(context); + LeafReaderContext ctx = searcher.reader().leaves().get(0); + SortedNumericDocValues values = valuesSource.longValues(ctx); + values.setDocument(0); + assertEquals(0, values.count()); + + config = ValuesSourceConfig.resolve( + context, null, "long", null, 42, null, null); + valuesSource = config.toValuesSource(context); + values = valuesSource.longValues(ctx); + values.setDocument(0); + assertEquals(1, values.count()); + assertEquals(42, values.valueAt(0)); + } + } + + public void testUnmappedLong() throws Exception { + IndexService indexService = createIndex("index", Settings.EMPTY, "type"); + client().prepareIndex("index", "type", "1") + .setSource() + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); + + try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { + QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L); + + ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve( + context, ValueType.NUMBER, "long", null, null, null, null); + ValuesSource.Numeric valuesSource = config.toValuesSource(context); + assertNull(valuesSource); + + config = ValuesSourceConfig.resolve( + context, ValueType.NUMBER, "long", null, 42, null, null); + valuesSource = config.toValuesSource(context); + LeafReaderContext ctx = searcher.reader().leaves().get(0); + SortedNumericDocValues values = valuesSource.longValues(ctx); + values.setDocument(0); + assertEquals(1, values.count()); + assertEquals(42, values.valueAt(0)); + } + } + + public void testBoolean() throws Exception { + IndexService indexService = createIndex("index", Settings.EMPTY, "type", + "bool", "type=boolean"); + client().prepareIndex("index", "type", "1") + .setSource("bool", true) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); + + try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { + QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L); + + ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve( + context, null, "bool", null, null, null, null); + ValuesSource.Numeric valuesSource = config.toValuesSource(context); + LeafReaderContext ctx = searcher.reader().leaves().get(0); + SortedNumericDocValues values = valuesSource.longValues(ctx); + values.setDocument(0); + assertEquals(1, values.count()); + assertEquals(1, values.valueAt(0)); + } + } + + public void testEmptyBoolean() throws Exception { + IndexService indexService = createIndex("index", Settings.EMPTY, "type", + "bool", "type=boolean"); + client().prepareIndex("index", "type", "1") + .setSource() + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); + + try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { + QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L); + + ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve( + context, null, "bool", null, null, null, null); + ValuesSource.Numeric valuesSource = config.toValuesSource(context); + LeafReaderContext ctx = searcher.reader().leaves().get(0); + SortedNumericDocValues values = valuesSource.longValues(ctx); + values.setDocument(0); + assertEquals(0, values.count()); + + config = ValuesSourceConfig.resolve( + context, null, "bool", null, true, null, null); + valuesSource = config.toValuesSource(context); + values = valuesSource.longValues(ctx); + values.setDocument(0); + assertEquals(1, values.count()); + assertEquals(1, values.valueAt(0)); + } + } + + public void testUnmappedBoolean() throws Exception { + IndexService indexService = createIndex("index", Settings.EMPTY, "type"); + client().prepareIndex("index", "type", "1") + .setSource() + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); + + try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { + QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L); + + ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve( + context, ValueType.BOOLEAN, "bool", null, null, null, null); + ValuesSource.Numeric valuesSource = config.toValuesSource(context); + assertNull(valuesSource); + + config = ValuesSourceConfig.resolve( + context, ValueType.BOOLEAN, "bool", null, true, null, null); + valuesSource = config.toValuesSource(context); + LeafReaderContext ctx = searcher.reader().leaves().get(0); + SortedNumericDocValues values = valuesSource.longValues(ctx); + values.setDocument(0); + assertEquals(1, values.count()); + assertEquals(1, values.valueAt(0)); + } + } +} diff --git a/core/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java b/core/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java index 7e0d47dfcd..8ec873704d 100644 --- a/core/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java @@ -22,7 +22,6 @@ package org.elasticsearch.search.builder; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.ParsingException; -import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput; @@ -31,10 +30,13 @@ 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.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryParseContext; +import org.elasticsearch.index.query.RandomQueryBuilder; import org.elasticsearch.search.AbstractSearchTestCase; import org.elasticsearch.search.rescore.QueryRescorerBuilder; import org.elasticsearch.search.sort.FieldSortBuilder; @@ -44,6 +46,7 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.io.IOException; +import java.util.Map; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -58,16 +61,15 @@ public class SearchSourceBuilderTests extends AbstractSearchTestCase { builder.prettyPrint(); } testSearchSourceBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertParseSearchSource(testSearchSourceBuilder, builder.bytes()); + assertParseSearchSource(testSearchSourceBuilder, createParser(builder)); } - private void assertParseSearchSource(SearchSourceBuilder testBuilder, BytesReference searchSourceAsBytes) throws IOException { - assertParseSearchSource(testBuilder, searchSourceAsBytes, ParseFieldMatcher.STRICT); + private void assertParseSearchSource(SearchSourceBuilder testBuilder, XContentParser parser) throws IOException { + assertParseSearchSource(testBuilder, parser, ParseFieldMatcher.STRICT); } - private void assertParseSearchSource(SearchSourceBuilder testBuilder, BytesReference searchSourceAsBytes, ParseFieldMatcher pfm) + private void assertParseSearchSource(SearchSourceBuilder testBuilder, XContentParser parser, ParseFieldMatcher pfm) throws IOException { - XContentParser parser = XContentFactory.xContent(searchSourceAsBytes).createParser(searchSourceAsBytes); QueryParseContext parseContext = new QueryParseContext(searchRequestParsers.queryParsers, parser, pfm); if (randomBoolean()) { parser.nextToken(); // sometimes we move it on the START_OBJECT to @@ -110,7 +112,7 @@ public class SearchSourceBuilderTests extends AbstractSearchTestCase { public void testParseIncludeExclude() throws IOException { { String restContent = " { \"_source\": { \"includes\": \"include\", \"excludes\": \"*.field2\"}}"; - try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(createParseContext(parser), searchRequestParsers.aggParsers, searchRequestParsers.suggesters, searchRequestParsers.searchExtParsers); assertArrayEquals(new String[]{"*.field2"}, searchSourceBuilder.fetchSource().excludes()); @@ -119,7 +121,7 @@ public class SearchSourceBuilderTests extends AbstractSearchTestCase { } { String restContent = " { \"_source\": false}"; - try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(createParseContext(parser), searchRequestParsers.aggParsers, searchRequestParsers.suggesters, searchRequestParsers.searchExtParsers); assertArrayEquals(new String[]{}, searchSourceBuilder.fetchSource().excludes()); @@ -142,7 +144,7 @@ public class SearchSourceBuilderTests extends AbstractSearchTestCase { " }\n" + " }\n" + " } }"; - try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { ParsingException e = expectThrows(ParsingException.class, () -> SearchSourceBuilder.fromXContent(createParseContext(parser), searchRequestParsers.aggParsers, searchRequestParsers.suggesters, searchRequestParsers.searchExtParsers)); assertEquals("[multi_match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]", e.getMessage()); @@ -152,7 +154,7 @@ public class SearchSourceBuilderTests extends AbstractSearchTestCase { public void testParseSort() throws IOException { { String restContent = " { \"sort\": \"foo\"}"; - try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(createParseContext(parser), searchRequestParsers.aggParsers, searchRequestParsers.suggesters, searchRequestParsers.searchExtParsers); assertEquals(1, searchSourceBuilder.sorts().size()); @@ -168,7 +170,7 @@ public class SearchSourceBuilderTests extends AbstractSearchTestCase { " { \"age\" : \"desc\" },\n" + " \"_score\"\n" + " ]}"; - try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(createParseContext(parser), searchRequestParsers.aggParsers, searchRequestParsers.suggesters, searchRequestParsers.searchExtParsers); assertEquals(5, searchSourceBuilder.sorts().size()); @@ -192,7 +194,7 @@ public class SearchSourceBuilderTests extends AbstractSearchTestCase { " }\n" + " }\n" + "}\n"; - try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(createParseContext(parser), searchRequestParsers.aggParsers, searchRequestParsers.suggesters, searchRequestParsers.searchExtParsers); assertEquals(1, searchSourceBuilder.aggregations().count()); @@ -208,7 +210,7 @@ public class SearchSourceBuilderTests extends AbstractSearchTestCase { " }\n" + " }\n" + "}\n"; - try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(createParseContext(parser), searchRequestParsers.aggParsers, searchRequestParsers.suggesters, searchRequestParsers.searchExtParsers); assertEquals(1, searchSourceBuilder.aggregations().count()); @@ -234,7 +236,7 @@ public class SearchSourceBuilderTests extends AbstractSearchTestCase { " }\n" + " }\n" + "}\n"; - try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(createParseContext(parser), searchRequestParsers.aggParsers, searchRequestParsers.suggesters, searchRequestParsers.searchExtParsers); assertEquals(1, searchSourceBuilder.rescores().size()); @@ -257,7 +259,7 @@ public class SearchSourceBuilderTests extends AbstractSearchTestCase { " }\n" + " } ]\n" + "}\n"; - try (XContentParser parser = XContentFactory.xContent(restContent).createParser(restContent)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(createParseContext(parser), searchRequestParsers.aggParsers, searchRequestParsers.suggesters, searchRequestParsers.searchExtParsers); assertEquals(1, searchSourceBuilder.rescores().size()); @@ -270,7 +272,7 @@ public class SearchSourceBuilderTests extends AbstractSearchTestCase { public void testTimeoutWithUnits() throws IOException { final String timeout = randomTimeValue(); final String query = "{ \"query\": { \"match_all\": {}}, \"timeout\": \"" + timeout + "\"}"; - try (XContentParser parser = XContentFactory.xContent(query).createParser(query)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, query)) { final SearchSourceBuilder builder = SearchSourceBuilder.fromXContent(createParseContext(parser), searchRequestParsers.aggParsers, searchRequestParsers.suggesters, searchRequestParsers.searchExtParsers); assertThat(builder.timeout(), equalTo(TimeValue.parseTimeValue(timeout, null, "timeout"))); @@ -280,7 +282,7 @@ public class SearchSourceBuilderTests extends AbstractSearchTestCase { public void testTimeoutWithoutUnits() throws IOException { final int timeout = randomIntBetween(1, 1024); final String query = "{ \"query\": { \"match_all\": {}}, \"timeout\": \"" + timeout + "\"}"; - try (XContentParser parser = XContentFactory.xContent(query).createParser(query)) { + try (XContentParser parser = createParser(JsonXContent.jsonXContent, query)) { final ElasticsearchParseException e = expectThrows( ElasticsearchParseException.class, @@ -290,16 +292,27 @@ public class SearchSourceBuilderTests extends AbstractSearchTestCase { } } - public void testEmptyPostFilter() throws IOException { - SearchSourceBuilder builder = new SearchSourceBuilder(); - String query = "{ \"post_filter\": {} }"; - assertParseSearchSource(builder, new BytesArray(query), ParseFieldMatcher.EMPTY); - } - - public void testEmptyQuery() throws IOException { - SearchSourceBuilder builder = new SearchSourceBuilder(); - String query = "{ \"query\": {} }"; - assertParseSearchSource(builder, new BytesArray(query), ParseFieldMatcher.EMPTY); + public void testToXContent() throws IOException { + //verify that only what is set gets printed out through toXContent + XContentType xContentType = randomFrom(XContentType.values()); + { + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); + XContentBuilder builder = XContentFactory.contentBuilder(xContentType); + searchSourceBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS); + BytesReference source = builder.bytes(); + Map<String, Object> sourceAsMap = XContentHelper.convertToMap(source, false).v2(); + assertEquals(0, sourceAsMap.size()); + } + { + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); + searchSourceBuilder.query(RandomQueryBuilder.createQuery(random())); + XContentBuilder builder = XContentFactory.contentBuilder(xContentType); + searchSourceBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS); + BytesReference source = builder.bytes(); + Map<String, Object> sourceAsMap = XContentHelper.convertToMap(source, false).v2(); + assertEquals(1, sourceAsMap.size()); + assertEquals("query", sourceAsMap.keySet().iterator().next()); + } } public void testParseIndicesBoost() throws IOException { diff --git a/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java b/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java index 6fe98a2a3e..07a70c91ef 100644 --- a/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java @@ -31,9 +31,9 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.mapper.ContentPath; @@ -128,7 +128,7 @@ public class HighlightBuilderTests extends ESTestCase { highlightBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS); XContentBuilder shuffled = shuffleXContent(builder); - XContentParser parser = XContentHelper.createParser(shuffled.bytes()); + XContentParser parser = createParser(shuffled); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.EMPTY); parser.nextToken(); HighlightBuilder secondHighlightBuilder; @@ -168,8 +168,8 @@ public class HighlightBuilderTests extends ESTestCase { } } - private static <T extends Throwable> T expectParseThrows(Class<T> exceptionClass, String highlightElement) throws IOException { - XContentParser parser = XContentFactory.xContent(highlightElement).createParser(highlightElement); + private <T extends Throwable> T expectParseThrows(Class<T> exceptionClass, String highlightElement) throws IOException { + XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.STRICT); return expectThrows(exceptionClass, () -> HighlightBuilder.fromXContent(context)); } @@ -266,7 +266,7 @@ public class HighlightBuilderTests extends ESTestCase { IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(index, indexSettings); // shard context will only need indicesQueriesRegistry for building Query objects nested in highlighter QueryShardContext mockShardContext = new QueryShardContext(0, idxSettings, null, null, null, null, null, indicesQueriesRegistry, - null, null, null, System::currentTimeMillis) { + null, null, System::currentTimeMillis) { @Override public MappedFieldType fieldMapper(String name) { TextFieldMapper.Builder builder = new TextFieldMapper.Builder(name); @@ -378,7 +378,7 @@ public class HighlightBuilderTests extends ESTestCase { String highlightElement = "{\n" + " \"tags_schema\" : \"styled\"\n" + "}\n"; - XContentParser parser = XContentFactory.xContent(highlightElement).createParser(highlightElement); + XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.EMPTY); HighlightBuilder highlightBuilder = HighlightBuilder.fromXContent(context); @@ -390,7 +390,7 @@ public class HighlightBuilderTests extends ESTestCase { highlightElement = "{\n" + " \"tags_schema\" : \"default\"\n" + "}\n"; - parser = XContentFactory.xContent(highlightElement).createParser(highlightElement); + parser = createParser(JsonXContent.jsonXContent, highlightElement); context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.EMPTY); highlightBuilder = HighlightBuilder.fromXContent(context); @@ -411,21 +411,21 @@ public class HighlightBuilderTests extends ESTestCase { */ public void testParsingEmptyStructure() throws IOException { String highlightElement = "{ }"; - XContentParser parser = XContentFactory.xContent(highlightElement).createParser(highlightElement); + XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.EMPTY); HighlightBuilder highlightBuilder = HighlightBuilder.fromXContent(context); assertEquals("expected plain HighlightBuilder", new HighlightBuilder(), highlightBuilder); highlightElement = "{ \"fields\" : { } }"; - parser = XContentFactory.xContent(highlightElement).createParser(highlightElement); + parser = createParser(JsonXContent.jsonXContent, highlightElement); context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.EMPTY); highlightBuilder = HighlightBuilder.fromXContent(context); assertEquals("defining no field should return plain HighlightBuilder", new HighlightBuilder(), highlightBuilder); highlightElement = "{ \"fields\" : { \"foo\" : { } } }"; - parser = XContentFactory.xContent(highlightElement).createParser(highlightElement); + parser = createParser(JsonXContent.jsonXContent, highlightElement); context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.EMPTY); highlightBuilder = HighlightBuilder.fromXContent(context); diff --git a/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightFieldTests.java b/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightFieldTests.java index f5ec3ddbb1..6580be5255 100644 --- a/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightFieldTests.java +++ b/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightFieldTests.java @@ -61,7 +61,7 @@ public class HighlightFieldTests extends ESTestCase { builder.startObject(); // we need to wrap xContent output in proper object to create a parser for it builder = highlightField.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - XContentParser parser = xcontentType.xContent().createParser(builder.bytes()); + XContentParser parser = createParser(builder); parser.nextToken(); // skip to the opening object token, fromXContent advances from here and starts with the field name HighlightField parsedField = HighlightField.fromXContent(parser); assertEquals(highlightField, parsedField); diff --git a/core/src/test/java/org/elasticsearch/search/internal/InternalNestedIdentityTests.java b/core/src/test/java/org/elasticsearch/search/internal/InternalNestedIdentityTests.java index 47421dec6c..0621b57443 100644 --- a/core/src/test/java/org/elasticsearch/search/internal/InternalNestedIdentityTests.java +++ b/core/src/test/java/org/elasticsearch/search/internal/InternalNestedIdentityTests.java @@ -57,7 +57,7 @@ public class InternalNestedIdentityTests extends ESTestCase { builder.prettyPrint(); } builder = nestedIdentity.innerToXContent(builder, ToXContent.EMPTY_PARAMS); - XContentParser parser = xcontentType.xContent().createParser(builder.bytes()); + XContentParser parser = createParser(builder); InternalNestedIdentity parsedNestedIdentity = InternalNestedIdentity.fromXContent(parser); assertEquals(nestedIdentity, parsedNestedIdentity); assertNull(parser.nextToken()); diff --git a/core/src/test/java/org/elasticsearch/search/internal/ShardSearchTransportRequestTests.java b/core/src/test/java/org/elasticsearch/search/internal/ShardSearchTransportRequestTests.java index 2eeb0a1813..be7e69ee76 100644 --- a/core/src/test/java/org/elasticsearch/search/internal/ShardSearchTransportRequestTests.java +++ b/core/src/test/java/org/elasticsearch/search/internal/ShardSearchTransportRequestTests.java @@ -198,8 +198,7 @@ public class ShardSearchTransportRequestTests extends AbstractSearchTestCase { IndexSettings indexSettings = new IndexSettings(indexMetadata.build(), Settings.EMPTY); final long nowInMillis = randomPositiveLong(); QueryShardContext context = new QueryShardContext( - 0, indexSettings, null, null, null, null, null, queriesRegistry, null, null, null, - () -> nowInMillis); + 0, indexSettings, null, null, null, null, null, queriesRegistry, null, null, () -> nowInMillis); readRequest.rewrite(context); QueryBuilder queryBuilder = readRequest.filteringAliases(); assertEquals(queryBuilder, QueryBuilders.boolQuery() diff --git a/core/src/test/java/org/elasticsearch/search/nested/SimpleNestedIT.java b/core/src/test/java/org/elasticsearch/search/nested/SimpleNestedIT.java index 442274d71e..73b512ddf9 100644 --- a/core/src/test/java/org/elasticsearch/search/nested/SimpleNestedIT.java +++ b/core/src/test/java/org/elasticsearch/search/nested/SimpleNestedIT.java @@ -506,120 +506,150 @@ public class SimpleNestedIT extends ESIntegTestCase { public void testSortNestedWithNestedFilter() throws Exception { assertAcked(prepareCreate("test") - .addMapping("type1", XContentFactory.jsonBuilder().startObject() + .addMapping("type1", XContentFactory.jsonBuilder() + .startObject() .startObject("type1") - .startObject("properties") - .startObject("grand_parent_values").field("type", "long").endObject() - .startObject("parent").field("type", "nested") - .startObject("properties") - .startObject("parent_values").field("type", "long").endObject() - .startObject("child").field("type", "nested") - .startObject("properties") - .startObject("child_values").field("type", "long").endObject() - .endObject() - .endObject() - .endObject() - .endObject() - .endObject() + .startObject("properties") + .startObject("grand_parent_values") + .field("type", "long") + .endObject() + .startObject("parent") + .field("type", "nested") + .startObject("properties") + .startObject("parent_values") + .field("type", "long") + .endObject() + .startObject("child") + .field("type", "nested") + .startObject("properties") + .startObject("child_values") + .field("type", "long") + .endObject() + .endObject() + .endObject() + .endObject() + .endObject() + .endObject() .endObject() - .endObject())); + .endObject())); ensureGreen(); // sum: 11 - client().prepareIndex("test", "type1", Integer.toString(1)).setSource(jsonBuilder().startObject() + client().prepareIndex("test", "type1", Integer.toString(1)).setSource(jsonBuilder() + .startObject() .field("grand_parent_values", 1L) - .startObject("parent") - .field("filter", false) - .field("parent_values", 1L) - .startObject("child") - .field("filter", true) - .field("child_values", 1L) - .startObject("child_obj") - .field("value", 1L) - .endObject() - .endObject() - .startObject("child") - .field("filter", false) - .field("child_values", 6L) - .endObject() - .endObject() - .startObject("parent") - .field("filter", true) - .field("parent_values", 2L) - .startObject("child") - .field("filter", false) - .field("child_values", -1L) - .endObject() - .startObject("child") - .field("filter", false) - .field("child_values", 5L) - .endObject() - .endObject() - .endObject()).execute().actionGet(); + .startArray("parent") + .startObject() + .field("filter", false) + .field("parent_values", 1L) + .startArray("child") + .startObject() + .field("filter", true) + .field("child_values", 1L) + .startObject("child_obj") + .field("value", 1L) + .endObject() + .endObject() + .startObject() + .field("filter", false) + .field("child_values", 6L) + .endObject() + .endArray() + .endObject() + .startObject() + .field("filter", true) + .field("parent_values", 2L) + .startArray("child") + .startObject() + .field("filter", false) + .field("child_values", -1L) + .endObject() + .startObject() + .field("filter", false) + .field("child_values", 5L) + .endObject() + .endArray() + .endObject() + .endArray() + .endObject()).execute().actionGet(); // sum: 7 - client().prepareIndex("test", "type1", Integer.toString(2)).setSource(jsonBuilder().startObject() + client().prepareIndex("test", "type1", Integer.toString(2)).setSource(jsonBuilder() + .startObject() .field("grand_parent_values", 2L) - .startObject("parent") - .field("filter", false) - .field("parent_values", 2L) - .startObject("child") - .field("filter", true) - .field("child_values", 2L) - .startObject("child_obj") - .field("value", 2L) - .endObject() - .endObject() - .startObject("child") - .field("filter", false) - .field("child_values", 4L) - .endObject() - .endObject() - .startObject("parent") - .field("parent_values", 3L) - .field("filter", true) - .startObject("child") - .field("child_values", -2L) - .field("filter", false) - .endObject() - .startObject("child") - .field("filter", false) - .field("child_values", 3L) - .endObject() - .endObject() + .startArray("parent") + .startObject() + .field("filter", false) + .field("parent_values", 2L) + .startArray("child") + .startObject() + .field("filter", true) + .field("child_values", 2L) + .startObject("child_obj") + .field("value", 2L) + .endObject() + .endObject() + .startObject() + .field("filter", false) + .field("child_values", 4L) + .endObject() + .endArray() + .endObject() + .startObject() + .field("parent_values", 3L) + .field("filter", true) + .startArray("child") + .startObject() + .field("child_values", -2L) + .field("filter", false) + .endObject() + .startObject() + .field("filter", false) + .field("child_values", 3L) + .endObject() + .endArray() + .endObject() + .endArray() .endObject()).execute().actionGet(); // sum: 2 - client().prepareIndex("test", "type1", Integer.toString(3)).setSource(jsonBuilder().startObject() + client().prepareIndex("test", "type1", Integer.toString(3)).setSource(jsonBuilder() + .startObject() .field("grand_parent_values", 3L) - .startObject("parent") - .field("parent_values", 3L) - .field("filter", false) - .startObject("child") - .field("filter", true) - .field("child_values", 3L) - .startObject("child_obj") - .field("value", 3L) - .endObject() - .endObject() - .startObject("child") - .field("filter", false) - .field("child_values", 1L) - .endObject() - .endObject() - .startObject("parent") - .field("parent_values", 4L) - .field("filter", true) - .startObject("child") - .field("filter", false) - .field("child_values", -3L) - .endObject() - .startObject("child") - .field("filter", false) - .field("child_values", 1L) - .endObject() - .endObject() - .endObject()).execute().actionGet(); + .startArray("parent") + .startObject() + .field("parent_values", 3L) + .field("filter", false) + .startArray("child") + .startObject() + .field("filter", true) + .field("child_values", 3L) + .startObject("child_obj") + .field("value", 3L) + .endObject() + .endObject() + .startObject() + .field("filter", false) + .field("child_values", 1L) + .endObject() + .endArray() + .endObject() + .startObject() + .field("parent_values", 4L) + .field("filter", true) + .startArray("child") + .startObject() + .field("filter", false) + .field("child_values", -3L) + .endObject() + .startObject() + .field("filter", false) + .field("child_values", 1L) + .endObject() + .endArray() + .endObject() + .endArray() + .endObject()).execute().actionGet(); refresh(); // Without nested filter diff --git a/core/src/test/java/org/elasticsearch/search/profile/query/RandomQueryGenerator.java b/core/src/test/java/org/elasticsearch/search/profile/query/RandomQueryGenerator.java index d40f85aa04..14fe8d5813 100644 --- a/core/src/test/java/org/elasticsearch/search/profile/query/RandomQueryGenerator.java +++ b/core/src/test/java/org/elasticsearch/search/profile/query/RandomQueryGenerator.java @@ -195,7 +195,6 @@ public class RandomQueryGenerator { return q; } - @Deprecated private static QueryBuilder randomFuzzyQuery(List<String> fields) { QueryBuilder q = QueryBuilders.fuzzyQuery(randomField(fields), randomQueryString(1)); diff --git a/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java b/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java index 36e02da1f1..22f2dd5380 100644 --- a/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java @@ -30,9 +30,9 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.mapper.ContentPath; import org.elasticsearch.index.mapper.MappedFieldType; @@ -118,7 +118,7 @@ public class QueryRescoreBuilderTests extends ESTestCase { XContentBuilder shuffled = shuffleXContent(builder); - XContentParser parser = XContentHelper.createParser(shuffled.bytes()); + XContentParser parser = createParser(shuffled); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.STRICT); parser.nextToken(); RescoreBuilder<?> secondRescoreBuilder = RescoreBuilder.parseFromXContent(context); @@ -139,7 +139,7 @@ public class QueryRescoreBuilderTests extends ESTestCase { IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(randomAsciiOfLengthBetween(1, 10), indexSettings); // shard context will only need indicesQueriesRegistry for building Query objects nested in query rescorer QueryShardContext mockShardContext = new QueryShardContext(0, idxSettings, null, null, null, null, null, indicesQueriesRegistry, - null, null, null, () -> nowInMillis) { + null, null, () -> nowInMillis) { @Override public MappedFieldType fieldMapper(String name) { TextFieldMapper.Builder builder = new TextFieldMapper.Builder(name); @@ -250,8 +250,8 @@ public class QueryRescoreBuilderTests extends ESTestCase { /** * create a new parser from the rescorer string representation and reset context with it */ - private static QueryParseContext createContext(String rescoreElement) throws IOException { - XContentParser parser = XContentFactory.xContent(rescoreElement).createParser(rescoreElement); + private QueryParseContext createContext(String rescoreElement) throws IOException { + XContentParser parser = createParser(JsonXContent.jsonXContent, rescoreElement); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.STRICT); // move to first token, this is where the internal fromXContent assertTrue(parser.nextToken() == XContentParser.Token.START_OBJECT); diff --git a/core/src/test/java/org/elasticsearch/search/scroll/RestClearScrollActionTests.java b/core/src/test/java/org/elasticsearch/search/scroll/RestClearScrollActionTests.java index 1ce831ca2d..d386f73adf 100644 --- a/core/src/test/java/org/elasticsearch/search/scroll/RestClearScrollActionTests.java +++ b/core/src/test/java/org/elasticsearch/search/scroll/RestClearScrollActionTests.java @@ -23,7 +23,6 @@ import org.elasticsearch.action.search.ClearScrollRequest; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; @@ -38,10 +37,10 @@ import static org.mockito.Mockito.mock; public class RestClearScrollActionTests extends ESTestCase { public void testParseClearScrollRequest() throws Exception { - XContentParser content = XContentHelper.createParser(XContentFactory.jsonBuilder() + XContentParser content = createParser(XContentFactory.jsonBuilder() .startObject() .array("scroll_id", "value_1", "value_2") - .endObject().bytes()); + .endObject()); ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); RestClearScrollAction.buildFromContent(content, clearScrollRequest); assertThat(clearScrollRequest.scrollIds(), contains("value_1", "value_2")); @@ -55,11 +54,11 @@ public class RestClearScrollActionTests extends ESTestCase { } public void testParseClearScrollRequestWithUnknownParamThrowsException() throws Exception { - XContentParser invalidContent = XContentHelper.createParser(XContentFactory.jsonBuilder() + XContentParser invalidContent = createParser(XContentFactory.jsonBuilder() .startObject() .array("scroll_id", "value_1", "value_2") .field("unknown", "keyword") - .endObject().bytes()); + .endObject()); ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); Exception e = expectThrows(IllegalArgumentException.class, diff --git a/core/src/test/java/org/elasticsearch/search/scroll/RestSearchScrollActionTests.java b/core/src/test/java/org/elasticsearch/search/scroll/RestSearchScrollActionTests.java index a2c9096d59..f4bb364516 100644 --- a/core/src/test/java/org/elasticsearch/search/scroll/RestSearchScrollActionTests.java +++ b/core/src/test/java/org/elasticsearch/search/scroll/RestSearchScrollActionTests.java @@ -24,7 +24,6 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; @@ -38,11 +37,11 @@ import static org.mockito.Mockito.mock; public class RestSearchScrollActionTests extends ESTestCase { public void testParseSearchScrollRequest() throws Exception { - XContentParser content = XContentHelper.createParser(XContentFactory.jsonBuilder() + XContentParser content = createParser(XContentFactory.jsonBuilder() .startObject() .field("scroll_id", "SCROLL_ID") .field("scroll", "1m") - .endObject().bytes()); + .endObject()); SearchScrollRequest searchScrollRequest = new SearchScrollRequest(); RestSearchScrollAction.buildFromContent(content, searchScrollRequest); @@ -60,11 +59,11 @@ public class RestSearchScrollActionTests extends ESTestCase { public void testParseSearchScrollRequestWithUnknownParamThrowsException() throws Exception { SearchScrollRequest searchScrollRequest = new SearchScrollRequest(); - XContentParser invalidContent = XContentHelper.createParser(XContentFactory.jsonBuilder() + XContentParser invalidContent = createParser(XContentFactory.jsonBuilder() .startObject() .field("scroll_id", "value_2") .field("unknown", "keyword") - .endObject().bytes()); + .endObject()); Exception e = expectThrows(IllegalArgumentException.class, () -> RestSearchScrollAction.buildFromContent(invalidContent, searchScrollRequest)); diff --git a/core/src/test/java/org/elasticsearch/search/scroll/SearchScrollIT.java b/core/src/test/java/org/elasticsearch/search/scroll/SearchScrollIT.java index f266268f0b..c8adf23451 100644 --- a/core/src/test/java/org/elasticsearch/search/scroll/SearchScrollIT.java +++ b/core/src/test/java/org/elasticsearch/search/scroll/SearchScrollIT.java @@ -25,13 +25,12 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.Priority; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; 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.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; @@ -525,11 +524,7 @@ public class SearchScrollIT extends ESIntegTestCase { response.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - BytesReference bytesReference = builder.bytes(); - Map<String, Object> map; - try (XContentParser parser = XContentFactory.xContent(bytesReference).createParser(bytesReference)) { - map = parser.map(); - } + Map<String, Object> map = XContentHelper.convertToMap(builder.bytes(), false).v2(); assertThat(map.get("succeeded"), is(succeed)); assertThat(map.get("num_freed"), equalTo(numFreed)); diff --git a/core/src/test/java/org/elasticsearch/search/searchafter/SearchAfterBuilderTests.java b/core/src/test/java/org/elasticsearch/search/searchafter/SearchAfterBuilderTests.java index 0c96eb15e0..beb69a0184 100644 --- a/core/src/test/java/org/elasticsearch/search/searchafter/SearchAfterBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/searchafter/SearchAfterBuilderTests.java @@ -25,9 +25,9 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.text.Text; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryParser; @@ -108,7 +108,7 @@ public class SearchAfterBuilderTests extends ESTestCase { // ensure that every number type remain the same before/after xcontent (de)serialization. // This is not a problem because the final type of each field value is extracted from associated sort field. // This little trick ensure that equals and hashcode are the same when using the xcontent serialization. - private static SearchAfterBuilder randomJsonSearchFromBuilder() throws IOException { + private SearchAfterBuilder randomJsonSearchFromBuilder() throws IOException { int numSearchAfter = randomIntBetween(1, 10); XContentBuilder jsonBuilder = XContentFactory.jsonBuilder(); jsonBuilder.startObject(); @@ -150,7 +150,7 @@ public class SearchAfterBuilderTests extends ESTestCase { } jsonBuilder.endArray(); jsonBuilder.endObject(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(jsonBuilder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, jsonBuilder.bytes()); parser.nextToken(); parser.nextToken(); parser.nextToken(); @@ -188,7 +188,7 @@ public class SearchAfterBuilderTests extends ESTestCase { builder.startObject(); searchAfterBuilder.innerToXContent(builder); builder.endObject(); - XContentParser parser = XContentHelper.createParser(shuffleXContent(builder).bytes()); + XContentParser parser = createParser(shuffleXContent(builder)); new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.STRICT); parser.nextToken(); parser.nextToken(); diff --git a/core/src/test/java/org/elasticsearch/search/slice/SliceBuilderTests.java b/core/src/test/java/org/elasticsearch/search/slice/SliceBuilderTests.java index 70e3ccf968..52bc3bd463 100644 --- a/core/src/test/java/org/elasticsearch/search/slice/SliceBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/slice/SliceBuilderTests.java @@ -34,7 +34,6 @@ import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.fielddata.IndexNumericFieldData; @@ -125,7 +124,7 @@ public class SliceBuilderTests extends ESTestCase { builder.startObject(); sliceBuilder.innerToXContent(builder); builder.endObject(); - XContentParser parser = XContentHelper.createParser(shuffleXContent(builder).bytes()); + XContentParser parser = createParser(shuffleXContent(builder)); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.STRICT); SliceBuilder secondSliceBuilder = SliceBuilder.fromXContent(context); diff --git a/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java b/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java index df50d3126c..d545a082b5 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java @@ -30,7 +30,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.env.Environment; @@ -138,7 +137,7 @@ public abstract class AbstractSortTestCase<T extends SortBuilder<T>> extends EST } testItem.toXContent(builder, ToXContent.EMPTY_PARAMS); XContentBuilder shuffled = shuffleXContent(builder); - XContentParser itemParser = XContentHelper.createParser(shuffled.bytes()); + XContentParser itemParser = createParser(shuffled); itemParser.nextToken(); /* @@ -212,7 +211,7 @@ public abstract class AbstractSortTestCase<T extends SortBuilder<T>> extends EST }); long nowInMillis = randomPositiveLong(); return new QueryShardContext(0, idxSettings, bitsetFilterCache, ifds, null, null, scriptService, - indicesQueriesRegistry, null, null, null, () -> nowInMillis) { + indicesQueriesRegistry, null, null, () -> nowInMillis) { @Override public MappedFieldType fieldMapper(String name) { return provideMappedFieldType(name); diff --git a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java index 408a7db202..60b9f14c36 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java @@ -21,8 +21,8 @@ package org.elasticsearch.search.sort; import org.apache.lucene.search.SortField; import org.elasticsearch.common.ParseFieldMatcher; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.search.DocValueFormat; @@ -128,7 +128,7 @@ public class FieldSortBuilderTests extends AbstractSortTestCase<FieldSortBuilder public void testReverseOptionFails() throws IOException { String json = "{ \"post_date\" : {\"reverse\" : true} },\n"; - XContentParser parser = XContentFactory.xContent(json).createParser(json); + XContentParser parser = createParser(JsonXContent.jsonXContent, json); // need to skip until parser is located on second START_OBJECT parser.nextToken(); parser.nextToken(); diff --git a/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java index 6fbd473f7e..131d19f600 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java @@ -25,13 +25,12 @@ import org.apache.lucene.search.SortField; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.ParsingException; -import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.geo.GeoDistance; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.mapper.LatLonPointFieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.query.GeoValidationMethod; @@ -206,7 +205,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc " } ],\n" + " \"reverse\" : true\n" + "}"; - XContentParser itemParser = XContentHelper.createParser(new BytesArray(json)); + XContentParser itemParser = createParser(JsonXContent.jsonXContent, json); itemParser.nextToken(); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, itemParser, ParseFieldMatcher.STRICT); @@ -227,7 +226,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc " } ],\n" + " \"reverse\" : \"true\"\n" + "}"; - XContentParser itemParser = XContentHelper.createParser(new BytesArray(json)); + XContentParser itemParser = createParser(JsonXContent.jsonXContent, json); itemParser.nextToken(); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, itemParser, ParseFieldMatcher.STRICT); @@ -244,7 +243,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc String json = "{\n" + " \"reverse\" : \"false\"\n" + "}"; - XContentParser itemParser = XContentHelper.createParser(new BytesArray(json)); + XContentParser itemParser = createParser(JsonXContent.jsonXContent, json); itemParser.nextToken(); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, itemParser, ParseFieldMatcher.STRICT); @@ -271,7 +270,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc " \"mode\" : \"SUM\",\n" + " \"coerce\" : true\n" + "}"; - XContentParser itemParser = XContentHelper.createParser(new BytesArray(json)); + XContentParser itemParser = createParser(JsonXContent.jsonXContent, json); itemParser.nextToken(); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, itemParser, ParseFieldMatcher.STRICT); @@ -292,7 +291,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc " \"mode\" : \"SUM\",\n" + " \"ignore_malformed\" : true\n" + "}"; - XContentParser itemParser = XContentHelper.createParser(new BytesArray(json)); + XContentParser itemParser = createParser(JsonXContent.jsonXContent, json); itemParser.nextToken(); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, itemParser, ParseFieldMatcher.STRICT); @@ -312,7 +311,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc " \"distance_type\" : \"sloppy_arc\",\n" + " \"mode\" : \"SUM\"\n" + "}"; - XContentParser itemParser = XContentHelper.createParser(new BytesArray(json)); + XContentParser itemParser = createParser(JsonXContent.jsonXContent, json); itemParser.nextToken(); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, itemParser, ParseFieldMatcher.STRICT); @@ -337,7 +336,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc " },\n" + " \"validation_method\" : \"STRICT\"\n" + " }"; - XContentParser itemParser = XContentHelper.createParser(new BytesArray(json)); + XContentParser itemParser = createParser(JsonXContent.jsonXContent, json); itemParser.nextToken(); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, itemParser, ParseFieldMatcher.STRICT); @@ -460,8 +459,8 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc assertEquals("Deprecated field [sort_mode] used, expected [mode] instead", ex.getMessage()); } - private static GeoDistanceSortBuilder parse(XContentBuilder sortBuilder) throws Exception { - XContentParser parser = XContentHelper.createParser(sortBuilder.bytes()); + private GeoDistanceSortBuilder parse(XContentBuilder sortBuilder) throws Exception { + XContentParser parser = createParser(sortBuilder); QueryParseContext parseContext = new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.STRICT); parser.nextToken(); return GeoDistanceSortBuilder.fromXContent(parseContext, null); diff --git a/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java index bffa132663..eff8685b9d 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java @@ -24,6 +24,7 @@ import org.apache.lucene.search.SortField; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.search.DocValueFormat; @@ -62,7 +63,7 @@ public class ScoreSortBuilderTests extends AbstractSortTestCase<ScoreSortBuilder public void testParseOrder() throws IOException { SortOrder order = randomBoolean() ? SortOrder.ASC : SortOrder.DESC; String scoreSortString = "{ \"_score\": { \"order\": \""+ order.toString() +"\" }}"; - XContentParser parser = XContentFactory.xContent(scoreSortString).createParser(scoreSortString); + XContentParser parser = createParser(JsonXContent.jsonXContent, scoreSortString); // need to skip until parser is located on second START_OBJECT parser.nextToken(); parser.nextToken(); @@ -75,7 +76,7 @@ public class ScoreSortBuilderTests extends AbstractSortTestCase<ScoreSortBuilder public void testReverseOptionFails() throws IOException { String json = "{ \"_score\": { \"reverse\": true }}"; - XContentParser parser = XContentFactory.xContent(json).createParser(json); + XContentParser parser = createParser(JsonXContent.jsonXContent, json); // need to skip until parser is located on second START_OBJECT parser.nextToken(); parser.nextToken(); diff --git a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java index d6f96c4c58..68bc4cfa00 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java @@ -22,8 +22,8 @@ package org.elasticsearch.search.sort; import org.apache.lucene.search.SortField; import org.elasticsearch.common.ParseFieldMatcher; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; @@ -167,7 +167,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild "\"mode\" : \"max\",\n" + "\"order\" : \"asc\"\n" + "} }\n"; - XContentParser parser = XContentFactory.xContent(scriptSort).createParser(scriptSort); + XContentParser parser = createParser(JsonXContent.jsonXContent, scriptSort); parser.nextToken(); parser.nextToken(); parser.nextToken(); @@ -193,7 +193,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild "\"mode\" : \"max\",\n" + "\"order\" : \"asc\"\n" + "} }\n"; - XContentParser parser = XContentFactory.xContent(scriptSort).createParser(scriptSort); + XContentParser parser = createParser(JsonXContent.jsonXContent, scriptSort); parser.nextToken(); parser.nextToken(); parser.nextToken(); @@ -213,7 +213,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild public void testParseBadFieldNameExceptions() throws IOException { String scriptSort = "{\"_script\" : {" + "\"bad_field\" : \"number\"" + "} }"; - XContentParser parser = XContentFactory.xContent(scriptSort).createParser(scriptSort); + XContentParser parser = createParser(JsonXContent.jsonXContent, scriptSort); parser.nextToken(); parser.nextToken(); parser.nextToken(); @@ -226,7 +226,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild public void testParseBadFieldNameExceptionsOnStartObject() throws IOException { String scriptSort = "{\"_script\" : {" + "\"bad_field\" : { \"order\" : \"asc\" } } }"; - XContentParser parser = XContentFactory.xContent(scriptSort).createParser(scriptSort); + XContentParser parser = createParser(JsonXContent.jsonXContent, scriptSort); parser.nextToken(); parser.nextToken(); parser.nextToken(); @@ -238,7 +238,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild public void testParseUnexpectedToken() throws IOException { String scriptSort = "{\"_script\" : {" + "\"script\" : [ \"order\" : \"asc\" ] } }"; - XContentParser parser = XContentFactory.xContent(scriptSort).createParser(scriptSort); + XContentParser parser = createParser(JsonXContent.jsonXContent, scriptSort); parser.nextToken(); parser.nextToken(); parser.nextToken(); diff --git a/core/src/test/java/org/elasticsearch/search/sort/SortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/SortBuilderTests.java index 9fa19146e8..d31dee0b3d 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/SortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/SortBuilderTests.java @@ -20,15 +20,14 @@ x * Licensed to Elasticsearch under one or more contributor package org.elasticsearch.search.sort; import org.elasticsearch.common.ParseFieldMatcher; -import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.indices.query.IndicesQueriesRegistry; import org.elasticsearch.search.SearchModule; @@ -238,8 +237,8 @@ public class SortBuilderTests extends ESTestCase { assertEquals(new ScoreSortBuilder(), result.get(5)); } - private static List<SortBuilder<?>> parseSort(String jsonString) throws IOException { - XContentParser itemParser = XContentHelper.createParser(new BytesArray(jsonString)); + private List<SortBuilder<?>> parseSort(String jsonString) throws IOException { + XContentParser itemParser = createParser(JsonXContent.jsonXContent, jsonString); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, itemParser, ParseFieldMatcher.STRICT); assertEquals(XContentParser.Token.START_OBJECT, itemParser.nextToken()); diff --git a/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java b/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java index 4a5b24d507..0f93e6a36e 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java @@ -26,7 +26,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryParseContext; @@ -128,7 +127,7 @@ public abstract class AbstractSuggestionBuilderTestCase<SB extends SuggestionBui xContentBuilder.endObject(); XContentBuilder shuffled = shuffleXContent(xContentBuilder, shuffleProtectedFields()); - XContentParser parser = XContentHelper.createParser(shuffled.bytes()); + XContentParser parser = createParser(shuffled); QueryParseContext context = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); // we need to skip the start object and the name, those will be parsed by outer SuggestBuilder parser.nextToken(); @@ -191,8 +190,7 @@ public abstract class AbstractSuggestionBuilderTestCase<SB extends SuggestionBui (Writeable.Reader<SB>) namedWriteableRegistry.getReader(SuggestionBuilder.class, original.getWriteableName())); } - protected static QueryParseContext newParseContext(final String xcontent) throws IOException { - XContentParser parser = XContentFactory.xContent(xcontent).createParser(xcontent); + protected static QueryParseContext newParseContext(XContentParser parser) throws IOException { final QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); return parseContext; } diff --git a/core/src/test/java/org/elasticsearch/search/suggest/SuggestBuilderTests.java b/core/src/test/java/org/elasticsearch/search/suggest/SuggestBuilderTests.java index 4aa7e67744..c9896ef573 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/SuggestBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/SuggestBuilderTests.java @@ -77,7 +77,7 @@ public class SuggestBuilderTests extends ESTestCase { xContentBuilder.prettyPrint(); } suggestBuilder.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS); - XContentParser parser = XContentHelper.createParser(xContentBuilder.bytes()); + XContentParser parser = createParser(xContentBuilder); QueryParseContext context = new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.STRICT); SuggestBuilder secondSuggestBuilder = SuggestBuilder.fromXContent(context, suggesters); assertNotSame(suggestBuilder, secondSuggestBuilder); diff --git a/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java b/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java index fd7a33ee5b..a74376554f 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java @@ -26,9 +26,8 @@ import org.apache.lucene.search.suggest.document.ContextSuggestField; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; @@ -191,7 +190,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { public void testQueryContextParsingBasic() throws Exception { XContentBuilder builder = jsonBuilder().value("context1"); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); CategoryContextMapping mapping = ContextBuilder.category("cat").build(); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1)); @@ -202,10 +201,10 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { public void testQueryContextParsingArray() throws Exception { XContentBuilder builder = jsonBuilder().startArray() - .value("context1") - .value("context2") + .value("context1") + .value("context2") .endArray(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); CategoryContextMapping mapping = ContextBuilder.category("cat").build(); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(2)); @@ -223,7 +222,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { .field("boost", 10) .field("prefix", true) .endObject(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); CategoryContextMapping mapping = ContextBuilder.category("cat").build(); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1)); @@ -246,7 +245,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { .field("prefix", false) .endObject() .endArray(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); CategoryContextMapping mapping = ContextBuilder.category("cat").build(); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(2)); @@ -271,7 +270,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase { .endObject() .value("context2") .endArray(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); CategoryContextMapping mapping = ContextBuilder.category("cat").build(); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(2)); diff --git a/core/src/test/java/org/elasticsearch/search/suggest/completion/GeoContextMappingTests.java b/core/src/test/java/org/elasticsearch/search/suggest/completion/GeoContextMappingTests.java index 2237c1a41d..a6879ebf29 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/completion/GeoContextMappingTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/completion/GeoContextMappingTests.java @@ -23,9 +23,8 @@ import org.apache.lucene.index.IndexableField; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; @@ -204,7 +203,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase { public void testParsingQueryContextBasic() throws Exception { XContentBuilder builder = jsonBuilder().value("ezs42e44yx96"); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); GeoContextMapping mapping = ContextBuilder.geo("geo").build(); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1 + 8)); @@ -223,7 +222,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase { .field("lat", 23.654242) .field("lon", 90.047153) .endObject(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); GeoContextMapping mapping = ContextBuilder.geo("geo").build(); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1 + 8)); @@ -246,7 +245,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase { .field("boost", 10) .array("neighbours", 1, 2, 3) .endObject(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); GeoContextMapping mapping = ContextBuilder.geo("geo").build(); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8)); @@ -284,7 +283,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase { .array("neighbours", 5) .endObject() .endArray(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); GeoContextMapping mapping = ContextBuilder.geo("geo").build(); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8 + 1 + 1 + 8)); @@ -327,7 +326,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase { .field("lon", 92.112583) .endObject() .endArray(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.bytes()); + XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes()); GeoContextMapping mapping = ContextBuilder.geo("geo").build(); List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser)); assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8)); diff --git a/core/src/test/java/org/elasticsearch/search/suggest/completion/QueryContextTestCase.java b/core/src/test/java/org/elasticsearch/search/suggest/completion/QueryContextTestCase.java index 2960a6e23d..f8a0b22901 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/completion/QueryContextTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/completion/QueryContextTestCase.java @@ -50,8 +50,7 @@ public abstract class QueryContextTestCase<QC extends ToXContent> extends ESTest QC toXContent = createTestModel(); XContentBuilder builder = XContentFactory.jsonBuilder(); toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS); - BytesReference bytesReference = builder.bytes(); - XContentParser parser = XContentFactory.xContent(bytesReference).createParser(bytesReference); + XContentParser parser = createParser(builder); parser.nextToken(); QC fromXContext = fromXContent(new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.STRICT)); assertEquals(toXContent, fromXContext); diff --git a/core/src/test/java/org/elasticsearch/search/suggest/phrase/DirectCandidateGeneratorTests.java b/core/src/test/java/org/elasticsearch/search/suggest/phrase/DirectCandidateGeneratorTests.java index aada48f79b..41713bbce9 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/phrase/DirectCandidateGeneratorTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/phrase/DirectCandidateGeneratorTests.java @@ -25,9 +25,9 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.indices.query.IndicesQueriesRegistry; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionContext.DirectCandidateGenerator; @@ -111,7 +111,7 @@ public class DirectCandidateGeneratorTests extends ESTestCase{ builder.prettyPrint(); } generator.toXContent(builder, ToXContent.EMPTY_PARAMS); - XContentParser parser = XContentHelper.createParser(shuffleXContent(builder).bytes()); + XContentParser parser = createParser(shuffleXContent(builder)); QueryParseContext context = new QueryParseContext(mockRegistry, parser, ParseFieldMatcher.STRICT); parser.nextToken(); DirectCandidateGeneratorBuilder secondGenerator = DirectCandidateGeneratorBuilder.fromXContent(context); @@ -149,9 +149,13 @@ public class DirectCandidateGeneratorTests extends ESTestCase{ "Required [field]"); // test two fieldnames - directGenerator = "{ \"field\" : \"f1\", \"field\" : \"f2\" }"; - assertIllegalXContent(directGenerator, ParsingException.class, + if (JsonXContent.isStrictDuplicateDetectionEnabled()) { + logger.info("Skipping test as it uses a custom duplicate check that is obsolete when strict duplicate checks are enabled."); + } else { + directGenerator = "{ \"field\" : \"f1\", \"field\" : \"f2\" }"; + assertIllegalXContent(directGenerator, ParsingException.class, "[direct_generator] failed to parse field [field]"); + } // test unknown field directGenerator = "{ \"unknown_param\" : \"f1\" }"; @@ -169,9 +173,9 @@ public class DirectCandidateGeneratorTests extends ESTestCase{ "[direct_generator] size doesn't support values of type: START_ARRAY"); } - private static void assertIllegalXContent(String directGenerator, Class<? extends Exception> exceptionClass, String exceptionMsg) + private void assertIllegalXContent(String directGenerator, Class<? extends Exception> exceptionClass, String exceptionMsg) throws IOException { - XContentParser parser = XContentFactory.xContent(directGenerator).createParser(directGenerator); + XContentParser parser = createParser(JsonXContent.jsonXContent, directGenerator); QueryParseContext context = new QueryParseContext(mockRegistry, parser, ParseFieldMatcher.STRICT); Exception e = expectThrows(exceptionClass, () -> DirectCandidateGeneratorBuilder.fromXContent(context)); assertEquals(exceptionMsg, e.getMessage()); diff --git a/core/src/test/java/org/elasticsearch/search/suggest/phrase/SmoothingModelTestCase.java b/core/src/test/java/org/elasticsearch/search/suggest/phrase/SmoothingModelTestCase.java index 29d7dc5e3b..044982540a 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/phrase/SmoothingModelTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/phrase/SmoothingModelTestCase.java @@ -36,7 +36,6 @@ import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryParseContext; @@ -99,7 +98,7 @@ public abstract class SmoothingModelTestCase extends ESTestCase { contentBuilder.startObject(); testModel.innerToXContent(contentBuilder, ToXContent.EMPTY_PARAMS); contentBuilder.endObject(); - XContentParser parser = XContentHelper.createParser(shuffleXContent(contentBuilder).bytes()); + XContentParser parser = createParser(shuffleXContent(contentBuilder)); QueryParseContext context = new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.STRICT); parser.nextToken(); // go to start token, real parsing would do that in the outer element parser SmoothingModel parsedModel = fromXContent(context); diff --git a/core/src/test/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilderTests.java b/core/src/test/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilderTests.java index 588957137f..1df99ce83a 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilderTests.java @@ -21,6 +21,8 @@ package org.elasticsearch.search.suggest.term; import com.carrotsearch.randomizedtesting.generators.RandomStrings; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.search.suggest.AbstractSuggestionBuilderTestCase; import org.elasticsearch.search.suggest.DirectSpellcheckerSettings; import org.elasticsearch.search.suggest.SortBy; @@ -215,8 +217,8 @@ public class TermSuggestionBuilderTests extends AbstractSuggestionBuilderTestCas " }\n" + " }\n" + "}"; - try { - final SuggestBuilder suggestBuilder = SuggestBuilder.fromXContent(newParseContext(suggest), suggesters); + try (XContentParser parser = createParser(JsonXContent.jsonXContent, suggest)) { + final SuggestBuilder suggestBuilder = SuggestBuilder.fromXContent(newParseContext(parser), suggesters); fail("Should not have been able to create SuggestBuilder from malformed JSON: " + suggestBuilder); } catch (Exception e) { assertThat(e.getMessage(), containsString("parsing failed")); |