org.elasticsearch.common.xcontent.LoggingDeprecationHandler Java Examples

The following examples show how to use org.elasticsearch.common.xcontent.LoggingDeprecationHandler. 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: ADStateManager.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
private ActionListener<GetResponse> onGetResponse(String adID, ActionListener<Optional<AnomalyDetector>> listener) {
    return ActionListener.wrap(response -> {
        if (response == null || !response.isExists()) {
            listener.onResponse(Optional.empty());
            return;
        }

        String xc = response.getSourceAsString();
        LOG.info("Fetched anomaly detector: {}", xc);

        try (
            XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, xc)
        ) {
            ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
            AnomalyDetector detector = AnomalyDetector.parse(parser, response.getId());
            currentDetectors.put(adID, new SimpleEntry<>(detector, clock.instant()));
            listener.onResponse(Optional.of(detector));
        } catch (Exception t) {
            LOG.error("Fail to parse detector {}", adID);
            LOG.error("Stack trace:", t);
            listener.onResponse(Optional.empty());
        }
    }, listener::onFailure);
}
 
Example #2
Source File: AnomalyDetectorRestTestCase.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
protected AnomalyDetector createAnomalyDetector(AnomalyDetector detector, Boolean refresh) throws IOException {
    Response response = TestHelpers
        .makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), toHttpEntity(detector), null);
    assertEquals("Create anomaly detector failed", RestStatus.CREATED, restStatus(response));

    Map<String, Object> detectorJson = jsonXContent
        .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, response.getEntity().getContent())
        .map();
    return new AnomalyDetector(
        (String) detectorJson.get("_id"),
        ((Integer) detectorJson.get("_version")).longValue(),
        detector.getName(),
        detector.getDescription(),
        detector.getTimeField(),
        detector.getIndices(),
        detector.getFeatureAttributes(),
        detector.getFilterQuery(),
        detector.getDetectionInterval(),
        detector.getWindowDelay(),
        detector.getUiMetadata(),
        detector.getSchemaVersion(),
        detector.getLastUpdateTime()
    );
}
 
Example #3
Source File: StoredLtrModelParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public void testSerializationModelDef() throws IOException {
    String modelDefnJson = "{\n" +
            "   \"type\": \"model/dummy\",\n" +
            "   \"definition\": \"completely ignored\",\n"+
            "   \"feature_normalizers\": {\n"+
            "     \"feature_2\": { \"min_max\":" +
            "           {\"minimum\": 1.0," +
            "            \"maximum\": 1.25}}}}";

    XContentParser xContent = jsonXContent.createParser(EMPTY,
            LoggingDeprecationHandler.INSTANCE, modelDefnJson);
    StoredLtrModel.LtrModelDefinition modelDef = StoredLtrModel.LtrModelDefinition.parse(xContent, null);

    BytesStreamOutput out = new BytesStreamOutput();
    modelDef.writeTo(out);
    out.close();

    BytesRef ref = out.bytes().toBytesRef();
    StreamInput input = ByteBufferStreamInput.wrap(ref.bytes, ref.offset, ref.length);

    StoredLtrModel.LtrModelDefinition modelUnserialized = new StoredLtrModel.LtrModelDefinition(input);
    assertEquals(modelUnserialized.getDefinition(), modelDef.getDefinition());
    assertEquals(modelUnserialized.getType(), modelDef.getType());
    assertEquals(modelUnserialized.getFtrNorms(), modelDef.getFtrNorms());

}
 
Example #4
Source File: GeoUtils.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the value as a geopoint. The following types of values are supported:
 * <p>
 * Object: has to contain either lat and lon or geohash fields
 * <p>
 * String: expected to be in "latitude, longitude" format or a geohash
 * <p>
 * Array: two or more elements, the first element is longitude, the second is latitude, the rest is ignored if ignoreZValue is true
 */
