org.elasticsearch.client.Request Java Examples

The following examples show how to use org.elasticsearch.client.Request. 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: IndexOps.java    From immutables with Apache License 2.0 6 votes vote down vote up
/**
 * Creates index in elastic search given a mapping. Mapping can contain nested fields expressed
 * as dots({@code .}).
 *
 * <p>Example
 * <pre>
 *  {@code
 *     b.a: long
 *     b.b: keyword
 *  }
 * </pre>
 *
 * @param mapping field and field type mapping
 * @throws IOException if there is an error
 */
Completable create(Map<String, String> mapping) {
  Objects.requireNonNull(mapping, "mapping");

  ObjectNode mappings = mapper.createObjectNode();

  ObjectNode properties = mappings.with("mappings").with("properties");
  for (Map.Entry<String, String> entry: mapping.entrySet()) {
    applyMapping(properties, entry.getKey(), entry.getValue());
  }

  // create index and mapping
  try {
    final HttpEntity entity = new StringEntity(mapper.writeValueAsString(mappings),
            ContentType.APPLICATION_JSON);
    final Request r = new Request("PUT", "/" + index);
    r.setEntity(entity);
    return transport.execute(r).ignoreElement();
  } catch (JsonProcessingException e) {
    throw new UncheckedIOException(e);
  }
}
 
Example #2
Source File: ElasticsearchIO.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public void close() throws IOException {
  // remove the scroll
  String requestBody = String.format("{\"scroll_id\" : [\"%s\"]}", scrollId);
  HttpEntity entity = new NStringEntity(requestBody, ContentType.APPLICATION_JSON);
  try {
    Request request = new Request("DELETE", "/_search/scroll");
    request.addParameters(Collections.emptyMap());
    request.setEntity(entity);
    restClient.performRequest(request);
  } finally {
    if (restClient != null) {
      restClient.close();
    }
  }
}
 
Example #3
Source File: JobIT.java    From zentity with Apache License 2.0 6 votes vote down vote up
public void testJobScopeIncludeAttributes() throws Exception {
    int testResourceSet = TEST_RESOURCES_A;
    prepareTestResources(testResourceSet);
    try {
        String endpoint = "_zentity/resolution/zentity_test_entity_a";
        Request postResolution = new Request("POST", endpoint);
        postResolution.setEntity(TEST_PAYLOAD_JOB_SCOPE_INCLUDE_ATTRIBUTES);
        Response response = client.performRequest(postResolution);
        JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
        assertEquals(json.get("hits").get("total").asInt(), 8);
        Set<String> docsExpected = new TreeSet<>();
        docsExpected.add("a0,0");
        docsExpected.add("a2,0");
        docsExpected.add("b0,0");
        docsExpected.add("b2,0");
        docsExpected.add("c0,0");
        docsExpected.add("c2,0");
        docsExpected.add("d0,0");
        docsExpected.add("d2,0");
        assertEquals(docsExpected, getActual(json));
    } finally {
        destroyTestResources(testResourceSet);
    }
}
 
Example #4
Source File: ElasticsearchSchema.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Queries {@code _alias} definition to automatically detect all indices
 *
 * @return list of indices
 * @throws IOException for any IO related issues
 * @throws IllegalStateException if reply is not understood
 */
private Set<String> indicesFromElastic() throws IOException {
  final String endpoint = "/_alias";
  final Response response = client.performRequest(new Request("GET", endpoint));
  try (InputStream is = response.getEntity().getContent()) {
    final JsonNode root = mapper.readTree(is);
    if (!(root.isObject() && root.size() > 0)) {
      final String message = String.format(Locale.ROOT, "Invalid response for %s/%s "
          + "Expected object of at least size 1 got %s (of size %d)", response.getHost(),
          response.getRequestLine(), root.getNodeType(), root.size());
      throw new IllegalStateException(message);
    }

    Set<String> indices = Sets.newHashSet(root.fieldNames());
    return indices;
  }
}
 
