org.elasticsearch.action.admin.indices.open.OpenIndexRequest Java Examples

The following examples show how to use org.elasticsearch.action.admin.indices.open.OpenIndexRequest. 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: CrudDemo.java    From javabase with Apache License 2.0 6 votes vote down vote up
/**
 * 索引的相关操作
 *
 * @param indicesAdminClient
 * @param indexName
 * @throws IOException
 */
private static void indexConfig(IndicesAdminClient indicesAdminClient, String indexName) throws IOException {
    //settings 设置
    String settings = getIndexSetting();
    // PUT /my_temp_index/_settings updatesettings
    showIndexSettings(indicesAdminClient,indexName);
    UpdateSettingsResponse updateSettingsResponse = indicesAdminClient.prepareUpdateSettings(indexName).setSettings(settings).execute().actionGet();
    log.info("更新 index setting:{}", updateSettingsResponse);


    //更新索引settings之前要关闭索引
    indicesAdminClient.close(new CloseIndexRequest().indices(indexName)).actionGet();
    //配置拼音自定义分析器
    indicesAdminClient.prepareUpdateSettings(indexName).setSettings(getIndexPinYinSetting()).execute().actionGet();
    //自定义分析器
    indicesAdminClient.prepareUpdateSettings(indexName).setSettings(getIndexDemoSetting()).execute().actionGet();
    //打开索引
    indicesAdminClient.open(new OpenIndexRequest().indices(indexName)).actionGet();

    //索引别名映射
    createAliasIndex(indicesAdminClient);

    showIndexSettings(indicesAdminClient,indexName);
}
 
Example #2
Source File: RestOpenIndexAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    OpenIndexRequest openIndexRequest = new OpenIndexRequest(Strings.splitStringByCommaToArray(request.param("index")));
    openIndexRequest.timeout(request.paramAsTime("timeout", openIndexRequest.timeout()));
    openIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", openIndexRequest.masterNodeTimeout()));
    openIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, openIndexRequest.indicesOptions()));
    client.admin().indices().open(openIndexRequest, new AcknowledgedRestListener<OpenIndexResponse>(channel));
}
 
Example #3
Source File: OpenIndexRequestBuilder.java    From elasticshell with Apache License 2.0 5 votes vote down vote up
@Override
protected XContentBuilder toXContent(OpenIndexRequest request, OpenIndexResponse response, XContentBuilder builder) throws IOException {
    return builder.startObject()
            .field(Fields.OK, true)
            .field(Fields.ACKNOWLEDGED, response.isAcknowledged())
            .endObject();
}
 
Example #4
Source File: Test.java    From dht-spider with MIT License 4 votes vote down vote up
public static void openIndex() throws Exception{
    OpenIndexRequest request = new OpenIndexRequest("haha");

    AcknowledgedResponse closeIndexResponse = client.indices().open(request, RequestOptions.DEFAULT);
    System.out.println(closeIndexResponse.isAcknowledged());
}
 
Example #5
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ActionFuture<OpenIndexResponse> open(final OpenIndexRequest request) {
    return execute(OpenIndexAction.INSTANCE, request);
}
 
Example #6
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void open(final OpenIndexRequest request, final ActionListener<OpenIndexResponse> listener) {
    execute(OpenIndexAction.INSTANCE, request, listener);
}
 
