org.elasticsearch.common.xcontent.XContentParser Java Examples

The following examples show how to use org.elasticsearch.common.xcontent.XContentParser. 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 Project: elasticsearch-learning-to-rank   Author: o19s   File: ValidatingLtrQueryBuilder.java    License: 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 #2
Source Project: crate   Author: crate   File: ESTestCase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Randomly shuffles the fields inside objects parsed using the {@link XContentParser} passed in.
 * Recursively goes through inner objects and also shuffles them. Exceptions for this
 * recursive shuffling behavior can be made by passing in the names of fields which
 * internally should stay untouched.
 */
public static XContentBuilder shuffleXContent(XContentParser parser, boolean prettyPrint, String... exceptFieldNames)
        throws IOException {
    XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType());
    if (prettyPrint) {
        xContentBuilder.prettyPrint();
    }
    Token token = parser.currentToken() == null ? parser.nextToken() : parser.currentToken();
    if (token == Token.START_ARRAY) {
        List<Object> shuffledList = shuffleList(parser.listOrderedMap(), new HashSet<>(Arrays.asList(exceptFieldNames)));
        return xContentBuilder.value(shuffledList);
    }
    //we need a sorted map for reproducibility, as we are going to shuffle its keys and write XContent back
    Map<String, Object> shuffledMap = shuffleMap((LinkedHashMap<String, Object>)parser.mapOrdered(),
        new HashSet<>(Arrays.asList(exceptFieldNames)));
    return xContentBuilder.map(shuffledMap);
}
 
Example #3
Source Project: Elasticsearch   Author: baidu   File: ParseContext.java    License: Apache License 2.0 6 votes vote down vote up
public void reset(XContentParser parser, Document document, SourceToParse source) {
    this.parser = parser;
    this.document = document;
    if (document != null) {
        this.documents = new ArrayList<>();
        this.documents.add(document);
    } else {
        this.documents = null;
    }
    this.uid = null;
    this.version = null;
    this.id = null;
    this.sourceToParse = source;
    this.source = source == null ? null : sourceToParse.source();
    this.path.reset();
    this.allEntries = new AllEntries();
    this.docBoost = 1.0f;
    this.dynamicMappingsUpdate = null;
}
 
Example #4
Source Project: Elasticsearch   Author: baidu   File: XContentSettingsLoader.java    License: Apache License 2.0 6 votes vote down vote up
private void serializeValue(Map<String, String> settings, StringBuilder sb, List<String> path, XContentParser parser, String fieldName) throws IOException {
    sb.setLength(0);
    for (String pathEle : path) {
        sb.append(pathEle).append('.');
    }
    sb.append(fieldName);
    String key = sb.toString();
    String currentValue = parser.text();
    String previousValue = settings.put(key, currentValue);
    if (previousValue != null) {
        throw new ElasticsearchParseException(
                "duplicate settings key [{}] found at line number [{}], column number [{}], previous value [{}], current value [{}]",
                key,
                parser.getTokenLocation().lineNumber,
                parser.getTokenLocation().columnNumber,
                previousValue,
                currentValue
        );
    }
}
 
Example #5
Source Project: elasticsearch-carrot2   Author: carrot2   File: SampleIndexTestCase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Roundtrip to/from JSON.
 */
protected static void checkJsonSerialization(ClusteringActionResponse result) throws IOException {
   XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
   builder.startObject();
   result.toXContent(builder, ToXContent.EMPTY_PARAMS);
   builder.endObject();
   String json = Strings.toString(builder);

   try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
       DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json)) {
      Map<String, Object> mapAndClose = parser.map();
      Assertions.assertThat(mapAndClose)
          .as("json-result")
          .containsKey(Fields.CLUSTERS);
   }
}
 
