Java Code Examples for org.elasticsearch.client.RestClient#performRequest()

The following examples show how to use org.elasticsearch.client.RestClient#performRequest() . 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: TestHelpers.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public static Response makeRequest(
    RestClient client,
    String method,
    String endpoint,
    Map<String, String> params,
    HttpEntity entity,
    List<Header> headers
) throws IOException {
    Request request = new Request(method, endpoint);

    RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
    if (headers != null) {
        headers.forEach(header -> options.addHeader(header.getName(), header.getValue()));
    }
    request.setOptions(options.build());

    if (params != null) {
        params.entrySet().forEach(it -> request.addParameter(it.getKey(), it.getValue()));
    }
    if (entity != null) {
        request.setEntity(entity);
    }
    return client.performRequest(request);
}
 
Example 2
Source File: ElasticsearchCollectorTest.java    From karaf-decanter with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000L)
public void testAll() throws Exception {
    HttpHost host = new HttpHost(HOST, HTTP_PORT, "http");
    RestClient client = RestClient.builder(new HttpHost[]{ host }).build();
    HttpEntity entity = new NStringEntity("{\"foo\":\"bar\"}", ContentType.APPLICATION_JSON);
    Request request = new Request("POST", "/test/_doc/");
    request.setEntity(entity);
    client.performRequest(request);

    MockDispatcher dispatcher = new MockDispatcher();
    ElasticsearchCollector collector = new ElasticsearchCollector();
    collector.setDispatcher(dispatcher);
    Hashtable<String, Object> configuration = new Hashtable<>();
    configuration.put("addresses", "http://localhost:" + HTTP_PORT);
    configuration.put("index", "test");
    collector.activate(configuration);

    collector.run();

    Assert.assertEquals(1, dispatcher.postedEvents.size());
    Assert.assertEquals("elasticsearch", dispatcher.postedEvents.get(0).getProperty("type"));
    Assert.assertEquals("decanter/collect/elasticsearch", dispatcher.postedEvents.get(0).getTopic());
}
 
Example 3
Source File: ElasticsearchBeamRuntimeTestIT.java    From components with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 4
Source File: WeikePath.java    From jframe with Apache License 2.0 6 votes vote down vote up
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());
    }
}
 
Example 5
Source File: ElasticsearchTemplateRecordConsumer.java    From baleen with Apache License 2.0 6 votes vote down vote up
/**
 * Create an index in Elasticsearch. If necessary, this function should check whether a new index
 * is required.
 *
 * @return true if a new index has been created, false otherwise
 */
public boolean createIndex() {
  RestClient client = esrResource.getClient();

  try {
    Response r = client.performRequest("HEAD", "/" + index);

    if (r.getStatusLine().getStatusCode() != 200) {
      client.performRequest("PUT", "/" + index);

      return true;
    }
  } catch (IOException ioe) {
    getMonitor().error("Unable to create index", ioe);
  }

  return false;
}
 
Example 6
Source File: ElasticsearchRest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Override
public boolean createIndex() {
  RestClient client = esrResource.getClient();

  try {
    Response r = client.performRequest("HEAD", "/" + index);

    if (r.getStatusLine().getStatusCode() != 200) {
      client.performRequest("PUT", "/" + index);

      return true;
    }
  } catch (IOException ioe) {
    getMonitor().error("Unable to create index", ioe);
  }

  return false;
}
 
Example 7
Source File: ElasticsearchRestClientTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
private static void test1(final RestClient client, final HttpEntity entity) throws IOException {
  final Request request = new Request("PUT", "/twitter/tweet/1");
  request.setEntity(entity);

  final Response indexResponse = client.performRequest(request);
  assertNotNull(indexResponse);
}
 
