public void testDeleteRoutingRequired()
      throws ExecutionException, InterruptedException, IOException {
    createIndexWithAlias();
    assertAcked(
        client()
            .admin()
            .indices()
            .preparePutMapping("test")
            .setType("test")
            .setSource(
                XContentFactory.jsonBuilder()
                    .startObject()
                    .startObject("test")
                    .startObject("_routing")
                    .field("required", true)
                    .endObject()
                    .endObject()
                    .endObject()));
    ensureYellow("test");

    int numDocs = iterations(10, 50);
    IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < numDocs - 2; i++) {
      indexRequestBuilders[i] =
          client()
              .prepareIndex("test", "test", Integer.toString(i))
              .setRouting(randomAsciiOfLength(randomIntBetween(1, 10)))
              .setSource("field", "value");
    }
    String firstDocId = Integer.toString(numDocs - 2);
    indexRequestBuilders[numDocs - 2] =
        client()
            .prepareIndex("test", "test", firstDocId)
            .setRouting("routing")
            .setSource("field", "value");
    String secondDocId = Integer.toString(numDocs - 1);
    String secondRouting = randomAsciiOfLength(randomIntBetween(1, 10));
    indexRequestBuilders[numDocs - 1] =
        client()
            .prepareIndex("test", "test", secondDocId)
            .setRouting(secondRouting)
            .setSource("field", "value");

    indexRandom(true, indexRequestBuilders);

    SearchResponse searchResponse = client().prepareSearch("test").get();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().totalHits(), equalTo((long) numDocs));

    DeleteResponse deleteResponse =
        client().prepareDelete("test", "test", firstDocId).setRouting("routing").get();
    assertThat(deleteResponse.isFound(), equalTo(true));
    GetResponse getResponse =
        client().prepareGet("test", "test", firstDocId).setRouting("routing").get();
    assertThat(getResponse.isExists(), equalTo(false));
    refresh();
    searchResponse = client().prepareSearch("test").get();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().totalHits(), equalTo((long) numDocs - 1));
  }
  public void testUpdate() {
    createIndexWithAlias();
    ensureYellow("test");

    UpdateRequestBuilder updateRequestBuilder =
        client()
            .prepareUpdate(indexOrAlias(), "type1", "1")
            .setUpsert("field1", "value1")
            .setDoc("field2", "value2");

    UpdateResponse updateResponse = updateRequestBuilder.get();
    assertThat(updateResponse.getIndex(), equalTo("test"));
    assertThat(updateResponse.getType(), equalTo("type1"));
    assertThat(updateResponse.getId(), equalTo("1"));
    assertThat(updateResponse.isCreated(), equalTo(true));

    GetResponse getResponse = client().prepareGet("test", "type1", "1").get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getSourceAsMap().containsKey("field1"), equalTo(true));
    assertThat(getResponse.getSourceAsMap().containsKey("field2"), equalTo(false));

    updateResponse = updateRequestBuilder.get();
    assertThat(updateResponse.getIndex(), equalTo("test"));
    assertThat(updateResponse.getType(), equalTo("type1"));
    assertThat(updateResponse.getId(), equalTo("1"));
    assertThat(updateResponse.isCreated(), equalTo(false));

    getResponse = client().prepareGet("test", "type1", "1").get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getSourceAsMap().containsKey("field1"), equalTo(true));
    assertThat(getResponse.getSourceAsMap().containsKey("field2"), equalTo(true));
  }
  /**
   * Basic test using Index &amp; Realtime Get with internal versioning. This test ensures routing
   * works correctly across versions.
   */
  public void testInternalVersion() throws Exception {
    createIndex("test");
    final boolean routing = randomBoolean();
    int numDocs = randomIntBetween(10, 20);
    for (int i = 0; i < numDocs; i++) {
      String routingKey = routing ? randomRealisticUnicodeOfLength(10) : null;
      String id = Integer.toString(i);
      assertThat(
          id,
          client()
              .prepareIndex("test", "type1", id)
              .setRouting(routingKey)
              .setSource("field1", English.intToEnglish(i))
              .get()
              .isCreated(),
          is(true));
      GetResponse get =
          client().prepareGet("test", "type1", id).setRouting(routingKey).setVersion(1).get();
      assertThat("Document with ID " + id + " should exist but doesn't", get.isExists(), is(true));
      assertThat(get.getVersion(), equalTo(1L));
      client()
          .prepareIndex("test", "type1", id)
          .setRouting(routingKey)
          .setSource("field1", English.intToEnglish(i))
          .execute()
          .actionGet();
      get = client().prepareGet("test", "type1", id).setRouting(routingKey).setVersion(2).get();
      assertThat("Document with ID " + id + " should exist but doesn't", get.isExists(), is(true));
      assertThat(get.getVersion(), equalTo(2L));
    }

    assertVersionCreated(compatibilityVersion(), "test");
  }
 @Override
 public HashMap<String, String> loadKey(String username) {
   logger.info("loading password for username {}", username);
   HashMap<String, String> ret = new HashMap<>();
   String riverIndexName = getRiverIndexName();
   refreshSearchIndex(riverIndexName);
   GetResponse resp =
       client.prepareGet(riverIndexName, riverName().name(), "_pwd").execute().actionGet();
   if (resp.isExists()) {
     if (logger.isDebugEnabled()) {
       logger.debug("Password document: {}", resp.getSourceAsString());
     }
     Map<String, Object> newset = resp.getSource();
     Set<String> keys = newset.keySet();
     for (String s : keys) {
       logger.info(
           "Added key {} with a value of {}",
           s,
           XContentMapValues.nodeStringValue(newset.get(s), null));
       ret.put(s, XContentMapValues.nodeStringValue(newset.get(s), null));
     }
   }
   if (ret.isEmpty()) {
     return null;
   }
   return ret;
 }
  @Test
  public void testUpsert() throws Exception {
    createIndex();
    ensureGreen();

    UpdateResponse updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
            .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
            .execute()
            .actionGet();
    assertTrue(updateResponse.isCreated());

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("1"));
    }

    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
            .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
            .execute()
            .actionGet();
    assertFalse(updateResponse.isCreated());

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("2"));
    }
  }
 private void test(
     String name, Class<? extends HashFunction> expectedHashFunction, boolean expectedUseType)
     throws Exception {
   Path zippedIndexDir = getDataPath("/org/elasticsearch/cluster/routing/" + name + ".zip");
   Settings baseSettings = prepareBackwardsDataDir(zippedIndexDir);
   internalCluster()
       .startNode(Settings.builder().put(baseSettings).put(Node.HTTP_ENABLED, true).build());
   ensureYellow("test");
   GetIndexResponse getIndexResponse = client().admin().indices().prepareGetIndex().get();
   assertArrayEquals(new String[] {"test"}, getIndexResponse.indices());
   GetSettingsResponse getSettingsResponse =
       client().admin().indices().prepareGetSettings("test").get();
   assertEquals(
       expectedHashFunction.getName(),
       getSettingsResponse.getSetting("test", IndexMetaData.SETTING_LEGACY_ROUTING_HASH_FUNCTION));
   assertEquals(
       Boolean.valueOf(expectedUseType).toString(),
       getSettingsResponse.getSetting("test", IndexMetaData.SETTING_LEGACY_ROUTING_USE_TYPE));
   SearchResponse allDocs = client().prepareSearch("test").get();
   assertSearchResponse(allDocs);
   assertHitCount(allDocs, 4);
   // Make sure routing works
   for (SearchHit hit : allDocs.getHits().hits()) {
     GetResponse get = client().prepareGet(hit.index(), hit.type(), hit.id()).get();
     assertTrue(get.isExists());
   }
 }
 @Override
 public void run() {
   long totalRefreshTime = 0;
   int numExecutedRefreshed = 0;
   while (run) {
     long docIdLimit = COUNT;
     for (long docId = 1; run && docId < docIdLimit; ) {
       try {
         for (int j = 0; j < 8; j++) {
           GetResponse getResponse =
               client.prepareGet(indexName, "type1", String.valueOf(++docId)).get();
           client
               .prepareIndex(indexName, "type1", getResponse.getId())
               .setSource(getResponse.getSource())
               .get();
         }
         long startTime = System.currentTimeMillis();
         client.admin().indices().prepareRefresh(indexName).execute().actionGet();
         totalRefreshTime += System.currentTimeMillis() - startTime;
         numExecutedRefreshed++;
         Thread.sleep(500);
       } catch (Throwable e) {
         e.printStackTrace();
       }
     }
   }
   avgRefreshTime = totalRefreshTime / numExecutedRefreshed;
   stopped = true;
 }
