private Optional<String> getMetadata(String id) { GetRequestBuilder request = esClient.prepareGet(MetadataIndexDefinition.INDEX_TYPE_METADATA, id) .setStoredFields(MetadataIndexDefinition.FIELD_VALUE); GetResponse response = request.get(); if (response.isExists()) { GetField field = response.getField(MetadataIndexDefinition.FIELD_VALUE); String value = String.valueOf(field.getValue()); return Optional.of(value); } return Optional.empty(); }
public static Status getRiverStatus(Client client, String riverName) { GetResponse statusResponse = client.prepareGet("_river", riverName, MongoDBRiver.STATUS_ID).get(); if (!statusResponse.isExists()) { return Status.UNKNOWN; } else { Object obj = XContentMapValues.extractValue(MongoDBRiver.TYPE + "." + MongoDBRiver.STATUS_FIELD, statusResponse.getSourceAsMap()); return Status.valueOf(obj.toString()); } }
@Test public void fail_to_get_bad_query() { GetRequestBuilder requestBuilder = es.client().prepareGet(new IndexType("unknown", "test"), "rule1"); try { requestBuilder.get(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class); assertThat(e.getMessage()).contains("Fail to execute ES get request for key 'rule1' on index 'unknown' on type 'test'"); } }
@Test public void get_with_string_timeout_is_not_implemented() { try { es.client().prepareGet(INDEX_TYPE_FAKE, "ruleKey").get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); } }
@Test public void get_with_time_value_timeout_is_not_yet_implemented() { try { es.client().prepareGet(INDEX_TYPE_FAKE, "ruleKey").get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); } }
@Test public void trace_logs() { logTester.setLevel(LoggerLevel.TRACE); es.client().prepareGet(INDEX_TYPE_FAKE, "ruleKey") .get(); assertThat(logTester.logs(LoggerLevel.TRACE)).hasSize(1); }
/** * Get the latest timestamp for a given namespace. */ @SuppressWarnings("unchecked") public static Timestamp<?> getLastTimestamp(Client client, MongoDBRiverDefinition definition) { client.admin().indices().prepareRefresh(definition.getRiverIndexName()).get(); GetResponse lastTimestampResponse = client.prepareGet(definition.getRiverIndexName(), definition.getRiverName(), definition.getMongoOplogNamespace()).get(); if (lastTimestampResponse.isExists()) { Map<String, Object> mongodbState = (Map<String, Object>) lastTimestampResponse.getSourceAsMap().get(TYPE); if (mongodbState != null) { Timestamp<?> lastTimestamp = Timestamp.on(mongodbState); if (lastTimestamp != null) { return lastTimestamp; } } } else { if (definition.getInitialTimestamp() != null) { return definition.getInitialTimestamp(); } } return null; }
private SearchGuardLicense createOrGetTrial(String msg) { long created = System.currentTimeMillis(); ThreadContext threadContext = threadPool.getThreadContext(); try(StoredContext ctx = threadContext.stashContext()) { threadContext.putHeader(ConfigConstants.SG_CONF_REQUEST_HEADER, "true"); GetResponse get = client.prepareGet(searchguardIndex, "sg", "tattr").get(); if(get.isExists()) { created = (long) get.getSource().get("val"); } else { try { client.index(new IndexRequest(searchguardIndex) .type("sg") .id("tattr") .setRefreshPolicy(RefreshPolicy.IMMEDIATE) .create(true) .source("{\"val\": "+System.currentTimeMillis()+"}", XContentType.JSON)).actionGet(); } catch (VersionConflictEngineException e) { //ignore } catch (Exception e) { LOGGER.error("Unable to index tattr", e); } } } return SearchGuardLicense.createTrialLicense(formatDate(created), clusterService, msg); } }
@Test public void recreate_index_on_definition_changes() { // v1 startNewCreator(new FakeIndexDefinition()); IndexType fakeIndexType = new IndexType("fakes", "fake"); String id = "1"; es.client().prepareIndex(fakeIndexType).setId(id).setSource(new FakeDoc().getFields()).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); assertThat(es.client().prepareGet(fakeIndexType, id).get().isExists()).isTrue(); // v2 startNewCreator(new FakeIndexDefinitionV2()); ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = mappings(); MappingMetaData mapping = mappings.get("fakes").get("fake"); assertThat(countMappingFields(mapping)).isEqualTo(3); assertThat(field(mapping, "updatedAt").get("type")).isEqualTo("date"); assertThat(field(mapping, "newField").get("type")).isEqualTo("integer"); assertThat(es.client().prepareGet(fakeIndexType, id).get().isExists()).isFalse(); }
@Test public void do_not_recreate_index_on_unchanged_definition() { // v1 startNewCreator(new FakeIndexDefinition()); IndexType fakeIndexType = new IndexType("fakes", "fake"); String id = "1"; es.client().prepareIndex(fakeIndexType).setId(id).setSource(new FakeDoc().getFields()).setRefreshPolicy(IMMEDIATE).get(); assertThat(es.client().prepareGet(fakeIndexType, id).get().isExists()).isTrue(); // v1 startNewCreator(new FakeIndexDefinition()); assertThat(es.client().prepareGet(fakeIndexType, id).get().isExists()).isTrue(); }
GetResponse response = elasticSearchClient .prepareGet(searchHit.getIndex(), MSG_DOC_TYPE, searchHit.getId()) .get(); assertEquals("indexed message id should match", messageId, response.getSource().get("messageId")); assertEquals("indexed payload should match", "some-payload", response.getSource().get("payload"));
GetResponse response = elasticSearchClient .prepareGet(searchHit.getIndex(), EVENT_DOC_TYPE, searchHit.getId()) .get();
GetResponse gr =tc.prepareGet("vulcan", "secrets", "s1").setRealtime(true).get(); Assert.assertTrue(gr.isExists()); gr =tc.prepareGet("vulcan", "secrets", "s1").setRealtime(false).get(); Assert.assertTrue(gr.isExists()); gr =tc.prepareGet("searchguard", "sg", "config").setRealtime(false).get(); Assert.assertFalse(gr.isExists()); gr =tc.prepareGet("searchguard", "sg", "config").setRealtime(true).get(); Assert.assertFalse(gr.isExists()); try { tc.threadPool().getThreadContext().putHeader("sg_impersonate_as", "worf"); gr = tc.prepareGet("vulcan", "secrets", "s1").get(); } finally { ctx.close(); Header header = encodeBasicHeader("worf", "worf"); tc.threadPool().getThreadContext().putHeader(header.getName(), header.getValue()); gr = tc.prepareGet("vulcan", "secrets", "s1").get(); Assert.fail(); } catch (ElasticsearchSecurityException e) { Header header = encodeBasicHeader("worf", "worf111"); tc.threadPool().getThreadContext().putHeader(header.getName(), header.getValue()); gr = tc.prepareGet("vulcan", "secrets", "s1").get(); Assert.fail(); } catch (ElasticsearchSecurityException e) {
GetResponse gr =tc.prepareGet("vulcan", "secrets", "s1").setRealtime(true).get(); Assert.assertTrue(gr.isExists()); gr =tc.prepareGet("vulcan", "secrets", "s1").setRealtime(false).get(); Assert.assertTrue(gr.isExists()); gr =tc.prepareGet("searchguard", "sg", "config").setRealtime(false).get(); Assert.assertFalse(gr.isExists()); gr =tc.prepareGet("searchguard", "sg", "config").setRealtime(true).get(); Assert.assertFalse(gr.isExists()); try { tc.threadPool().getThreadContext().putHeader("sg_impersonate_as", "worf"); gr = tc.prepareGet("vulcan", "secrets", "s1").get(); } finally { ctx.close(); Header header = encodeBasicHeader("worf", "worf"); tc.threadPool().getThreadContext().putHeader(header.getName(), header.getValue()); gr = tc.prepareGet("vulcan", "secrets", "s1").get(); Assert.fail(); } catch (ElasticsearchSecurityException e) { Header header = encodeBasicHeader("worf", "worf111"); tc.threadPool().getThreadContext().putHeader(header.getName(), header.getValue()); gr = tc.prepareGet("vulcan", "secrets", "s1").get(); Assert.fail(); } catch (ElasticsearchSecurityException e) {
/** * 获取数据 * ElasticSearch提供了根据索引名称,类别,文档ID来获取数据 */ @Test public void testGet(){ GetResponse getResponse=client.prepareGet("twitter", "tweet", "1").get(); System.out.println(getResponse.getSourceAsString()); }
private Optional<String> getMetadata(String id) { GetRequestBuilder request = esClient.prepareGet(MetadataIndexDefinition.INDEX_TYPE_METADATA, id) .setStoredFields(MetadataIndexDefinition.FIELD_VALUE); GetResponse response = request.get(); if (response.isExists()) { GetField field = response.getField(MetadataIndexDefinition.FIELD_VALUE); String value = String.valueOf(field.getValue()); return Optional.of(value); } return Optional.empty(); }
@Override public boolean isProductExists(ElasticSearchIndexConfig config, Long productId) { return searchClientService.getClient().prepareGet().setIndex(config.getIndexAliasName()) .setId(String.valueOf(productId)) .get() .isExists(); }
@Override public boolean isProductExists(final ElasticSearchIndexConfig config, final Long productId) { return searchClientService.getClient().prepareGet() .setIndex(config.getIndexAliasName()) .setId(String.valueOf(productId)).get().isExists(); }
public NamedQuery get(String name) { GetResponse response = getConnection().getClient() .prepareGet(getIndex(), getType(), name.toLowerCase()) .setFetchSource(true) .get(); if (response.isExists()) { return mapToNamedQuery(response.getSource()); } return null; }
public HttpSourceTest get(String url) { GetResponse response = getConnection().getClient().prepareGet(getIndex(), getType(), url) .setFetchSource(true) .get(); if (response.isExists()) { return mapToHttpSourceTest(response.getSource()); } return null; }