public static GeoPoint parseGeoPoint(Object value, final boolean ignoreZValue) throws ElasticsearchParseException {
    try {
        XContentBuilder content = JsonXContent.contentBuilder();
        content.startObject();
        content.field("null_value", value);
        content.endObject();

        try (InputStream stream = BytesReference.bytes(content).streamInput();
             XContentParser parser = JsonXContent.JSON_XCONTENT.createParser(
                 NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
            parser.nextToken(); // start object
            parser.nextToken(); // field name
            parser.nextToken(); // field value
            return parseGeoPoint(parser, new GeoPoint(), ignoreZValue);
        }

    } catch (IOException ex) {
        throw new ElasticsearchParseException("error parsing geopoint", ex);
    }
}
 
Example #5
Source File: PrecompiledTemplateFeature.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
@Override
public Query doToQuery(LtrQueryContext context, FeatureSet set, Map<String, Object> params) {
    List<String> missingParams = queryParams.stream()
            .filter((x) -> params == null || !params.containsKey(x))
            .collect(Collectors.toList());
    if (!missingParams.isEmpty()) {
        String names = missingParams.stream().collect(Collectors.joining(","));
        throw new IllegalArgumentException("Missing required param(s): [" + names + "]");
    }

    String query = MustacheUtils.execute(template, params);
    try {
        XContentParser parser = XContentFactory.xContent(query)
                .createParser(context.getQueryShardContext().getXContentRegistry(),
                        LoggingDeprecationHandler.INSTANCE, query);
        QueryBuilder queryBuilder = parseInnerQueryBuilder(parser);
        // XXX: QueryShardContext extends QueryRewriteContext (for now)
        return Rewriteable.rewrite(queryBuilder, context.getQueryShardContext()).toQuery(context.getQueryShardContext());
    } catch (IOException | ParsingException | IllegalArgumentException e) {
        // wrap common exceptions as well so we can attach the feature's name to the stack
        throw new QueryShardException(context.getQueryShardContext(), "Cannot create query while parsing feature [" + name + "]", e);
    }
}
 
Example #6
Source File: StoredFeature.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    builder.field(NAME.getPreferredName(), name);
    builder.field(PARAMS.getPreferredName(), queryParams);
    builder.field(TEMPLATE_LANGUAGE.getPreferredName(), templateLanguage);
    if (templateAsString) {
        builder.field(TEMPLATE.getPreferredName(), template);
    } else {
        builder.field(TEMPLATE.getPreferredName());
        // it's ok to use NamedXContentRegistry.EMPTY because we don't really parse we copy the structure...
        XContentParser parser = XContentFactory.xContent(template).createParser(NamedXContentRegistry.EMPTY,
                LoggingDeprecationHandler.INSTANCE, template);
        builder.copyCurrentStructure(parser);
    }
    builder.endObject();
    return builder;
}
 
Example #7
Source File: XGBoostJsonParser.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
@Override
public NaiveAdditiveDecisionTree parse(FeatureSet set, String model) {
    XGBoostDefinition modelDefinition;
    try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
            LoggingDeprecationHandler.INSTANCE, model)
    ) {
        modelDefinition = XGBoostDefinition.parse(parser, set);
    } catch (IOException e) {
        throw new IllegalArgumentException("Cannot parse model", e);
    }

    Node[] trees = modelDefinition.getTrees(set);
    float[] weights = new float[trees.length];
    // Tree weights are already encoded in outputs
    Arrays.fill(weights, 1F);
    return new NaiveAdditiveDecisionTree(trees, weights, set.size(), modelDefinition.normalizer);
}
 
