Java Code Examples for org.elasticsearch.common.xcontent.ToXContent.Params#param()

The following examples show how to use org.elasticsearch.common.xcontent.ToXContent.Params#param() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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);
    }
}