org.elasticsearch.common.ParsingException Java Examples

The following examples show how to use org.elasticsearch.common.ParsingException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XGBoostJsonParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public void testMissingFeat() throws IOException {
    String model = "[{" +
            "\"nodeid\": 0," +
            "\"split\":\"feat2\"," +
            "\"depth\":0," +
            "\"split_condition\":0.123," +
            "\"yes\":1," +
            "\"no\": 2," +
            "\"missing\":2,"+
            "\"children\": [" +
            "   {\"nodeid\": 1, \"depth\": 1, \"leaf\": 0.5}," +
            "   {\"nodeid\": 2, \"depth\": 1, \"leaf\": 0.2}" +
            "]}]";
    FeatureSet set = new StoredFeatureSet("set", singletonList(randomFeature("feat1")));
    assertThat(expectThrows(ParsingException.class, () -> parser.parse(set, model)).getMessage(),
            CoreMatchers.containsString("Unknown feature [feat2]"));
}
 
Example #2
Source File: ExplorerQueryBuilder.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public static ExplorerQueryBuilder fromXContent(XContentParser parser) throws IOException {
    final ExplorerQueryBuilder builder;

    try {
        builder = PARSER.parse(parser, null);
    } catch (IllegalArgumentException iae) {
        throw new ParsingException(parser.getTokenLocation(), iae.getMessage(), iae);
    }

    if (builder.query == null) {
        throw new ParsingException(parser.getTokenLocation(), "Field [" + QUERY_NAME + "] is mandatory.");
    }
    if (builder.statsType() == null) {
        throw new ParsingException(parser.getTokenLocation(), "Field [" + TYPE_NAME + "] is mandatory.");
    }
    return builder;
}
 
Example #3
Source File: XGBoostJsonParser.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public static SplitParserState parse(XContentParser parser, FeatureSet set) {
    SplitParserState split = PARSER.apply(parser, set);
    if (split.isSplit()) {
        if (!split.splitHasAllFields()) {
            throw new ParsingException(parser.getTokenLocation(), "This split does not have all the required fields");
        }
        if (!split.splitHasValidChildren()) {
            throw new ParsingException(parser.getTokenLocation(), "Split structure is invalid, yes, no and/or" +
                    " missing branches does not point to the proper children.");
        }
        if (!set.hasFeature(split.split)) {
            throw new ParsingException(parser.getTokenLocation(), "Unknown feature [" + split.split + "]");
        }
    } else if (!split.leafHasAllFields()) {
        throw new ParsingException(parser.getTokenLocation(), "This leaf does not have all the required fields");
    }
    return split;
}
 
Example #4
Source File: XGBoostJsonParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public void testReadSplitWithUnknownParams() throws IOException {
    String model = "{" +
            "\"not_param\": \"value\"," +
            "\"splits\": [{" +
            "   \"nodeid\": 0," +
            "   \"split\":\"feat1\"," +
            "   \"depth\":0," +
            "   \"split_condition\":0.123," +
            "   \"yes\":1," +
            "   \"no\": 2," +
            "   \"missing\":2,"+
            "   \"children\": [" +
            "      {\"nodeid\": 1, \"depth\": 1, \"leaf\": 0.5}," +
            "      {\"nodeid\": 2, \"depth\": 1, \"leaf\": 0.2}" +
            "]}]}";

    FeatureSet set = new StoredFeatureSet("set", singletonList(randomFeature("feat1")));
    assertThat(expectThrows(ParsingException.class, () -> parser.parse(set, model)).getMessage(),
            CoreMatchers.containsString("Unable to parse XGBoost object"));
}
 
Example #5
Source File: XGBoostJsonParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public void testBadObjectiveParam() throws IOException {
    String model = "{" +
            "\"objective\": \"reg:invalid\"," +
            "\"splits\": [{" +
            "   \"nodeid\": 0," +
            "   \"split\":\"feat1\"," +
            "   \"depth\":0," +
            "   \"split_condition\":0.123," +
            "   \"yes\":1," +
            "   \"no\": 2," +
            "   \"missing\":2,"+
            "   \"children\": [" +
            "      {\"nodeid\": 1, \"depth\": 1, \"leaf\": 0.5}," +
            "      {\"nodeid\": 2, \"depth\": 1, \"leaf\": 0.2}" +
            "]}]}";

    FeatureSet set = new StoredFeatureSet("set", singletonList(randomFeature("feat1")));
    assertThat(expectThrows(ParsingException.class, () -> parser.parse(set, model)).getMessage(),
            CoreMatchers.containsString("Unable to parse XGBoost object"));
}
 