Example #5
Source File: JobIT.java    From zentity with Apache License 2.0 6 votes vote down vote up
public void testJobScopeIncludeAttributesTerms() throws Exception {
    int testResourceSet = TEST_RESOURCES_A;
    prepareTestResources(testResourceSet);
    try {
        String endpoint = "_zentity/resolution/zentity_test_entity_a";
        Request postResolution = new Request("POST", endpoint);
        postResolution.setEntity(TEST_PAYLOAD_JOB_SCOPE_INCLUDE_ATTRIBUTES_TERMS);
        Response response = client.performRequest(postResolution);
        JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
        assertEquals(json.get("hits").get("total").asInt(), 8);
        Set<String> docsExpected = new TreeSet<>();
        docsExpected.add("a0,0");
        docsExpected.add("a2,0");
        docsExpected.add("b0,0");
        docsExpected.add("b2,0");
        docsExpected.add("c0,0");
        docsExpected.add("c2,0");
        docsExpected.add("d0,0");
        docsExpected.add("d2,0");
        assertEquals(docsExpected, getActual(json));
    } finally {
        destroyTestResources(testResourceSet);
    }
}
 
Example #6
Source File: ElasticsearchContainerTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void elasticsearchOssImage() throws IOException {
    try (
        // oosContainer {
        ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:" + ELASTICSEARCH_VERSION)
        // }
    ) {
        container.start();
        Response response = getClient(container).performRequest(new Request("GET", "/"));
        assertThat(response.getStatusLine().getStatusCode(), is(200));
        // The OSS image does not have any feature under Elastic License
        assertThrows("We should not have /_xpack endpoint with an OSS License",
            ResponseException.class,
            () -> getClient(container).performRequest(new Request("GET", "/_xpack/")));
    }
}
 
Example #7
Source File: JobIT.java    From zentity with Apache License 2.0 6 votes vote down vote up
public void testJobTerms() throws Exception {
    int testResourceSet = TEST_RESOURCES_A;
    prepareTestResources(testResourceSet);
    try {
        String endpoint = "_zentity/resolution/zentity_test_entity_a";
        Request postResolution = new Request("POST", endpoint);
        postResolution.setEntity(TEST_PAYLOAD_JOB_TERMS);
        Response response = client.performRequest(postResolution);
        JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
        assertEquals(json.get("hits").get("total").asInt(), 6);
        Set<String> docsExpected = new TreeSet<>();
        docsExpected.add("a0,0");
        docsExpected.add("b0,0");
        docsExpected.add("c0,1");
        docsExpected.add("a1,2");
        docsExpected.add("b1,3");
        docsExpected.add("c1,4");
        assertEquals(docsExpected, getActual(json));
    } finally {
        destroyTestResources(testResourceSet);
    }
}
 
Example #8
Source File: JobIT.java    From zentity with Apache License 2.0 6 votes vote down vote up
public void testJobAttributes() throws Exception {
    int testResourceSet = TEST_RESOURCES_A;
    prepareTestResources(testResourceSet);
    try {
        String endpoint = "_zentity/resolution/zentity_test_entity_a";
        Request postResolution = new Request("POST", endpoint);
        postResolution.setEntity(TEST_PAYLOAD_JOB_ATTRIBUTES);
        Response response = client.performRequest(postResolution);
        JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
        assertEquals(json.get("hits").get("total").asInt(), 6);
        Set<String> docsExpected = new TreeSet<>();
        docsExpected.add("a0,0");
        docsExpected.add("b0,0");
        docsExpected.add("c0,1");
        docsExpected.add("a1,2");
        docsExpected.add("b1,3");
        docsExpected.add("c1,4");
        assertEquals(docsExpected, getActual(json));
    } finally {
        destroyTestResources(testResourceSet);
    }
}
 
