Elasticsearch(五) idea es环境搭建以及对ES基本操作

步骤一、 创建一个SpringBoot工程,pom.xml 如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.0.RELEASE
         
    
    com.xiaohui
    spring-boot-es-client
    0.0.1-SNAPSHOT
    spring-boot-es-client
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            com.alibaba
            fastjson
            1.2.58
        
        
            org.elasticsearch
            elasticsearch
            6.8.3
        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            6.8.3
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


步骤二、创建mapping映射对象如下:

这是查询出来的,可以调整为新增
GET /huizi/_mapping
返回
{
  "huizi" : {
    "mappings" : {
      "product" : {
        "properties" : {
          "band" : {
            "type" : "keyword"
          },
          "category" : {
            "type" : "keyword"
          },
          "id" : {
            "type" : "keyword"
          },
          "images" : {
            "type" : "keyword",
            "index" : false
          },
          "price" : {
            "type" : "double"
          },
          "title" : {
            "type" : "text",
            "analyzer" : "ik_max_word"
          }
        }
      }
    }
  }
}

步骤三:编写ES单元测试类 代码如下:

package com.xiaohui;

import com.alibaba.fastjson.JSON;
import com.xiaohui.domain.Product;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
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.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.*;

import java.io.IOException;

public class EsTest {


    private RestHighLevelClient client;

    @BeforeEach
    public void getClient(){
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.0.122", 9200, "http")));
    }

    @Test
    public void addDoc(){
        //1 构造请求数据
        Product product  = new Product(1L,"小米手机","手机",
                "小米",2699.00,"http://www.baidu.com/jjj.jpg");
        String jsonobj = JSON.toJSONString(product);
        //准备请求对象
        IndexRequest indexRequest = new IndexRequest("huizi","product","1");
        //将请求体封装到请求对象中
        indexRequest.source(jsonobj, XContentType.JSON);
        try {
            //发送请求
            IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
            System.out.println(JSON.toJSONString(indexResponse));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void getDoc(){
        //定义要获取的id
        String id = "1";
        //构造请求体
        GetRequest getRequest = new GetRequest("huizi", "product", id);
        //发送请求
        try {
            GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
            System.out.println(JSON.toJSONString(getResponse));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @Test
    public void updateDoc(){
        //构造请求对象
        UpdateRequest updateRequest = new UpdateRequest("huizi", "product","1");
        //2 构造请求数据
        Product product  = new Product(1L,"大米手机","手机",
                "大米",2699.00,"http://www.baidu.com/jjj.jpg");
        String jsonobj = JSON.toJSONString(product);
        updateRequest.doc(jsonobj, XContentType.JSON);
        try {
            UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
            System.out.println(JSON.toJSONString(updateResponse));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 批量新增
     */
    @Test
    public void bulkAddDoc(){

        BulkRequest bulkRequest = new BulkRequest();
        for (long i = 10; i < 20; i++) {
            Product product  = new Product(i,"小米手机","手机",
                    "小米",2699.00+i,"http://www.baidu.com/jjj.jpg");
            String jsonobj = JSON.toJSONString(product);
            IndexRequest indexRequest = new IndexRequest("huizi2","product",i+"");
            indexRequest.source(jsonobj, XContentType.JSON);

            bulkRequest.add(indexRequest);
        }

        try {
            BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
            System.out.println("返回: "+bulkResponse);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void deleteById(){

        DeleteRequest deleteRequest = new DeleteRequest("huizi","product","8");
        try {
            DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
            System.out.println(JSON.toJSONString(deleteResponse));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @AfterEach
    public void close(){
        if(null!= client){
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

 

你可能感兴趣的:(Elasticsearch,elasticsearch)