Java Code Examples for org.elasticsearch.common.xcontent.XContentParser#text()

The following examples show how to use org.elasticsearch.common.xcontent.XContentParser#text() . 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: AbstractXContentParser.java    From crate with Apache License 2.0 6 votes vote down vote up
static Map<String, String> readMapStrings(XContentParser parser, MapStringsFactory mapStringsFactory) throws IOException {
    Map<String, String> map = mapStringsFactory.newMap();
    XContentParser.Token token = parser.currentToken();
    if (token == null) {
        token = parser.nextToken();
    }
    if (token == XContentParser.Token.START_OBJECT) {
        token = parser.nextToken();
    }
    for (; token == XContentParser.Token.FIELD_NAME; token = parser.nextToken()) {
        // Must point to field name
        String fieldName = parser.currentName();
        // And then the value...
        parser.nextToken();
        String value = parser.text();
        map.put(fieldName, value);
    }
    return map;
}
 
Example 2
Source File: IdFieldMapper.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
    XContentParser parser = context.parser();
    if (parser.currentName() != null && parser.currentName().equals(Defaults.NAME) && parser.currentToken().isValue()) {
        // we are in the parse Phase
        String id = parser.text();
        if (context.id() != null && !context.id().equals(id)) {
            throw new MapperParsingException("Provided id [" + context.id() + "] does not match the content one [" + id + "]");
        }
        context.id(id);
    } // else we are in the pre/post parse phase

    if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) {
        fields.add(new Field(fieldType().names().indexName(), context.id(), fieldType()));
    }
    if (fieldType().hasDocValues()) {
        fields.add(new BinaryDocValuesField(fieldType().names().indexName(), new BytesRef(context.id())));
    }
}
 
Example 3
Source File: SuggestUtils.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static boolean parseSuggestContext(XContentParser parser, MapperService mapperService, String fieldName,
        SuggestionSearchContext.SuggestionContext suggestion, ParseFieldMatcher parseFieldMatcher) throws IOException {
    
    if ("analyzer".equals(fieldName)) {
        String analyzerName = parser.text();
        Analyzer analyzer = mapperService.analysisService().analyzer(analyzerName);
        if (analyzer == null) {
            throw new IllegalArgumentException("Analyzer [" + analyzerName + "] doesn't exists");
        }
        suggestion.setAnalyzer(analyzer);
    } else if ("field".equals(fieldName)) {
        suggestion.setField(parser.text());
    } else if ("size".equals(fieldName)) {
        suggestion.setSize(parser.intValue());
    } else if (parseFieldMatcher.match(fieldName, Fields.SHARD_SIZE)) {
        suggestion.setShardSize(parser.intValue());
    } else {
       return false;
    }
    return true;
    
}
 
Example 4
Source File: FeatureData.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public static FeatureData parse(XContentParser parser) throws IOException {
    String featureId = null;
    Double data = null;
    String parsedFeatureName = null;

    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
    while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
        String fieldName = parser.currentName();
        parser.nextToken();

        switch (fieldName) {
            case FEATURE_ID_FIELD:
                featureId = parser.text();
                break;
            case FEATURE_NAME_FIELD:
                parsedFeatureName = parser.text();
                break;
            case DATA_FIELD:
                data = parser.doubleValue();
                break;
            default:
                break;
        }
    }
    return new FeatureData(featureId, parsedFeatureName, data);
}
 