Example #6
Source File: XGBoostJsonParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public void testMissingField() throws IOException {
    String model = "[{" +
            "\"nodeid\": 0," +
            "\"split\":\"feat1\"," +
            "\"depth\":0," +
            "\"split_condition\":0.123," +
            "\"no\": 2," +
            "\"missing\":2,"+
            "\"children\": [" +
            "   {\"nodeid\": 1, \"depth\": 1, \"leaf\": 0.5}," +
            "   {\"nodeid\": 2, \"depth\": 1, \"leaf\": 0.2}" +
            "]}]";
    FeatureSet set = new StoredFeatureSet("set", singletonList(randomFeature("feat1")));
    assertThat(expectThrows(ParsingException.class, () -> parser.parse(set, model)).getMessage(),
            CoreMatchers.containsString("This split does not have all the required fields"));
}
 
Example #7
Source File: XGBoostJsonParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public void testBadStruct() throws IOException {
    String model = "[{" +
            "\"nodeid\": 0," +
            "\"split\":\"feat1\"," +
            "\"depth\":0," +
            "\"split_condition\":0.123," +
            "\"yes\":1," +
            "\"no\": 3," +
            "\"children\": [" +
            "   {\"nodeid\": 1, \"depth\": 1, \"leaf\": 0.5}," +
            "   {\"nodeid\": 2, \"depth\": 1, \"leaf\": 0.2}" +
            "]}]";
    FeatureSet set = new StoredFeatureSet("set", singletonList(randomFeature("feat1")));
    assertThat(expectThrows(ParsingException.class, () -> parser.parse(set, model)).getMessage(),
            CoreMatchers.containsString("Split structure is invalid, yes, no and/or"));
}
 
Example #8
Source File: ValidatingLtrQueryBuilder.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public static ValidatingLtrQueryBuilder fromXContent(XContentParser parser,
                                                     LtrRankerParserFactory factory) throws IOException {
    try {
        ValidatingLtrQueryBuilder builder = new ValidatingLtrQueryBuilder(factory);
        PARSER.parse(parser, builder, null);
        if (builder.element == null) {
            throw new ParsingException(parser.getTokenLocation(), "Element of type [" + SUPPORTED_TYPES.stream().collect(joining(",")) +
                    "] is mandatory.");
        }
        if (builder.validation == null) {
            throw new ParsingException(parser.getTokenLocation(), "Expected field [" + VALIDATION.getPreferredName() + "]");
        }

        return builder;
    } catch (IllegalArgumentException iae) {
        throw new ParsingException(parser.getTokenLocation(), iae.getMessage(), iae);
    }
}
 
Example #9
Source File: StoredLtrQueryBuilder.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public static StoredLtrQueryBuilder fromXContent(FeatureStoreLoader storeLoader,
                                                 XContentParser parser) throws IOException {
    storeLoader = Objects.requireNonNull(storeLoader);
    final StoredLtrQueryBuilder builder = new StoredLtrQueryBuilder(storeLoader);
    try {
        PARSER.parse(parser, builder, null);
    } catch (IllegalArgumentException iae) {
        throw new ParsingException(parser.getTokenLocation(), iae.getMessage(), iae);
    }
    if (builder.modelName() == null && builder.featureSetName() == null) {
        throw new ParsingException(parser.getTokenLocation(), "Either [" + MODEL_NAME + "] or [" + FEATURESET_NAME + "] must be set.");
    }
    if (builder.params() == null) {
        throw new ParsingException(parser.getTokenLocation(), "Field [" + PARAMS + "] is mandatory.");
    }
    return builder;
}
 