Example #6
Source Project: crate   Author: crate   File: AbstractXContentParser.java    License: Apache License 2.0 6 votes vote down vote up
static List<Object> readList(XContentParser parser, MapFactory mapFactory) throws IOException {
    XContentParser.Token token = parser.currentToken();
    if (token == null) {
        token = parser.nextToken();
    }
    if (token == XContentParser.Token.FIELD_NAME) {
        token = parser.nextToken();
    }
    if (token == XContentParser.Token.START_ARRAY) {
        token = parser.nextToken();
    } else {
        throw new XContentParseException(parser.getTokenLocation(), "Failed to parse list:  expecting "
                + XContentParser.Token.START_ARRAY + " but got " + token);
    }

    ArrayList<Object> list = new ArrayList<>();
    for (; token != null && token != XContentParser.Token.END_ARRAY; token = parser.nextToken()) {
        list.add(readValue(parser, mapFactory, token));
    }
    return list;
}
 
Example #7
Source Project: Elasticsearch   Author: baidu   File: XContentSettingsLoader.java    License: Apache License 2.0 6 votes vote down vote up
private void serializeObject(Map<String, String> settings, StringBuilder sb, List<String> path, XContentParser parser, String objFieldName) throws IOException {
    if (objFieldName != null) {
        path.add(objFieldName);
    }

    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.START_OBJECT) {
            serializeObject(settings, sb, path, parser, currentFieldName);
        } else if (token == XContentParser.Token.START_ARRAY) {
            serializeArray(settings, sb, path, parser, currentFieldName);
        } else if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_NULL) {
            // ignore this
        } else {
            serializeValue(settings, sb, path, parser, currentFieldName);

        }
    }

    if (objFieldName != null) {
        path.remove(path.size() - 1);
    }
}
 
Example #8
Source Project: crate   Author: crate   File: UserDefinedFunctionsMetaDataTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testUserDefinedFunctionToXContentWithEmptyMetadata() throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder();

    // reflects the logic used to process custom metadata in the cluster state
    builder.startObject();
    UserDefinedFunctionsMetaData functions = UserDefinedFunctionsMetaData.of();
    functions.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();

    XContentParser parser = JsonXContent.JSON_XCONTENT.createParser(
        xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.toBytes(BytesReference.bytes(builder)));
    parser.nextToken(); // enter START_OBJECT
    UserDefinedFunctionsMetaData functions2 = UserDefinedFunctionsMetaData.fromXContent(parser);
    assertEquals(functions, functions2);
}
 
Example #9
Source Project: crate   Author: crate   File: UsersPrivilegesMetaData.java    License: Apache License 2.0 6 votes vote down vote up
public static UsersPrivilegesMetaData fromXContent(XContentParser parser) throws IOException {
    UsersPrivilegesMetaData metaData = new UsersPrivilegesMetaData();
    while (parser.nextToken() == XContentParser.Token.FIELD_NAME) {
        String userName = parser.currentName();
        Set<Privilege> privileges = metaData.getUserPrivileges(userName);
        if (privileges == null) {
            privileges = new HashSet<>();
            metaData.createPrivileges(userName, privileges);
        }
        XContentParser.Token token;
        while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            if (token == XContentParser.Token.START_OBJECT) {
                privilegeFromXContent(parser, privileges);
            }
        }
    }
    return metaData;
}
 
Example #10
Source Project: crate   Author: crate   File: JsonXContentGenerator.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void writeRawField(String name, InputStream content, XContentType contentType) throws IOException {
    if (mayWriteRawData(contentType) == false) {
        // EMPTY is safe here because we never call namedObject when writing raw data
        try (XContentParser parser = XContentFactory.xContent(contentType)
                // It's okay to pass the throwing deprecation handler
                // because we should not be writing raw fields when
                // generating JSON
                .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, content)) {
            parser.nextToken();
            writeFieldName(name);
            copyCurrentStructure(parser);
        }
    } else {
        writeStartRaw(name);
        flush();
        copyStream(content, os);
        writeEndRaw();
    }
}
 
