org.elasticsearch.common.xcontent.ToXContent.Params Java Examples

The following examples show how to use org.elasticsearch.common.xcontent.ToXContent.Params. 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: GenTermValuesHandler.java    From elasticsearch-taste with Apache License 2.0 5 votes vote down vote up
public MultiTermVectorsListener(final int numOfThread,
        final RequestHandler[] requestHandlers,
        final Params eventParams, final Map<String, DocInfo> idMap,
        final ExecutorService executor, final ESLogger logger) {
    this.requestHandlers = requestHandlers;
    this.eventParams = eventParams;
    this.idMap = idMap;
    this.executor = executor;
    this.logger = logger;
    this.numOfThread = numOfThread > 1 ? numOfThread : 1;
}
 
Example #2
Source File: SnapshotId.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    builder.field(NAME, name);
    builder.field(UUID, uuid);
    builder.endObject();
    return builder;
}
 
Example #3
Source File: RerouteExplanation.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    builder.field("command", command.name());
    builder.field("parameters", command);
    builder.startArray("decisions");
    decisions.toXContent(builder, params);
    builder.endArray();
    builder.endObject();
    return builder;
}
 
Example #4
Source File: RoutingExplanations.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startArray("explanations");
    for (RerouteExplanation explanation : explanations) {
        explanation.toXContent(builder, params);
    }
    builder.endArray();
    return builder;
}
 
Example #5
Source File: ShardAllocationDecision.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    if (allocateDecision.isDecisionTaken()) {
        allocateDecision.toXContent(builder, params);
    }
    if (moveDecision.isDecisionTaken()) {
        moveDecision.toXContent(builder, params);
    }
    return builder;
}
 
Example #6
Source File: PendingClusterStateStats.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject(Fields.QUEUE);
    builder.field(Fields.TOTAL, total);
    builder.field(Fields.PENDING, pending);
    builder.field(Fields.COMMITTED, committed);
    builder.endObject();
    return builder;
}
 
Example #7
Source File: UserRequestHandler.java    From elasticsearch-taste with Apache License 2.0 5 votes vote down vote up
private void doUserUpdate(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap,
        final Map<String, Object> userMap, final String index,
        final String type, final String userIdField,
        final String timestampField, final Long userId,
        final OpType opType, final RequestHandlerChain chain) {
    userMap.put(userIdField, userId);
    userMap.put(timestampField, new Date());

    final OnResponseListener<IndexResponse> responseListener = response -> {
        paramMap.put(userIdField, userId);
        chain.execute(params, listener, requestMap, paramMap);
    };
    final OnFailureListener failureListener = t -> {
        if (t instanceof DocumentAlreadyExistsException
                || t instanceof EsRejectedExecutionException) {
            sleep(t);
            execute(params, listener, requestMap, paramMap, chain);
        } else {
            listener.onError(t);
        }
    };
    client.prepareIndex(index, type, userId.toString()).setSource(userMap)
            .setRefresh(true).setOpType(opType)
            .execute(on(responseListener, failureListener));
}
 
Example #8
Source File: UserRequestHandler.java    From elasticsearch-taste with Apache License 2.0 5 votes vote down vote up
private void doUserIndexExists(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap, final RequestHandlerChain chain) {
    final String index = params.param(
            TasteConstants.REQUEST_PARAM_USER_INDEX, params.param("index"));

    try {
        indexCreationLock.lock();
        final IndicesExistsResponse indicesExistsResponse = client.admin()
                .indices().prepareExists(index).execute().actionGet();
        if (indicesExistsResponse.isExists()) {
            doUserMappingCreation(params, listener, requestMap, paramMap,
                    chain);
        } else {
            doUserIndexCreation(params, listener, requestMap, paramMap,
                    chain, index);
        }
    } catch (final Exception e) {
        final List<Throwable> errorList = getErrorList(paramMap);
        if (errorList.size() >= maxRetryCount) {
            listener.onError(e);
        } else {
            sleep(e);
            errorList.add(e);
            fork(() -> execute(params, listener, requestMap, paramMap,
                    chain));
        }
    } finally {
        indexCreationLock.unlock();
    }
}
 
