springboot集成ElasticSearch笔记

https://www.elastic.co/cn/downloads/elasticsearch
elasticSearch官方下载

https://www.bilibili.com/video/BV17a4y1x7zq?p=12
借鉴狂神视频教学

https://www.elastic.co/guide/cn/index.html

https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
官方文档地址

springboot集成ElasticSearch笔记_第1张图片
https://www.elastic.co/guide/en/elasticsearch/client/index.html

springboot集成ElasticSearch笔记_第2张图片

JAVA REST Client已过期
现在先用JAVA REST Client学习

springboot集成ElasticSearch笔记_第3张图片

导入依赖
<repositories>
    <repository>
        <id>es-snapshotsid>
        <name>elasticsearch snapshot reponame>
        <url>https://snapshots.elastic.co/maven/url>
    repository>
repositories>




<dependency>
    <groupId>org.elasticsearch.clientgroupId>
    <artifactId>elasticsearch-rest-high-level-clientartifactId>
    <version>7.17.2version>
dependency>

找到原生的依赖

Initialization
RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http")));


client.close();

配置的基本问题,要保证导入的依赖和es版本一致

config类

package com.speoki.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration //xml文件
public class ElasticSearchClientConfig {



    @Bean
 public RestHighLevelClient restHighLevelCliClient(){
RestHighLevelClient client = new RestHighLevelClient(
     RestClient.builder(
 new HttpHost("localhost",9200,"http")));
 return client;
    }

}

springboot集成ElasticSearch笔记_第4张图片
源码在外部库看
springboot集成ElasticSearch笔记_第5张图片
构建有三个类RestClientBuilder,RestHighLevelClient高级客户端,RestClient普通的客户端

索引的API操作

Autowired自动装配config类,@Qualifier名字指定包

测试类测试

package com.speoki;

import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
class SpeokiEsApiApplicationTests {
    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;
//这里的名字要和方法名一致,不一致的话要用Qualifier
//测试索引的创建 request
    @Test
    void testCreateIndex() throws IOException {
        //创建索引请求
        CreateIndexRequest request = new CreateIndexRequest("speoki_index");
       //客户端执行创建请求,请求后获得响应
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(response);

    }

}

删除索引的时候返回
springboot集成ElasticSearch笔记_第6张图片
acknowledged:"true"

springboot集成ElasticSearch笔记_第7张图片

package com.speoki;

import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
class SpeokiEsApiApplicationTests {
    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;
//这里的名字要和方法名一致,不一致的话要用Qualifier
//测试索引的创建 request put speoki_index
    @Test
    void testCreateIndex() throws IOException {
        //创建索引请求
        CreateIndexRequest request = new CreateIndexRequest("speoki_index");
       //客户端执行创建请求,请求后获得响应
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(response);

    }
//测试获取索引

    @Test
    void testExistsIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("speoki_index");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    //删除索引
@Test
    void deleteIndex() throws IOException {
    DeleteIndexRequest request = new DeleteIndexRequest("speoki_index");
    AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);

    System.out.println(delete);
    System.out.println(delete.isAcknowledged());

    }

}



文档的API操作


    @Test
    //测试添加文档
void testAddDocument() throws IOException {
        //创建对象
 User user = new User();
 //创建请求
    IndexRequest request = new IndexRequest("speoki_index");
//规则 put/speoki_index/_doc/1
    request.id("1");
    request.timeout(TimeValue.timeValueSeconds(1));
    request.timeout("1s");

    //将我们的数据放入请求  json
    IndexRequest source = request.source(JSON.toJSONString(user), XContentType.JSON);

//客户端发送请求,获取响应结果
    IndexResponse index = client.index(request, RequestOptions.DEFAULT);
    System.out.println(index.toString());
    System.out.println(index.status());//对应命令返回的状态
}