Example #11
Source Project: Elasticsearch   Author: baidu   File: GND.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public SignificanceHeuristic parse(XContentParser parser, ParseFieldMatcher parseFieldMatcher, SearchContext context)
        throws IOException, QueryParsingException {
    String givenName = parser.currentName();
    boolean backgroundIsSuperset = true;
    XContentParser.Token token = parser.nextToken();
    while (!token.equals(XContentParser.Token.END_OBJECT)) {
        if (parseFieldMatcher.match(parser.currentName(), BACKGROUND_IS_SUPERSET)) {
            parser.nextToken();
            backgroundIsSuperset = parser.booleanValue();
        } else {
            throw new ElasticsearchParseException("failed to parse [{}] significance heuristic. unknown field [{}]", givenName, parser.currentName());
        }
        token = parser.nextToken();
    }
    return newHeuristic(true, backgroundIsSuperset);
}
 
Example #12
Source Project: crate   Author: crate   File: IndexTemplateMetaData.java    License: Apache License 2.0 6 votes vote down vote up
private static String skipTemplateName(XContentParser parser) throws IOException {
    XContentParser.Token token = parser.nextToken();
    if (token != null && token == XContentParser.Token.START_OBJECT) {
        token = parser.nextToken();
        if (token == XContentParser.Token.FIELD_NAME) {
            String currentFieldName = parser.currentName();
            if (VALID_FIELDS.contains(currentFieldName)) {
                return currentFieldName;
            } else {
                // we just hit the template name, which should be ignored and we move on
                parser.nextToken();
            }
        }
    }

    return null;
}
 
Example #13
Source Project: elasticsearch-carrot2   Author: carrot2   File: SampleIndexTestCase.java    License: Apache License 2.0 6 votes vote down vote up
protected static Map<String, Object> checkHttpResponse(HttpResponse response) throws IOException {
   byte[] responseBytes = response.getEntity().getContent().readAllBytes();
   String responseString = new String(responseBytes, StandardCharsets.UTF_8);

   String responseDescription =
       "HTTP response status: " + response.getStatusLine().toString() + ", " +
           "HTTP body: " + responseString;

   Assertions.assertThat(response.getStatusLine().getStatusCode())
       .describedAs(responseDescription)
       .isEqualTo(HttpStatus.SC_OK);

   try (XContentParser parser = XContentHelper.createParser(
       NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
       new BytesArray(responseBytes),
       XContentType.fromMediaTypeOrFormat(response.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue()))) {
      Map<String, Object> map = parser.map();
      Assertions.assertThat(map)
          .describedAs(responseDescription)
          .doesNotContainKey("error");
      return map;
   }
}
 
Example #14
Source Project: anomaly-detection   Author: opendistro-for-elasticsearch   File: AnomalyDetectorPlugin.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ScheduledJobParser getJobParser() {
    return (parser, id, jobDocVersion) -> {
        XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
        return AnomalyDetectorJob.parse(parser);
    };
}
 
Example #15
Source Project: Elasticsearch   Author: baidu   File: PercolatorQueriesRegistry.java    License: Apache License 2.0 5 votes vote down vote up
private Query parseQuery(String type, XContentParser parser) {
    String[] previousTypes = null;
    if (type != null) {
        QueryParseContext.setTypesWithPrevious(new String[]{type});
    }
    QueryParseContext context = queryParserService.getParseContext();
    try {
        context.reset(parser);
        // This means that fields in the query need to exist in the mapping prior to registering this query
        // The reason that this is required, is that if a field doesn't exist then the query assumes defaults, which may be undesired.
        //
        // Even worse when fields mentioned in percolator queries do go added to map after the queries have been registered
        // then the percolator queries don't work as expected any more.
        //
        // Query parsing can't introduce new fields in mappings (which happens when registering a percolator query),
        // because field type can't be inferred from queries (like document do) so the best option here is to disallow
        // the usage of unmapped fields in percolator queries to avoid unexpected behaviour
        //
        // if index.percolator.map_unmapped_fields_as_string is set to true, query can contain unmapped fields which will be mapped
        // as an analyzed string.
        context.setAllowUnmappedFields(false);
        context.setMapUnmappedFieldAsString(mapUnmappedFieldsAsString ? true : false);
        return queryParserService.parseInnerQuery(context);
    } catch (IOException e) {
        throw new QueryParsingException(context, "Failed to parse", e);
    } finally {
        if (type != null) {
            QueryParseContext.setTypes(previousTypes);
        }
        context.reset(null);
    }
}
 