Beispiel #8
0
 @CheckForNull
 public QProfileRule findByActiveRuleId(int activeRuleId) {
   GetResponse activeRuleResponse =
       index
           .client()
           .prepareGet(INDEX_RULES, ESActiveRule.TYPE_ACTIVE_RULE, Integer.toString(activeRuleId))
           .setFields(FIELD_SOURCE, FIELD_PARENT)
           .execute()
           .actionGet();
   Map<String, Object> activeRuleSource = activeRuleResponse.getSourceAsMap();
   if (activeRuleSource != null) {
     Map<String, Object> ruleSource =
         index
             .client()
             .prepareGet(
                 INDEX_RULES,
                 TYPE_RULE,
                 (String) activeRuleResponse.getField(FIELD_PARENT).getValue())
             .execute()
             .actionGet()
             .getSourceAsMap();
     if (ruleSource != null) {
       return new QProfileRule(ruleSource, activeRuleSource);
     }
   }
   return null;
 }
  @Test
  public void testNullShape() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type1")
            .startObject("properties")
            .startObject("location")
            .field("type", "geo_shape")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();
    prepareCreate("test").addMapping("type1", mapping).execute().actionGet();
    ensureGreen();

    client()
        .prepareIndex("test", "type1", "aNullshape")
        .setSource("{\"location\": null}")
        .execute()
        .actionGet();
    GetResponse result = client().prepareGet("test", "type1", "aNullshape").execute().actionGet();
    assertThat(result.getField("location"), nullValue());
  }