Example #9
Source File: JobIT.java    From zentity with Apache License 2.0 6 votes vote down vote up
public void testJobNoScope() throws Exception {
    int testResourceSet = TEST_RESOURCES_A;
    prepareTestResources(testResourceSet);
    try {
        String endpoint = "_zentity/resolution/zentity_test_entity_a";
        Request postResolution = new Request("POST", endpoint);
        postResolution.setEntity(TEST_PAYLOAD_JOB_NO_SCOPE);
        Response response = client.performRequest(postResolution);
        JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
        assertEquals(json.get("hits").get("total").asInt(), 40);
        JsonPointer pathAttributes = JsonPointer.compile("/_attributes");
        JsonPointer pathNull = JsonPointer.compile("/_attributes/attribute_type_string_null");
        JsonPointer pathUnused = JsonPointer.compile("/_attributes/attribute_type_string_unused");
        for (JsonNode doc : json.get("hits").get("hits")) {
            assertEquals(doc.at(pathAttributes).isMissingNode(), false);
            assertEquals(doc.at(pathNull).isMissingNode(), true);
            assertEquals(doc.at(pathUnused).isMissingNode(), true);
        }
    } finally {
        destroyTestResources(testResourceSet);
    }
}
 
Example #10
Source File: ElasticsearchContainerTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void restClientClusterHealth() throws IOException {
    // httpClientContainer {
    // Create the elasticsearch container.
    try (ElasticsearchContainer container = new ElasticsearchContainer()) {
        // Start the container. This step might take some time...
        container.start();

        // Do whatever you want with the rest client ...
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD));

        client = RestClient.builder(HttpHost.create(container.getHttpHostAddress()))
            .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
            .build();

        Response response = client.performRequest(new Request("GET", "/_cluster/health"));
    // }}
        assertThat(response.getStatusLine().getStatusCode(), is(200));
        assertThat(EntityUtils.toString(response.getEntity()), containsString("cluster_name"));
    // httpClientContainer {{
    }
    // }
}
 
Example #11
Source File: RxJavaTransport.java    From immutables with Apache License 2.0 6 votes vote down vote up
Single<Response> execute(Request request) {
  Objects.requireNonNull(request, "request");
  return Single.create(source -> {
    restClient.performRequestAsync(request, new ResponseListener() {
      @Override
      public void onSuccess(Response response) {
        source.onSuccess(response);
      }

      @Override
      public void onFailure(Exception exception) {
        source.onError(exception);
      }
    });
  });
}
 
Example #12
Source File: JobIT.java    From zentity with Apache License 2.0 6 votes vote down vote up
public void testJobScopeExcludeAndIncludeAttributes() throws Exception {
    int testResourceSet = TEST_RESOURCES_A;
    prepareTestResources(testResourceSet);
    try {
        String endpoint = "_zentity/resolution/zentity_test_entity_a";
        Request postResolution = new Request("POST", endpoint);
        postResolution.setEntity(TEST_PAYLOAD_JOB_SCOPE_EXCLUDE_AND_INCLUDE_ATTRIBUTES);
        Response response = client.performRequest(postResolution);
        JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
        assertEquals(json.get("hits").get("total").asInt(), 4);
        Set<String> docsExpected = new TreeSet<>();
        docsExpected.add("a2,0");
        docsExpected.add("b2,0");
        docsExpected.add("c2,0");
        docsExpected.add("d2,0");
        assertEquals(docsExpected, getActual(json));
    } finally {
        destroyTestResources(testResourceSet);
    }
}
 
Example #13
Source File: ScrollingTest.java    From immutables with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures there are no pending scroll contexts in elastic search cluster.
 * Queries {@code /_nodes/stats/indices/search} endpoint.
 * @see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html">Indices Stats</a>
 */
@SuppressWarnings("unused") // for errorprone
private void assertNoActiveScrolls() throws Exception {
  // get node stats
  final Response response = restClient
          .performRequest(new Request("GET", "/_nodes/stats/indices/search"));

  try (InputStream is = response.getEntity().getContent()) {
    final ObjectNode node = backend().objectMapper.readValue(is, ObjectNode.class);
    final String path = "/indices/search/scroll_current";
    final JsonNode scrollCurrent = node.with("nodes").elements().next().at(path);
    if (scrollCurrent.isMissingNode()) {
      throw new IllegalStateException("Couldn't find node at " + path);
    }

    final int activeScrolls = scrollCurrent.asInt();
    if (activeScrolls != 0) {
      throw new AssertionError(String.format("Expected no active scrolls but got %d. " +
              "Current index stats %s", activeScrolls, node));
    }
  }
}
 