Example 8
Source File: TestElasticsearchAppender.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000L)
public void test() throws Exception {
    Marshaller marshaller = new JsonMarshaller();
    ElasticsearchAppender appender = new ElasticsearchAppender();
    appender.marshaller = marshaller;
    Dictionary<String, Object> config = new Hashtable<>();
    config.put(ElasticsearchAppender.ADDRESSES_PROPERTY, "http://" + HOST + ":" + HTTP_PORT);
    config.put(EventFilter.PROPERTY_NAME_EXCLUDE_CONFIG, ".*refused.*");
    config.put(EventFilter.PROPERTY_VALUE_EXCLUDE_CONFIG, ".*refused.*");
    appender.open(config);
    appender.handleEvent(new Event("testTopic", MapBuilder.<String, String>newMapBuilder().put("a", "b").put("c", "d").map()));
    appender.handleEvent(new Event("testTopic", MapBuilder.<String, String>newMapBuilder().put("a", "b").put("c", "d").map()));
    appender.handleEvent(new Event("testTopic", MapBuilder.<String, String>newMapBuilder().put("a", "b").put("c", "d").map()));
    appender.handleEvent(new Event("testTopic", MapBuilder.<String, String>newMapBuilder().put("refused", "b").put("c", "d").map()));
    appender.handleEvent(new Event("testTopic", MapBuilder.<String, String>newMapBuilder().put("a", "refused").put("c", "d").map()));
    appender.close();

    HttpHost host = new HttpHost(HOST, HTTP_PORT, "http");
    RestClient client = RestClient.builder(new HttpHost[]{ host }).build();

    String responseString = "";
    while (!responseString.contains("\"count\":3")) {
        Thread.sleep(200);
        Request request = new Request("GET", "/_count");
        Response response = client.performRequest(request);
        responseString = EntityUtils.toString(response.getEntity());
    }
}
 
Example 9
Source File: ElasticsearchSourceIT.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private static boolean dataIsAvailable(RestClient restClient, int size) {
  try {
    Response r = restClient.performRequest("GET", "/twitter/tweet/_search?size=" + size);
    Reader reader = new InputStreamReader(r.getEntity().getContent());
    JsonArray hits = new JsonParser()
        .parse(reader)
        .getAsJsonObject()
        .getAsJsonObject("hits")
        .getAsJsonArray("hits");
    return hits.size() == size;
  } catch (IOException ignored) {
    return false;
  }
}
 
Example 10
Source File: ElasticsearchPlugin.java    From hawkular-alerts with Apache License 2.0 5 votes vote down vote up
protected void writeAlert(Action a) throws Exception {
    String url = a.getProperties().get(PROP_URL);
    String index = a.getProperties().get(PROP_INDEX);
    String type = a.getProperties().get(PROP_TYPE);
    String[] urls = url.split(",");
    HttpHost[] hosts = new HttpHost[urls.length];
    for (int i=0; i<urls.length; i++) {
        hosts[i] = HttpHost.create(urls[0]);
    }
    RestClient client = RestClient.builder(hosts)
            .setHttpClientConfigCallback(httpClientBuilder -> {
                httpClientBuilder.useSystemProperties();
                CredentialsProvider credentialsProvider = checkBasicCredentials(a);
                if (credentialsProvider != null) {
                    httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                }
                return httpClientBuilder;
            }).build();

    HttpEntity document = new NStringEntity(transform(a), ContentType.APPLICATION_JSON);
    String endpoint = "/" + index + "/" + type;
    Header[] headers = checkHeaders(a);
    Response response = headers == null ? client.performRequest("POST", endpoint, Collections.EMPTY_MAP, document) :
            client.performRequest("POST", endpoint, Collections.EMPTY_MAP, document, headers);
    log.debugf(response.toString());
    client.close();
}
 
