Search in sources :

Example 31 with GetRequest

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();
}
Also used : GetRequest(org.elasticsearch.action.get.GetRequest) IndexMainType(org.sonar.server.es.IndexType.IndexMainType) IndexRequest(org.elasticsearch.action.index.IndexRequest) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) MappingMetadata(org.elasticsearch.cluster.metadata.MappingMetadata) Test(org.junit.Test)

Example 32 with GetRequest

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;
    }
}
Also used : GetRequest(org.elasticsearch.action.get.GetRequest) GetResponse(org.elasticsearch.action.get.GetResponse) DocumentMapping(com.b2international.index.mapping.DocumentMapping)

Example 33 with GetRequest

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());
}
Also used : Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HttpHost(org.apache.http.HttpHost) GetRequest(org.elasticsearch.action.get.GetRequest) RestClient(org.elasticsearch.client.RestClient) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) GetResponse(org.elasticsearch.action.get.GetResponse) BasicHeader(org.apache.http.message.BasicHeader) Test(org.junit.Test)

Example 34 with GetRequest

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("没有找到该数据!");
    }
}
Also used : GetRequest(org.elasticsearch.action.get.GetRequest) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 35 with GetRequest

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();
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) RestStatus(org.elasticsearch.rest.RestStatus) IndexResponse(org.elasticsearch.action.index.IndexResponse) TransportAddress(org.elasticsearch.common.transport.TransportAddress) GetRequest(org.elasticsearch.action.get.GetRequest) GetResponse(org.elasticsearch.action.get.GetResponse) Settings(org.elasticsearch.common.settings.Settings) Date(java.util.Date)

Aggregations

GetRequest (org.elasticsearch.action.get.GetRequest)45 GetResponse (org.elasticsearch.action.get.GetResponse)29 Test (org.junit.Test)14 IOException (java.io.IOException)13 IndexRequest (org.elasticsearch.action.index.IndexRequest)9 HashMap (java.util.HashMap)7 TikaDocument (org.icij.extract.document.TikaDocument)7 FetchSourceContext (org.elasticsearch.search.fetch.subphase.FetchSourceContext)6 ArrayList (java.util.ArrayList)5 DocumentFactory (org.icij.extract.document.DocumentFactory)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ParsingReader (org.apache.tika.parser.ParsingReader)4 ElasticsearchException (org.elasticsearch.ElasticsearchException)4 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)4 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)4 PathIdentifier (org.icij.extract.document.PathIdentifier)4 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)3 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)3