Example 5
Source File: QueryRescorer.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public RescoreSearchContext parse(XContentParser parser, SearchContext context) throws IOException {
    Token token;
    String fieldName = null;
    QueryRescoreContext rescoreContext = new QueryRescoreContext(this);
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            fieldName = parser.currentName();
            if ("rescore_query".equals(fieldName)) {
                ParsedQuery parsedQuery = context.queryParserService().parse(parser);
                rescoreContext.setParsedQuery(parsedQuery);
            }
        } else if (token.isValue()) {
            if ("query_weight".equals(fieldName)) {
                rescoreContext.setQueryWeight(parser.floatValue());
            } else if ("rescore_query_weight".equals(fieldName)) {
                rescoreContext.setRescoreQueryWeight(parser.floatValue());
            } else if ("score_mode".equals(fieldName)) {
                String sScoreMode = parser.text();
                if ("avg".equals(sScoreMode)) {
                    rescoreContext.setScoreMode(ScoreMode.Avg);
                } else if ("max".equals(sScoreMode)) {
                    rescoreContext.setScoreMode(ScoreMode.Max);
                } else if ("min".equals(sScoreMode)) {
                    rescoreContext.setScoreMode(ScoreMode.Min);
                } else if ("total".equals(sScoreMode)) {
                    rescoreContext.setScoreMode(ScoreMode.Total);
                } else if ("multiply".equals(sScoreMode)) {
                    rescoreContext.setScoreMode(ScoreMode.Multiply);
                } else {
                    throw new IllegalArgumentException("[rescore] illegal score_mode [" + sScoreMode + "]");
                }
            } else {
                throw new IllegalArgumentException("rescore doesn't support [" + fieldName + "]");
            }
        }
    }
    return rescoreContext;
}
 
Example 6
Source File: TermsParametersParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private OrderElement parseOrderParam(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    XContentParser.Token token;
    OrderElement orderParam = null;
    String orderKey = null;
    boolean orderAsc = false;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            orderKey = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            String dir = parser.text();
            if ("asc".equalsIgnoreCase(dir)) {
                orderAsc = true;
            } else if ("desc".equalsIgnoreCase(dir)) {
                orderAsc = false;
            } else {
                throw new SearchParseException(context, "Unknown terms order direction [" + dir + "] in terms aggregation ["
                        + aggregationName + "]", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " for [order] in [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }
    if (orderKey == null) {
        throw new SearchParseException(context, "Must specify at least one field for [order] in [" + aggregationName + "].",
                parser.getTokenLocation());
    } else {
        orderParam = new OrderElement(orderKey, orderAsc);
    }
    return orderParam;
}
 
Example 7
Source File: Fuzziness.java    From crate with Apache License 2.0 5 votes vote down vote up
public static Fuzziness parse(XContentParser parser) throws IOException {
    XContentParser.Token token = parser.currentToken();
    switch (token) {
        case VALUE_STRING:
        case VALUE_NUMBER:
            final String fuzziness = parser.text();
            if (AUTO.asString().equalsIgnoreCase(fuzziness)) {
                return AUTO;
            } else if (fuzziness.toUpperCase(Locale.ROOT).startsWith(AUTO.asString() + ":")) {
                return parseCustomAuto(fuzziness);
            }
            try {
                final int minimumSimilarity = Integer.parseInt(fuzziness);
                switch (minimumSimilarity) {
                    case 0:
                        return ZERO;
                    case 1:
                        return ONE;
                    case 2:
                        return TWO;
                    default:
                        return build(fuzziness);
                }
            } catch (NumberFormatException ex) {
                return build(fuzziness);
            }

        default:
            throw new IllegalArgumentException("Can't parse fuzziness on token: [" + token + "]");
    }
}
 
Example 8
Source File: ViewsMetaData.java    From crate with Apache License 2.0 5 votes vote down vote up
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 9
Source File: AllocateAllocationCommand.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public AllocateAllocationCommand fromXContent(XContentParser parser) throws IOException {
    String index = null;
    int shardId = -1;
    String nodeId = null;
    boolean allowPrimary = false;

    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.isValue()) {
            if ("index".equals(currentFieldName)) {
                index = parser.text();
            } else if ("shard".equals(currentFieldName)) {
                shardId = parser.intValue();
            } else if ("node".equals(currentFieldName)) {
                nodeId = parser.text();
            } else if ("allow_primary".equals(currentFieldName) || "allowPrimary".equals(currentFieldName)) {
                allowPrimary = parser.booleanValue();
            } else {
                throw new ElasticsearchParseException("[{}] command does not support field [{}]", NAME, currentFieldName);
            }
        } else {
            throw new ElasticsearchParseException("[{}] command does not support complex json tokens [{}]", NAME, token);
        }
    }
    if (index == null) {
        throw new ElasticsearchParseException("[{}] command missing the index parameter", NAME);
    }
    if (shardId == -1) {
        throw new ElasticsearchParseException("[{}] command missing the shard parameter", NAME);
    }
    if (nodeId == null) {
        throw new ElasticsearchParseException("[{}] command missing the node parameter", NAME);
    }
    return new AllocateAllocationCommand(new ShardId(index, shardId), nodeId, allowPrimary);
}
 
