Java Code Examples for org.elasticsearch.common.xcontent.XContentParser#textOrNull()
The following examples show how to use
org.elasticsearch.common.xcontent.XContentParser#textOrNull() .
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: InnerHitsQueryParserHelper.java From Elasticsearch with Apache License 2.0 | 6 votes |
public InnerHitsSubSearchContext parse(QueryParseContext parserContext) throws IOException, QueryParsingException { String fieldName = null; XContentParser.Token token; String innerHitName = null; SubSearchContext subSearchContext = new SubSearchContext(SearchContext.current()); try { XContentParser parser = parserContext.parser(); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { fieldName = parser.currentName(); } else if (token.isValue()) { if ("name".equals(fieldName)) { innerHitName = parser.textOrNull(); } else { parseCommonInnerHitOptions(parser, token, fieldName, subSearchContext, sortParseElement, sourceParseElement, highlighterParseElement, scriptFieldsParseElement, fieldDataFieldsParseElement); } } else { parseCommonInnerHitOptions(parser, token, fieldName, subSearchContext, sortParseElement, sourceParseElement, highlighterParseElement, scriptFieldsParseElement, fieldDataFieldsParseElement); } } } catch (Exception e) { throw new QueryParsingException(parserContext, "Failed to parse [_inner_hits]", e); } return new InnerHitsSubSearchContext(innerHitName, subSearchContext); }
Example 2
Source File: IcuCollationKeyFieldMapper.java From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 | 6 votes |
@Override protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException { final String value; if (context.externalValueSet()) { value = context.externalValue().toString(); } else { XContentParser parser = context.parser(); if (parser.currentToken() == XContentParser.Token.VALUE_NULL) { value = fieldType().nullValueAsString(); } else { value = parser.textOrNull(); } } if (value == null) { return; } RawCollationKey key = collator.getRawCollationKey(value, null); final BytesRef binaryValue = new BytesRef(key.bytes, 0, key.size); if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { Field field = new Field(fieldType().name(), binaryValue, fieldType()); fields.add(field); } if (fieldType().hasDocValues()) { fields.add(getDVField.apply(fieldType().name(), binaryValue)); } }
Example 3
Source File: StringFieldMapper.java From Elasticsearch with Apache License 2.0 | 5 votes |
/** * Parse a field as though it were a string. * @param context parse context used during parsing * @param nullValue value to use for null * @param defaultBoost default boost value returned unless overwritten in the field * @return the parsed field and the boost either parsed or defaulted * @throws IOException if thrown while parsing */ public static ValueAndBoost parseCreateFieldForString(ParseContext context, String nullValue, float defaultBoost) throws IOException { if (context.externalValueSet()) { return new ValueAndBoost(context.externalValue().toString(), defaultBoost); } XContentParser parser = context.parser(); if (parser.currentToken() == XContentParser.Token.VALUE_NULL) { return new ValueAndBoost(nullValue, defaultBoost); } if (parser.currentToken() == XContentParser.Token.START_OBJECT) { XContentParser.Token token; String currentFieldName = null; String value = nullValue; float boost = defaultBoost; 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 if ("boost".equals(currentFieldName) || "_boost".equals(currentFieldName)) { boost = parser.floatValue(); } else { throw new IllegalArgumentException("unknown property [" + currentFieldName + "]"); } } } return new ValueAndBoost(value, boost); } return new ValueAndBoost(parser.textOrNull(), defaultBoost); }
Example 4
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 5
Source File: ViewsMetaData.java From crate with Apache License 2.0 | 5 votes |
public static ViewsMetaData fromXContent(XContentParser parser) throws IOException { Map<String, ViewMetaData> views = new HashMap<>(); if (parser.nextToken() == XContentParser.Token.FIELD_NAME && parser.currentName().equals(TYPE)) { if (parser.nextToken() == XContentParser.Token.START_OBJECT) { while (parser.nextToken() == XContentParser.Token.FIELD_NAME) { String viewName = parser.currentName(); if (parser.nextToken() == XContentParser.Token.START_OBJECT) { String stmt = null; String owner = null; while (parser.nextToken() != XContentParser.Token.END_OBJECT) { if ("stmt".equals(parser.currentName())) { parser.nextToken(); stmt = parser.text(); } if ("owner".equals(parser.currentName())) { parser.nextToken(); owner = parser.textOrNull(); } } if (stmt == null) { throw new ElasticsearchParseException("failed to parse views, expected field 'stmt' in object"); } views.put(viewName, new ViewMetaData(stmt, owner)); } } } if (parser.nextToken() != XContentParser.Token.END_OBJECT) { // each custom metadata is packed inside an object. // each custom must move the parser to the end otherwise possible following customs won't be read throw new ElasticsearchParseException("failed to parse views, expected an object token at the end"); } } return new ViewsMetaData(views); }
Example 6
Source File: UserDefinedFunctionMetaData.java From crate with Apache License 2.0 | 5 votes |
private static String parseStringField(XContentParser parser) throws IOException { if (parser.nextToken() != XContentParser.Token.VALUE_STRING && parser.currentToken() != XContentParser.Token.VALUE_NULL) { throw new UnhandledServerException("failed to parse function"); } return parser.textOrNull(); }
Example 7
Source File: SortParseElement.java From Elasticsearch with Apache License 2.0 | 4 votes |
private void addCompoundSortField(XContentParser parser, SearchContext context, List<SortField> sortFields) throws Exception { XContentParser.Token token; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { String fieldName = parser.currentName(); boolean reverse = false; String missing = null; String innerJsonName = null; String unmappedType = null; MultiValueMode sortMode = null; NestedInnerQueryParseSupport nestedFilterParseHelper = null; token = parser.nextToken(); if (token == XContentParser.Token.VALUE_STRING) { String direction = parser.text(); if (direction.equals("asc")) { reverse = SCORE_FIELD_NAME.equals(fieldName); } else if (direction.equals("desc")) { reverse = !SCORE_FIELD_NAME.equals(fieldName); } else { throw new IllegalArgumentException("sort direction [" + fieldName + "] not supported"); } addSortField(context, sortFields, fieldName, reverse, unmappedType, missing, sortMode, nestedFilterParseHelper); } else { if (parsers.containsKey(fieldName)) { sortFields.add(parsers.get(fieldName).parse(parser, context)); } else { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { innerJsonName = parser.currentName(); } else if (token.isValue()) { if (context.parseFieldMatcher().match(innerJsonName, REVERSE_FIELD)) { reverse = parser.booleanValue(); } else if ("order".equals(innerJsonName)) { if ("asc".equals(parser.text())) { reverse = SCORE_FIELD_NAME.equals(fieldName); } else if ("desc".equals(parser.text())) { reverse = !SCORE_FIELD_NAME.equals(fieldName); } } else if ("missing".equals(innerJsonName)) { missing = parser.textOrNull(); } else if (context.parseFieldMatcher().match(innerJsonName, IGNORE_UNMAPPED)) { // backward compatibility: ignore_unmapped has been replaced with unmapped_type if (unmappedType == null // don't override if unmapped_type has been provided too && parser.booleanValue()) { unmappedType = LongFieldMapper.CONTENT_TYPE; } } else if (context.parseFieldMatcher().match(innerJsonName, UNMAPPED_TYPE)) { unmappedType = parser.textOrNull(); } else if ("mode".equals(innerJsonName)) { sortMode = MultiValueMode.fromString(parser.text()); } else if ("nested_path".equals(innerJsonName) || "nestedPath".equals(innerJsonName)) { if (nestedFilterParseHelper == null) { nestedFilterParseHelper = new NestedInnerQueryParseSupport(parser, context); } nestedFilterParseHelper.setPath(parser.text()); } else { throw new IllegalArgumentException("sort option [" + innerJsonName + "] not supported"); } } else if (token == XContentParser.Token.START_OBJECT) { if ("nested_filter".equals(innerJsonName) || "nestedFilter".equals(innerJsonName)) { if (nestedFilterParseHelper == null) { nestedFilterParseHelper = new NestedInnerQueryParseSupport(parser, context); } nestedFilterParseHelper.filter(); } else { throw new IllegalArgumentException("sort option [" + innerJsonName + "] not supported"); } } } addSortField(context, sortFields, fieldName, reverse, unmappedType, missing, sortMode, nestedFilterParseHelper); } } } } }
Example 8
Source File: IndicesQueryParser.java From Elasticsearch with Apache License 2.0 | 4 votes |
@Override public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); Query noMatchQuery = null; boolean queryFound = false; boolean indicesFound = false; boolean currentIndexMatchesIndices = false; String queryName = null; String currentFieldName = null; XContentParser.Token token; XContentStructure.InnerQuery innerQuery = null; XContentStructure.InnerQuery innerNoMatchQuery = null; 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 (parseContext.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) { innerQuery = new XContentStructure.InnerQuery(parseContext, null); queryFound = true; } else if (parseContext.parseFieldMatcher().match(currentFieldName, NO_MATCH_QUERY)) { innerNoMatchQuery = new XContentStructure.InnerQuery(parseContext, null); } else { throw new QueryParsingException(parseContext, "[indices] query does not support [" + currentFieldName + "]"); } } else if (token == XContentParser.Token.START_ARRAY) { if ("indices".equals(currentFieldName)) { if (indicesFound) { throw new QueryParsingException(parseContext, "[indices] indices or index already specified"); } indicesFound = true; Collection<String> indices = new ArrayList<>(); while (parser.nextToken() != XContentParser.Token.END_ARRAY) { String value = parser.textOrNull(); if (value == null) { throw new QueryParsingException(parseContext, "[indices] no value specified for 'indices' entry"); } indices.add(value); } currentIndexMatchesIndices = matchesIndices(parseContext.index().name(), indices.toArray(new String[indices.size()])); } else { throw new QueryParsingException(parseContext, "[indices] query does not support [" + currentFieldName + "]"); } } else if (token.isValue()) { if ("index".equals(currentFieldName)) { if (indicesFound) { throw new QueryParsingException(parseContext, "[indices] indices or index already specified"); } indicesFound = true; currentIndexMatchesIndices = matchesIndices(parseContext.index().name(), parser.text()); } else if (parseContext.parseFieldMatcher().match(currentFieldName, NO_MATCH_QUERY)) { String type = parser.text(); if ("all".equals(type)) { noMatchQuery = Queries.newMatchAllQuery(); } else if ("none".equals(type)) { noMatchQuery = Queries.newMatchNoDocsQuery(); } } else if ("_name".equals(currentFieldName)) { queryName = parser.text(); } else { throw new QueryParsingException(parseContext, "[indices] query does not support [" + currentFieldName + "]"); } } } if (!queryFound) { throw new QueryParsingException(parseContext, "[indices] requires 'query' element"); } if (!indicesFound) { throw new QueryParsingException(parseContext, "[indices] requires 'indices' or 'index' element"); } Query chosenQuery; if (currentIndexMatchesIndices) { chosenQuery = innerQuery.asQuery(); } else { // If noMatchQuery is set, it means "no_match_query" was "all" or "none" if (noMatchQuery != null) { chosenQuery = noMatchQuery; } else { // There might be no "no_match_query" set, so default to the match_all if not set if (innerNoMatchQuery == null) { chosenQuery = Queries.newMatchAllQuery(); } else { chosenQuery = innerNoMatchQuery.asQuery(); } } } if (queryName != null) { parseContext.addNamedQuery(queryName, chosenQuery); } return chosenQuery; }
Example 9
Source File: MappingMetaData.java From Elasticsearch with Apache License 2.0 | 4 votes |
private void innerParse(XContentParser parser, ParseContext context) throws IOException { if (!context.parsingStillNeeded()) { return; } XContentParser.Token token = parser.currentToken(); if (token == null) { token = parser.nextToken(); } if (token == XContentParser.Token.START_OBJECT) { token = parser.nextToken(); } String idPart = context.idParsingStillNeeded() ? id().pathElements()[context.locationId] : null; String routingPart = context.routingParsingStillNeeded() ? routing().pathElements()[context.locationRouting] : null; String timestampPart = context.timestampParsingStillNeeded() ? timestamp().pathElements()[context.locationTimestamp] : null; String versionPart = context.versionParsingStillNeeded() ? version().pathElements()[context.locationVersion] : null; for (; token == XContentParser.Token.FIELD_NAME; token = parser.nextToken()) { // Must point to field name String fieldName = parser.currentName(); // And then the value... token = parser.nextToken(); boolean incLocationId = false; boolean incLocationRouting = false; boolean incLocationTimestamp = false; boolean incLocationVersion = false; if (context.idParsingStillNeeded() && fieldName.equals(idPart)) { if (context.locationId + 1 == id.pathElements().length) { if (!token.isValue()) { throw new MapperParsingException("id field must be a value but was either an object or an array"); } context.id = parser.textOrNull(); context.idResolved = true; } else { incLocationId = true; } } if (context.routingParsingStillNeeded() && fieldName.equals(routingPart)) { if (context.locationRouting + 1 == routing.pathElements().length) { context.routing = parser.textOrNull(); context.routingResolved = true; } else { incLocationRouting = true; } } if (context.timestampParsingStillNeeded() && fieldName.equals(timestampPart)) { if (context.locationTimestamp + 1 == timestamp.pathElements().length) { context.timestamp = parser.textOrNull(); context.timestampResolved = true; } else { incLocationTimestamp = true; } } if (context.versionParsingStillNeeded() && fieldName.equals(versionPart)) { if (context.locationVersion + 1 == version.pathElements().length) { context.version = parser.textOrNull(); context.versionResolved = true; } else { incLocationVersion = true; } } if (incLocationId || incLocationRouting || incLocationTimestamp || incLocationVersion) { if (token == XContentParser.Token.START_OBJECT) { context.locationId += incLocationId ? 1 : 0; context.locationRouting += incLocationRouting ? 1 : 0; context.locationTimestamp += incLocationTimestamp ? 1 : 0; context.locationVersion += incLocationVersion ? 1 : 0; innerParse(parser, context); context.locationId -= incLocationId ? 1 : 0; context.locationRouting -= incLocationRouting ? 1 : 0; context.locationTimestamp -= incLocationTimestamp ? 1 : 0; } } else { parser.skipChildren(); } if (!context.parsingStillNeeded()) { return; } } }
Example 10
Source File: MinHashFieldMapper.java From elasticsearch-minhash with Apache License 2.0 | 4 votes |
@Override protected void parseCreateField(final ParseContext context, final List<IndexableField> fields) throws IOException { String value; if (context.externalValueSet()) { value = context.externalValue().toString(); } else { final XContentParser parser = context.parser(); if (parser.currentToken() == XContentParser.Token.VALUE_NULL) { value = fieldType().nullValueAsString(); } else { value = parser.textOrNull(); } } if (value == null) { return; } final byte[] minhashValue = MinHash.calculate(minhashAnalyzer, value); if (fieldType().stored()) { fields.add( new Field(fieldType().name(), minhashValue, fieldType())); } if (fieldType().hasDocValues()) { CustomMinHashDocValuesField field = (CustomMinHashDocValuesField) context .doc().getByKey(fieldType().name()); if (field == null) { field = new CustomMinHashDocValuesField(fieldType().name(), minhashValue); context.doc().addWithKey(fieldType().name(), field); } else { field.add(minhashValue); } } if (copyBitsTo != null) { parseCopyBitsFields( context.createExternalValueContext( MinHash.toBinaryString(minhashValue)), copyBitsTo.copyBitsToFields); } }
Example 11
Source File: KeywordFieldMapper.java From crate with Apache License 2.0 | 4 votes |
@Override protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException { String value; if (context.externalValueSet()) { value = context.externalValue().toString(); } else { XContentParser parser = context.parser(); if (parser.currentToken() == XContentParser.Token.VALUE_NULL) { value = fieldType().nullValueAsString(); } else { value = parser.textOrNull(); } } if (value == null || value.length() > ignoreAbove) { return; } final NamedAnalyzer normalizer = fieldType().normalizer(); if (normalizer != null) { try (TokenStream ts = normalizer.tokenStream(name(), value)) { final CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class); ts.reset(); if (ts.incrementToken() == false) { throw new IllegalStateException("The normalization token stream is " + "expected to produce exactly 1 token, but got 0 for analyzer " + normalizer + " and input \"" + value + "\""); } final String newValue = termAtt.toString(); if (ts.incrementToken()) { throw new IllegalStateException("The normalization token stream is " + "expected to produce exactly 1 token, but got 2+ for analyzer " + normalizer + " and input \"" + value + "\""); } ts.end(); value = newValue; } } // convert to utf8 only once before feeding postings/dv/stored fields final BytesRef binaryValue = new BytesRef(value); if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { Field field = new Field(fieldType().name(), binaryValue, fieldType()); fields.add(field); if (fieldType().hasDocValues() == false && fieldType().omitNorms()) { createFieldNamesField(context, fields); } } if (fieldType().hasDocValues()) { fields.add(new SortedSetDocValuesField(fieldType().name(), binaryValue)); } }
Example 12
Source File: FunctionArgumentDefinition.java From crate with Apache License 2.0 | 4 votes |
private static String parseStringField(XContentParser parser) throws IOException { if (parser.nextToken() != XContentParser.Token.VALUE_STRING && parser.currentToken() != XContentParser.Token.VALUE_NULL) { throw new IllegalArgumentException("Failed to parse string field"); } return parser.textOrNull(); }
Example 13
Source File: UsersPrivilegesMetaData.java From crate with Apache License 2.0 | 4 votes |
private static void privilegeFromXContent(XContentParser parser, Set<Privilege> privileges) throws IOException { XContentParser.Token currentToken; State state = null; Privilege.Type type = null; Privilege.Clazz clazz = null; String ident = null; String grantor = null; while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (currentToken == XContentParser.Token.FIELD_NAME) { String currentFieldName = parser.currentName(); currentToken = parser.nextToken(); switch (currentFieldName) { case "state": if (currentToken != XContentParser.Token.VALUE_NUMBER) { throw new ElasticsearchParseException( "failed to parse privilege, 'state' value is not a number [{}]", currentToken); } state = State.values()[parser.intValue()]; break; case "type": if (currentToken != XContentParser.Token.VALUE_NUMBER) { throw new ElasticsearchParseException( "failed to parse privilege, 'type' value is not a number [{}]", currentToken); } type = Privilege.Type.values()[parser.intValue()]; break; case "class": if (currentToken != XContentParser.Token.VALUE_NUMBER) { throw new ElasticsearchParseException( "failed to parse privilege, 'class' value is not a number [{}]", currentToken); } clazz = Privilege.Clazz.values()[parser.intValue()]; break; case "ident": if (currentToken != XContentParser.Token.VALUE_STRING && currentToken != XContentParser.Token.VALUE_NULL) { throw new ElasticsearchParseException( "failed to parse privilege, 'ident' value is not a string or null [{}]", currentToken); } ident = parser.textOrNull(); break; case "grantor": if (currentToken != XContentParser.Token.VALUE_STRING) { throw new ElasticsearchParseException( "failed to parse privilege, 'grantor' value is not a string [{}]", currentToken); } grantor = parser.text(); break; default: throw new ElasticsearchParseException("failed to parse privilege"); } } else if (currentToken == XContentParser.Token.END_ARRAY) { // empty privileges set return; } } privileges.add(new Privilege(state, type, clazz, ident, grantor)); }
Example 14
Source File: LicenseKey.java From crate with Apache License 2.0 | 4 votes |
private static String parseStringField(XContentParser parser) throws IOException { parser.nextToken(); return parser.textOrNull(); }