Java Code Examples for org.elasticsearch.client.Request#setEntity()

The following examples show how to use org.elasticsearch.client.Request#setEntity() . 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: 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 2
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 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: 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 5
Source File: EmbeddedElasticsearchPolicy.java    From calcite with Apache License 2.0 6 votes vote down vote up
void insertBulk(String index, List<ObjectNode> documents) throws IOException {
  Objects.requireNonNull(index, "index");
  Objects.requireNonNull(documents, "documents");

  if (documents.isEmpty()) {
    // nothing to process
    return;
  }

  List<String> bulk = new ArrayList<>(documents.size() * 2);
  for (ObjectNode doc: documents) {
    bulk.add(String.format(Locale.ROOT, "{\"index\": {\"_index\":\"%s\"}}", index));
    bulk.add(mapper().writeValueAsString(doc));
  }

  final StringEntity entity = new StringEntity(String.join("\n", bulk) + "\n",
      ContentType.APPLICATION_JSON);

  final Request r = new Request("POST", "/_bulk?refresh");
  r.setEntity(entity);
  restClient().performRequest(r);
}
 
Example 6
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 7
Source File: EmbeddedElasticsearchPolicy.java    From calcite 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 index index of the index
 * @param mapping field and field type mapping
 * @throws IOException if there is an error
 */
void createIndex(String index, Map<String, String> mapping) throws IOException {
  Objects.requireNonNull(index, "index");
  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
  final HttpEntity entity = new StringEntity(mapper().writeValueAsString(mappings),
      ContentType.APPLICATION_JSON);
  final Request r = new Request("PUT", "/" + index);
  r.setEntity(entity);
  restClient().performRequest(r);
}
 
Example 8
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 9
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 10
Source File: JobIT.java    From zentity with Apache License 2.0 6 votes vote down vote up
public void testJobObject() throws Exception {
    int testResourceSet = TEST_RESOURCES_A;
    prepareTestResources(testResourceSet);
    try {
        String endpoint = "_zentity/resolution/zentity_test_entity_a";
        Set<String> docsExpectedA = new TreeSet<>();
        docsExpectedA.add("a0,0");
        docsExpectedA.add("a2,0");
        docsExpectedA.add("a4,0");
        docsExpectedA.add("a6,0");
        docsExpectedA.add("a8,0");

        // Boolean true
        Request q1 = new Request("POST", endpoint);
        q1.setEntity(TEST_PAYLOAD_JOB_OBJECT);
        Response r1 = client.performRequest(q1);
        JsonNode j1 = Json.MAPPER.readTree(r1.getEntity().getContent());
        assertEquals(j1.get("hits").get("total").asInt(), 5);
        assertEquals(docsExpectedA, getActual(j1));

    } finally {
        destroyTestResources(testResourceSet);
    }
}
 