Example #14
Source File: JobIT.java    From zentity with Apache License 2.0 6 votes vote down vote up
public void testJobResolverWeight() throws Exception {
    int testResourceSet = TEST_RESOURCES_B;
    prepareTestResources(testResourceSet);
    try {
        String endpoint = "_zentity/resolution/zentity_test_entity_b";
        Request postResolution = new Request("POST", endpoint);
        postResolution.setEntity(TEST_PAYLOAD_JOB_RESOLVER_WEIGHT);
        Response response = client.performRequest(postResolution);
        JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
        assertEquals(json.get("hits").get("total").asInt(), 4);
        Set<String> docsExpected = new TreeSet<>();
        docsExpected.add("a2,0");
        docsExpected.add("a3,0");
        docsExpected.add("a4,1");
        docsExpected.add("a5,1");
        assertEquals(docsExpected, getActual(json));
    } finally {
        destroyTestResources(testResourceSet);
    }
}
 
Example #15
Source File: AbstractITCase.java    From zentity with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void startRestClient() throws IOException {
    client = RestClient.builder(new HttpHost("localhost", HTTP_TEST_PORT)).build();
    try {
        Response response = client.performRequest(new Request("GET", "/"));
        JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
        assertTrue(json.get("tagline").textValue().equals("You Know, for Search"));
    } catch (IOException e) {
        // If we have an exception here, let's ignore the test
        assumeThat("Integration tests are skipped", e.getMessage(), not(containsString("Connection refused")));
        fail("Something wrong is happening. REST Client seemed to raise an exception.");
        if (client != null) {
            client.close();
            client = null;
        }
    }
}
 
Example #16
Source File: ZentityPluginIT.java    From zentity with Apache License 2.0 6 votes vote down vote up
public void testPluginIsLoaded() throws Exception {
    Response response = client.performRequest(new Request("GET", "_nodes/plugins"));
    JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
    Iterator<Map.Entry<String, JsonNode>> nodes = json.get("nodes").fields();
    while (nodes.hasNext()) {
        Map.Entry<String, JsonNode> entry = nodes.next();
        JsonNode node = entry.getValue();
        boolean pluginFound = false;
        for (JsonNode plugin : node.get("plugins")) {
            String pluginName = plugin.get("name").textValue();
            if (pluginName.equals("zentity")) {
                pluginFound = true;
                break;
            }
        }
        assertTrue(pluginFound);
    }
}
 
Example #17
Source File: ElasticDataStoreIT.java    From elasticgeo with GNU General Public License v3.0 6 votes vote down vote up
@Test(expected=IOException.class)
public void testConstructionWithBadClient() throws IOException {
    Map<String,Serializable> params = createConnectionParams();
    String indexName = ElasticDataStoreFactory.getValue(ElasticDataStoreFactory.INDEX_NAME, params);

    RestClient mockClient = mock(RestClient.class);
    Response mockResponse = mock(Response.class);
    HttpEntity mockEntity = mock(HttpEntity.class);
    StatusLine mockStatusLine = mock(StatusLine.class);
    when(mockResponse.getEntity()).thenReturn(mockEntity);
    when(mockResponse.getStatusLine()).thenReturn(mockStatusLine);
    when(mockStatusLine.getStatusCode()).thenReturn(400);
    when(mockClient.performRequest(any(Request.class))).thenReturn(mockResponse);

    new ElasticDataStore(mockClient, indexName);
}
 
Example #18
Source File: ElasticsearchIO.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public boolean advance() throws IOException {
  if (batchIterator.hasNext()) {
    current = batchIterator.next();
    return true;
  } else {
    String requestBody =
        String.format(
            "{\"scroll\" : \"%s\",\"scroll_id\" : \"%s\"}",
            source.spec.getScrollKeepalive(), scrollId);
    HttpEntity scrollEntity = new NStringEntity(requestBody, ContentType.APPLICATION_JSON);
    Request request = new Request("GET", "/_search/scroll");
    request.addParameters(Collections.emptyMap());
    request.setEntity(scrollEntity);
    Response response = restClient.performRequest(request);
    JsonNode searchResult = parseResponse(response.getEntity());
    updateScrollId(searchResult);
    return readNextBatchAndReturnFirstDocument(searchResult);
  }
}
 