Example 10
Source File: UsersPrivilegesMetaData.java    From crate with Apache License 2.0 4 votes vote down vote up
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 11
Source File: HistogramParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {

    ValuesSourceParser vsParser = ValuesSourceParser.numeric(aggregationName, InternalHistogram.TYPE, context)
            .targetValueType(ValueType.NUMERIC)
            .formattable(true)
            .build();

    boolean keyed = false;
    long minDocCount = 0;
    InternalOrder order = (InternalOrder) InternalOrder.KEY_ASC;
    long interval = -1;
    ExtendedBounds extendedBounds = null;
    long offset = 0;

    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 (vsParser.token(currentFieldName, token, parser)) {
            continue;
        } else if (token.isValue()) {
            if ("interval".equals(currentFieldName)) {
                interval = parser.longValue();
            } else if ("min_doc_count".equals(currentFieldName) || "minDocCount".equals(currentFieldName)) {
                minDocCount = parser.longValue();
            } else if ("keyed".equals(currentFieldName)) {
                keyed = parser.booleanValue();
            } else if ("offset".equals(currentFieldName)) {
                offset = parser.longValue();
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in aggregation [" + aggregationName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if ("order".equals(currentFieldName)) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        currentFieldName = parser.currentName();
                    } else if (token == XContentParser.Token.VALUE_STRING) {
                        String dir = parser.text();
                        boolean asc = "asc".equals(dir);
                        if (!asc && !"desc".equals(dir)) {
                            throw new SearchParseException(context, "Unknown order direction [" + dir + "] in aggregation ["
                                    + aggregationName + "]. Should be either [asc] or [desc]", parser.getTokenLocation());
                        }
                        order = resolveOrder(currentFieldName, asc);
                    }
                }
            } else if (context.parseFieldMatcher().match(currentFieldName, EXTENDED_BOUNDS)) {
                extendedBounds = new ExtendedBounds();
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        currentFieldName = parser.currentName();
                    } else if (token.isValue()) {
                        if ("min".equals(currentFieldName)) {
                            extendedBounds.min = parser.longValue(true);
                        } else if ("max".equals(currentFieldName)) {
                            extendedBounds.max = parser.longValue(true);
                        } else {
                            throw new SearchParseException(context, "Unknown extended_bounds key for a " + token + " in aggregation ["
                                    + aggregationName + "]: [" + currentFieldName + "].", parser.getTokenLocation());
                        }
                    }
                }

            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in aggregation [" + aggregationName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in aggregation [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }

    if (interval < 1) {
        throw new SearchParseException(context,
                "Missing required field [interval] for histogram aggregation [" + aggregationName + "]", parser.getTokenLocation());
    }

    Rounding rounding = new Rounding.Interval(interval);
    if (offset != 0) {
        rounding = new Rounding.OffsetRounding((Rounding.Interval) rounding, offset);
    }

    if (extendedBounds != null) {
        // with numeric histogram, we can process here and fail fast if the bounds are invalid
        extendedBounds.processAndValidate(aggregationName, context, ValueParser.RAW);
    }

    return new HistogramAggregator.Factory(aggregationName, vsParser.config(), rounding, order, keyed, minDocCount, extendedBounds,
            new InternalHistogram.Factory());

}
 
