0
点赞
收藏
分享

微信扫一扫

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

zidea 2022-04-18 阅读 52
java后端

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xiaohui</groupId>
    <artifactId>spring-boot-es-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-es-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.8.3</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.8.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

步骤二、创建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();
            }
        }
    }

}
举报

相关推荐

0 条评论