Example 11
Source File: JobIT.java    From zentity with Apache License 2.0 6 votes vote down vote up
public void testJobScopeExcludeAndIncludeAttributesTerms() 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_TERMS);
        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 12
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 13
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 14
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 15
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 16
Source File: JobIT.java    From zentity with Apache License 2.0 5 votes vote down vote up
public void testJobExplanationTerms() 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.addParameter("_attributes", "false");
        postResolution.addParameter("_explanation", "true");
        postResolution.addParameter("_source", "false");
        postResolution.addParameter("max_hops", "1");
        postResolution.addParameter("max_docs_per_query", "2");
        postResolution.setEntity(TEST_PAYLOAD_JOB_EXPLANATION_TERMS);
        Response response = client.performRequest(postResolution);
        JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
        assertEquals(json.get("hits").get("total").asInt(), 3);
        Set<String> docsExpected = new TreeSet<>();
        docsExpected.add("a0,0");
        docsExpected.add("a1,1");
        docsExpected.add("a2,1");
        assertEquals(docsExpected, getActual(json));
        for (JsonNode doc : json.get("hits").get("hits")) {
            String expected = "";
            switch (doc.get("_id").asText()) {
                case "a0":
                    expected = "{\"resolvers\":{\"resolver_a\":{\"attributes\":[\"attribute_a\"]},\"resolver_type_date_a\":{\"attributes\":[\"attribute_a\",\"attribute_type_date\"]}},\"matches\":[{\"attribute\":\"attribute_a\",\"target_field\":\"field_a.clean\",\"target_value\":\"a_00\",\"input_value\":\"a_00\",\"input_matcher\":\"matcher_a\",\"input_matcher_params\":{}},{\"attribute\":\"attribute_a\",\"target_field\":\"field_a.keyword\",\"target_value\":\"a_00\",\"input_value\":\"a_00\",\"input_matcher\":\"matcher_b\",\"input_matcher_params\":{}},{\"attribute\":\"attribute_type_date\",\"target_field\":\"type_date\",\"target_value\":\"1999-12-31T23:59:57.0000\",\"input_value\":\"1999-12-31T23:59:57.0000\",\"input_matcher\":\"matcher_c\",\"input_matcher_params\":{\"format\":\"yyyy-MM-dd'T'HH:mm:ss.0000\",\"window\":\"1d\"}}]}";
                    break;
                case "a1":
                    expected = "{\"resolvers\":{\"resolver_c\":{\"attributes\":[\"attribute_d\"]},\"resolver_type_date_c\":{\"attributes\":[\"attribute_d\",\"attribute_type_date\"]}},\"matches\":[{\"attribute\":\"attribute_d\",\"target_field\":\"field_d.clean\",\"target_value\":\"d_00\",\"input_value\":\"d_00\",\"input_matcher\":\"matcher_a\",\"input_matcher_params\":{}},{\"attribute\":\"attribute_d\",\"target_field\":\"field_d.keyword\",\"target_value\":\"d_00\",\"input_value\":\"d_00\",\"input_matcher\":\"matcher_b\",\"input_matcher_params\":{}},{\"attribute\":\"attribute_type_date\",\"target_field\":\"type_date\",\"target_value\":\"1999-12-31T23:59:59.0000\",\"input_value\":\"1999-12-31T23:59:57.0000\",\"input_matcher\":\"matcher_c\",\"input_matcher_params\":{\"format\":\"yyyy-MM-dd'T'HH:mm:ss.0000\",\"window\":\"1d\"}}]}";
                    break;
                case "a2":
                    expected = "{\"resolvers\":{\"resolver_c\":{\"attributes\":[\"attribute_d\"]},\"resolver_object\":{\"attributes\":[\"attribute_object\"]},\"resolver_type_boolean\":{\"attributes\":[\"attribute_type_boolean\"]},\"resolver_type_date_c\":{\"attributes\":[\"attribute_d\",\"attribute_type_date\"]},\"resolver_type_double\":{\"attributes\":[\"attribute_type_double\"]},\"resolver_type_float\":{\"attributes\":[\"attribute_type_float\"]},\"resolver_type_integer\":{\"attributes\":[\"attribute_type_integer\"]},\"resolver_type_long\":{\"attributes\":[\"attribute_type_long\"]},\"resolver_type_string\":{\"attributes\":[\"attribute_type_string\"]}},\"matches\":[{\"attribute\":\"attribute_d\",\"target_field\":\"field_d.clean\",\"target_value\":\"d_00\",\"input_value\":\"d_00\",\"input_matcher\":\"matcher_a\",\"input_matcher_params\":{}},{\"attribute\":\"attribute_d\",\"target_field\":\"field_d.keyword\",\"target_value\":\"d_00\",\"input_value\":\"d_00\",\"input_matcher\":\"matcher_b\",\"input_matcher_params\":{}},{\"attribute\":\"attribute_object\",\"target_field\":\"object.a.b.c.keyword\",\"target_value\":\"a\",\"input_value\":\"a\",\"input_matcher\":\"matcher_b\",\"input_matcher_params\":{}},{\"attribute\":\"attribute_type_boolean\",\"target_field\":\"type_boolean\",\"target_value\":true,\"input_value\":true,\"input_matcher\":\"matcher_b\",\"input_matcher_params\":{}},{\"attribute\":\"attribute_type_date\",\"target_field\":\"type_date\",\"target_value\":\"2000-01-01T00:00:00.0000\",\"input_value\":\"1999-12-31T23:59:57.0000\",\"input_matcher\":\"matcher_c\",\"input_matcher_params\":{\"format\":\"yyyy-MM-dd'T'HH:mm:ss.0000\",\"window\":\"1d\"}},{\"attribute\":\"attribute_type_double\",\"target_field\":\"type_double\",\"target_value\":3.141592653589793,\"input_value\":3.141592653589793,\"input_matcher\":\"matcher_b\",\"input_matcher_params\":{}},{\"attribute\":\"attribute_type_float\",\"target_field\":\"type_float\",\"target_value\":1.0,\"input_value\":1.0,\"input_matcher\":\"matcher_b\",\"input_matcher_params\":{}},{\"attribute\":\"attribute_type_integer\",\"target_field\":\"type_integer\",\"target_value\":1,\"input_value\":1,\"input_matcher\":\"matcher_b\",\"input_matcher_params\":{}},{\"attribute\":\"attribute_type_long\",\"target_field\":\"type_long\",\"target_value\":922337203685477,\"input_value\":922337203685477,\"input_matcher\":\"matcher_b\",\"input_matcher_params\":{}},{\"attribute\":\"attribute_type_string\",\"target_field\":\"type_string\",\"target_value\":\"a\",\"input_value\":\"a\",\"input_matcher\":\"matcher_b\",\"input_matcher_params\":{}}]}";
                    break;
            }
            assertEquals(expected, Json.MAPPER.writeValueAsString(doc.get("_explanation")));
        }
    } finally {
        destroyTestResources(testResourceSet);
    }
}
 
