Search in sources :

Example 1 with XContentLocation

use of org.opensearch.common.xcontent.XContentLocation in project OpenSearch by opensearch-project.

the class RankEvalResponseTests method testToXContent.

public void testToXContent() throws IOException {
    EvalQueryQuality coffeeQueryQuality = new EvalQueryQuality("coffee_query", 0.1);
    coffeeQueryQuality.addHitsAndRatings(Arrays.asList(searchHit("index", 123, 5), searchHit("index", 456, null)));
    RankEvalResponse response = new RankEvalResponse(0.123, Collections.singletonMap("coffee_query", coffeeQueryQuality), Collections.singletonMap("beer_query", new ParsingException(new XContentLocation(0, 0), "someMsg")));
    XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
    String xContent = BytesReference.bytes(response.toXContent(builder, ToXContent.EMPTY_PARAMS)).utf8ToString();
    assertEquals(("{" + "    \"metric_score\": 0.123," + "    \"details\": {" + "        \"coffee_query\": {" + "            \"metric_score\": 0.1," + "            \"unrated_docs\": [{\"_index\":\"index\",\"_id\":\"456\"}]," + "            \"hits\":[{\"hit\":{\"_index\":\"index\",\"_id\":\"123\",\"_score\":1.0}," + "                       \"rating\":5}," + "                      {\"hit\":{\"_index\":\"index\",\"_id\":\"456\",\"_score\":1.0}," + "                       \"rating\":null}" + "                     ]" + "        }" + "    }," + "    \"failures\": {" + "        \"beer_query\": {" + "          \"error\" : {\"root_cause\": [{\"type\":\"parsing_exception\", \"reason\":\"someMsg\",\"line\":0,\"col\":0}]," + "                       \"type\":\"parsing_exception\"," + "                       \"reason\":\"someMsg\"," + "                       \"line\":0,\"col\":0" + "                      }" + "        }" + "    }" + "}").replaceAll("\\s+", ""), xContent);
}
Also used : ParsingException(org.opensearch.common.ParsingException) XContentLocation(org.opensearch.common.xcontent.XContentLocation) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 2 with XContentLocation

use of org.opensearch.common.xcontent.XContentLocation in project OpenSearch by opensearch-project.

the class AggregatorFactories method parseAggregators.