Example #19
Source File: IndexServiceImpl.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
@Override
public PageResult<Map<String, String>> list(String queryStr, String indices) throws IOException {
    Response response = elasticsearchRestTemplate.getClient().getLowLevelClient()
            .performRequest(new Request(
                    "GET",
                    "/_cat/indices?h=health,status,index,docsCount,docsDeleted,storeSize&s=cds:desc&format=json&index="+StrUtil.nullToEmpty(indices)
            ));

    List<Map<String, String>> listOfIndicesFromEs = null;
    if (response != null) {
        String rawBody = EntityUtils.toString(response.getEntity());
        TypeReference<List<HashMap<String, String>>> typeRef = new TypeReference<List<HashMap<String, String>>>() {};
        listOfIndicesFromEs = mapper.readValue(rawBody, typeRef);
    }
    return PageResult.<Map<String, String>>builder().data(listOfIndicesFromEs).code(0).build();
}
 
Example #20
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 #21
Source File: ElasticsearchClientAsyncInstrumentation.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
private static void onBeforeExecute(@Advice.Argument(0) Request request,
                                    @Advice.Argument(value = 1, readOnly = false) ResponseListener responseListener,
                                    @Advice.Local("span") Span span,
                                    @Advice.Local("wrapped") boolean wrapped,
                                    @Advice.Local("helper") ElasticsearchRestClientInstrumentationHelper<HttpEntity, Response, ResponseListener> helper) {

    helper = esClientInstrHelperManager.getForClassLoaderOfClass(Request.class);
    if (helper != null) {
        span = helper.createClientSpan(request.getMethod(), request.getEndpoint(), request.getEntity());
        if (span != null) {
            responseListener = helper.<ResponseListener>wrapResponseListener(responseListener, span);
            wrapped = true;
        }
    }
}
 
Example #22
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 #23
Source File: ElasticSearchMetricSender.java    From jmeter-elasticsearch-backend-listener with MIT License 6 votes vote down vote up
public int getElasticSearchVersion() {
	Request request = new Request("GET", "/" );
	int elasticSearchVersion = -1;
	 try {
         Response response = this.client.performRequest(setAuthorizationHeader(request));
         if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK && logger.isErrorEnabled()) {
             logger.error("Unable to perform request to ElasticSearch engine for index {}. Response status: {}",
                          this.esIndex, response.getStatusLine().toString());
         }else {
        	 String responseBody = EntityUtils.toString(response.getEntity());
 			 JSONObject elasticSearchConfig = new JSONObject(responseBody);
 			 JSONObject version  = (JSONObject) elasticSearchConfig.get("version");
 			 String elasticVersion =  version.get("number").toString();
 			 elasticSearchVersion = Integer.parseInt(elasticVersion.split("\\.")[0]);
             logger.info("ElasticSearch Version : "  + Integer.toString(elasticSearchVersion));
         }
     } catch (Exception e) {
         if (logger.isErrorEnabled()) {
             logger.error("Exception" + e);
             logger.error("ElasticSearch Backend Listener was unable to perform request to the ElasticSearch engine. Check your JMeter console for more info.");
         }
     }
	 return elasticSearchVersion;
}
 
Example #24
Source File: RestElasticClientTest.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testLegacyGetTypes() throws IOException {
    String content = "{\"status_s\": {\"mappings\": " +
            "{\"active\": {\"properties\": {\"status_s\": {\"type\": \"keyword\"}}}}}}";
    InputStream inputStream = new ByteArrayInputStream(content.getBytes());
    when(mockEntity.getContent()).thenReturn(inputStream);
    mockVersion("6.0.0");
    when(mockRestClient.performRequest(new Request("GET", "/status_s/_mapping"))).thenReturn(mockResponse);

    List<String> names = client.getTypes("status_s");
    assertEquals(1, names.size());
    assertEquals("active", names.get(0));
}
 