Example 17
Source File: EmbeddedElasticsearchPolicy.java    From calcite with Apache License 2.0 5 votes vote down vote up
void insertDocument(String index, ObjectNode document) throws IOException {
  Objects.requireNonNull(index, "index");
  Objects.requireNonNull(document, "document");
  String uri = String.format(Locale.ROOT,
        "/%s/_doc?refresh", index);
  StringEntity entity = new StringEntity(mapper().writeValueAsString(document),
      ContentType.APPLICATION_JSON);
  final Request r = new Request("POST", uri);
  r.setEntity(entity);
  restClient().performRequest(r);
}
 
Example 18
Source File: ElasticsearchOps.java    From immutables with Apache License 2.0 5 votes vote down vote up
Single<WriteResult> insertDocument(ObjectNode document) throws IOException {
  Objects.requireNonNull(document, "document");
  String uri = String.format(Locale.ROOT, "/%s/_doc?refresh", index);
  StringEntity entity = new StringEntity(mapper().writeValueAsString(document),
          ContentType.APPLICATION_JSON);
  final Request r = new Request("POST", uri);
  r.setEntity(entity);
  return transport.execute(r).map(x -> WriteResult.unknown());
}
 
Example 19
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 20
Source File: JobIT.java    From zentity with Apache License 2.0 4 votes vote down vote up
public void testJobAttributesIds() 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_IDS);
        Response response = client.performRequest(postResolution);
        JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
        assertEquals(json.get("hits").get("total").asInt(), 30);
        Set<String> docsExpected = new TreeSet<>();
        docsExpected.add("a0,0");
        docsExpected.add("a6,0");
        docsExpected.add("b0,0");
        docsExpected.add("a2,1");
        docsExpected.add("a7,1");
        docsExpected.add("a8,1");
        docsExpected.add("a9,1");
        docsExpected.add("b2,1");
        docsExpected.add("b6,1");
        docsExpected.add("b7,1");
        docsExpected.add("b8,1");
        docsExpected.add("b9,1");
        docsExpected.add("c0,1");
        docsExpected.add("c2,1");
        docsExpected.add("c6,1");
        docsExpected.add("c7,1");
        docsExpected.add("c8,1");
        docsExpected.add("c9,1");
        docsExpected.add("a1,2");
        docsExpected.add("a3,2");
        docsExpected.add("a4,2");
        docsExpected.add("a5,2");
        docsExpected.add("b3,2");
        docsExpected.add("b4,2");
        docsExpected.add("b5,2");
        docsExpected.add("c3,2");
        docsExpected.add("c4,2");
        docsExpected.add("c5,2");
        docsExpected.add("b1,3");
        docsExpected.add("c1,4");
        assertEquals(docsExpected, getActual(json));
    } finally {
        destroyTestResources(testResourceSet);
    }
}