Example #10
Source File: PrecompiledTemplateFeature.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
@Override
public Query doToQuery(LtrQueryContext context, FeatureSet set, Map<String, Object> params) {
    List<String> missingParams = queryParams.stream()
            .filter((x) -> params == null || !params.containsKey(x))
            .collect(Collectors.toList());
    if (!missingParams.isEmpty()) {
        String names = missingParams.stream().collect(Collectors.joining(","));
        throw new IllegalArgumentException("Missing required param(s): [" + names + "]");
    }

    String query = MustacheUtils.execute(template, params);
    try {
        XContentParser parser = XContentFactory.xContent(query)
                .createParser(context.getQueryShardContext().getXContentRegistry(),
                        LoggingDeprecationHandler.INSTANCE, query);
        QueryBuilder queryBuilder = parseInnerQueryBuilder(parser);
        // XXX: QueryShardContext extends QueryRewriteContext (for now)
        return Rewriteable.rewrite(queryBuilder, context.getQueryShardContext()).toQuery(context.getQueryShardContext());
    } catch (IOException | ParsingException | IllegalArgumentException e) {
        // wrap common exceptions as well so we can attach the feature's name to the stack
        throw new QueryShardException(context.getQueryShardContext(), "Cannot create query while parsing feature [" + name + "]", e);
    }
}
 
Example #11
Source File: MultiValuesSourceParser.java    From elasticsearch-linear-regression with Apache License 2.0 6 votes vote down vote up
private void parseMissingAndAdd(final String aggregationName, final String currentFieldName,
    XContentParser parser, final Map<String, Object> missing) throws IOException {
  XContentParser.Token token = parser.currentToken();
  if (token == null) {
    token = parser.nextToken();
  }

  if (token == XContentParser.Token.FIELD_NAME) {
    final String fieldName = parser.currentName();
    if (missing.containsKey(fieldName)) {
      throw new ParsingException(parser.getTokenLocation(),
          "Missing field [" + fieldName + "] already defined as [" + missing.get(fieldName)
              + "] in [" + aggregationName + "].");
    }
    parser.nextToken();
    missing.put(fieldName, parser.objectText());
  } else {
    throw new ParsingException(parser.getTokenLocation(),
        "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName + "]");
  }
}
 
Example #12
Source File: StoredLtrModelParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testParseFailureOnBogusField() throws IOException {
    String modelString = "{\n" +
            " \"name\":\"my_model\",\n" +
            " \"bogusField\": \"foo\",\n" +
            " \"feature_set\":" +
            StoredFeatureSetParserTests.generateRandomFeatureSet() +
            "," +
            " \"model\": {\n" +
            "   \"type\": \"model/dummy\",\n" +
            "   \"definition\": \"completely ignored\"\n"+
            " }" +
            "}";
    assertThat(expectThrows(ParsingException.class, () -> parse(modelString)).getMessage(),
            containsString("bogusField"));
}
 
Example #13
Source File: StoredFeatureSetParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testParseErrorOnExtraField() throws IOException {
    String set = "{\"name\" : \"my_set\",\n" +
            "\"random_field\": \"oops\"," +
            "\"features\": [\n" +
            StoredFeatureParserTests.generateTestFeature() +
            "]}";
    assertThat(expectThrows(ParsingException.class,
            () -> parse(set)).getMessage(),
            containsString("[2:1] [featureset] unknown field [random_field]"));
}
 
Example #14
Source File: StoredFeatureSetParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testParseWithInconsistentExternalName() throws IOException {
    String set = "{\"name\" : \"my_set\",\n" +
            "\"features\": [\n" +
            StoredFeatureParserTests.generateTestFeature() +
            "]}";
    assertThat(expectThrows(ParsingException.class,
            () -> parse(set, "my_set2")).getMessage(),
            equalTo("Invalid [name], expected [my_set2] but got [my_set]"));
}
 
Example #15
Source File: StoredFeatureSetParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testParseErrorOnMissingName() throws IOException {
    String missingName = "{" +
            "\"features\": [\n" +
            StoredFeatureParserTests.generateTestFeature() +
            "]}";
    assertThat(expectThrows(ParsingException.class,
            () -> parse(missingName)).getMessage(),
            equalTo("Field [name] is mandatory"));
}
 