Example #8
Source File: ChecksumBlobStoreFormat.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Reads blob with specified name without resolving the blobName using using {@link #blobName} method.
 *
 * @param blobContainer blob container
 * @param blobName blob name
 */
public T readBlob(BlobContainer blobContainer, String blobName) throws IOException {
    final BytesReference bytes = Streams.readFully(blobContainer.readBlob(blobName));
    final String resourceDesc = "ChecksumBlobStoreFormat.readBlob(blob=\"" + blobName + "\")";
    try (ByteArrayIndexInput indexInput =
             new ByteArrayIndexInput(resourceDesc, BytesReference.toBytes(bytes))) {
        CodecUtil.checksumEntireFile(indexInput);
        CodecUtil.checkHeader(indexInput, codec, VERSION, VERSION);
        long filePointer = indexInput.getFilePointer();
        long contentSize = indexInput.length() - CodecUtil.footerLength() - filePointer;
        try (XContentParser parser = XContentHelper.createParser(namedXContentRegistry, LoggingDeprecationHandler.INSTANCE,
                                                                 bytes.slice((int) filePointer, (int) contentSize), XContentType.SMILE)) {
            return reader.apply(parser);
        }
    } catch (CorruptIndexException | IndexFormatTooOldException | IndexFormatTooNewException ex) {
        // we trick this into a dedicated exception with the original stacktrace
        throw new CorruptStateException(ex);
    }
}
 
Example #9
Source File: StoredLtrModel.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    builder.field(NAME.getPreferredName(), name);
    builder.field(FEATURE_SET.getPreferredName());
    featureSet.toXContent(builder, params);
    builder.startObject(MODEL.getPreferredName());
    builder.field(LtrModelDefinition.MODEL_TYPE.getPreferredName(), rankingModelType);
    builder.field(LtrModelDefinition.MODEL_DEFINITION.getPreferredName());
    if (modelAsString) {
        builder.value(rankingModel);
    } else {
        try (XContentParser parser = JsonXContent.jsonXContent.createParser(EMPTY,
                LoggingDeprecationHandler.INSTANCE, rankingModel)
        ) {
            builder.copyCurrentStructure(parser);
        }
    }
    builder.field(LtrModelDefinition.FEATURE_NORMALIZERS.getPreferredName());
    this.parsedFtrNorms.toXContent(builder, params);
    builder.endObject();
    builder.endObject();
    return builder;
}
 
Example #10
Source File: BlobStoreRepository.java    From crate with Apache License 2.0 6 votes vote down vote up
private RepositoryData getRepositoryData(long indexGen) {
    if (indexGen == RepositoryData.EMPTY_REPO_GEN) {
        return RepositoryData.EMPTY;
    }
    try {
        final String snapshotsIndexBlobName = INDEX_FILE_PREFIX + Long.toString(indexGen);

        RepositoryData repositoryData;
        // EMPTY is safe here because RepositoryData#fromXContent calls namedObject
        try (InputStream blob = blobContainer().readBlob(snapshotsIndexBlobName);
             XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY,
                                                                               LoggingDeprecationHandler.INSTANCE, blob)) {
            repositoryData = RepositoryData.snapshotsFromXContent(parser, indexGen);
        }
        return repositoryData;
    } catch (NoSuchFileException ex) {
        // repository doesn't have an index blob, its a new blank repo
        return RepositoryData.EMPTY;
    } catch (IOException ioe) {
        throw new RepositoryException(metadata.name(), "could not read repository data from index blob", ioe);
    }
}
 
Example #11
Source File: DocumentParser.java    From crate with Apache License 2.0 5 votes vote down vote up
ParsedDocument parseDocument(SourceToParse source, MetadataFieldMapper[] metadataFieldsMappers) throws MapperParsingException {
    validateType();

    final Mapping mapping = docMapper.mapping();
    final ParseContext.InternalParseContext context;
    final XContentType xContentType = source.getXContentType();

    try (XContentParser parser = XContentHelper.createParser(docMapperParser.getXContentRegistry(),
        LoggingDeprecationHandler.INSTANCE, source.source(), xContentType)) {
        context = new ParseContext.InternalParseContext(indexSettings, docMapperParser, docMapper, source, parser);
        validateStart(parser);
        internalParseDocument(mapping, metadataFieldsMappers, context, parser);
        validateEnd(parser);
    } catch (Exception e) {
        throw wrapInMapperParsingException(source, e);
    }
    String remainingPath = context.path().pathAsText("");
    if (remainingPath.isEmpty() == false) {
        throw new IllegalStateException("found leftover path elements: " + remainingPath);
    }

    return new ParsedDocument(
        context.version(),
        context.seqID(),
        context.sourceToParse().id(),
        source.routing(),
        context.docs(),
        context.sourceToParse().source(),
        createDynamicUpdate(mapping, docMapper, context.getDynamicMappers())
    );
}
 
