栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

RestHighLevelClient快速入门

RestHighLevelClient快速入门

创建RestHighLevelClient对象

elasticsearch:
  host: 101.35.247.167
  port: 9200

package com.zhang.elasticsearch.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
@Component
public class ElasticsearchConfig {
    private String host;
    private int port;
    private String scheme = "http";

    public void setHost(String host) {
        this.host = host;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void setScheme(String scheme) {
        this.scheme = scheme;
    }

    @Bean
    public RestHighLevelClient Client() {
        return new RestHighLevelClient(RestClient.builder(new HttpHost(
                host, port, scheme)));

    }
}

package com.zhang.elasticsearch;

import com.alibaba.fastjson.JSON;
import com.zhang.elasticsearch.domain.User;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.*;
import org.elasticsearch.client.ml.PostDataRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.Map;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;


@SpringBootTest
class ElasticsearchApplicationTests {
    @Autowired
    RestHighLevelClient client;

    @Test
    void contextLoads() {
        System.out.println(client);
    }

    
    //创建索引  IndicesClient对索引进行CRUD,IndexRequest则是对文档进行CRUD
    @Test
    public void addIndex() throws IOException {
        //获得索引库对象
        IndicesClient indices = client.indices();

        //创建索引请求
        CreateIndexRequest index = new CreateIndexRequest("zyp");

        //创建索引
        CreateIndexResponse result = indices.create(index, RequestOptions.DEFAULT);

        System.out.println(result.isAcknowledged());
    }
    
    @Test
    public void createIndex() throws IOException {
        //获得所有客户端
        IndicesClient indices = client.indices();
        //创建添加映射请求
        PutMappingRequest putMappingReq = new PutMappingRequest("zyp");
        XContentBuilder mapping;
        //构造映射
        //{"properties":{"name":{"type":"text"},"age":{"type":"long"},"adr":{"type":"text","analyzer:"ik_smart""}}} startObject对应左花括号,endObject对应有括号
        mapping = jsonBuilder().startObject().startObject("properties").startObject("name").
                field("type", "text").endObject()
                .startObject("age").field("type", "long").endObject()
                .startObject("adr").field("type", "text").field("analyzer", "ik_smart").endObject()
                .endObject().endObject();
        putMappingReq.type("docs");
        putMappingReq.source(mapping);
        AcknowledgedResponse acknowledgedResponse = indices.putMapping(putMappingReq, RequestOptions.DEFAULT);
        System.out.println(acknowledgedResponse.isAcknowledged());
    }

    
    @Test
    public void addDoc() throws IOException {
        //创建要存入的对象
        User user = new User();
        user.setName("张三");
        user.setAge(23);
        user.setAddress("永丰");

        //将对象转为json字符串
        String UserJson = JSON.toJSonString(user);

        //创建索引请求
        IndexRequest indexRequest = new IndexRequest("zyp").id("6").source(UserJson, XContentType.JSON);

        //创建文档
        IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(index.getResult());
    }

    @Test
    
    public void updateDoc() throws IOException {
        //创建要存入的对象
        User user = new User();
        user.setName("张三");
        user.setAge(23);
        user.setAddress("永丰县");

        //讲对象转为json字符串
        String UserJson = JSON.toJSonString(user);

        //获得更新索引请求对象
        IndexRequest updateRequest = new IndexRequest("zyp").id("6").source(UserJson, XContentType.JSON);
        IndexResponse index = client.index(updateRequest, RequestOptions.DEFAULT);

        System.out.println(index.getResult());
    }

    @Test
    
    public void getDoc() throws IOException {
        //获得查询索引的请求对象
        GetRequest gerRequest = new GetRequest("zyp").id("6");

        //获得文档对象
        GetResponse doc = client.get(gerRequest, RequestOptions.DEFAULT);

        //获得文档数据
        System.out.println(doc.getSourceAsString());
    }

    @Test
    
    public void delDoc() throws IOException {
        //获得删除的索引请求对象
        DeleteRequest delRequest = new DeleteRequest("zyp").id("6");

        //删除文档
        DeleteResponse delete = client.delete(delRequest, RequestOptions.DEFAULT);
        System.out.println(delete.getIndex());
    }

    
    @Test
    public void delIndex() throws IOException {
        IndicesClient indices = client.indices();
        DeleteIndexRequest delReq = new DeleteIndexRequest("zyp");
        AcknowledgedResponse delete = indices.delete(delReq, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());
    }
    //从数据库中批量导入数据到es
   @Test
    void contextLoads() throws IOException {
        //查询mysql中所有数据
        List goodes = goodsMapper.listGoods();

        //创建批量处理对象
        BulkRequest bulkRequest = new BulkRequest();

        //循环添加新增处理请求
        for (Goods goods : goodes) {
            goods.setSpec(JSON.parseObject(goods.getSpecStr(), Map.class));
            String goodsJson = JSON.toJSonString(goods);
            IndexRequest indexRequest = new IndexRequest("goods").id(goods.getId() + "").source(goodsJson, XContentType.JSON);
            bulkRequest.add(indexRequest);
        }

        //提交批量处理对象
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);

        //查看添加状态
        System.out.println(bulk.status());

    }


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/663423.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号