Example #9
Source File: SettingsFilter.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static Settings filterSettings(Params params, Settings settings) {
    String patterns = params.param(SETTINGS_FILTER_PARAM);
    Settings filteredSettings = settings;
    if (patterns != null && patterns.isEmpty() == false) {
        filteredSettings = SettingsFilter.filterSettings(patterns, filteredSettings);
    }
    return filteredSettings;
}
 
Example #10
Source File: PreferenceRequestHandler.java    From elasticsearch-taste with Apache License 2.0 5 votes vote down vote up
private void doPreferenceIndexExists(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap, final RequestHandlerChain chain) {
    final String index = params.param("index");

    try {
        indexCreationLock.lock();
        final IndicesExistsResponse indicesExistsResponse = client.admin()
                .indices().prepareExists(index).execute().actionGet();
        if (indicesExistsResponse.isExists()) {
            doPreferenceMappingCreation(params, listener, requestMap,
                    paramMap, chain);
        } else {
            doPreferenceIndexCreation(params, listener, requestMap,
                    paramMap, chain, index);
        }
    } catch (final Exception e) {
        final List<Throwable> errorList = getErrorList(paramMap);
        if (errorList.size() >= maxRetryCount) {
            listener.onError(e);
        } else {
            sleep(e);
            errorList.add(e);
            fork(() -> execute(params, listener, requestMap, paramMap,
                    chain));
        }
    } finally {
        indexCreationLock.unlock();
    }
}
 
Example #11
Source File: ItemRequestHandler.java    From elasticsearch-taste with Apache License 2.0 5 votes vote down vote up
private void doItemUpdate(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap,
        final Map<String, Object> itemMap, final String index,
        final String type, final String itemIdField,
        final String timestampField, final Long itemId,
        final OpType opType, final RequestHandlerChain chain) {
    itemMap.put(itemIdField, itemId);
    itemMap.put(timestampField, new Date());
    final OnResponseListener<IndexResponse> responseListener = response -> {
        paramMap.put(itemIdField, itemId);
        chain.execute(params, listener, requestMap, paramMap);
    };
    final OnFailureListener failureListener = t -> {
        sleep(t);
        if (t instanceof DocumentAlreadyExistsException
                || t instanceof EsRejectedExecutionException) {
            execute(params, listener, requestMap, paramMap, chain);
        } else {
            listener.onError(t);
        }
    };
    client.prepareIndex(index, type, itemId.toString()).setSource(itemMap)
            .setRefresh(true).setOpType(opType)
            .execute(on(responseListener, failureListener));
}
 
Example #12
Source File: ItemRequestHandler.java    From elasticsearch-taste with Apache License 2.0 5 votes vote down vote up
private void doItemIndexExists(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap, final RequestHandlerChain chain) {
    final String index = params.param(
            TasteConstants.REQUEST_PARAM_ITEM_INDEX, params.param("index"));

    try {
        indexCreationLock.lock();
        final IndicesExistsResponse indicesExistsResponse = client.admin()
                .indices().prepareExists(index).execute().actionGet();
        if (indicesExistsResponse.isExists()) {
            doItemMappingCreation(params, listener, requestMap, paramMap,
                    chain);
        } else {
            doItemIndexCreation(params, listener, requestMap, paramMap,
                    chain, index);
        }
    } catch (final Exception e) {
        final List<Throwable> errorList = getErrorList(paramMap);
        if (errorList.size() >= maxRetryCount) {
            listener.onError(e);
        } else {
            sleep(e);
            errorList.add(e);
            fork(() -> execute(params, listener, requestMap, paramMap,
                    chain));
        }
    } finally {
        indexCreationLock.unlock();
    }
}
 
Example #13
Source File: RequestHandlerChain.java    From elasticsearch-taste with Apache License 2.0 5 votes vote down vote up
public void execute(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap) {
    synchronized (handlers) {
        if (position < handlers.length) {
            final RequestHandler handler = handlers[position];
            position++;
            handler.execute(params, listener, requestMap, paramMap, this);
        }
    }
}
 
Example #14
Source File: ReindexingService.java    From elasticsearch-reindexing with Apache License 2.0 5 votes vote down vote up
/**
 * Execute the reindexing
 *
 * @param params   Rest request
 * @param content  Content of rest request in {}
 * @param listener is to receive the response back
 * @return
 */