Example #12
Source File: StoredFeatureSetParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public static String generateRandomFeatureSet(String name, Consumer<StoredFeature> features, int nbFeat) throws IOException {
    StringBuilder sb = new StringBuilder();
    sb.append("{\"name\" : \"")
            .append(name)
            .append("\",\n");
    sb.append("\"features\":[");
    boolean first = true;
    // Simply avoid adding the same feature twice because of random string
    Set<String> addedFeatures = new HashSet<>();
    while(nbFeat-->0) {
        String featureString = generateRandomFeature();
        StoredFeature feature = StoredFeature.parse(jsonXContent.createParser(EMPTY,
                LoggingDeprecationHandler.INSTANCE, featureString));
        if (!addedFeatures.add(feature.name())) {
            continue;
        }
        if (!first) {
            sb.append(",");
        }
        first = false;
        sb.append(featureString);
        if (features != null) {
            features.accept(feature);
        }
    }
    sb.append("]}");
    return sb.toString();
}
 
Example #13
Source File: FeaturesParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testParseArray() throws IOException {
    RestAddFeatureToSet.FeaturesParserState fparser = new RestAddFeatureToSet.FeaturesParserState();
    int nFeat = random().nextInt(18)+1;
    String featuresArray = IntStream.range(0, nFeat)
            .mapToObj((i) -> generateTestFeature("feat" + i))
            .collect(Collectors.joining(","));
    XContentParser parser = jsonXContent.createParser(NamedXContentRegistry.EMPTY,
            LoggingDeprecationHandler.INSTANCE, "{\"features\":[" + featuresArray + "]}");
    fparser.parse(parser);
    assertEquals(nFeat, fparser.getFeatures().size());
    assertEquals("feat0", fparser.getFeatures().get(0).name());
}
 
Example #14
Source File: QueryAction.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
protected void updateRequestWithCollapse(Select select, SearchRequestBuilder request) throws SqlParseException {
    for (Hint hint : select.getHints()) {
        if (hint.getType() == HintType.COLLAPSE && hint.getParams() != null && 0 < hint.getParams().length) {
            try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, hint.getParams()[0].toString())) {
                request.setCollapse(CollapseBuilder.fromXContent(parser));
            } catch (IOException e) {
                throw new SqlParseException("could not parse collapse hint: " + e.getMessage());
            }
        }
    }
}
 
Example #15
Source File: QueryAction.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
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());
            }
        }
    }
}
 
Example #16
Source File: Paramer.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public static ToXContent fullParamer(QueryStringQueryBuilder query, Paramer paramer) {
    if (paramer.analysis != null) {
        query.analyzer(paramer.analysis);
    }

    if (paramer.boost != null) {
        query.boost(paramer.boost);
    }

    if (paramer.slop != null) {
        query.phraseSlop(paramer.slop);
    }

    if (paramer.defaultField != null) {
        query.defaultField(paramer.defaultField);
    }

    if (paramer.tieBreaker != null) {
        query.tieBreaker(paramer.tieBreaker);
    }

    if (paramer.operator != null) {
        query.defaultOperator(paramer.operator);
    }

    if (paramer.type != null) {
        query.type(MultiMatchQueryBuilder.Type.parse(paramer.type.toLowerCase(Locale.ROOT), LoggingDeprecationHandler.INSTANCE));
    }

    if (paramer.minimumShouldMatch != null) {
        query.minimumShouldMatch(paramer.minimumShouldMatch);
    }

    query.fields(paramer.fieldsBoosts);

    return query;
}
 
