@SuppressWarnings("unchecked")
 protected <T extends ESEntity> T buildEntityFromGetResponse(
     Class<T> entityClass, MultiGetItemResponse item) {
   GetResponse response = item.getResponse();
   if (!response.isExists())
     throw new DaoException(
         String.format(
             "Entity %s does not exist in %s/%s",
             response.getId(), response.getIndex(), response.getType()));
   if (entityClass == null) throw new IllegalArgumentException("Provided Entity class is null");
   else if (entityClass.equals(ESNode.class))
     return (T) ESNode.Builder.buildFromGetReponse(response);
   else if (entityClass.equals(ESWay.class))
     return (T) ESWay.Builder.buildFromGetReponse(response);
   else throw new IllegalArgumentException(entityClass.getSimpleName() + " is not a known Entity");
 }
Exemplo n.º 2
0
  @Test
  public void testGetSlash() {
    final String id = "get-test/id-2";
    final IndexRequestBuilder indexRequestBuilder =
        restClient().prepareIndex(index, type, id).setSource("field", "value").setRefresh(true);
    indexRequestBuilder.execute().actionGet();

    final GetRequestBuilder getRequestBuilder = restClient().prepareGet(index, type, id);
    final ListenableActionFuture<GetResponse> execute1 = getRequestBuilder.execute();
    final GetResponse get = execute1.actionGet();
    assertEquals(get.getIndex(), index);
    assertEquals(get.getType(), type);
    assertEquals(get.getId(), id);
    assertEquals(get.getVersion(), 1);
    assertTrue(get.getFields().isEmpty());
    assertEquals(get.getSource().get("field"), "value");
  }
  public void testIndexGetAndDelete() throws ExecutionException, InterruptedException {
    createIndexWithAlias();
    ensureYellow("test");

    int numDocs = iterations(10, 50);
    for (int i = 0; i < numDocs; i++) {
      IndexResponse indexResponse =
          client()
              .prepareIndex(indexOrAlias(), "type", Integer.toString(i))
              .setSource("field", "value-" + i)
              .get();
      assertThat(indexResponse.isCreated(), equalTo(true));
      assertThat(indexResponse.getIndex(), equalTo("test"));
      assertThat(indexResponse.getType(), equalTo("type"));
      assertThat(indexResponse.getId(), equalTo(Integer.toString(i)));
    }
    refresh();

    String docId = Integer.toString(randomIntBetween(0, numDocs - 1));
    GetResponse getResponse = client().prepareGet(indexOrAlias(), "type", docId).get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getIndex(), equalTo("test"));
    assertThat(getResponse.getType(), equalTo("type"));
    assertThat(getResponse.getId(), equalTo(docId));

    DeleteResponse deleteResponse = client().prepareDelete(indexOrAlias(), "type", docId).get();
    assertThat(deleteResponse.isFound(), equalTo(true));
    assertThat(deleteResponse.getIndex(), equalTo("test"));
    assertThat(deleteResponse.getType(), equalTo("type"));
    assertThat(deleteResponse.getId(), equalTo(docId));

    getResponse = client().prepareGet(indexOrAlias(), "type", docId).get();
    assertThat(getResponse.isExists(), equalTo(false));

    refresh();

    SearchResponse searchResponse = client().prepareSearch(indexOrAlias()).get();
    assertThat(searchResponse.getHits().totalHits(), equalTo((long) numDocs - 1));
  }
  @Test
  public void testGetDocWithMultivaluedFields() throws Exception {
    try {
      client.admin().indices().prepareDelete("test").execute().actionGet();
    } catch (Exception e) {
      // fine
    }
    String mapping1 =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type1")
            .startObject("properties")
            .startObject("field")
            .field("type", "string")
            .field("store", "yes")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();
    String mapping2 =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type2")
            .startObject("properties")
            .startObject("field")
            .field("type", "string")
            .field("store", "yes")
            .endObject()
            .endObject()
            .startObject("_source")
            .field("enabled", false)
            .endObject()
            .endObject()
            .endObject()
            .string();
    client
        .admin()
        .indices()
        .prepareCreate("test")
        .addMapping("type1", mapping1)
        .addMapping("type2", mapping2)
        .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))
        .execute()
        .actionGet();

    ClusterHealthResponse clusterHealth =
        client.admin().cluster().health(clusterHealthRequest().waitForGreenStatus()).actionGet();
    assertThat(clusterHealth.isTimedOut(), equalTo(false));
    assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));

    GetResponse response = client.prepareGet("test", "type1", "1").execute().actionGet();
    assertThat(response.isExists(), equalTo(false));
    response = client.prepareGet("test", "type2", "1").execute().actionGet();
    assertThat(response.isExists(), equalTo(false));

    client
        .prepareIndex("test", "type1", "1")
        .setSource(jsonBuilder().startObject().field("field", "1", "2").endObject())
        .execute()
        .actionGet();

    client
        .prepareIndex("test", "type2", "1")
        .setSource(jsonBuilder().startObject().field("field", "1", "2").endObject())
        .execute()
        .actionGet();

    response = client.prepareGet("test", "type1", "1").setFields("field").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getType(), equalTo("type1"));
    assertThat(response.getFields().size(), equalTo(1));
    assertThat(response.getFields().get("field").getValues().size(), equalTo(1));
    assertThat(((List) response.getFields().get("field").getValues().get(0)).size(), equalTo(2));
    assertThat(
        ((List) response.getFields().get("field").getValues().get(0)).get(0).toString(),
        equalTo("1"));
    assertThat(
        ((List) response.getFields().get("field").getValues().get(0)).get(1).toString(),
        equalTo("2"));

    response = client.prepareGet("test", "type2", "1").setFields("field").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getType(), equalTo("type2"));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getFields().size(), equalTo(1));
    assertThat(response.getFields().get("field").getValues().size(), equalTo(1));
    assertThat(((List) response.getFields().get("field").getValues().get(0)).size(), equalTo(2));
    assertThat(
        ((List) response.getFields().get("field").getValues().get(0)).get(0).toString(),
        equalTo("1"));
    assertThat(
        ((List) response.getFields().get("field").getValues().get(0)).get(1).toString(),
        equalTo("2"));

    // Now test values being fetched from stored fields.
    client.admin().indices().prepareRefresh("test").execute().actionGet();
    response = client.prepareGet("test", "type1", "1").setFields("field").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getFields().size(), equalTo(1));
    assertThat(response.getFields().get("field").getValues().size(), equalTo(1));
    assertThat(((List) response.getFields().get("field").getValues().get(0)).size(), equalTo(2));
    assertThat(
        ((List) response.getFields().get("field").getValues().get(0)).get(0).toString(),
        equalTo("1"));
    assertThat(
        ((List) response.getFields().get("field").getValues().get(0)).get(1).toString(),
        equalTo("2"));

    response = client.prepareGet("test", "type2", "1").setFields("field").execute().actionGet();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getFields().size(), equalTo(1));
    assertThat(response.getFields().get("field").getValues().size(), equalTo(1));
    assertThat(((List) response.getFields().get("field").getValues().get(0)).size(), equalTo(2));
    assertThat(
        ((List) response.getFields().get("field").getValues().get(0)).get(0).toString(),
        equalTo("1"));
    assertThat(
        ((List) response.getFields().get("field").getValues().get(0)).get(1).toString(),
        equalTo("2"));
  }