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 File: GND.java From Elasticsearch with Apache License 2.0 | 6 votes |
@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 #2
Source File: XContentSettingsLoader.java From Elasticsearch with Apache License 2.0 | 6 votes |
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 #3
Source File: AbstractXContentParser.java From crate with Apache License 2.0 | 6 votes |
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 #4
Source File: XContentSettingsLoader.java From Elasticsearch with Apache License 2.0 | 6 votes |
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 #5
Source File: IndexTemplateMetaData.java From crate with Apache License 2.0 | 6 votes |
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 #6
Source File: SampleIndexTestCase.java From elasticsearch-carrot2 with Apache License 2.0 | 6 votes |
/** * 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 #7
Source File: ParseContext.java From Elasticsearch with Apache License 2.0 | 6 votes |
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 #8
Source File: JsonXContentGenerator.java From crate with Apache License 2.0 | 6 votes |
@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 #9
Source File: UsersPrivilegesMetaData.java From crate with Apache License 2.0 | 6 votes |
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 File: SampleIndexTestCase.java From elasticsearch-carrot2 with Apache License 2.0 | 6 votes |
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 #11
Source File: ESTestCase.java From crate with Apache License 2.0 | 6 votes |
/** * 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 #12
Source File: UserDefinedFunctionsMetaDataTest.java From crate with Apache License 2.0 | 6 votes |
@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 #13
Source File: ValidatingLtrQueryBuilder.java From elasticsearch-learning-to-rank with Apache License 2.0 | 6 votes |
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 #14
Source File: Settings.java From crate with Apache License 2.0 | 5 votes |
/** * Loads settings from the actual string content that represents them using {@link #fromXContent(XContentParser)} */ public Builder loadFromSource(String source, XContentType xContentType) { try (XContentParser parser = XContentFactory.xContent(xContentType) .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source)) { this.put(fromXContent(parser, true, true)); } catch (Exception e) { throw new SettingsException("Failed to load settings from [" + source + "]", e); } return this; }
Example #15
Source File: ImportSettingsParseElement.java From elasticsearch-inout-plugin with Apache License 2.0 | 5 votes |
@Override public void parse(XContentParser parser, ImportContext context) throws Exception { XContentParser.Token token = parser.currentToken(); if (token.isValue()) { ((ImportContext)context).settings(parser.booleanValue()); } }
Example #16
Source File: LoggingSearchExtBuilder.java From elasticsearch-learning-to-rank with Apache License 2.0 | 5 votes |
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 #17
Source File: ConfigurationLoader.java From openshift-elasticsearch-plugin with Apache License 2.0 | 5 votes |
private Settings toSettings(final BytesReference ref, final String type) { if (ref == null || ref.length() == 0) { return null; } XContentParser parser = null; try { parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, ref, XContentType.JSON); parser.nextToken(); parser.nextToken(); if(!type.equals((parser.currentName()))) { return null; } parser.nextToken(); return Settings.builder().put(new JsonSettingsLoader(true).load(parser.binaryValue())).build(); } catch (final IOException e) { throw ExceptionsHelper.convertToElastic(e); } finally { if(parser != null) { parser.close(); } } }
Example #18
Source File: UserDefinedFunctionMetaData.java From crate with Apache License 2.0 | 5 votes |
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 #19
Source File: IndexWarmersMetaData.java From Elasticsearch with Apache License 2.0 | 5 votes |
@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 #20
Source File: TermSuggestParser.java From Elasticsearch with Apache License 2.0 | 5 votes |
private void parseTokenValue(XContentParser parser, MapperService mapperService, String fieldName, TermSuggestionContext suggestion, DirectSpellcheckerSettings settings, ParseFieldMatcher parseFieldMatcher) throws IOException { if (!(SuggestUtils.parseSuggestContext(parser, mapperService, fieldName, suggestion, parseFieldMatcher) || SuggestUtils.parseDirectSpellcheckerSettings( parser, fieldName, settings, parseFieldMatcher))) { throw new IllegalArgumentException("suggester[term] doesn't support [" + fieldName + "]"); } }
Example #21
Source File: TemplateQueryParser.java From Elasticsearch with Apache License 2.0 | 5 votes |
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 #22
Source File: TerminateAfterParseElement.java From Elasticsearch with Apache License 2.0 | 5 votes |
@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 #23
Source File: ClusterShardHealth.java From crate with Apache License 2.0 | 5 votes |
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 #24
Source File: StandardnumberMapper.java From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 | 5 votes |
@Override protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException { if (context.externalValueSet()) { return; } XContentParser parser = context.parser(); if (parser.currentToken() == XContentParser.Token.VALUE_NULL) { return; } String value = fieldType().nullValueAsString(); if (parser.currentToken() == XContentParser.Token.START_OBJECT) { XContentParser.Token token; String currentFieldName = null; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else { if ("value".equals(currentFieldName) || "_value".equals(currentFieldName)) { value = parser.textOrNull(); } } } } else { value = parser.textOrNull(); } try { Collection<CharSequence> stdnums = service.lookup(settings, value); for (CharSequence stdnum : stdnums) { Field field = new Field(fieldType().name(), stdnum.toString(), fieldType()); fields.add(field); } } catch (NumberFormatException e) { logger.trace(e.getMessage(), e); context.createExternalValueContext("unknown"); } }
Example #25
Source File: DocumentParser.java From crate with Apache License 2.0 | 5 votes |
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 #26
Source File: ImportMappingsParseElement.java From elasticsearch-inout-plugin with Apache License 2.0 | 5 votes |
@Override public void parse(XContentParser parser, ImportContext context) throws Exception { XContentParser.Token token = parser.currentToken(); if (token.isValue()) { ((ImportContext)context).mappings(parser.booleanValue()); } }
Example #27
Source File: LinearRankerParser.java From elasticsearch-learning-to-rank with Apache License 2.0 | 5 votes |
@Override public LinearRanker parse(FeatureSet set, String model) { try (XContentParser parser = JsonXContent.jsonXContent.createParser(EMPTY, LoggingDeprecationHandler.INSTANCE, model) ) { return parse(parser, set); } catch (IOException e) { throw new IllegalArgumentException(e.getMessage(), e); } }
Example #28
Source File: IndexFeatureStoreTests.java From elasticsearch-learning-to-rank with Apache License 2.0 | 5 votes |
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())); }
Example #29
Source File: GeoExtensionProcessor.java From elasticsearch-plugin-geoshape with MIT License | 5 votes |
private ShapeBuilder<?,?, ?> getShapeBuilderFromObject(Object object) throws IOException{ XContentBuilder contentBuilder = JsonXContent.contentBuilder().value(object); XContentParser parser = JsonXContent.jsonXContent.createParser( NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(contentBuilder).streamInput() ); parser.nextToken(); return ShapeParser.parse(parser); }
Example #30
Source File: QueryAction.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
protected void updateRequestWithIndicesOptions(Select select, SearchRequestBuilder request) throws SqlParseException { for (Hint hint : select.getHints()) { if (hint.getType() == HintType.INDICES_OPTIONS && hint.getParams() != null && 0 < hint.getParams().length) { String param = hint.getParams()[0].toString(); try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, param)) { request.setIndicesOptions(IndicesOptions.fromMap(parser.map(), SearchRequest.DEFAULT_INDICES_OPTIONS)); } catch (IOException e) { throw new SqlParseException("could not parse indices_options hint: " + e.getMessage()); } } } }