Example #16
Source Project: crate   Author: crate   File: GeoJSONUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static Shape geoJSONString2Shape(String geoJSON) {
    try {
        XContentParser parser = JsonXContent.JSON_XCONTENT.createParser(
            NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, geoJSON);
        parser.nextToken();
        return ShapeParser.parse(parser).build();
    } catch (Throwable t) {
        throw new IllegalArgumentException(String.format(Locale.ENGLISH,
            "Cannot convert GeoJSON \"%s\" to shape", geoJSON), t);
    }
}
 
Example #17
Source Project: crate   Author: crate   File: UserDefinedFunctionMetaData.java    License: Apache License 2.0 5 votes vote down vote up
public static DataType<?> fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token = parser.currentToken();
    if (token != XContentParser.Token.START_OBJECT) {
        throw new IllegalArgumentException("Expected a START_OBJECT but got " + parser.currentToken());
    }
    int id = DataTypes.NOT_SUPPORTED.id();
    DataType<?> innerType = DataTypes.UNDEFINED;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            String fieldName = parser.currentName();
            if ("id".equals(fieldName)) {
                if (parser.nextToken() != XContentParser.Token.VALUE_NUMBER) {
                    throw new IllegalArgumentException("Expected a VALUE_NUMBER but got " + parser.currentToken());
                }
                id = parser.intValue();
            } else if ("inner_type".equals(fieldName)) {
                if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
                    throw new IllegalArgumentException("Expected a START_OBJECT but got " + parser.currentToken());
                }
                innerType = fromXContent(parser);
            }
        }
    }
    if (id == ArrayType.ID) {
        return new ArrayType<>(innerType);
    }
    return DataTypes.fromId(id);
}
 
Example #18
Source Project: elasticsearch-learning-to-rank   Author: o19s   File: LoggingSearchExtBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static LogSpec parse(XContentParser parser, Void context) throws IOException {
    try {
        LogSpec spec = PARSER.parse(parser, null);
        if (spec.namedQuery == null && spec.rescoreIndex == null) {
            throw new ParsingException(parser.getTokenLocation(), "Either " +
                    "[" + NAMED_QUERY + "] or [" + RESCORE_INDEX + "] must be set.");
        }
        if (spec.rescoreIndex != null && spec.rescoreIndex < 0) {
            throw new ParsingException(parser.getTokenLocation(), "[" + RESCORE_INDEX + "] must be a non-negative integer.");
        }
        return spec;
    } catch (IllegalArgumentException iae) {
        throw new ParsingException(parser.getTokenLocation(), iae.getMessage(), iae);
    }
}
 
Example #19
Source Project: Elasticsearch   Author: baidu   File: TemplateQueryParser.java    License: Apache License 2.0 5 votes vote down vote up
public static Template parse(XContentParser parser, ParseFieldMatcher parseFieldMatcher, String... parameters) throws IOException {

        Map<String, ScriptService.ScriptType> parameterMap = new HashMap<>(parametersToTypes);
        for (String parameter : parameters) {
            parameterMap.put(parameter, ScriptService.ScriptType.INLINE);
        }
        return parse(parser, parameterMap, parseFieldMatcher);
    }
 