Example #7
Source File: RequestUtils.java    From ranger with Apache License 2.0 4 votes vote down vote up
public static <Request extends ActionRequest> List<String> getIndexFromRequest(Request request) {
	List<String> indexs = new ArrayList<>();

	if (request instanceof SingleShardRequest) {
		indexs.add(((SingleShardRequest<?>) request).index());
		return indexs;
	}

	if (request instanceof ReplicationRequest) {
		indexs.add(((ReplicationRequest<?>) request).index());
		return indexs;
	}

	if (request instanceof InstanceShardOperationRequest) {
		indexs.add(((InstanceShardOperationRequest<?>) request).index());
		return indexs;
	}

	if (request instanceof CreateIndexRequest) {
		indexs.add(((CreateIndexRequest) request).index());
		return indexs;
	}

	if (request instanceof PutMappingRequest) {
		indexs.add(((PutMappingRequest) request).getConcreteIndex().getName());
		return indexs;
	}

	if (request instanceof SearchRequest) {
		return Arrays.asList(((SearchRequest) request).indices());
	}

	if (request instanceof IndicesStatsRequest) {
		return Arrays.asList(((IndicesStatsRequest) request).indices());
	}

	if (request instanceof OpenIndexRequest) {
		return Arrays.asList(((OpenIndexRequest) request).indices());
	}

	if (request instanceof DeleteIndexRequest) {
		return Arrays.asList(((DeleteIndexRequest) request).indices());
	}

	if (request instanceof BulkRequest) {
		@SuppressWarnings("rawtypes") List<DocWriteRequest<?>> requests = ((BulkRequest) request).requests();

		if (CollectionUtils.isNotEmpty(requests)) {
			for (DocWriteRequest<?> docWriteRequest : requests) {
				indexs.add(docWriteRequest.index());
			}
			return indexs;
		}
	}

	if (request instanceof MultiGetRequest) {
		List<Item> items = ((MultiGetRequest) request).getItems();
		if (CollectionUtils.isNotEmpty(items)) {
			for (Item item : items) {
				indexs.add(item.index());
			}
			return indexs;
		}
	}

	// No matched request type to find specific index , set default value *
	indexs.add("*");
	return indexs;
}
 
Example #8
Source File: ElasticSearchAuditDestination.java    From ranger with Apache License 2.0 4 votes vote down vote up
private RestHighLevelClient newClient() {
    try {
        final CredentialsProvider credentialsProvider;
        if(!user.isEmpty()) {
            credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(user, password));
        } else {
            credentialsProvider = null;
        }

        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
                RestClient.builder(
                        MiscUtil.toArray(hosts, ",").stream()
                                .map(x -> new HttpHost(x, port, protocol))
                                .<HttpHost>toArray(i -> new HttpHost[i])
                ).setHttpClientConfigCallback(clientBuilder ->
                        (credentialsProvider != null) ? clientBuilder.setDefaultCredentialsProvider(credentialsProvider) : clientBuilder));
        LOG.debug("Initialized client");
        boolean exits = false;
        try {
            exits = restHighLevelClient.indices().open(new OpenIndexRequest(this.index), RequestOptions.DEFAULT).isShardsAcknowledged();
        } catch (Exception e) {
            LOG.warn("Error validating index " + this.index);
        }
        if(exits) {
            LOG.debug("Index exists");
        } else {
            LOG.info("Index does not exist");
        }
        return restHighLevelClient;
    } catch (Throwable t) {
        lastLoggedAt.updateAndGet(lastLoggedAt -> {
            long now = System.currentTimeMillis();
            long elapsed = now - lastLoggedAt;
            if (elapsed > TimeUnit.MINUTES.toMillis(1)) {
                LOG.fatal("Can't connect to ElasticSearch server: " + connectionString(), t);
                return now;
            } else {
                return lastLoggedAt;
            }
        });
        return null;
    }
}
 
Example #9
Source File: OpenIndexRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
public OpenIndexRequestBuilder(Client client, JsonToString<JsonInput> jsonToString, StringToJson<JsonOutput> stringToJson) {
    super(client, new OpenIndexRequest(null), jsonToString, stringToJson);
}
 
Example #10
Source File: OpenIndexRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
@Override
protected ActionFuture<OpenIndexResponse> doExecute(OpenIndexRequest request) {
    return client.admin().indices().open(request);
}
 
Example #11
Source File: IndicesAdminClient.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Open an index based on the index name.
 *
 * @param request The close index request
 * @return The result future
 * @see org.elasticsearch.client.Requests#openIndexRequest(String)
 */
ActionFuture<OpenIndexResponse> open(OpenIndexRequest request);
 
Example #12
Source File: IndicesAdminClient.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Open an index based on the index name.
 *
 * @param request  The close index request
 * @param listener A listener to be notified with a result
 * @see org.elasticsearch.client.Requests#openIndexRequest(String)
 */
void open(OpenIndexRequest request, ActionListener<OpenIndexResponse> listener);
 
Example #13
Source File: Requests.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an open index request.
 *
 * @param index The index to open
 * @return The delete index request
 * @see org.elasticsearch.client.IndicesAdminClient#open(org.elasticsearch.action.admin.indices.open.OpenIndexRequest)
 */
public static OpenIndexRequest openIndexRequest(String index) {
    return new OpenIndexRequest(index);
}