Example #16
Source File: StoredFeatureSetParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testParseErrorOnDups() throws IOException {
    String set = "{\"name\" : \"my_set\",\n" +
            "\"features\": [\n" +
            StoredFeatureParserTests.generateTestFeature() + "," +
            StoredFeatureParserTests.generateTestFeature() +
            "]}";
    assertThat(expectThrows(ParsingException.class,
            () -> parse(set)).getMessage(),
            containsString("feature names must be unique in a set"));
}
 
Example #17
Source File: LoggingSearchExtBuilderTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testFailOnNoLogSpecs() throws IOException {
    String data = "{}";
    ParsingException exc = expectThrows(ParsingException.class,
            () ->parse(createParser(JsonXContent.jsonXContent, data)));
    assertThat(exc.getMessage(),
        containsString("should define at least one [log_specs]"));
}
 
Example #18
Source File: LoggingSearchExtBuilderTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testFailOnEmptyLogSpecs() throws IOException {
    String data = "{\"log_specs\":[]}";
    ParsingException exc = expectThrows(ParsingException.class,
            () ->parse(createParser(JsonXContent.jsonXContent, data)));
    assertThat(exc.getMessage(),
            containsString("should define at least one [log_specs]"));
}
 
Example #19
Source File: StoredLtrModelParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testParseFailureOnMissingModel() throws IOException {
    String modelString = "{\n" +
            " \"name\":\"my_model\",\n" +
            " \"feature_set\":" +
            StoredFeatureSetParserTests.generateRandomFeatureSet() +
            "}";
    assertThat(expectThrows(ParsingException.class, () -> parse(modelString)).getMessage(),
            equalTo("Field [model] is mandatory"));
}
 
Example #20
Source File: LoggingSearchExtBuilderTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testFailOnBadLogSpec() throws IOException {
    String data = "{\"log_specs\":[" +
            "{\"name\":\"name1\",\"missing_as_zero\":true}," +
            "]}";
    ParsingException exc = expectThrows(ParsingException.class,
            () ->parse(createParser(JsonXContent.jsonXContent, data)));
    assertThat(exc.getCause().getCause().getMessage(),
            containsString("Either [named_query] or [rescore_index] must be set"));
}
 
Example #21
Source File: LoggingSearchExtBuilderTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testFailOnNegativeRescoreIndex() throws IOException {
    String data = "{\"log_specs\":[" +
            "{\"name\":\"name1\",\"rescore_index\":-1, \"missing_as_zero\":true}," +
            "]}";
    ParsingException exc = expectThrows(ParsingException.class,
            () ->parse(createParser(JsonXContent.jsonXContent, data)));
    assertThat(exc.getCause().getCause().getMessage(),
            containsString("non-negative"));
}
 
Example #22
Source File: LinearRankerParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testBadStructure() throws IOException {
    StoredFeatureSet set = new StoredFeatureSet("test", singletonList(LtrTestUtils.randomFeature("feature")));
    LinearRankerParser parser = new LinearRankerParser();
    expectThrows(ParsingException.class, () -> parser.parse(set, "{ \"feature\": {} }"));
    expectThrows(ParsingException.class, () -> parser.parse(set, "{ \"feature\": [] }"));
    expectThrows(ParsingException.class, () -> parser.parse(set, "{ \"feature\": null }"));
    expectThrows(ParsingException.class, () -> parser.parse(set, "{ \"feature\": \"hmm\" }"));
    expectThrows(ParsingException.class, () -> parser.parse(set, "{ \"feature\": \"1.2\" }"));
    expectThrows(ParsingException.class, () -> parser.parse(set, "[]"));
}
 
Example #23
Source File: ExplorerQueryBuilderTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testMissingQuery() throws Exception {
    String query =  " {" +
                    "  \"match_explorer\": {" +
                    "   \"type\": \"stddev_raw_tf\"" +
                    "  }" +
                    "}";

    expectThrows(ParsingException.class, () -> parseQuery(query));
}
 
