use of org.elasticsearch.action.get.GetRequest in project sonarqube by SonarSource.
the class IndexCreatorTest method recreate_index_on_definition_changes.
@Test
public void recreate_index_on_definition_changes() {
// v1
run(new FakeIndexDefinition());
IndexMainType fakeIndexType = main(Index.simple("fakes"), "fake");
String id = "1";
es.client().index(new IndexRequest(fakeIndexType.getIndex().getName(), fakeIndexType.getType()).id(id).source(new FakeDoc().getFields()).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE));
assertThat(es.client().get(new GetRequest(fakeIndexType.getIndex().getName(), fakeIndexType.getType(), id)).isExists()).isTrue();
// v2
run(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")).containsEntry("type", "date");
assertThat(field(mapping, "newField")).containsEntry("type", "integer");
assertThat(es.client().get(new GetRequest(fakeIndexType.getIndex().getName(), fakeIndexType.getType(), id)).isExists()).isFalse();
}
use of org.elasticsearch.action.get.GetRequest in project snow-owl by b2ihealthcare.
the class EsDocumentSearcher method get.
@Override
public <T> T get(Class<T> type, String key) throws IOException {
checkArgument(!Strings.isNullOrEmpty(key), "Key cannot be empty");
final DocumentMapping mapping = admin.mappings().getMapping(type);
final GetRequest req = new GetRequest(admin.getTypeIndex(mapping), key).fetchSourceContext(FetchSourceContext.FETCH_SOURCE);
final GetResponse res = admin.client().get(req);
if (res.isExists()) {
final byte[] bytes = res.getSourceAsBytes();
return mapper.readValue(bytes, 0, bytes.length, type);
} else {
return null;
}
}
use of org.elasticsearch.action.get.GetRequest in project warn-report by saaavsaaa.
the class ElasticClient method get.
@Test
public void get() throws IOException {
RestClientBuilder builder = RestClient.builder(new HttpHost("sl010a-analysisdb1", 9200, "https"), new HttpHost("sl010a-analysisdb2", 9200, "https"), new HttpHost("sl010a-analysisdb3", 9200, "https"));
Header[] defaultHeaders = new Header[] { new BasicHeader("Authorization", "Basic YWRtaW46YWRtaW4=") };
builder.setDefaultHeaders(defaultHeaders);
RestClient restClient = builder.build();
RestHighLevelClient client = new RestHighLevelClient(restClient);
GetRequest getRequest = new GetRequest("test-index", "test-all", "26269");
GetResponse getResponse = client.get(getRequest);
System.out.println(getResponse.getSourceAsString());
}
use of org.elasticsearch.action.get.GetRequest in project pancm_project by xuwujing.
the class EsHighLevelRestTest1 method queryById.
/**
* 查询数据
*
* @throws IOException
*/
private static void queryById() {
String type = "_doc";
String index = "test1";
// 唯一编号
String id = "1";
// 创建查询请求
GetRequest getRequest = new GetRequest(index, type, id);
GetResponse getResponse = null;
try {
getResponse = client.get(getRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ElasticsearchException e) {
// 如果是索引不存在
if (e.status() == RestStatus.NOT_FOUND) {
System.out.println("该索引库不存在!" + index);
}
}
// 如果存在该数据则返回对应的结果
if (getResponse.isExists()) {
long version = getResponse.getVersion();
String sourceAsString = getResponse.getSourceAsString();
Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
byte[] sourceAsBytes = getResponse.getSourceAsBytes();
System.out.println("查询返回结果String:" + sourceAsString);
System.out.println("查询返回结果Map:" + sourceAsMap);
} else {
System.out.println("没有找到该数据!");
}
}
use of org.elasticsearch.action.get.GetRequest in project baseio by generallycloud.
the class TestPut method test.
@SuppressWarnings("resource")
public static void test() throws Exception {
Settings esSettings = Settings.builder().build();
/*
这里的连接方式指的是没有安装x-pack插件,如果安装了x-pack则参考{@link ElasticsearchXPackClient}
1. java客户端的方式是以tcp协议在9300端口上进行通信
2. http客户端的方式是以http协议在9200端口上进行通信
*/
TransportClient client = new PreBuiltTransportClient(esSettings).addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
System.out.println("ElasticsearchClient 连接成功");
String index = "twitter";
IndexResponse putResponse = client.prepareIndex(index, "tweet", "1").setSource(jsonBuilder().startObject().field("user", "kimchy").field("postDate", new Date()).field("message", "trying out Elasticsearch").endObject()).get();
// Index name
String _index = putResponse.getIndex();
// Type name
String _type = putResponse.getType();
// Document ID (generated or not)
String _id = putResponse.getId();
// Version (if it's the first time you index this document, you will get: 1)
long _version = putResponse.getVersion();
// status has stored current instance statement.
RestStatus status = putResponse.status();
System.out.println(_index);
System.out.println(_type);
System.out.println(_id);
System.out.println(_version);
System.out.println(status);
GetRequest getRequest = new GetRequest("twitter", _type, _id);
GetResponse getResponse = client.get(getRequest).get();
System.out.println(getResponse.getSource());
client.close();
}
Aggregations