Example 11
Source File: RestWriterVariant.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@Override
public TestClient getTestClient(Config config)
    throws IOException {
  final ElasticsearchRestWriter restWriter = new ElasticsearchRestWriter(config);
  final RestHighLevelClient highLevelClient = restWriter.getRestHighLevelClient();
  return new TestClient() {
    @Override
    public GetResponse get(GetRequest getRequest)
        throws IOException {
      return highLevelClient.get(getRequest);
    }

    @Override
    public void recreateIndex(String indexName)
        throws IOException {
      RestClient restClient = restWriter.getRestLowLevelClient();
      try {
        restClient.performRequest("DELETE", "/" + indexName);
      } catch (Exception e) {
        // ok since index may not exist
      }

      String indexSettings = "{\"settings\" : {\"index\":{\"number_of_shards\":1,\"number_of_replicas\":1}}}";
      HttpEntity entity = new StringEntity(indexSettings, ContentType.APPLICATION_JSON);

      Response putResponse = restClient.performRequest("PUT", "/" + indexName, Collections.emptyMap(), entity);
      Assert.assertEquals(putResponse.getStatusLine().getStatusCode(),200, "Recreate index succeeded");
    }

    @Override
    public void close()
        throws IOException {
      restWriter.close();

    }
  };
}
 
Example 12
Source File: ElasticsearchIOTestCommon.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Test that the default predicate correctly parses chosen error code. */
void testDefaultRetryPredicate(RestClient restClient) throws IOException {

  HttpEntity entity1 = new NStringEntity(BAD_REQUEST, ContentType.APPLICATION_JSON);
  Request request = new Request("POST", "/_bulk");
  request.addParameters(Collections.emptyMap());
  request.setEntity(entity1);
  Response response1 = restClient.performRequest(request);
  assertTrue(CUSTOM_RETRY_PREDICATE.test(response1.getEntity()));

  HttpEntity entity2 = new NStringEntity(OK_REQUEST, ContentType.APPLICATION_JSON);
  request.setEntity(entity2);
  Response response2 = restClient.performRequest(request);
  assertFalse(DEFAULT_RETRY_PREDICATE.test(response2.getEntity()));
}
 
Example 13
Source File: ElasticsearchIOTestUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Executes a match query for given field/value and returns the count of results.
 *
 * @param connectionConfiguration Specifies the index and type
 * @param restClient To use to execute the call
 * @param field The field to query
 * @param value The value to match
 * @return The count of documents in the search result
 * @throws IOException On error communicating with Elasticsearch
 */
static int countByMatch(
    ConnectionConfiguration connectionConfiguration,
    RestClient restClient,
    String field,
    String value)
    throws IOException {
  String requestBody =
      "{\n"
          + "  \"query\" : {\"match\": {\n"
          + "    \""
          + field
          + "\": \""
          + value
          + "\"\n"
          + "  }}\n"
          + "}\n";
  String endPoint =
      String.format(
          "/%s/%s/_search",
          connectionConfiguration.getIndex(), connectionConfiguration.getType());
  HttpEntity httpEntity = new NStringEntity(requestBody, ContentType.APPLICATION_JSON);

  Request request = new Request("GET", endPoint);
  request.addParameters(Collections.emptyMap());
  request.setEntity(httpEntity);
  Response response = restClient.performRequest(request);
  JsonNode searchResult = parseResponse(response.getEntity());
  if (getBackendVersion(connectionConfiguration) >= 7) {
    return searchResult.path("hits").path("total").path("value").asInt();
  } else {
    return searchResult.path("hits").path("total").asInt();
  }
}
 
Example 14
Source File: ElasticsearchIOTestUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/** 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);
  Request request = new Request("POST", endPoint);
  request.addParameters(Collections.singletonMap("refresh", "wait_for"));
  request.setEntity(requestBody);
  Response response = restClient.performRequest(request);
  ElasticsearchIO.checkForErrors(
      response.getEntity(), ElasticsearchIO.getBackendVersion(connectionConfiguration), false);
}
 
Example 15
Source File: ElasticsearchIOTestUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Synchronously deletes the target if it exists and then (re)creates it as a copy of source
 * synchronously.
 */
static void copyIndex(RestClient restClient, String source, String target) throws IOException {
  deleteIndex(restClient, target);
  HttpEntity entity =
      new NStringEntity(
          String.format(
              "{\"source\" : { \"index\" : \"%s\" }, \"dest\" : { \"index\" : \"%s\" } }",
              source, target),
          ContentType.APPLICATION_JSON);
  Request request = new Request("POST", "/_reindex");
  request.addParameters(Collections.singletonMap("refresh", "wait_for"));
  request.setEntity(entity);
  restClient.performRequest(request);
}
 