Example 12
Source File: ChildrenParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    String childType = null;

    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 (token == XContentParser.Token.VALUE_STRING) {
            if ("type".equals(currentFieldName)) {
                childType = parser.text();
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }

    if (childType == null) {
        throw new SearchParseException(context, "Missing [child_type] field for children aggregation [" + aggregationName + "]",
                parser.getTokenLocation());
    }

    ValuesSourceConfig<ValuesSource.Bytes.WithOrdinals.ParentChild> config = new ValuesSourceConfig<>(ValuesSource.Bytes.WithOrdinals.ParentChild.class);
    DocumentMapper childDocMapper = context.mapperService().documentMapper(childType);

    String parentType = null;
    Query parentFilter = null;
    Query childFilter = null;
    if (childDocMapper != null) {
        ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper();
        if (!parentFieldMapper.active()) {
            throw new SearchParseException(context, "[children] no [_parent] field not configured that points to a parent type", parser.getTokenLocation());
        }
        parentType = parentFieldMapper.type();
        DocumentMapper parentDocMapper = context.mapperService().documentMapper(parentType);
        if (parentDocMapper != null) {
            // TODO: use the query API
            parentFilter = parentDocMapper.typeFilter();
            childFilter = childDocMapper.typeFilter();
            ParentChildIndexFieldData parentChildIndexFieldData = context.fieldData().getForField(parentFieldMapper.fieldType());
            config.fieldContext(new FieldContext(parentFieldMapper.fieldType().names().indexName(), parentChildIndexFieldData, parentFieldMapper.fieldType()));
        } else {
            config.unmapped(true);
        }
    } else {
        config.unmapped(true);
    }
    return new ParentToChildrenAggregator.Factory(aggregationName, config, parentType, parentFilter, childFilter);
}
 
Example 13
Source File: MetaData.java    From crate with Apache License 2.0 4 votes vote down vote up
public static MetaData fromXContent(XContentParser parser) throws IOException {
    Builder builder = new Builder();

    // we might get here after the meta-data element, or on a fresh parser
    XContentParser.Token token = parser.currentToken();
    String currentFieldName = parser.currentName();
    if (!"meta-data".equals(currentFieldName)) {
        token = parser.nextToken();
        if (token == XContentParser.Token.START_OBJECT) {
            // move to the field name (meta-data)
            token = parser.nextToken();
            if (token != XContentParser.Token.FIELD_NAME) {
                throw new IllegalArgumentException("Expected a field name but got " + token);
            }
            // move to the next object
            token = parser.nextToken();
        }
        currentFieldName = parser.currentName();
    }

    if (!"meta-data".equals(parser.currentName())) {
        throw new IllegalArgumentException("Expected [meta-data] as a field name but got " + currentFieldName);
    }
    if (token != XContentParser.Token.START_OBJECT) {
        throw new IllegalArgumentException("Expected a START_OBJECT but got " + 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 ("cluster_coordination".equals(currentFieldName)) {
                builder.coordinationMetaData(CoordinationMetaData.fromXContent(parser));
            } else if ("settings".equals(currentFieldName)) {
                builder.persistentSettings(Settings.fromXContent(parser));
            } else if ("indices".equals(currentFieldName)) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    builder.put(IndexMetaData.Builder.fromXContent(parser), false);
                }
            } else if ("templates".equals(currentFieldName)) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    builder.put(IndexTemplateMetaData.Builder.fromXContent(parser, parser.currentName()));
                }
            } else {
                try {
                    Custom custom = parser.namedObject(Custom.class, currentFieldName, null);
                    builder.putCustom(custom.getWriteableName(), custom);
                } catch (NamedObjectNotFoundException ex) {
                    LOGGER.warn("Skipping unknown custom object with type {}", currentFieldName);
                    parser.skipChildren();
                }
            }
        } else if (token.isValue()) {
            if ("version".equals(currentFieldName)) {
                builder.version = parser.longValue();
            } else if ("cluster_uuid".equals(currentFieldName) || "uuid".equals(currentFieldName)) {
                builder.clusterUUID = parser.text();
            } else if ("cluster_uuid_committed".equals(currentFieldName)) {
                builder.clusterUUIDCommitted = parser.booleanValue();
            } else {
                throw new IllegalArgumentException("Unexpected field [" + currentFieldName + "]");
            }
        } else {
            throw new IllegalArgumentException("Unexpected token " + token);
        }
    }
    return builder.build();
}
 