Example #24
Source File: ExplorerQueryBuilderTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testMissingType() throws Exception {
    String query =  " {" +
                    "  \"match_explorer\": {" +
                    "    \"query\": {" +
                    "      \"match\": {" +
                    "        \"title\": \"test\"" +
                    "      }" +
                    "    }" +
                    "  }" +
                    "}";

    expectThrows(ParsingException.class, () -> parseQuery(query));
}
 
Example #25
Source File: ExactPhraseQueryBuilder.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
public static ExactPhraseQueryBuilder fromXContent(XContentParser parser) throws IOException {
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = null;
    QueryBuilder query = null;
    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (currentFieldName != null) {
                if (QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                    query = parseInnerQueryBuilder(parser);
                } else {
                    throw new ParsingException(parser.getTokenLocation(), "[nested] query does not support [" + currentFieldName + "]");
                }
            }
        } else if (token.isValue()) {
            if (currentFieldName != null) {
                if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                    boost = parser.floatValue();
                } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                    queryName = parser.text();
                } else {
                    throw new ParsingException(parser.getTokenLocation(), "[nested] query does not support [" + currentFieldName + "]");
                }
            }
        }
    }
    return new ExactPhraseQueryBuilder(query).queryName(queryName).boost(boost);
}
 
Example #26
Source File: XContentParserUtils.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Makes sure that current token is of type {@link Token#FIELD_NAME} and the field name is equal to the provided one
 * @throws ParsingException if the token is not of type {@link Token#FIELD_NAME} or is not equal to the given field name
 */
public static void ensureFieldName(XContentParser parser, Token token, String fieldName) throws IOException {
    ensureExpectedToken(Token.FIELD_NAME, token, parser::getTokenLocation);
    String currentName = parser.currentName();
    if (currentName.equals(fieldName) == false) {
        String message = "Failed to parse object: expecting field with name [%s] but found [%s]";
        throw new ParsingException(parser.getTokenLocation(), String.format(Locale.ROOT, message, fieldName, currentName));
    }
}
 
Example #27
Source File: XContentParserUtils.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Makes sure that provided token is of the expected type
 *
 * @throws ParsingException if the token is not equal to the expected type
 */
public static void ensureExpectedToken(Token expected, Token actual, Supplier<XContentLocation> location) {
    if (actual != expected) {
        String message = "Failed to parse object: expecting token of type [%s] but found [%s]";
        throw new ParsingException(location.get(), String.format(Locale.ROOT, message, expected, actual));
    }
}
 
Example #28
Source File: RestSimpleFeatureStore.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void parse(XContentParser parser) throws IOException {
    PARSER.parse(parser, this, expectedName);
    if (element == null) {
        throw new ParsingException(parser.getTokenLocation(), "Element of type [" + SUPPORTED_TYPES.stream().collect(joining(",")) +
                "] is mandatory.");
    }
}
 
Example #29
Source File: StorableElement.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
void resolveName(XContentParser parser, String name) {
    if (this.name == null && name != null) {
        this.name = name;
    } else if ( this.name == null /* && name == null */) {
        throw new ParsingException(parser.getTokenLocation(), "Field [name] is mandatory");
    } else if ( /* this.name != null && */ name != null && !this.name.equals(name)) {
        throw new ParsingException(parser.getTokenLocation(), "Invalid [name], expected ["+name+"] but got [" + this.name+"]");
    }
}
 
Example #30
Source File: StoredLtrModel.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public static StoredLtrModel parse(XContentParser parser, String name) {
    try {
        ParsingState state = PARSER.apply(parser, null);
        state.resolveName(parser, name);
        if (state.featureSet == null) {
            throw new ParsingException(parser.getTokenLocation(), "Field [feature_set] is mandatory");
        }
        if (state.rankingModel == null) {
            throw new ParsingException(parser.getTokenLocation(), "Field [model] is mandatory");
        }
        return new StoredLtrModel(state.getName(), state.featureSet, state.rankingModel);
    } catch (IllegalArgumentException iae) {
        throw new ParsingException(parser.getTokenLocation(), iae.getMessage(), iae);
    }
}