Example 16
Source File: ElasticsearchIOTestUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
private static void deleteIndex(RestClient restClient, String index) throws IOException {
  try {
    closeIndex(restClient, index);
    Request request = new Request("DELETE", String.format("/%s", index));
    request.addParameters(Collections.singletonMap("refresh", "wait_for"));
    restClient.performRequest(request);
  } 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;
    }
  }
}
 
Example 17
Source File: DumpDataFromElasticsearch.java    From ache with Apache License 2.0 4 votes vote down vote up
@Override
public void execute() throws Exception {

    FileWriter fw = new FileWriter(new File(this.outputFile));

    ElasticSearchConfig config = new ElasticSearchConfig(Arrays.asList(hostAddress));

    RestClient client = ElasticSearchClientFactory.createClient(config);

    Map<String, ?> esQuery;
    if (this.query == null || this.query.isEmpty()) {
        esQuery = ImmutableMap.of(
                "match_all", EMPTY_MAP
        );
    } else {
        esQuery = ImmutableMap.of(
                "query_string", ImmutableMap.of(
                        "default_field", "_all",
                        "query", this.query
                )
        );
    }

    Map<String, ?> body = ImmutableMap.of(
            "query", esQuery
    );

    AbstractHttpEntity entity = createJsonEntity(serializeAsJson(body));

    String searchEndpoint = String
            .format("/%s/%s/_search?scroll=%dm", indexName, typeName, esTimeout);
    Response scrollResponse = client.performRequest("POST", searchEndpoint, EMPTY_MAP, entity);

    JsonNode jsonResponse = mapper.readTree(scrollResponse.getEntity().getContent());
    List<String> hits = getHits(jsonResponse);
    String scrollId = getScrollId(jsonResponse);
    int i = 0;
    while (hits.size() > 0) {
        System.out.printf("Processing scroll: %d hits: %d\n", ++i, hits.size());
        for (String hit : hits) {
            fw.write(hit);
            fw.write('\n');
        }
        fw.flush();

        // Execute next scroll search request
        Map<String, ?> scrollBody = ImmutableMap.of(
                "scroll", (this.esTimeout + "m"),
                "scroll_id", scrollId
        );
        entity = createJsonEntity(serializeAsJson(scrollBody));

        String scrollEndpoint = "/_search/scroll";
        scrollResponse = client.performRequest("POST", scrollEndpoint, EMPTY_MAP, entity);

        jsonResponse = mapper.readTree(scrollResponse.getEntity().getContent());
        hits = getHits(jsonResponse);
        scrollId = getScrollId(jsonResponse);
    }
    client.close();
    fw.close();
    System.out.println("Done.");
}
 
Example 18
Source File: ElasticsearchIOTestUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
private static void closeIndex(RestClient restClient, String index) throws IOException {
  Request request = new Request("POST", String.format("/%s/_close", index));
  restClient.performRequest(request);
}
 
Example 19
Source File: TemplateElasticsearchUpdater.java    From elasticsearch-beyonder with Apache License 2.0 2 votes vote down vote up
/**
 * Check if a template exists
    * @param client Elasticsearch client
 * @param template template name
    * @return true if the template exists
    * @throws IOException if something goes wrong
 */
public static boolean isTemplateExist(RestClient client, String template) throws IOException {
	Response response = client.performRequest(new Request("HEAD", "/_template/" + template));
	return response.getStatusLine().getStatusCode() == 200;
}
 
Example 20
Source File: IndexElasticsearchUpdater.java    From elasticsearch-beyonder with Apache License 2.0 2 votes vote down vote up
/**
 * Check if an index already exists
 * @param client Elasticsearch client
 * @param index Index name
 * @return true if index already exists
 * @throws Exception if the elasticsearch API call is failing
 */
public static boolean isIndexExist(RestClient client, String index) throws Exception {
	Response response = client.performRequest(new Request("HEAD", "/" + index));
	return response.getStatusLine().getStatusCode() == 200;
}