//获取文档,查看是否存在 get /index/_doc/1
@Test
void testDocExists() throws IOException {
    GetRequest getRequest = new GetRequest("speoki_index", "1");
//不获取_source的上下文了
    getRequest.fetchSourceContext(new FetchSourceContext(false));
getRequest.storedFields("_none_");
boolean exists=client.exists(getRequest,RequestOptions.DEFAULT);
    System.out.println(exists);

    }

//获取文档的信息

@Test
void testGetDoc() throws IOException {
    GetRequest getRequest = new GetRequest("speoki_index", "1");
    GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
    System.out.println(getResponse.getSourceAsString());
    //打印文档的内容
    System.out.println(getResponse);

    }



    //更新文档的信息
    @Test
    void testUpdateDoc() throws IOException {
        UpdateRequest updateRequest = new UpdateRequest("speoki_index","1");
        updateRequest.timeout("1s");
        User user = new User("狂神说JAVA",18);
        updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
        UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
        System.out.println(update);
        System.out.println(update.status());
    }

    //删除文档记录
    @Test
    void testDelete00Request() throws IOException {
        DeleteRequest request = new DeleteRequest("speoki_index", "1");
    request.timeout("1s");
        DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);
        System.out.println(delete);
        System.out.println(delete.status());
    }

    //批量插入数据

    //SearchRequest搜索请求
    //SearchSourceBuilder条件构造
    //HighlightBuilder构建高亮
    //精确查询 TermQueryBuilder
    //MatchAllQueryBuilder
    //xxxQueryBuilder 对应我们刚刚看到的索引名
    
    @Test
    void testBulkRequest() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("10s");


        ArrayList<User> userList = new ArrayList<>();
        userList.add(new User("speoki1",3)  );
        userList.add(new User("speoki2",3)  );
        userList.add(new User("speoki3",3)  );
        userList.add(new User("speoki4",3)  );
        userList.add(new User("speoki5",3)  );


        //批处理请求
        for (int i = 0; i <userList.size() ; i++) {
            bulkRequest.add(new IndexRequest("speoki_index").id(""+(i+1)).source(JSON.toJSONString(userList.get(i)),XContentType.JSON));
        }
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulk.hasFailures());
//返回false代表成功

    }
    //查询

    @Test
    void testSearch() throws IOException {
        SearchRequest searchRequest =  new  SearchRequest("speoki_index");

    ///真实项目应该放在一个utils类中    SearchRequest searchRequest =  new  SearchRequest(ESConst.ES_INDEX);

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//构建搜索条件 QueryBuilder qb = new QueryBuilder();
        //使用工具类
        TermQueryBuilder tqb = QueryBuilders.termQuery("name", "speoki1");
//        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        sourceBuilder.query(tqb);
//设置分页
//        sourceBuilder.from();
//        sourceBuilder.size();
//    有默认的
      sourceBuilder.timeout(new TimeValue(60,TimeUnit.SECONDS));
searchRequest.source(sourceBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(search);
        System.out.println("======================");
        System.out.println(JSON.toJSONString(search.getHits()));
        System.out.println("=====================");
        for (SearchHit hit : search.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }


    }



    //查询

京东实战

server.port=9090

spring.thymeleaf.cache=false
#关闭thymeleaf的缓存

@Controller
public class IndexController {

    @GetMapping({"/","/index"})
    public String index(){
        return "index";
    }
}

数据问题

在这里插入图片描述
爬取数据:获取请求返回的页面信息,筛选出我们想要的数据
jsoup包

 
        
        <dependency>
            <groupId>org.jsoupgroupId>
            <artifactId>jsoupartifactId>
            <version>1.10.2version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.70version>
        dependency>
        
        <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-devtoolsartifactId>
        <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        

京东页面结构springboot集成ElasticSearch笔记_第8张图片

爬虫的写法

package com.speoki.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.net.URL;

public class HtmlParseUtil {
    public static void main(String[] args) throws IOException {
        //获取请求 https://search.jd.com/Search?keyword=java
         //记得联网 ajax不能获取,模拟浏览器
        String url = AConst.AURL;
        //解析网页 Jsoup返回Document就是浏览器Document对象
        Document parse = Jsoup.parse(new URL(url), 30000);
        //所有你在js中可以使用的方法这里都可以使用
        Element element = parse.getElementById("J_goodsList");
        System.out.println(element.html());

    }
}

springboot集成ElasticSearch笔记_第9张图片
获取所有的li标签内的内容

springboot集成ElasticSearch笔记_第10张图片
springboot集成ElasticSearch笔记_第11张图片
这里没有src。。。。
springboot集成ElasticSearch笔记_第12张图片
打印一下

图片是懒加载的

package com.speoki.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;

public class HtmlParseUtil {
    public static void main(String[] args) throws IOException {
        //获取请求 https://search.jd.com/Search?keyword=java
         //记得联网 ajax不能获取,模拟浏览器
        String url = AConst.AURL;
        //解析网页 Jsoup返回Document就是浏览器Document对象
        Document parse = Jsoup.parse(new URL(url), 30000);
        //所有你在js中可以使用的方法这里都可以使用
        Element element = parse.getElementById("J_goodsList");
    //    System.out.println(element.html());
//爬取页面,获取请求返回的信息。筛选出需要的数据
      //获取所有的li标签下的内容
        Elements li = element.getElementsByTag("li");
//打印所有li标签下的信息
        for (Element e : li) {
       //     System.out.println(e);
        //获取图片地址
            //关于这种图片特别多的网站,所有的图片都是延迟加载的
        //source-data-lazy-img
            //data-lazy-img
            String img = e.getElementsByTag("img").eq(0).attr("data-lazy-img");
            //获取商品价格p-price
            String price = e.getElementsByClass("p-price").eq(0).text();
            //获取商品名字p-name
String title=e.getElementsByClass("p-name").eq(0).text();
            System.out.println("==================");
            System.out.println(img);
            System.out.println(price);
            System.out.println(title);
        }
    }
}

====================

    public static void main(String[] args) throws IOException {
       new HtmlParseUtil().parseJD("并发").forEach(System.out::println);
       //首先调用方法,然后将得到的对象输出 forEach(System.out::println)
    }
    public ArrayList<Content> parseJD(String keywords) throws IOException {
        //获取请求 https://search.jd.com/Search?keyword=java
        //记得联网 ajax不能获取,模拟浏览器
      //  String url = AConst.AURL;
        //解析网页 Jsoup返回Document就是浏览器Document对象
        Document parse = Jsoup.parse(new URL("https://search.jd.com/Search?keyword="+keywords), 30000);
        //所有你在js中可以使用的方法这里都可以使用
        Element element = parse.getElementById("J_goodsList");
        //    System.out.println(element.html());
//爬取页面,获取请求返回的信息。筛选出需要的数据
        //获取所有的li标签下的内容

        ArrayList<Content> goodsList = new ArrayList<>();

        Elements li = element.getElementsByTag("li");
//打印所有li标签下的信息
        for (Element e : li) {
            //     System.out.println(e);
            //获取图片地址
            //关于这种图片特别多的网站,所有的图片都是延迟加载的
            //source-data-lazy-img
            //data-lazy-img
            String img = e.getElementsByTag("img").eq(0).attr("data-lazy-img");
            //获取商品价格p-price
            String price = e.getElementsByClass("p-price").eq(0).text();
            //获取商品名字p-name
            String title=e.getElementsByClass("p-name").eq(0).text();

            System.out.println("==================");
            System.out.println(img);
            System.out.println(price);
            System.out.println(title);


            Content content = new Content();
            content.setTitle(title);
            content.setImg(img);
            content.setPrice(price);
            goodsList.add(content);


        }
        return goodsList;
    }
}

springboot集成ElasticSearch笔记_第13张图片
URL不支持中文需要设置

npm显示理想树解决方法
\

springboot集成ElasticSearch笔记_第14张图片
springboot集成ElasticSearch笔记_第15张图片

你可能感兴趣的:(es)