Example #17
Source File: DocumentMapperParser.java    From crate with Apache License 2.0 5 votes vote down vote up
private Tuple<String, Map<String, Object>> extractMapping(String type, String source) throws MapperParsingException {
    Map<String, Object> root;
    try (XContentParser parser = XContentType.JSON.xContent()
            .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, source)) {
        root = parser.mapOrdered();
    } catch (Exception e) {
        throw new MapperParsingException("failed to parse mapping definition", e);
    }
    return extractMapping(type, root);
}
 
Example #18
Source File: MapperService.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the mappings (formatted as JSON) into a map
 */
public static Map<String, Object> parseMapping(NamedXContentRegistry xContentRegistry, String mappingSource) throws Exception {
    try (XContentParser parser = XContentType.JSON.xContent()
            .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, mappingSource)) {
        return parser.map();
    }
}
 
Example #19
Source File: RestExecuteAnomalyDetectorAction.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
private RestActionListener<GetResponse> onGetAnomalyDetectorResponse(RestChannel channel, AnomalyDetectorExecutionInput input) {
    return new RestActionListener<GetResponse>(channel) {
        @Override
        protected void processResponse(GetResponse response) throws Exception {
            if (!response.isExists()) {
                XContentBuilder message = channel
                    .newErrorBuilder()
                    .startObject()
                    .field("message", "Can't find anomaly detector with id:" + response.getId())
                    .endObject();
                channel.sendResponse(new BytesRestResponse(RestStatus.NOT_FOUND, message));
                return;
            }
            XContentParser parser = XContentType.JSON
                .xContent()
                .createParser(
                    channel.request().getXContentRegistry(),
                    LoggingDeprecationHandler.INSTANCE,
                    response.getSourceAsBytesRef().streamInput()
                );

            ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
            AnomalyDetector detector = AnomalyDetector.parse(parser, response.getId(), response.getVersion());

            anomalyDetectorRunner
                .executeDetector(
                    detector,
                    input.getPeriodStart(),
                    input.getPeriodEnd(),
                    getPreviewDetectorActionListener(channel, detector)
                );
        }
    };
}
 
Example #20
Source File: MetaDataStateFormat.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the state from a given file and compares the expected version against the actual version of
 * the state.
 */
public final T read(NamedXContentRegistry namedXContentRegistry, Path file) throws IOException {
    try (Directory dir = newDirectory(file.getParent())) {
        try (IndexInput indexInput = dir.openInput(file.getFileName().toString(), IOContext.DEFAULT)) {
            // We checksum the entire file before we even go and parse it. If it's corrupted we barf right here.
            CodecUtil.checksumEntireFile(indexInput);
            CodecUtil.checkHeader(indexInput, STATE_FILE_CODEC, MIN_COMPATIBLE_STATE_FILE_VERSION, STATE_FILE_VERSION);
            final XContentType xContentType = XContentType.values()[indexInput.readInt()];
            if (xContentType != FORMAT) {
                throw new IllegalStateException("expected state in " + file + " to be " + FORMAT + " format but was " + xContentType);
            }
            long filePointer = indexInput.getFilePointer();
            long contentSize = indexInput.length() - CodecUtil.footerLength() - filePointer;
            try (IndexInput slice = indexInput.slice("state_xcontent", filePointer, contentSize)) {
                try (InputStreamIndexInput in = new InputStreamIndexInput(slice, contentSize)) {
                    try (XContentParser parser = XContentFactory.xContent(FORMAT)
                            .createParser(namedXContentRegistry, LoggingDeprecationHandler.INSTANCE,
                                in)) {
                        return fromXContent(parser);
                    }
                }
            }
        } catch (CorruptIndexException | IndexFormatTooOldException | IndexFormatTooNewException ex) {
            // we trick this into a dedicated exception with the original stacktrace
            throw new CorruptStateException(ex);
        }
    }
}
 
Example #21
Source File: Settings.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #22
Source File: CreateIndexRequest.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the aliases that will be associated with the index when it gets created
 */