Example #25
Source File: ElasticsearchAppender.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
private void send(Event event) throws Exception {
    String indexName = getIndexName(getValue(config, INDEX_PREFIX_PROPERTY, INDEX_PREFIX_DEFAULT), getDate(event));
    String jsonSt = marshaller.marshal(event);

    String endpoint;
    if (config.get("index.type") != null) {
        endpoint = String.format("/%s/%s", indexName, config.get("index.type"));
    } else {
        endpoint = String.format("/%s/_doc/", indexName);
    }
    HttpEntity entity = new NStringEntity(jsonSt, ContentType.APPLICATION_JSON);
    Request request = new Request("POST", endpoint);
    request.setEntity(entity);
    client.performRequest(request);
}
 
Example #26
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 #27
Source File: ElasticsearchIO.java    From beam with Apache License 2.0 5 votes vote down vote up
private long queryCount(
    @Nonnull RestClient restClient, @Nonnull String endPoint, @Nonnull String query)
    throws IOException {
  Request request = new Request("GET", endPoint);
  request.setEntity(new NStringEntity(query, ContentType.APPLICATION_JSON));
  JsonNode searchResult = parseResponse(restClient.performRequest(request).getEntity());
  return searchResult.path("count").asLong();
}
 
Example #28
Source File: ElasticsearchIO.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public boolean start() throws IOException {
  restClient = source.spec.getConnectionConfiguration().createClient();

  String query = source.spec.getQuery() != null ? source.spec.getQuery().get() : null;
  if (query == null) {
    query = "{\"query\": { \"match_all\": {} }}";
  }
  if ((source.backendVersion >= 5) && source.numSlices != null && source.numSlices > 1) {
    // if there is more than one slice, add the slice to the user query
    String sliceQuery =
        String.format("\"slice\": {\"id\": %s,\"max\": %s}", source.sliceId, source.numSlices);
    query = query.replaceFirst("\\{", "{" + sliceQuery + ",");
  }
  String endPoint =
      String.format(
          "/%s/%s/_search",
          source.spec.getConnectionConfiguration().getIndex(),
          source.spec.getConnectionConfiguration().getType());
  Map<String, String> params = new HashMap<>();
  params.put("scroll", source.spec.getScrollKeepalive());
  if (source.backendVersion == 2) {
    params.put("size", String.valueOf(source.spec.getBatchSize()));
    if (source.shardPreference != null) {
      params.put("preference", "_shards:" + source.shardPreference);
    }
  }
  HttpEntity queryEntity = new NStringEntity(query, ContentType.APPLICATION_JSON);
  Request request = new Request("GET", endPoint);
  request.addParameters(params);
  request.setEntity(queryEntity);
  Response response = restClient.performRequest(request);
  JsonNode searchResult = parseResponse(response.getEntity());
  updateScrollId(searchResult);
  return readNextBatchAndReturnFirstDocument(searchResult);
}
 
Example #29
Source File: ElasticsearchIO.java    From beam with Apache License 2.0 5 votes vote down vote up
private static JsonNode getStats(
    ConnectionConfiguration connectionConfiguration, boolean shardLevel) throws IOException {
  HashMap<String, String> params = new HashMap<>();
  if (shardLevel) {
    params.put("level", "shards");
  }
  String endpoint = String.format("/%s/_stats", connectionConfiguration.getIndex());
  try (RestClient restClient = connectionConfiguration.createClient()) {
    Request request = new Request("GET", endpoint);
    request.addParameters(params);
    return parseResponse(restClient.performRequest(request).getEntity());
  }
}
 
Example #30
Source File: ElasticsearchClientTestResource.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@GET
@Path("/connection")
@Produces(MediaType.TEXT_PLAIN)
public String testConnection() throws IOException, NoSuchAlgorithmException {
    try (RestClient restClient = createRestClient()) {
        Response response = restClient.performRequest(new Request("GET", "/"));

        checkStatus(response, 200);

        return "OK";
    }
}