private static AggregatorFactories.Builder parseAggregators(XContentParser parser, int level) throws IOException {
    Matcher validAggMatcher = VALID_AGG_NAME.matcher("");
    AggregatorFactories.Builder factories = new AggregatorFactories.Builder();
    XContentParser.Token token = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token != XContentParser.Token.FIELD_NAME) {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [aggs]: aggregations definitions must start with the name of the aggregation.");
        }
        final String aggregationName = parser.currentName();
        if (!validAggMatcher.reset(aggregationName).matches()) {
            throw new ParsingException(parser.getTokenLocation(), "Invalid aggregation name [" + aggregationName + "]. Aggregation names can contain any character except '[', ']', and '>'");
        }
        token = parser.nextToken();
        if (token != XContentParser.Token.START_OBJECT) {
            throw new ParsingException(parser.getTokenLocation(), "Aggregation definition for [" + aggregationName + " starts with a [" + token + "], expected a [" + XContentParser.Token.START_OBJECT + "].");
        }
        BaseAggregationBuilder aggBuilder = null;
        AggregatorFactories.Builder subFactories = null;
        Map<String, Object> metadata = null;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token != XContentParser.Token.FIELD_NAME) {
                throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.FIELD_NAME + "] under a [" + XContentParser.Token.START_OBJECT + "], but got a [" + token + "] in [" + aggregationName + "]", parser.getTokenLocation());
            }
            final String fieldName = parser.currentName();
            token = parser.nextToken();
            if (token == XContentParser.Token.START_OBJECT) {
                switch(fieldName) {
                    case "meta":
                        metadata = parser.map();
                        break;
                    case "aggregations":
                    case "aggs":
                        if (subFactories != null) {
                            throw new ParsingException(parser.getTokenLocation(), "Found two sub aggregation definitions under [" + aggregationName + "]");
                        }
                        subFactories = parseAggregators(parser, level + 1);
                        break;
                    default:
                        if (aggBuilder != null) {
                            throw new ParsingException(parser.getTokenLocation(), "Found two aggregation type definitions in [" + aggregationName + "]: [" + aggBuilder.getType() + "] and [" + fieldName + "]");
                        }
                        try {
                            aggBuilder = parser.namedObject(BaseAggregationBuilder.class, fieldName, aggregationName);
                        } catch (NamedObjectNotFoundException ex) {
                            String message = String.format(Locale.ROOT, "Unknown aggregation type [%s]%s", fieldName, SuggestingErrorOnUnknown.suggest(fieldName, ex.getCandidates()));
                            throw new ParsingException(new XContentLocation(ex.getLineNumber(), ex.getColumnNumber()), message, ex);
                        }
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] under [" + fieldName + "], but got a [" + token + "] in [" + aggregationName + "]");
            }
        }
        if (aggBuilder == null) {
            throw new ParsingException(parser.getTokenLocation(), "Missing definition for aggregation [" + aggregationName + "]", parser.getTokenLocation());
        } else {
            if (metadata != null) {
                aggBuilder.setMetadata(metadata);
            }
            if (subFactories != null) {
                aggBuilder.subAggregations(subFactories);
            }
            if (aggBuilder instanceof AggregationBuilder) {
                factories.addAggregator((AggregationBuilder) aggBuilder);
            } else {
                factories.addPipelineAggregator((PipelineAggregationBuilder) aggBuilder);
            }
        }
    }
    return factories;
}
Also used : GlobalAggregationBuilder(org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Matcher(java.util.regex.Matcher) GlobalAggregationBuilder(org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) XContentLocation(org.opensearch.common.xcontent.XContentLocation) ParsingException(org.opensearch.common.ParsingException) NamedObjectNotFoundException(org.opensearch.common.xcontent.NamedObjectNotFoundException) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 3 with XContentLocation

use of org.opensearch.common.xcontent.XContentLocation in project OpenSearch by opensearch-project.

the class ClientYamlTestSuiteTests method testMultipleValidationErrors.

public void testMultipleValidationErrors() {
    int firstLineNumber = between(1, 10000);
    List<ClientYamlTestSection> sections = new ArrayList<>();
    {
        ContainsAssertion containsAssertion = new ContainsAssertion(new XContentLocation(firstLineNumber, 0), randomAlphaOfLength(randomIntBetween(3, 30)), randomDouble());
        sections.add(new ClientYamlTestSection(new XContentLocation(0, 0), "section1", SkipSection.EMPTY, Collections.singletonList(containsAssertion)));
    }
    int secondLineNumber = between(1, 10000);
    int thirdLineNumber = between(1, 10000);
    List<ExecutableSection> doSections = new ArrayList<>();
    {
        DoSection doSection = new DoSection(new XContentLocation(secondLineNumber, 0));
        doSection.setExpectedWarningHeaders(singletonList("foo"));
        doSection.setApiCallSection(new ApiCallSection("test"));
        doSections.add(doSection);
    }
    {
        DoSection doSection = new DoSection(new XContentLocation(thirdLineNumber, 0));
        ApiCallSection apiCall = new ApiCallSection("test");
        apiCall.setNodeSelector(NodeSelector.SKIP_DEDICATED_MASTERS);
        doSection.setApiCallSection(apiCall);
        doSections.add(doSection);
    }
    sections.add(new ClientYamlTestSection(new XContentLocation(0, 0), "section2", SkipSection.EMPTY, doSections));
    ClientYamlTestSuite testSuite = new ClientYamlTestSuite("api", "name", SetupSection.EMPTY, TeardownSection.EMPTY, sections);
    Exception e = expectThrows(IllegalArgumentException.class, testSuite::validate);
    assertEquals("api/name:\n" + "attempted to add a [contains] assertion without a corresponding [\"skip\": \"features\": \"contains\"] so runners " + "that do not support the [contains] assertion can skip the test at line [" + firstLineNumber + "],\n" + "attempted to add a [do] with a [warnings] section without a corresponding [\"skip\": \"features\": \"warnings\"] so " + "runners that do not support the [warnings] section can skip the test at line [" + secondLineNumber + "],\n" + "attempted to add a [do] with a [node_selector] section without a corresponding [\"skip\": \"features\": \"node_selector\"] " + "so runners that do not support the [node_selector] section can skip the test at line [" + thirdLineNumber + "]", e.getMessage());
}
Also used : ArrayList(java.util.ArrayList) XContentLocation(org.opensearch.common.xcontent.XContentLocation) ParsingException(org.opensearch.common.ParsingException)

Example 4 with XContentLocation

use of org.opensearch.common.xcontent.XContentLocation in project OpenSearch by opensearch-project.

the class GreaterThanAssertion method parse.

public static GreaterThanAssertion parse(XContentParser parser) throws IOException {
    XContentLocation location = parser.getTokenLocation();
    Tuple<String, Object> stringObjectTuple = ParserUtils.parseTuple(parser);
    if (!(stringObjectTuple.v2() instanceof Comparable)) {
        throw new IllegalArgumentException("gt section can only be used with objects that support natural ordering, found " + stringObjectTuple.v2().getClass().getSimpleName());
    }
    return new GreaterThanAssertion(location, stringObjectTuple.v1(), stringObjectTuple.v2());
}
Also used : XContentLocation(org.opensearch.common.xcontent.XContentLocation)

Example 5 with XContentLocation

use of org.opensearch.common.xcontent.XContentLocation in project OpenSearch by opensearch-project.

the class LessThanOrEqualToAssertion method parse.

public static LessThanOrEqualToAssertion parse(XContentParser parser) throws IOException {
    XContentLocation location = parser.getTokenLocation();
    Tuple<String, Object> stringObjectTuple = ParserUtils.parseTuple(parser);
    if (false == stringObjectTuple.v2() instanceof Comparable) {
        throw new IllegalArgumentException("lte section can only be used with objects that support natural ordering, found " + stringObjectTuple.v2().getClass().getSimpleName());
    }
    return new LessThanOrEqualToAssertion(location, stringObjectTuple.v1(), stringObjectTuple.v2());
}
Also used : XContentLocation(org.opensearch.common.xcontent.XContentLocation)

Aggregations

XContentLocation (org.opensearch.common.xcontent.XContentLocation)27 ParsingException (org.opensearch.common.ParsingException)11 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 NamedObjectNotFoundException (org.opensearch.common.xcontent.NamedObjectNotFoundException)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 SearchParseException (org.opensearch.search.SearchParseException)2 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)1 ShardSearchFailure (org.opensearch.action.search.ShardSearchFailure)1 ClusterBlockException (org.opensearch.cluster.block.ClusterBlockException)1 Tuple (org.opensearch.common.collect.Tuple)1 ToXContentObject (org.opensearch.common.xcontent.ToXContentObject)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1 ShardId (org.opensearch.index.shard.ShardId)1