public CreateIndexRequest aliases(BytesReference source) {
    // EMPTY is safe here because we never call namedObject
    try (XContentParser parser = XContentHelper
            .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source)) {
        //move to the first alias
        parser.nextToken();
        while ((parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            alias(Alias.fromXContent(parser));
        }
        return this;
    } catch (IOException e) {
        throw new ElasticsearchParseException("Failed to parse aliases", e);
    }
}
 
Example #23
Source File: PutIndexTemplateRequest.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the aliases that will be associated with the index when it gets created
 */
public PutIndexTemplateRequest aliases(BytesReference source) {
    // EMPTY is safe here because we never call namedObject
    try (XContentParser parser = XContentHelper
            .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source)) {
        //move to the first alias
        parser.nextToken();
        while ((parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            alias(Alias.fromXContent(parser));
        }
        return this;
    } catch (IOException e) {
        throw new ElasticsearchParseException("Failed to parse aliases", e);
    }
}
 
Example #24
Source File: OptionParser.java    From crate with Apache License 2.0 5 votes vote down vote up
@Nullable
private static org.apache.lucene.search.MultiTermQuery.RewriteMethod rewrite(@Nullable Object fuzzyRewrite) {
    if (fuzzyRewrite == null) {
        return null;
    }
    return QueryParsers.parseRewriteMethod(fuzzyRewrite.toString(), LoggingDeprecationHandler.INSTANCE);
}
 
Example #25
Source File: MatchPredicate.java    From crate with Apache License 2.0 5 votes vote down vote up
public static String getMatchType(@Nullable String matchType, DataType<?> columnType) {
    if (matchType == null) {
        return defaultMatchType(columnType);
    }
    if (columnType.equals(DataTypes.STRING)) {
        try {
            MultiMatchQueryType.parse(matchType, LoggingDeprecationHandler.INSTANCE);
            return matchType;
        } catch (ElasticsearchParseException e) {
            throw new IllegalArgumentException(String.format(
                Locale.ENGLISH,
                "invalid MATCH type '%s' for type '%s'",
                matchType,
                columnType), e);
        }
    } else if (columnType.equals(DataTypes.GEO_SHAPE)) {
        if (!SUPPORTED_GEO_MATCH_TYPES.contains(matchType)) {
            throw new IllegalArgumentException(String.format(
                Locale.ENGLISH,
                "invalid MATCH type '%s' for type '%s', valid types are: [%s]",
                matchType,
                columnType,
                String.join(",", SUPPORTED_GEO_MATCH_TYPES)));
        }
        return matchType;
    }
    throw new IllegalArgumentException("No match type for dataType: " + columnType);
}
 
Example #26
Source File: TestHelpers.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
public static SearchSourceBuilder randomFeatureQuery() throws IOException {
    String query = "{\"query\":{\"match\":{\"user\":{\"query\":\"kimchy\",\"operator\":\"OR\",\"prefix_length\":0,"
        + "\"max_expansions\":50,\"fuzzy_transpositions\":true,\"lenient\":false,\"zero_terms_query\":\"NONE\","
        + "\"auto_generate_synonyms_phrase_query\":true,\"boost\":1}}}}";
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    SearchModule searchModule = new SearchModule(Settings.EMPTY, false, Collections.emptyList());
    XContentParser parser = XContentType.JSON
        .xContent()
        .createParser(new NamedXContentRegistry(searchModule.getNamedXContents()), LoggingDeprecationHandler.INSTANCE, query);
    searchSourceBuilder.parseXContent(parser);
    return searchSourceBuilder;
}
 