Example 14
Source File: AnomalyResult.java    From anomaly-detection with Apache License 2.0 4 votes vote down vote up
public static AnomalyResult parse(XContentParser parser) throws IOException {
    String detectorId = null;
    Double anomalyScore = null;
    Double anomalyGrade = null;
    Double confidence = null;
    List<FeatureData> featureData = new ArrayList<>();
    Instant dataStartTime = null;
    Instant dataEndTime = null;
    Instant executionStartTime = null;
    Instant executionEndTime = null;
    String error = null;

    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
    while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
        String fieldName = parser.currentName();
        parser.nextToken();

        switch (fieldName) {
            case DETECTOR_ID_FIELD:
                detectorId = parser.text();
                break;
            case ANOMALY_SCORE_FIELD:
                anomalyScore = parser.doubleValue();
                break;
            case ANOMALY_GRADE_FIELD:
                anomalyGrade = parser.doubleValue();
                break;
            case CONFIDENCE_FIELD:
                confidence = parser.doubleValue();
                break;
            case FEATURE_DATA_FIELD:
                ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser::getTokenLocation);
                while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                    featureData.add(FeatureData.parse(parser));
                }
                break;
            case DATA_START_TIME_FIELD:
                dataStartTime = ParseUtils.toInstant(parser);
                break;
            case DATA_END_TIME_FIELD:
                dataEndTime = ParseUtils.toInstant(parser);
                break;
            case EXECUTION_START_TIME_FIELD:
                executionStartTime = ParseUtils.toInstant(parser);
                break;
            case EXECUTION_END_TIME_FIELD:
                executionEndTime = ParseUtils.toInstant(parser);
                break;
            case ERROR_FIELD:
                error = parser.text();
                break;
            default:
                parser.skipChildren();
                break;
        }
    }
    return new AnomalyResult(
        detectorId,
        anomalyScore,
        anomalyGrade,
        confidence,
        featureData,
        dataStartTime,
        dataEndTime,
        executionStartTime,
        executionEndTime,
        error
    );
}
 