Example #20
Source Project: crate   Author: crate   File: BooleanFieldMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
    if (fieldType().indexOptions() == IndexOptions.NONE && !fieldType().stored() && !fieldType().hasDocValues()) {
        return;
    }

    Boolean value = context.parseExternalValue(Boolean.class);
    if (value == null) {
        XContentParser.Token token = context.parser().currentToken();
        if (token == XContentParser.Token.VALUE_NULL) {
            if (fieldType().nullValue() != null) {
                value = fieldType().nullValue();
            }
        } else {
            value = context.parser().booleanValue();
        }
    }

    if (value == null) {
        return;
    }
    if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) {
        fields.add(new Field(fieldType().name(), value ? "T" : "F", fieldType()));
    }
    if (fieldType().hasDocValues()) {
        fields.add(new SortedNumericDocValuesField(fieldType().name(), value ? 1 : 0));
    } else {
        createFieldNamesField(context, fields);
    }
}
 
Example #21
Source Project: Elasticsearch   Author: baidu   File: SuggestUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean parseDirectSpellcheckerSettings(XContentParser parser, String fieldName,
            DirectSpellcheckerSettings suggestion, ParseFieldMatcher parseFieldMatcher) throws IOException {
        if ("accuracy".equals(fieldName)) {
            suggestion.accuracy(parser.floatValue());
        } else if (parseFieldMatcher.match(fieldName, Fields.SUGGEST_MODE)) {
            suggestion.suggestMode(SuggestUtils.resolveSuggestMode(parser.text()));
        } else if ("sort".equals(fieldName)) {
            suggestion.sort(SuggestUtils.resolveSort(parser.text()));
        } else if (parseFieldMatcher.match(fieldName, Fields.STRING_DISTANCE)) {
        suggestion.stringDistance(SuggestUtils.resolveDistance(parser.text()));
        } else if (parseFieldMatcher.match(fieldName, Fields.MAX_EDITS)) {
        suggestion.maxEdits(parser.intValue());
            if (suggestion.maxEdits() < 1 || suggestion.maxEdits() > LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE) {
                throw new IllegalArgumentException("Illegal max_edits value " + suggestion.maxEdits());
            }
        } else if (parseFieldMatcher.match(fieldName, Fields.MAX_INSPECTIONS)) {
        suggestion.maxInspections(parser.intValue());
        } else if (parseFieldMatcher.match(fieldName, Fields.MAX_TERM_FREQ)) {
        suggestion.maxTermFreq(parser.floatValue());
        } else if (parseFieldMatcher.match(fieldName, Fields.PREFIX_LENGTH)) {
        suggestion.prefixLength(parser.intValue());
        } else if (parseFieldMatcher.match(fieldName, Fields.MIN_WORD_LENGTH)) {
        suggestion.minQueryLength(parser.intValue());
        } else if (parseFieldMatcher.match(fieldName, Fields.MIN_DOC_FREQ)) {
        suggestion.minDocFreq(parser.floatValue());
        } else {
            return false;
        }
        return true;
}
 
Example #22
Source Project: elasticsearch-carrot2   Author: carrot2   File: SampleIndexTestCase.java    License: Apache License 2.0 5 votes vote down vote up
protected static void expectErrorResponseWithMessage(HttpResponse response,
                                                     int expectedStatus,
                                                     String messageSubstring) throws IOException {
   byte[] responseBytes = response.getEntity().getContent().readAllBytes();
   String responseString = new String(responseBytes, StandardCharsets.UTF_8);
   String responseDescription =
       "HTTP response status: " + response.getStatusLine().toString() + ", " +
           "HTTP body: " + responseString;

   Assertions.assertThat(response.getStatusLine().getStatusCode())
       .describedAs(responseDescription)
       .isEqualTo(expectedStatus);

   XContentType xContentType = XContentType.fromMediaTypeOrFormat(
       response.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
   try (XContentParser parser = XContentHelper.createParser(
       NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
       new BytesArray(responseBytes), xContentType)) {
      Map<String, Object> responseJson = parser.mapOrdered();

      Assertions.assertThat(responseJson)
          .describedAs(responseString)
          .containsKey("error");

      Assertions.assertThat(responseJson.get("error").toString())
          .describedAs(responseString)
          .contains(messageSubstring);
   }
}
 
Example #23
Source Project: elasticsearch-inout-plugin   Author: crate   File: Importer.java    License: Apache License 2.0 5 votes vote down vote up
private Map<String, Object> getMapFromJSONFile(File file) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }
    byte[] bytes = sb.toString().getBytes();
    XContentParser parser = XContentFactory.xContent(bytes).createParser(bytes);
    Map<String, Object> map = parser.map();
    return map;
}
 