Beispiel #10
0
  @Test
  public void testUpsert() throws Exception {
    createIndex();
    ClusterHealthResponse clusterHealth =
        client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
    assertThat(clusterHealth.timedOut(), equalTo(false));
    assertThat(clusterHealth.status(), equalTo(ClusterHealthStatus.GREEN));

    client
        .prepareUpdate("test", "type1", "1")
        .setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
        .setScript("ctx._source.field += 1")
        .execute()
        .actionGet();

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client.prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.sourceAsMap().get("field").toString(), equalTo("1"));
    }

    client
        .prepareUpdate("test", "type1", "1")
        .setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
        .setScript("ctx._source.field += 1")
        .execute()
        .actionGet();

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client.prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.sourceAsMap().get("field").toString(), equalTo("2"));
    }
  }
  void assertRealtimeGetWorks(String indexName) {
    assertAcked(
        client()
            .admin()
            .indices()
            .prepareUpdateSettings(indexName)
            .setSettings(Settings.builder().put("refresh_interval", -1).build()));
    SearchRequestBuilder searchReq =
        client().prepareSearch(indexName).setQuery(QueryBuilders.matchAllQuery());
    SearchHit hit = searchReq.get().getHits().getAt(0);
    String docId = hit.getId();
    // foo is new, it is not a field in the generated index
    client().prepareUpdate(indexName, "doc", docId).setDoc("foo", "bar").get();
    GetResponse getRsp = client().prepareGet(indexName, "doc", docId).get();
    Map<String, Object> source = getRsp.getSourceAsMap();
    assertThat(source, Matchers.hasKey("foo"));

    assertAcked(
        client()
            .admin()
            .indices()
            .prepareUpdateSettings(indexName)
            .setSettings(
                Settings.builder()
                    .put("refresh_interval", EngineConfig.DEFAULT_REFRESH_INTERVAL)
                    .build()));
  }
  @Override
  public Date readDatetimeValue(String spaceKey, String propertyName) throws IOException {
    Date lastDate = null;
    String documentName = prepareValueStoreDocumentName(spaceKey, propertyName);

    if (logger.isDebugEnabled())
      logger.debug(
          "Going to read datetime value from {} property for space {}. Document name is {}.",
          propertyName,
          spaceKey,
          documentName);

    refreshSearchIndex(getRiverIndexName());
    GetResponse lastSeqGetResponse =
        client
            .prepareGet(getRiverIndexName(), riverName.name(), documentName)
            .execute()
            .actionGet();
    if (lastSeqGetResponse.isExists()) {
      Object timestamp = lastSeqGetResponse.getSourceAsMap().get(STORE_FIELD_VALUE);
      if (timestamp != null) {
        lastDate = DateTimeUtils.parseISODateTime(timestamp.toString());
      }
    } else {
      if (logger.isDebugEnabled())
        logger.debug("{} document doesn't exist in remore river persistent store", documentName);
    }
    return lastDate;
  }
 private List<KnapsackState> get(String name) throws IOException {
   ImmutableList.Builder<KnapsackState> builder = ImmutableList.builder();
   try {
     logger.debug("get knapsack states: {}", name);
     final Client client = injector.getInstance(Client.class);
     createIndexIfNotExist(client);
     GetResponse getResponse =
         client.prepareGet(INDEX_NAME, MAPPING_NAME, name).execute().actionGet();
     if (!getResponse.isExists()) {
       return builder.build();
     }
     XContentParser parser = xContent(JSON).createParser(getResponse.getSourceAsBytes());
     while (parser.nextToken() != START_ARRAY) {
       // forward
     }
     while (parser.nextToken() != END_ARRAY) {
       KnapsackState state = new KnapsackState();
       builder.add(state.fromXContent(parser));
     }
     return builder.build();
   } catch (Throwable t) {
     logger.error("get settings failed", t);
     return null;
   }
 }
 @Override
 public <T> T mapResult(GetResponse response, Class<T> clazz) {
   T result = mapEntity(response.getSourceAsString(), clazz);
   if (result != null) {
     setPersistentEntityId(result, response.getId(), clazz);
   }
   return result;
 }
  @Test
  public void testGet() {

    GetResponse response =
        client
            .prepareGet("asdf2014", "asdf", "1")
            .setOperationThreaded(false)
            .execute()
            .actionGet();
    if (response != null) System.out.println("Id: " + response.getId());
  }
 protected ESShape getShape(Iterator<MultiGetItemResponse> iterator, int size) {
   ESShapeBuilder shapeBuilder = new ESShapeBuilder(size);
   for (int i = 0; i < size; i++) {
     GetResponse response = iterator.next().getResponse();
     if (!response.isExists()) continue;
     @SuppressWarnings("unchecked")
     Map<String, Object> shape = (Map<String, Object>) response.getField("shape").getValue();
     @SuppressWarnings("unchecked")
     List<Double> coordinates = (List<Double>) shape.get("coordinates");
     shapeBuilder.addLocation(coordinates.get(1), coordinates.get(0));
   }
   return shapeBuilder.build();
 }
  public void testIndexWithShadowReplicasCleansUp() throws Exception {
    Path dataPath = createTempDir();
    Settings nodeSettings = nodeSettings(dataPath);

    final int nodeCount = randomIntBetween(2, 5);
    logger.info("--> starting {} nodes", nodeCount);
    final List<String> nodes = internalCluster().startNodesAsync(nodeCount, nodeSettings).get();
    final String IDX = "test";
    final Tuple<Integer, Integer> numPrimariesAndReplicas = randomPrimariesAndReplicas(nodeCount);
    final int numPrimaries = numPrimariesAndReplicas.v1();
    final int numReplicas = numPrimariesAndReplicas.v2();
    logger.info(
        "--> creating index {} with {} primary shards and {} replicas",
        IDX,
        numPrimaries,
        numReplicas);

    Settings idxSettings =
        Settings.builder()
            .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, numPrimaries)
            .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, numReplicas)
            .put(IndexMetaData.SETTING_DATA_PATH, dataPath.toAbsolutePath().toString())
            .put(IndexMetaData.SETTING_SHADOW_REPLICAS, true)
            .put(IndexMetaData.SETTING_SHARED_FILESYSTEM, true)
            .build();

    prepareCreate(IDX).setSettings(idxSettings).addMapping("doc", "foo", "type=text").get();
    ensureGreen(IDX);

    client().prepareIndex(IDX, "doc", "1").setSource("foo", "bar").get();
    client().prepareIndex(IDX, "doc", "2").setSource("foo", "bar").get();
    flushAndRefresh(IDX);

    GetResponse gResp1 = client().prepareGet(IDX, "doc", "1").get();
    GetResponse gResp2 = client().prepareGet(IDX, "doc", "2").get();
    assertThat(gResp1.getSource().get("foo"), equalTo("bar"));
    assertThat(gResp2.getSource().get("foo"), equalTo("bar"));

    logger.info("--> performing query");
    SearchResponse resp = client().prepareSearch(IDX).setQuery(matchAllQuery()).get();
    assertHitCount(resp, 2);

    logger.info("--> deleting index " + IDX);
    assertAcked(client().admin().indices().prepareDelete(IDX));
    assertAllIndicesRemovedAndDeletionCompleted(
        internalCluster().getInstances(IndicesService.class));
    assertPathHasBeenCleared(dataPath);
    // TODO: uncomment the test below when https://github.com/elastic/elasticsearch/issues/17695 is
    // resolved.
    // assertIndicesDirsDeleted(nodes);
  }
  @Test
  public void testThatGetFromTranslogShouldWorkWithIncludeExcludeAndFields() throws Exception {
    client.admin().indices().prepareDelete().execute().actionGet();
    String index = "test";
    String type = "type1";

    String mapping =
        jsonBuilder()
            .startObject()
            .startObject("source_excludes")
            .startObject("_source")
            .array("includes", "included")
            .array("exlcudes", "excluded")
            .endObject()
            .endObject()
            .endObject()
            .string();

    client
        .admin()
        .indices()
        .prepareCreate(index)
        .addMapping(type, mapping)
        .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))
        .execute()
        .actionGet();

    client
        .prepareIndex(index, type, "1")
        .setSource(
            jsonBuilder()
                .startObject()
                .field("field", "1", "2")
                .field("included", "should be seen")
                .field("excluded", "should not be seen")
                .endObject())
        .execute()
        .actionGet();

    GetResponse responseBeforeFlush =
        client
            .prepareGet(index, type, "1")
            .setFields("_source", "included", "excluded")
            .execute()
            .actionGet();
    client.admin().indices().prepareFlush(index).execute().actionGet();
    GetResponse responseAfterFlush =
        client
            .prepareGet(index, type, "1")
            .setFields("_source", "included", "excluded")
            .execute()
            .actionGet();

    assertThat(responseBeforeFlush.isExists(), is(true));
    assertThat(responseAfterFlush.isExists(), is(true));
    assertThat(responseBeforeFlush.getSourceAsMap(), not(hasKey("excluded")));
    assertThat(responseBeforeFlush.getSourceAsMap(), not(hasKey("field")));
    assertThat(responseBeforeFlush.getSourceAsMap(), hasKey("included"));
    assertThat(responseBeforeFlush.getSourceAsString(), is(responseAfterFlush.getSourceAsString()));
  }