Example 15
Source File: OntologyMapper.java    From BioSolr with Apache License 2.0 4 votes vote down vote up
@Override
public void parse(ParseContext context) throws IOException {
	String iri;
	XContentParser parser = context.parser();
	XContentParser.Token token = parser.currentToken();

	if (token == XContentParser.Token.VALUE_STRING) {
		iri = parser.text();
	} else {
		throw new MapperParsingException(name() + " does not contain String value");
	}

	ContentPath.Type origPathType = context.path().pathType();
	context.path().pathType(ContentPath.Type.FULL);
	context.path().add(names.name());

	try {
		OntologyHelper helper = getHelper(ontologySettings, threadPool);

		OntologyData data = findOntologyData(helper, iri);
		if (data == null) {
			logger.debug("Cannot find OWL class for IRI {}", iri);
		} else {
			addFieldData(context, getPredefinedMapper(FieldMappings.URI, context), Collections.singletonList(iri));

			// Look up the label(s)
			addFieldData(context, getPredefinedMapper(FieldMappings.LABEL, context), data.getLabels());

			// Look up the synonyms
			addFieldData(context, getPredefinedMapper(FieldMappings.SYNONYMS, context), data.getLabels());

			// Add the child details
			addRelatedNodesWithLabels(context, data.getChildIris(), getPredefinedMapper(FieldMappings.CHILD_URI, context), data.getChildLabels(),
					getPredefinedMapper(FieldMappings.CHILD_LABEL, context));

			// Add the parent details
			addRelatedNodesWithLabels(context, data.getParentIris(), getPredefinedMapper(FieldMappings.PARENT_URI, context), data.getParentLabels(),
					getPredefinedMapper(FieldMappings.PARENT_LABEL, context));

			if (ontologySettings.isIncludeIndirect()) {
				// Add the descendant details
				addRelatedNodesWithLabels(context, data.getDescendantIris(), getPredefinedMapper(FieldMappings.DESCENDANT_URI, context), data.getDescendantLabels(),
						getPredefinedMapper(FieldMappings.DESCENDANT_LABEL, context));

				// Add the ancestor details
				addRelatedNodesWithLabels(context, data.getAncestorIris(), getPredefinedMapper(FieldMappings.ANCESTOR_URI, context), data.getAncestorLabels(),
						getPredefinedMapper(FieldMappings.ANCESTOR_LABEL, context));
			}

			if (ontologySettings.isIncludeRelations()) {
				// Add the related nodes
				Map<String, Collection<String>> relations = data.getRelationIris();

				for (String relation : relations.keySet()) {
					// Sanitise the relation name
					String sanRelation = relation.replaceAll("\\W+", "_");
					String uriMapperName = sanRelation + DYNAMIC_URI_FIELD_SUFFIX;
					String labelMapperName = sanRelation + DYNAMIC_LABEL_FIELD_SUFFIX;

					// Get the mapper for the relation
					FieldMapper<String> uriMapper = mappers.get(context.path().fullPathAsText(uriMapperName));
					FieldMapper<String> labelMapper = mappers.get(context.path().fullPathAsText(labelMapperName));

					if (uriMapper == null) {
						// No mappers created yet - build new ones for URI and label
						BuilderContext builderContext = new BuilderContext(context.indexSettings(), context.path());
						uriMapper = MapperBuilders.stringField(uriMapperName)
								.store(true)
								.index(true)
								.tokenized(false)
								.build(builderContext);
						labelMapper = MapperBuilders.stringField(labelMapperName)
								.store(true)
								.index(true)
								.tokenized(true)
								.build(builderContext);
					}

					addRelatedNodesWithLabels(context, relations.get(relation), uriMapper, helper.findLabelsForIRIs(relations.get(relation)),
							labelMapper);
				}

				if (ontologySettings.isIncludeParentPaths()) {
					// Add the parent paths
					addFieldData(context, getPredefinedMapper(FieldMappings.PARENT_PATHS, context), data.getParentPaths());
				}
			}
		}

		helper.updateLastCallTime();
	} catch (OntologyHelperException e) {
		throw new ElasticsearchException("Could not initialise ontology helper", e);
	} finally {
		context.path().remove();
		context.path().pathType(origPathType);
	}
}
 
Example 16
Source File: RepositoriesMetaData.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public RepositoriesMetaData fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token;
    List<RepositoryMetaData> repository = new ArrayList<>();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            String name = parser.currentName();
            if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
                throw new ElasticsearchParseException("failed to parse repository [{}], expected object", name);
            }
            String type = null;
            Settings settings = Settings.EMPTY;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    String currentFieldName = parser.currentName();
                    if ("type".equals(currentFieldName)) {
                        if (parser.nextToken() != XContentParser.Token.VALUE_STRING) {
                            throw new ElasticsearchParseException("failed to parse repository [{}], unknown type", name);
                        }
                        type = parser.text();
                    } else if ("settings".equals(currentFieldName)) {
                        if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
                            throw new ElasticsearchParseException("failed to parse repository [{}], incompatible params", name);
                        }
                        settings = Settings.settingsBuilder().put(SettingsLoader.Helper.loadNestedFromMap(parser.mapOrdered())).build();
                    } else {
                        throw new ElasticsearchParseException("failed to parse repository [{}], unknown field [{}]", name, currentFieldName);
                    }
                } else {
                    throw new ElasticsearchParseException("failed to parse repository [{}]", name);
                }
            }
            if (type == null) {
                throw new ElasticsearchParseException("failed to parse repository [{}], missing repository type", name);
            }
            repository.add(new RepositoryMetaData(name, type, settings));
        } else {
            throw new ElasticsearchParseException("failed to parse repositories");
        }
    }
    return new RepositoriesMetaData(repository.toArray(new RepositoryMetaData[repository.size()]));
}
 
