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
官方文档地址

在这里插入图片描述
https://www.elastic.co/guide/en/elasticsearch/client/index.html

在这里插入图片描述

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

在这里插入图片描述

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




<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.17.2</version>
</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;
    }

}

在这里插入图片描述
源码在外部库看
在这里插入图片描述
构建有三个类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);

    }

}

删除索引的时候返回
在这里插入图片描述
acknowledged:"true"

在这里插入图片描述

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包

 <!-- jsoup解析页面 -->
        <!-- 解析网页 爬视频可 研究tiko -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.10.2</version>
        </dependency>
        <!--引入fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.70</version>
        </dependency>
        <!-- devtools热部署 -->
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!-- 导入了elasticsearch-->

京东页面结构在这里插入图片描述

爬虫的写法

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());

    }
}

在这里插入图片描述
获取所有的li标签内的内容

在这里插入图片描述
在这里插入图片描述
这里没有src。。。。
在这里插入图片描述
打印一下

图片是懒加载的

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;
    }
}

在这里插入图片描述
URL不支持中文需要设置

npm显示理想树解决方法
\

在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中集成Elasticsearch时,需要进行mapping配置。根据引用\[2\]中的代码示例,可以创建一个名为EsGoodsRepository的接口,并继承ElasticsearchRepository。在该接口中,可以定义对应的实体类和主键类型。这样就可以通过该接口来进行Elasticsearch的操作了。 另外,根据引用\[1\]中的说明,为了允许外部访问Elasticsearch,需要修改配置文件。配置文件的路径在config/elasticsearch.yml。可以使用vi命令打开该文件进行修改。 总结起来,集成Elasticsearch时,需要创建EsGoodsRepository接口,并继承ElasticsearchRepository,同时进行mapping配置。另外,还需要修改Elasticsearch的配置文件以允许外部访问。 #### 引用[.reference_title] - *1* *3* [SpringBoot集成Elasticsearch7.4 实战(一)](https://blog.csdn.net/qq_37284798/article/details/128718272)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SpringBoot集成elasticsearch使用(增删改查)](https://blog.csdn.net/ljfgyy0205/article/details/130086495)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值