Beispiel #19
0
  @Override
  public final String esGet(String index, String type, String id) {
    try {
      final GetResponse gr = client.prepareGet(index, type, id).execute().actionGet();

      if (!gr.exists()) return "Doesn't exist";

      return gr.getSourceAsString();
    } catch (ElasticSearchException e) {
      log.debug("ElasticSearchException {}", e);

      return e.getMessage();
    }
  }
 public T get(String id) {
   GetResponse result = client.prepareGet(index, entity, id).execute().actionGet();
   String source = result.getSourceAsString();
   System.out.println("Loaded SLA: " + source);
   try {
     T entity = mapper.readValue(source, clazz);
     entity.setId(result.getId());
     logger.info("Successfully obtained entity with id [{}]", id);
     return entity;
   } catch (IOException e) {
     logger.error("Error retrieving entity with id " + id, e);
     return null;
   }
 }
 @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");
 }
 protected void createRiver(String jsonDefinition, String river, Object... args) throws Exception {
   logger.info("Create river [{}]", river);
   String setting = getJsonSettings(jsonDefinition, args);
   logger.info("River setting [{}]", setting);
   node.client().prepareIndex("_river", river, "_meta").setSource(setting).execute().actionGet();
   logger.debug("Running Cluster Health");
   ClusterHealthResponse clusterHealth =
       node.client()
           .admin()
           .cluster()
           .health(clusterHealthRequest().waitForGreenStatus())
           .actionGet();
   logger.info("Done Cluster Health, status " + clusterHealth.getStatus());
   GetResponse response =
       getNode().client().prepareGet("_river", river, "_meta").execute().actionGet();
   assertThat(response.isExists(), equalTo(true));
 }
  /** Reconfigure the river. Must be stopped! */
  public synchronized void reconfigure() {
    if (!closed) throw new IllegalStateException("Remote River must be stopped to reconfigure it!");

    logger.info("reconfiguring Remote River");
    String riverIndexName = getRiverIndexName();
    refreshSearchIndex(riverIndexName);
    GetResponse resp =
        client.prepareGet(riverIndexName, riverName().name(), "_meta").execute().actionGet();
    if (resp.isExists()) {
      if (logger.isDebugEnabled()) {
        logger.debug("Configuration document: {}", resp.getSourceAsString());
      }
      Map<String, Object> newset = resp.getSource();
      configure(newset);
    } else {
      throw new IllegalStateException(
          "Configuration document not found to reconfigure remote river " + riverName().name());
    }
  }
 public RiverState load(Client client) throws IOException {
   if (coordinateIndex == null || coordinateType == null || coordinateId == null) {
     return this;
   }
   GetResponse get = null;
   try {
     get = client.prepareGet(coordinateIndex, coordinateType, coordinateId).execute().actionGet();
   } catch (Exception e) {
     // ignore
   }
   if (get != null && get.isExists()) {
     XContentParser parser =
         XContentFactory.xContent(XContentType.JSON).createParser(get.getSourceAsBytes());
     fromXContent(parser);
   } else {
     counter = 0L;
   }
   return this;
 }