Example 17
Source File: BucketScriptParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public PipelineAggregatorFactory parse(String reducerName, XContentParser parser, SearchContext context) throws IOException {
    XContentParser.Token token;
    Script script = null;
    String currentFieldName = null;
    Map<String, String> bucketsPathsMap = null;
    String format = null;
    GapPolicy gapPolicy = GapPolicy.SKIP;

    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if (context.parseFieldMatcher().match(currentFieldName, FORMAT)) {
                format = parser.text();
            } else if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
                bucketsPathsMap = new HashMap<>();
                bucketsPathsMap.put("_value", parser.text());
            } else if (context.parseFieldMatcher().match(currentFieldName, GAP_POLICY)) {
                gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation());
            } else if (context.parseFieldMatcher().match(currentFieldName, ScriptField.SCRIPT)) {
                script = Script.parse(parser, context.parseFieldMatcher());
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
                List<String> paths = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    String path = parser.text();
                    paths.add(path);
                }
                bucketsPathsMap = new HashMap<>();
                for (int i = 0; i < paths.size(); i++) {
                    bucketsPathsMap.put("_value" + i, paths.get(i));
                }
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (context.parseFieldMatcher().match(currentFieldName, ScriptField.SCRIPT)) {
                script = Script.parse(parser, context.parseFieldMatcher());
            } else if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
                Map<String, Object> map = parser.map();
                bucketsPathsMap = new HashMap<>();
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    bucketsPathsMap.put(entry.getKey(), String.valueOf(entry.getValue()));
                }
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + reducerName + "].",
                    parser.getTokenLocation());
        }
    }

    if (bucketsPathsMap == null) {
        throw new SearchParseException(context, "Missing required field [" + BUCKETS_PATH.getPreferredName()
                + "] for series_arithmetic aggregation [" + reducerName + "]", parser.getTokenLocation());
    }

    if (script == null) {
        throw new SearchParseException(context, "Missing required field [" + ScriptField.SCRIPT.getPreferredName()
                + "] for series_arithmetic aggregation [" + reducerName + "]", parser.getTokenLocation());
    }

    ValueFormatter formatter = null;
    if (format != null) {
        formatter = ValueFormat.Patternable.Number.format(format).formatter();
    } else {
        formatter = ValueFormatter.RAW;
    }

    return new BucketScriptPipelineAggregator.Factory(reducerName, bucketsPathsMap, script, formatter, gapPolicy);
}
 
Example 18
Source File: NestedQueryParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();
    final ToBlockJoinQueryBuilder builder = new ToBlockJoinQueryBuilder(parseContext);

    float boost = 1.0f;
    ScoreMode scoreMode = ScoreMode.Avg;
    String queryName = 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 ("query".equals(currentFieldName)) {
                builder.query();
            } else if (parseContext.parseFieldMatcher().match(currentFieldName, FILTER_FIELD)) {
                builder.filter();
            } else if ("inner_hits".equals(currentFieldName)) {
                builder.setInnerHits(innerHitsQueryParserHelper.parse(parseContext));
            } else {
                throw new QueryParsingException(parseContext, "[nested] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if ("path".equals(currentFieldName)) {
                builder.setPath(parser.text());
            } else if ("boost".equals(currentFieldName)) {
                boost = parser.floatValue();
            } else if ("score_mode".equals(currentFieldName) || "scoreMode".equals(currentFieldName)) {
                String sScoreMode = parser.text();
                if ("avg".equals(sScoreMode)) {
                    scoreMode = ScoreMode.Avg;
                } else if ("min".equals(sScoreMode)) {
                    scoreMode = ScoreMode.Min;
                } else if ("max".equals(sScoreMode)) {
                    scoreMode = ScoreMode.Max;
                } else if ("total".equals(sScoreMode) || "sum".equals(sScoreMode)) {
                    scoreMode = ScoreMode.Total;
                } else if ("none".equals(sScoreMode)) {
                    scoreMode = ScoreMode.None;
                } else {
                    throw new QueryParsingException(parseContext, "illegal score_mode for nested query [" + sScoreMode + "]");
                }
            } else if ("_name".equals(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new QueryParsingException(parseContext, "[nested] query does not support [" + currentFieldName + "]");
            }
        }
    }

    builder.setScoreMode(scoreMode);
    ToParentBlockJoinQuery joinQuery = builder.build();
    if (joinQuery != null) {
        joinQuery.setBoost(boost);
        if (queryName != null) {
            parseContext.addNamedQuery(queryName, joinQuery);
        }
    }
    return joinQuery;
}
 