public String execute(final Params params, final BytesReference content, final ActionListener<Void> listener) {

    final String url = params.param("url");
    // set scroll to 1m if there is no
    final String scroll = params.param("scroll", "1m");
    final String fromIndex = params.param("index");
    final String fromType = params.param("type");
    final String toIndex = params.param("toindex");
    final String toType = params.param("totype");
    final String[] fields = params.paramAsBoolean("parent", true) ? new String[]{"_source", "_parent"} : new String[]{"_source"};
    final boolean deletion = params.paramAsBoolean("deletion", false);

    final ReindexingListener reindexingListener = new ReindexingListener(url, fromIndex, fromType, toIndex, toType, scroll, deletion, listener);

    // Create search request builder
    final SearchRequestBuilder builder = client.prepareSearch(fromIndex)
            .setScroll(scroll).addFields(fields);
    if (fromType != null && fromType.trim().length() > 0) {
        builder.setTypes(fromType.split(","));
    }
    if (content == null || content.length() == 0) {
        builder.setQuery(QueryBuilders.matchAllQuery()).setSize(
                Integer.parseInt(params.param("size", "1000")));
    } else {
        builder.setExtraSource(content);
    }
    builder.execute(reindexingListener);  // async
    reindexingListenerMap.put(reindexingListener.getName(), reindexingListener);
    return reindexingListener.getName();
}
 
Example #15
Source File: TopK.java    From elasticsearch-topk-plugin with Apache License 2.0 5 votes vote down vote up
void toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    builder.field(CommonFields.KEY, term);
    builder.field(CommonFields.DOC_COUNT, count);
    aggregations.toXContentInternal(builder, params);
    builder.endObject();
}
 