Example #27
Source File: AbstractSearchAction.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
private RestResponseListener<SearchResponse> search(RestChannel channel, Class<T> clazz) {
    return new RestResponseListener<SearchResponse>(channel) {
        @Override
        public RestResponse buildResponse(SearchResponse response) throws Exception {
            if (response.isTimedOut()) {
                return new BytesRestResponse(RestStatus.REQUEST_TIMEOUT, response.toString());
            }

            if (clazz == AnomalyDetector.class) {
                for (SearchHit hit : response.getHits()) {
                    XContentParser parser = XContentType.JSON
                        .xContent()
                        .createParser(
                            channel.request().getXContentRegistry(),
                            LoggingDeprecationHandler.INSTANCE,
                            hit.getSourceAsString()
                        );
                    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);

                    // write back id and version to anomaly detector object
                    ToXContentObject xContentObject = AnomalyDetector.parse(parser, hit.getId(), hit.getVersion());
                    XContentBuilder builder = xContentObject.toXContent(jsonBuilder(), EMPTY_PARAMS);
                    hit.sourceRef(BytesReference.bytes(builder));
                }
            }

            return new BytesRestResponse(RestStatus.OK, response.toXContent(channel.newBuilder(), EMPTY_PARAMS));
        }
    };
}
 
Example #28
Source File: IndexFeatureStoreTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
private void assertNameAndTypes(StorableElement elt, BytesReference ref) throws IOException {
    XContentParser parser = XContentFactory.xContent(Requests.INDEX_CONTENT_TYPE).createParser(NamedXContentRegistry.EMPTY,
            LoggingDeprecationHandler.INSTANCE, ref.streamInput());
    Map<String,Object> map = parser.map();
    assertEquals(elt.name(), map.get("name"));
    assertEquals(elt.type(), map.get("type"));
    assertTrue(map.containsKey(elt.type()));
}
 
Example #29
Source File: StoredFeature.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
private XContentParser createParser(Object source, NamedXContentRegistry registry) throws IOException {
    if (source instanceof String) {
        return XContentFactory.xContent((String) source).createParser(registry, LoggingDeprecationHandler.INSTANCE, (String) source);
    } else if (source instanceof BytesReference) {
        BytesRef ref = ((BytesReference) source).toBytesRef();
        return XContentFactory.xContent(ref.bytes, ref.offset, ref.length)
                .createParser(registry, LoggingDeprecationHandler.INSTANCE,
                        ref.bytes, ref.offset, ref.length);
    } else if (source instanceof byte[]) {
        return XContentFactory.xContent((byte[]) source).createParser(registry, LoggingDeprecationHandler.INSTANCE, (byte[]) source);
    } else {
        throw new IllegalArgumentException("Template engine returned an unsupported object type [" +
                source.getClass().getCanonicalName() + "]");
    }
}
 
Example #30
Source File: AnomalyDetectorProfileRunner.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
private ActionListener<SearchResponse> onGetLatestAnomalyResult(ActionListener<DetectorProfile> listener, String detectorId) {
    return ActionListener.wrap(searchResponse -> {
        SearchHits hits = searchResponse.getHits();
        if (hits.getHits().length == 0L) {
            listener.onResponse(new DetectorProfile());
        } else {
            SearchHit hit = hits.getAt(0);

            try (
                XContentParser parser = XContentType.JSON
                    .xContent()
                    .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, hit.getSourceAsString())
            ) {
                ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
                AnomalyResult result = parser.namedObject(AnomalyResult.class, AnomalyResult.PARSE_FIELD_NAME, null);
                DetectorProfile profile = new DetectorProfile();
                if (result.getError() != null) {
                    profile.setError(result.getError());
                }
                listener.onResponse(profile);

            } catch (IOException | XContentParseException | NullPointerException e) {
                logger.error("Fail to parse anomaly result with " + hit.toString());
                listener.onFailure(new RuntimeException("Fail to find detector error: " + detectorId, e));
            }
        }
    }, exception -> {
        if (exception instanceof IndexNotFoundException) {
            listener.onResponse(new DetectorProfile());
        } else {
            logger.error("Fail to find any anomaly result after AD job enabled time for detector {}", detectorId);
            listener.onFailure(new RuntimeException("Fail to find detector error: " + detectorId, exception));
        }
    });
}