Esempio n. 1
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");
  }
 private String getLastUpdate() {
   String res;
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
   try {
     GetResponse response =
         client.prepareGet(indexName, "stats", "1").setFields("last_update").execute().actionGet();
     if (!response.getFields().isEmpty()) {
       res = (String) response.getField("last_update").getValue();
     } else {
       res = sdf.format(new Date(0));
     }
   } catch (ElasticsearchIllegalStateException ex) {
     logger.error("Could not get last_update, use Date(0)", ex.getLocalizedMessage());
     res = sdf.format(new Date(0));
   } catch (NullPointerException ex) {
     logger.error("Could not get last_update, use Date(0)", ex.getLocalizedMessage());
     res = sdf.format(new Date(0));
   }
   return res;
 }
Esempio n. 3
0
 public final Map<String, GetField> getModelFields(
     final ModelIdT id, final Logger logger, final Marker logMarker, final String... fieldNames)
     throws InvalidModelException, IoExceptionT, NoSuchModelExceptionT {
   logger.debug(logMarker, "getting model {} from index {}", id, indexName);
   try {
     final GetResponse response =
         client
             .prepareGet(indexName, documentType, id.toString())
             .setFields(fieldNames)
             .execute()
             .actionGet();
     if (!response.isExists()) {
       throw exceptionFactory.newNoSuchModelException(id);
     }
     return response.getFields();
   } catch (final IndexNotFoundException e) {
     logger.warn(logMarker, "tried to get model {} from missing index {}", id, indexName);
     throw exceptionFactory.newNoSuchModelException(id);
   } catch (final ElasticsearchException e) {
     throw exceptionFactory.newIoException(
         e, String.format("error getting model %s from index %s", id, indexName));
   }
 }
  @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"));
  }