Example #24
Source Project: elasticsearch-inout-plugin   Author: crate   File: ImportSettingsParseElement.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, ImportContext context)
        throws Exception {
    XContentParser.Token token = parser.currentToken();
    if (token.isValue()) {
        ((ImportContext)context).settings(parser.booleanValue());
    }
}
 
Example #25
Source Project: Elasticsearch   Author: baidu   File: IndexWarmersMetaData.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IndexWarmersMetaData fromMap(Map<String, Object> map) throws IOException {
    // if it starts with the type, remove it
    if (map.size() == 1 && map.containsKey(TYPE)) {
        map = (Map<String, Object>) map.values().iterator().next();
    }
    XContentBuilder builder = XContentFactory.smileBuilder().map(map);
    try (XContentParser parser = XContentFactory.xContent(XContentType.SMILE).createParser(builder.bytes())) {
        // move to START_OBJECT
        parser.nextToken();
        return fromXContent(parser);
    }
}
 
Example #26
Source Project: Elasticsearch   Author: baidu   File: TerminateAfterParseElement.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, SearchContext context) throws Exception {
    XContentParser.Token token = parser.currentToken();
    if (token == XContentParser.Token.VALUE_NUMBER) {
        int terminateAfterCount = parser.intValue();
        if (terminateAfterCount <= 0) {
            throw new IllegalArgumentException("terminateAfter must be > 0");
        }
        context.terminateAfter(parser.intValue());
    }
}
 
Example #27
Source Project: crate   Author: crate   File: ClusterShardHealth.java    License: Apache License 2.0 5 votes vote down vote up
public static ClusterShardHealth fromXContent(XContentParser parser) throws IOException {
    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
    XContentParser.Token token = parser.nextToken();
    ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser::getTokenLocation);
    String shardIdStr = parser.currentName();
    ClusterShardHealth parsed = innerFromXContent(parser, Integer.valueOf(shardIdStr));
    ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser::getTokenLocation);
    return parsed;
}
 
Example #28
Source Project: crate   Author: crate   File: DocumentParser.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean isEmptyDoc(Mapping mapping, XContentParser parser) throws IOException {
    if (mapping.root.isEnabled()) {
        final XContentParser.Token token = parser.nextToken();
        if (token == XContentParser.Token.END_OBJECT) {
            // empty doc, we can handle it...
            return true;
        } else if (token != XContentParser.Token.FIELD_NAME) {
            throw new MapperParsingException("Malformed content, after first object, either the type field or the actual properties should exist");
        }
    }
    return false;
}
 
Example #29
Source Project: elasticsearch-inout-plugin   Author: crate   File: ImportMappingsParseElement.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, ImportContext context)
        throws Exception {
    XContentParser.Token token = parser.currentToken();
    if (token.isValue()) {
        ((ImportContext)context).mappings(parser.booleanValue());
    }
}
 
Example #30
Source Project: elasticsearch-learning-to-rank   Author: o19s   File: IndexFeatureStoreTests.java    License: Apache License 2.0 5 votes vote down vote up
private void assertNameAndTypes(StorableElement elt, BytesReference ref) throws IOException {
    XContentParser parser = XContentFactory.xContent(Requests.INDEX_CONTENT_TYPE).createParser(NamedXContentRegistry.EMPTY,
            LoggingDeprecationHandler.INSTANCE, ref.streamInput());
    Map<String,Object> map = parser.map();
    assertEquals(elt.name(), map.get("name"));
    assertEquals(elt.type(), map.get("type"));
    assertTrue(map.containsKey(elt.type()));
}