本文整理汇总了Java中org.elasticsearch.client.RestClient.performRequest方法的典型用法代码示例。如果您正苦于以下问题:Java RestClient.performRequest方法的具体用法?Java RestClient.performRequest怎么用?Java RestClient.performRequest使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.elasticsearch.client.RestClient
的用法示例。
在下文中一共展示了RestClient.performRequest方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: bulkIndexMember
import org.elasticsearch.client.RestClient; //导入方法依赖的package包/类
private void bulkIndexMember(List<?> memList) throws Exception {
StringBuilder buf = new StringBuilder(1024);
for (Object mem : memList) {
buf.append("{\"index\": {}}");
buf.append("\n");
buf.append(Gson.toJson(mem));
buf.append("\n");
}
long startTime = System.currentTimeMillis();
RestClient client = Plugin.client;
HttpEntity entity = new NStringEntity(buf.toString(), ContentType.APPLICATION_JSON);
Response indexResponse = client.performRequest("POST", "/weike/member/_bulk",
Collections.<String, String>emptyMap(), entity);
if (LOG.isDebugEnabled()) {
LOG.debug("indexMember {}ms", System.currentTimeMillis() - startTime);
LOG.debug("indexResponse {}", indexResponse.toString());
}
}
示例2: buildShards
import org.elasticsearch.client.RestClient; //导入方法依赖的package包/类
private List<Shard> buildShards(Nodes nodes, RestClient client) throws IOException {
Response response = client.performRequest("GET", "test/_stats", singletonMap("level", "shards"));
List<Object> shardStats = ObjectPath.createFromResponse(response).evaluate("indices.test.shards.0");
ArrayList<Shard> shards = new ArrayList<>();
for (Object shard : shardStats) {
final String nodeId = ObjectPath.evaluate(shard, "routing.node");
final Boolean primary = ObjectPath.evaluate(shard, "routing.primary");
final Node node = nodes.getSafe(nodeId);
final SeqNoStats seqNoStats;
if (node.getVersion().onOrAfter(Version.V_6_0_0_alpha1_UNRELEASED)) {
Integer maxSeqNo = ObjectPath.evaluate(shard, "seq_no.max_seq_no");
Integer localCheckpoint = ObjectPath.evaluate(shard, "seq_no.local_checkpoint");
Integer globalCheckpoint = ObjectPath.evaluate(shard, "seq_no.global_checkpoint");
seqNoStats = new SeqNoStats(maxSeqNo, localCheckpoint, globalCheckpoint);
} else {
seqNoStats = null;
}
shards.add(new Shard(node, primary, seqNoStats));
}
return shards;
}
示例3: readVersionsFromInfo
import org.elasticsearch.client.RestClient; //导入方法依赖的package包/类
private static Version readVersionsFromInfo(RestClient restClient, int numHosts) throws IOException {
Version version = null;
for (int i = 0; i < numHosts; i++) {
//we don't really use the urls here, we rely on the client doing round-robin to touch all the nodes in the cluster
Response response = restClient.performRequest("GET", "/");
ClientYamlTestResponse restTestResponse = new ClientYamlTestResponse(response);
Object latestVersion = restTestResponse.evaluate("version.number");
if (latestVersion == null) {
throw new RuntimeException("elasticsearch version not found in the response");
}
final Version currentVersion = Version.fromString(latestVersion.toString());
if (version == null) {
version = currentVersion;
} else if (version.onOrAfter(currentVersion)) {
version = currentVersion;
}
}
return version;
}
示例4: doDeactivate
import org.elasticsearch.client.RestClient; //导入方法依赖的package包/类
private ReplicationResult doDeactivate(TransportContext ctx, ReplicationTransaction tx, RestClient restClient) throws ReplicationException, JSONException, IOException {
ReplicationLog log = tx.getLog();
ObjectMapper mapper = new ObjectMapper();
IndexEntry content = mapper.readValue(tx.getContent().getInputStream(), IndexEntry.class);
Response deleteResponse = restClient.performRequest(
"DELETE",
"/" + content.getIndex() + "/" + content.getType() + "/" + DigestUtils.md5Hex(content.getPath()),
Collections.<String, String>emptyMap());
LOG.debug(deleteResponse.toString());
log.info(getClass().getSimpleName() + ": Delete Call returned " + deleteResponse.getStatusLine().getStatusCode() + ": " + deleteResponse.getStatusLine().getReasonPhrase());
if (deleteResponse.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED || deleteResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return ReplicationResult.OK;
}
LOG.error("Could not delete " + content.getType() + " at " + content.getPath());
return new ReplicationResult(false, 0, "Replication failed");
}
示例5: doActivate
import org.elasticsearch.client.RestClient; //导入方法依赖的package包/类
/**
* Perform the replication. All logic is covered in {@link ElasticSearchIndexContentBuilder} so we only need to transmit the JSON to ElasticSearch
*
* @param ctx
* @param tx
* @param restClient
* @return
* @throws ReplicationException
*/
private ReplicationResult doActivate(TransportContext ctx, ReplicationTransaction tx, RestClient restClient) throws ReplicationException, JSONException, IOException {
ReplicationLog log = tx.getLog();
ObjectMapper mapper = new ObjectMapper();
IndexEntry content = mapper.readValue(tx.getContent().getInputStream(), IndexEntry.class);
if (content != null) {
log.info(getClass().getSimpleName() + ": Indexing " + content.getPath());
String contentString = mapper.writeValueAsString(content.getContent());
log.debug(getClass().getSimpleName() + ": Index-Content: " + contentString);
LOG.debug("Index-Content: " + contentString);
HttpEntity entity = new NStringEntity(contentString, "UTF-8");
Response indexResponse = restClient.performRequest(
"PUT",
"/" + content.getIndex() + "/" + content.getType() + "/" + DigestUtils.md5Hex(content.getPath()),
Collections.<String, String>emptyMap(),
entity);
LOG.debug(indexResponse.toString());
log.info(getClass().getSimpleName() + ": " + indexResponse.getStatusLine().getStatusCode() + ": " + indexResponse.getStatusLine().getReasonPhrase());
if (indexResponse.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED || indexResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return ReplicationResult.OK;
}
}
LOG.error("Could not replicate");
return new ReplicationResult(false, 0, "Replication failed");
}
示例6: insertTestDocuments
import org.elasticsearch.client.RestClient; //导入方法依赖的package包/类
/** Inserts the given number of test documents into Elasticsearch. */
static void insertTestDocuments(ConnectionConfiguration connectionConfiguration,
long numDocs, RestClient restClient) throws IOException {
List<String> data =
ElasticSearchIOTestUtils.createDocuments(
numDocs, ElasticSearchIOTestUtils.InjectionMode.DO_NOT_INJECT_INVALID_DOCS);
StringBuilder bulkRequest = new StringBuilder();
int i = 0;
for (String document : data) {
bulkRequest.append(String.format(
"{ \"index\" : { \"_index\" : \"%s\", \"_type\" : \"%s\", \"_id\" : \"%s\" } }%n%s%n",
connectionConfiguration.getIndex(), connectionConfiguration.getType(), i++, document));
}
String endPoint = String.format("/%s/%s/_bulk", connectionConfiguration.getIndex(),
connectionConfiguration.getType());
HttpEntity requestBody =
new NStringEntity(bulkRequest.toString(), ContentType.APPLICATION_JSON);
Response response = restClient.performRequest("POST", endPoint,
Collections.singletonMap("refresh", "true"), requestBody);
ElasticsearchIO
.checkForErrors(response, ElasticsearchIO.getBackendVersion(connectionConfiguration));
}
示例7: refreshIndexAndGetCurrentNumDocs
import org.elasticsearch.client.RestClient; //导入方法依赖的package包/类
/**
* Forces a refresh of the given index to make recently inserted documents available for search.
*
* @return The number of docs in the index
*/
static long refreshIndexAndGetCurrentNumDocs(
ConnectionConfiguration connectionConfiguration, RestClient restClient) throws IOException {
long result = 0;
try {
String endPoint = String.format("/%s/_refresh", connectionConfiguration.getIndex());
restClient.performRequest("POST", endPoint);
endPoint = String.format("/%s/%s/_search", connectionConfiguration.getIndex(),
connectionConfiguration.getType());
Response response = restClient.performRequest("GET", endPoint);
JsonNode searchResult = ElasticsearchIO.parseResponse(response);
result = searchResult.path("hits").path("total").asLong();
} catch (IOException e) {
// it is fine to ignore bellow exceptions because in testWriteWithBatchSize* sometimes,
// we call upgrade before any doc have been written
// (when there are fewer docs processed than batchSize).
// In that cases index/type has not been created (created upon first doc insertion)
if (!e.getMessage().contains("index_not_found_exception")){
throw e;
}
}
return result;
}
示例8: init
import org.elasticsearch.client.RestClient; //导入方法依赖的package包/类
@Before
public void init() throws IOException, ExecutionException, InterruptedException {
client = ElasticsearchTestUtils.createClient(ElasticsearchTestConstants.HOSTS.split(":")[0],
ElasticsearchTestConstants.TRANSPORT_PORT, ElasticsearchTestConstants.CLUSTER_NAME);
datastoreProperties = new ElasticsearchDatastoreProperties("datastore");
datastoreProperties.init();
datastoreProperties.nodes.setValue(ElasticsearchTestConstants.HOSTS);
RestClient restClient = ElasticsearchConnection.createClient(datastoreProperties);
BasicHeader emptyHeader = new BasicHeader("", "");
Map<String, String> emptyParams = new HashMap<>();
ElasticsearchTestUtils.deleteIndex(INDEX_NAME, client);
Response checkExistsResponse = restClient.performRequest("HEAD", "/" + INDEX_NAME, emptyParams);
ElasticsearchResponse checkExists = new ElasticsearchResponse(checkExistsResponse);
if (!checkExists.isOk()) {
// create index for test, name is 'beam'
restClient.performRequest("PUT", "/" + INDEX_NAME, emptyHeader);
}
}
示例9: indexMember
import org.elasticsearch.client.RestClient; //导入方法依赖的package包/类
private void indexMember(String sellerId, Object mem) throws IOException {
if (sellerId == null)
sellerId = "";
long startTime = System.currentTimeMillis();
RestClient client = Plugin.client;
String json = Gson.toJson(mem);
HttpEntity entity = new NStringEntity(json, ContentType.APPLICATION_JSON);
String path = "/weike/member";
if (!"".equals(sellerId)) {
path += "?routing=" + sellerId;
}
Response indexResponse = client.performRequest("POST", path, Collections.<String, String>emptyMap(), entity);
if (LOG.isDebugEnabled()) {
LOG.debug("indexMember {}ms", System.currentTimeMillis() - startTime);
LOG.debug("indexResponse {}", indexResponse.toString());
}
}
示例10: testUncompressedResponseByDefault
import org.elasticsearch.client.RestClient; //导入方法依赖的package包/类
public void testUncompressedResponseByDefault() throws IOException {
RestClient client = client();
Response response = client.performRequest("GET", "/");
assertEquals(200, response.getStatusLine().getStatusCode());
assertNull(response.getHeader(HttpHeaders.CONTENT_ENCODING));
response = client.performRequest("POST", "/company/employees/1", Collections.emptyMap(), SAMPLE_DOCUMENT);
assertEquals(201, response.getStatusLine().getStatusCode());
assertNull(response.getHeader(HttpHeaders.CONTENT_ENCODING));
}
示例11: doTest
import org.elasticsearch.client.RestClient; //导入方法依赖的package包/类
/**
* Test Connection to ElasticSearch by getting basic informations
*
* @param ctx
* @param tx
* @param restClient
* @return
* @throws ReplicationException
*/
private ReplicationResult doTest(TransportContext ctx, ReplicationTransaction tx, RestClient restClient) throws ReplicationException, IOException {
ReplicationLog log = tx.getLog();
Response response = restClient.performRequest("GET", "/", Collections.singletonMap("pretty", "true"));
log.info(getClass().getSimpleName() + ": ---------------------------------------");
log.info(getClass().getSimpleName() + ": " + response.toString());
log.info(getClass().getSimpleName() + ": " + EntityUtils.toString(response.getEntity()));
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return ReplicationResult.OK;
}
return new ReplicationResult(false, 0, "Replication test failed");
}
示例12: elasticsearchTest
import org.elasticsearch.client.RestClient; //导入方法依赖的package包/类
@Test
public void elasticsearchTest() throws IOException {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("elastic", "changeme"));
RestClient client = RestClient.builder(getElasticsearchResource().getHost())
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
.build();
Response response = client.performRequest("GET", "/");
assertThat(response.getStatusLine().getStatusCode(), is(200));
}
开发者ID:dadoonet,项目名称:testcontainers-java-module-elasticsearch,代码行数:13,代码来源:ElasticsearchResourceBaseTest.java
示例13: elasticsearchTest
import org.elasticsearch.client.RestClient; //导入方法依赖的package包/类
@Test
public void elasticsearchTest() throws IOException {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("elastic", "changeme"));
RestClient client = RestClient.builder(container.getHost())
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
.build();
Response response = client.performRequest("GET", "/");
assertThat(response.getStatusLine().getStatusCode(), is(200));
}
开发者ID:dadoonet,项目名称:testcontainers-java-module-elasticsearch,代码行数:14,代码来源:ElasticsearchContainerTest.java
示例14: main
import org.elasticsearch.client.RestClient; //导入方法依赖的package包/类
public static void main(String[] args) {
try {
RestClient client = null;
String url = "localhost";
int port = 9200;
CredentialsProvider credentialsProvider = setCredentials("elastic", "changeme");
RestClient restClient = createRestClient(url, port, credentialsProvider);
System.out.println("Connected");
//index a document
HttpEntity entity = new NStringEntity(
"{\n"
+ " \"user\" : \"kimchy\",\n"
+ " \"post_date\" : \"2009-11-15T14:12:12\",\n"
+ " \"message\" : \"trying out Elasticsearch\"\n"
+ "}", ContentType.APPLICATION_JSON);
Response indexResponse = restClient.performRequest(
"PUT",
"/mds/test/12345678",
Collections.<String, String>emptyMap(),
entity);
System.out.println(indexResponse.toString());
} catch (IOException ex) {
Logger.getLogger(RestClientTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例15: deleteIndex
import org.elasticsearch.client.RestClient; //导入方法依赖的package包/类
/** Deletes the given index synchronously. */
static void deleteIndex(ConnectionConfiguration connectionConfiguration,
RestClient restClient) throws IOException {
try {
restClient.performRequest("DELETE", String.format("/%s", connectionConfiguration.getIndex()));
} catch (IOException e) {
// it is fine to ignore this expression as deleteIndex occurs in @before,
// so when the first tests is run, the index does not exist yet
if (!e.getMessage().contains("index_not_found_exception")){
throw e;
}
}
}