Example 19
Source File: AnomalyDetectorJob.java    From anomaly-detection with Apache License 2.0 4 votes vote down vote up
public static AnomalyDetectorJob parse(XContentParser parser) throws IOException {
    String name = null;
    Schedule schedule = null;
    TimeConfiguration windowDelay = null;
    // we cannot set it to null as isEnabled() would do the unboxing and results in null pointer exception
    Boolean isEnabled = Boolean.FALSE;
    Instant enabledTime = null;
    Instant disabledTime = null;
    Instant lastUpdateTime = null;
    Long lockDurationSeconds = DEFAULT_AD_JOB_LOC_DURATION_SECONDS;

    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
    while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
        String fieldName = parser.currentName();
        parser.nextToken();

        switch (fieldName) {
            case NAME_FIELD:
                name = parser.text();
                break;
            case SCHEDULE_FIELD:
                schedule = ScheduleParser.parse(parser);
                break;
            case WINDOW_DELAY_FIELD:
                windowDelay = TimeConfiguration.parse(parser);
                break;
            case IS_ENABLED_FIELD:
                isEnabled = parser.booleanValue();
                break;
            case ENABLED_TIME_FIELD:
                enabledTime = ParseUtils.toInstant(parser);
                break;
            case DISABLED_TIME_FIELD:
                disabledTime = ParseUtils.toInstant(parser);
                break;
            case LAST_UPDATE_TIME_FIELD:
                lastUpdateTime = ParseUtils.toInstant(parser);
                break;
            case LOCK_DURATION_SECONDS:
                lockDurationSeconds = parser.longValue();
                break;
            default:
                parser.skipChildren();
                break;
        }
    }
    return new AnomalyDetectorJob(
        name,
        schedule,
        windowDelay,
        isEnabled,
        enabledTime,
        disabledTime,
        lastUpdateTime,
        lockDurationSeconds
    );
}
 
Example 20
Source File: FieldValueFactorFunctionParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ScoreFunction parse(QueryParseContext parseContext, XContentParser parser) throws IOException, QueryParsingException {

    String currentFieldName = null;
    String field = null;
    float boostFactor = 1;
    FieldValueFactorFunction.Modifier modifier = FieldValueFactorFunction.Modifier.NONE;
    Double missing = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if ("field".equals(currentFieldName)) {
                field = parser.text();
            } else if ("factor".equals(currentFieldName)) {
                boostFactor = parser.floatValue();
            } else if ("modifier".equals(currentFieldName)) {
                modifier = FieldValueFactorFunction.Modifier.valueOf(parser.text().toUpperCase(Locale.ROOT));
            } else if ("missing".equals(currentFieldName)) {
                missing = parser.doubleValue();
            } else {
                throw new QueryParsingException(parseContext, NAMES[0] + " query does not support [" + currentFieldName + "]");
            }
        } else if("factor".equals(currentFieldName) && (token == XContentParser.Token.START_ARRAY || token == XContentParser.Token.START_OBJECT)) {
            throw new QueryParsingException(parseContext, "[" + NAMES[0] + "] field 'factor' does not support lists or objects");
        }
    }

    if (field == null) {
        throw new QueryParsingException(parseContext, "[" + NAMES[0] + "] required field 'field' missing");
    }

    SearchContext searchContext = SearchContext.current();
    MappedFieldType fieldType = searchContext.mapperService().smartNameFieldType(field);
    IndexNumericFieldData fieldData = null;
    if (fieldType == null) {
        if(missing == null) {
            throw new ElasticsearchException("Unable to find a field mapper for field [" + field + "]. No 'missing' value defined.");
        }
    } else {
        fieldData = searchContext.fieldData().getForField(fieldType);
    }
    return new FieldValueFactorFunction(field, boostFactor, modifier, missing, fieldData);
}