Example #16
Source File: XContentHelper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a "raw" (bytes) field, handling cases where the bytes are compressed, and tries to optimize writing using
 * {@link XContentBuilder#rawField(String, org.elasticsearch.common.bytes.BytesReference)}.
 */
public static void writeRawField(String field, BytesReference source, XContentBuilder builder, ToXContent.Params params) throws IOException {
    Compressor compressor = CompressorFactory.compressor(source);
    if (compressor != null) {
        InputStream compressedStreamInput = compressor.streamInput(source.streamInput());
        builder.rawField(field, compressedStreamInput);
    } else {
        builder.rawField(field, source);
    }
}
 
Example #17
Source File: PreferenceRequestHandler.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
private void doPreferenceMappingCreation(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap, final RequestHandlerChain chain) {
    final String index = params.param("index");
    final String type = params
            .param("type", TasteConstants.PREFERENCE_TYPE);
    final String userIdField = params.param(
            TasteConstants.REQUEST_PARAM_USER_ID_FIELD,
            TasteConstants.USER_ID_FIELD);
    final String itemIdField = params.param(
            TasteConstants.REQUEST_PARAM_ITEM_ID_FIELD,
            TasteConstants.ITEM_ID_FIELD);
    final String valueField = params.param(
            TasteConstants.REQUEST_PARAM_VALUE_FIELD,
            TasteConstants.VALUE_FIELD);
    final String timestampField = params.param(
            TasteConstants.REQUEST_PARAM_TIMESTAMP_FIELD,
            TasteConstants.TIMESTAMP_FIELD);

    try (XContentBuilder jsonBuilder = XContentFactory.jsonBuilder()) {
        final ClusterHealthResponse healthResponse = client
                .admin()
                .cluster()
                .prepareHealth(index)
                .setWaitForYellowStatus()
                .setTimeout(
                        params.param("timeout",
                                DEFAULT_HEALTH_REQUEST_TIMEOUT)).execute()
                .actionGet();
        if (healthResponse.isTimedOut()) {
            listener.onError(new OperationFailedException(
                    "Failed to create index: " + index + "/" + type));
        }

        final XContentBuilder builder = jsonBuilder//
                .startObject()//
                .startObject(type)//
                .startObject("properties")//

                // @timestamp
                .startObject(timestampField)//
                .field("type", "date")//
                .field("format", "date_optional_time")//
                .endObject()//

                // user_id
                .startObject(userIdField)//
                .field("type", "long")//
                .endObject()//

                // item_id
                .startObject(itemIdField)//
                .field("type", "long")//
                .endObject()//

                // value
                .startObject(valueField)//
                .field("type", "double")//
                .endObject()//

                .endObject()//
                .endObject()//
                .endObject();

        final PutMappingResponse mappingResponse = client.admin().indices()
                .preparePutMapping(index).setType(type).setSource(builder)
                .execute().actionGet();
        if (mappingResponse.isAcknowledged()) {
            fork(() -> execute(params, listener, requestMap, paramMap,
                    chain));
        } else {
            listener.onError(new OperationFailedException(
                    "Failed to create mapping for " + index + "/" + type));
        }
    } catch (final Exception e) {
        listener.onError(e);
    }
}
 
Example #18
Source File: DefaultRequestHandler.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
@Override
public abstract void execute(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap, RequestHandlerChain chain);
 
Example #19
Source File: ItemRequestHandler.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
private void doItemCreation(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap,
        final Map<String, Object> itemMap, final String index,
        final String type, final String itemIdField,
        final String timestampField, final RequestHandlerChain chain) {
    final OnResponseListener<SearchResponse> responseListener = response -> {
        validateRespose(response);

        Number currentId = null;
        final SearchHits hits = response.getHits();
        if (hits.getTotalHits() != 0) {
            final SearchHit[] searchHits = hits.getHits();
            final SearchHitField field = searchHits[0].getFields().get(
                    itemIdField);
            if (field != null) {
                currentId = field.getValue();
            }
        }
        final Long itemId;
        if (currentId == null) {
            itemId = Long.valueOf(1);
        } else {
            itemId = Long.valueOf(currentId.longValue() + 1);
        }
        doItemUpdate(params, listener, requestMap, paramMap, itemMap,
                index, type, itemIdField, timestampField, itemId,
                OpType.CREATE, chain);
    };
    final OnFailureListener failureListener = t -> {
        final List<Throwable> errorList = getErrorList(paramMap);
        if (errorList.size() >= maxRetryCount) {
            listener.onError(t);
        } else {
            sleep(t);
            errorList.add(t);
            doItemIndexExists(params, listener, requestMap, paramMap,
                    chain);
        }
    };
    client.prepareSearch(index).setTypes(type)
            .setQuery(QueryBuilders.matchAllQuery()).addField(itemIdField)
            .addSort(itemIdField, SortOrder.DESC).setSize(1)
            .execute(on(responseListener, failureListener));
}
 
Example #20
Source File: UserRequestHandler.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
private void doUserMappingCreation(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap, final RequestHandlerChain chain) {
    final String index = params.param(
            TasteConstants.REQUEST_PARAM_USER_INDEX, params.param("index"));
    final String type = params.param(
            TasteConstants.REQUEST_PARAM_USER_TYPE,
            TasteConstants.USER_TYPE);
    final String userIdField = params.param(
            TasteConstants.REQUEST_PARAM_USER_ID_FIELD,
            TasteConstants.USER_ID_FIELD);
    final String timestampField = params.param(
            TasteConstants.REQUEST_PARAM_TIMESTAMP_FIELD,
            TasteConstants.TIMESTAMP_FIELD);

    try (XContentBuilder jsonBuilder = XContentFactory.jsonBuilder()) {
        final ClusterHealthResponse healthResponse = client
                .admin()
                .cluster()
                .prepareHealth(index)
                .setWaitForYellowStatus()
                .setTimeout(
                        params.param("timeout",
                                DEFAULT_HEALTH_REQUEST_TIMEOUT)).execute()
                .actionGet();
        if (healthResponse.isTimedOut()) {
            listener.onError(new OperationFailedException(
                    "Failed to create index: " + index + "/" + type));
        }

        final XContentBuilder builder = jsonBuilder//
                .startObject()//
                .startObject(type)//
                .startObject("properties")//

                // @timestamp
                .startObject(timestampField)//
                .field("type", "date")//
                .field("format", "date_optional_time")//
                .endObject()//

                // user_id
                .startObject(userIdField)//
                .field("type", "long")//
                .endObject()//

                // system_id
                .startObject("system_id")//
                .field("type", "string")//
                .field("index", "not_analyzed")//
                .endObject()//

                .endObject()//
                .endObject()//
                .endObject();

        final PutMappingResponse mappingResponse = client.admin().indices()
                .preparePutMapping(index).setType(type).setSource(builder)
                .execute().actionGet();
        if (mappingResponse.isAcknowledged()) {
            fork(() -> execute(params, listener, requestMap, paramMap,
                    chain));
        } else {
            listener.onError(new OperationFailedException(
                    "Failed to create mapping for " + index + "/" + type));
        }
    } catch (final Exception e) {
        listener.onError(e);
    }
}
 
Example #21
Source File: UserRequestHandler.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
private void doUserCreation(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap,
        final Map<String, Object> userMap, final String index,
        final String type, final String userIdField,
        final String timestampField, final RequestHandlerChain chain) {
    final OnResponseListener<SearchResponse> responseListener = response -> {
        validateRespose(response);

        Number currentId = null;
        final SearchHits hits = response.getHits();
        if (hits.getTotalHits() != 0) {
            final SearchHit[] searchHits = hits.getHits();
            final SearchHitField field = searchHits[0].getFields().get(
                    userIdField);
            if (field != null) {
                currentId = field.getValue();
            }
        }
        final Long userId;
        if (currentId == null) {
            userId = Long.valueOf(1);
        } else {
            userId = Long.valueOf(currentId.longValue() + 1);
        }
        doUserUpdate(params, listener, requestMap, paramMap, userMap,
                index, type, userIdField, timestampField, userId,
                OpType.CREATE, chain);
    };
    final OnFailureListener failureListener = t -> {
        final List<Throwable> errorList = getErrorList(paramMap);
        if (errorList.size() >= maxRetryCount) {
            listener.onError(t);
        } else {
            sleep(t);
            errorList.add(t);
            doUserIndexExists(params, listener, requestMap, paramMap,
                    chain);
        }
    };
    client.prepareSearch(index).setTypes(type)
            .setQuery(QueryBuilders.matchAllQuery()).addField(userIdField)
            .addSort(userIdField, SortOrder.DESC).setSize(1)
            .execute(on(responseListener, failureListener));
}
 
Example #22
Source File: ItemRequestHandler.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
private void doItemMappingCreation(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap, final RequestHandlerChain chain) {
    final String index = params.param(
            TasteConstants.REQUEST_PARAM_ITEM_INDEX, params.param("index"));
    final String type = params.param(
            TasteConstants.REQUEST_PARAM_ITEM_TYPE,
            TasteConstants.ITEM_TYPE);
    final String itemIdField = params.param(
            TasteConstants.REQUEST_PARAM_ITEM_ID_FIELD,
            TasteConstants.ITEM_ID_FIELD);
    final String timestampField = params.param(
            TasteConstants.REQUEST_PARAM_TIMESTAMP_FIELD,
            TasteConstants.TIMESTAMP_FIELD);

    try (XContentBuilder jsonBuilder = XContentFactory.jsonBuilder()) {
        final ClusterHealthResponse healthResponse = client
                .admin()
                .cluster()
                .prepareHealth(index)
                .setWaitForYellowStatus()
                .setTimeout(
                        params.param("timeout",
                                DEFAULT_HEALTH_REQUEST_TIMEOUT)).execute()
                .actionGet();
        if (healthResponse.isTimedOut()) {
            listener.onError(new OperationFailedException(
                    "Failed to create index: " + index + "/" + type));
        }

        final XContentBuilder builder = jsonBuilder//
                .startObject()//
                .startObject(type)//
                .startObject("properties")//

                // @timestamp
                .startObject(timestampField)//
                .field("type", "date")//
                .field("format", "date_optional_time")//
                .endObject()//

                // item_id
                .startObject(itemIdField)//
                .field("type", "long")//
                .endObject()//

                // system_id
                .startObject("system_id")//
                .field("type", "string")//
                .field("index", "not_analyzed")//
                .endObject()//

                .endObject()//
                .endObject()//
                .endObject();

        final PutMappingResponse mappingResponse = client.admin().indices()
                .preparePutMapping(index).setType(type).setSource(builder)
                .execute().actionGet();
        if (mappingResponse.isAcknowledged()) {
            fork(() -> execute(params, listener, requestMap, paramMap,
                    chain));
        } else {
            listener.onError(new OperationFailedException(
                    "Failed to create mapping for " + index + "/" + type));
        }
    } catch (final Exception e) {
        listener.onError(e);
    }
}
 
Example #23
Source File: RequestHandler.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
void execute(Params params, RequestHandler.OnErrorListener listener,
Map<String, Object> requestMap, Map<String, Object> paramMap,
RequestHandlerChain chain);