Beispiel #25
0
  @CheckForNull
  @Override
  public DOMAIN getNullableByKey(KEY key) {
    GetRequestBuilder request =
        client
            .prepareGet()
            .setType(this.getIndexType())
            .setIndex(this.getIndexName())
            .setId(this.getKeyValue(key))
            .setFetchSource(true)
            .setRouting(this.getKeyValue(key));

    GetResponse response = request.get();

    if (response.isExists()) {
      return toDoc(response.getSource());
    }
    return null;
  }
 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;
 }
  @Override
  public Article get(long id) {
    GetResponse getResponse =
        transportClient.prepareGet(INDICE, TYPE, String.valueOf(id)).execute().actionGet();
    if (getResponse.isExists()) {
      Map<String, Object> map = getResponse.getSource();
      String title = (String) map.get(TITLE);
      String summary = (String) map.get(SUMMARY);
      String idStr = getResponse.getId();
      logger.debug("get id : {}", idStr);

      Article article = new Article();
      article.setId(Long.parseLong(idStr));
      article.setTitle(title);
      article.setSummary(summary);
      return article;
    } else {
      return null;
    }
  }
 @Override
 public Table load(String key) {
   logger.info("Load called for: " + key);
   GetResponse response =
       elasticsearchConnection
           .getClient()
           .prepareGet()
           .setIndex(TABLE_META_INDEX)
           .setType(TABLE_META_TYPE)
           .setId(key)
           .execute()
           .actionGet();
   if (!response.isExists()) {
     return null;
   }
   try {
     return objectMapper.readValue(response.getSourceAsBytes(), Table.class);
   } catch (Exception e) {
     throw new RuntimeException("Error getting data for table: " + key);
   }
 }
 @Override
 public void run() {
   while (run) {
     int childIdLimit = PARENT_COUNT * NUM_CHILDREN_PER_PARENT;
     for (int childId = 1; run && childId < childIdLimit; ) {
       try {
         for (int j = 0; j < 8; j++) {
           GetResponse getResponse =
               client
                   .prepareGet(indexName, "child", String.valueOf(++childId))
                   .setFields("_source", "_parent")
                   .setRouting("1") // Doesn't matter what value, since there is only one shard
                   .get();
           client
               .prepareIndex(indexName, "child", Integer.toString(childId) + "_" + j)
               .setParent(getResponse.getField("_parent").getValue().toString())
               .setSource(getResponse.getSource())
               .get();
         }
         client.admin().indices().prepareRefresh(indexName).execute().actionGet();
         Thread.sleep(1000);
         if (childId % 500 == 0) {
           NodesStatsResponse statsResponse =
               client
                   .admin()
                   .cluster()
                   .prepareNodesStats()
                   .clear()
                   .setIndices(true)
                   .execute()
                   .actionGet();
           System.out.println(
               "Deleted docs: " + statsResponse.getAt(0).getIndices().getDocs().getDeleted());
         }
       } catch (Throwable e) {
         e.printStackTrace();
       }
     }
   }
 }
  /**
   * Get the river definition by its name
   *
   * @param name
   * @return
   */
  public T get(String name) {
    if (logger.isDebugEnabled()) logger.debug("get({})", name);
    T river = null;

    if (name != null) {
      GetRequestBuilder rb = new GetRequestBuilder(client, SMDSearchProperties.ES_META_INDEX);
      rb.setType(SMDSearchProperties.ES_META_RIVERS);
      rb.setId(name);

      try {
        GetResponse response = rb.execute().actionGet();
        if (response.isExists()) {
          river = getHelper().toRiver(buildInstance(), response.getSourceAsMap());
        }
      } catch (IndexMissingException e) {
        // Index does not exists, so RIVER does not exist...
      }
    }

    if (logger.isDebugEnabled()) logger.debug("/get({})={}", name, river);
    return river;
  }