ES集成SpringBoot

官方文档https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html

1.导包

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.15.2</version>
</dependency>

2.写代码

//构建springboot项目可以直接勾选NoSQL选项中的ES,在官网初始化中复制这个代码
//注入到Bean中就可以调用测试了!!!搞定!1.构建项目导包。2.注入。3使用
@Configuration
public class ElasticsearchConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        //new HttpHost("localhost", 9200, "http"), //不是集群构建一个就行了
                        new HttpHost("localhost", 9200, "http")));
        return client;
    }
}

3.测试

  1.索引(库)操作

@Qualifier("restHighLevelClient")
    @Autowired
    private RestHighLevelClient client;
    @Test
    void createIndex() throws IOException {
        //测试API
        //索引创建 Request
        CreateIndexRequest dlb_index = new CreateIndexRequest("dlb_index");
        // 执行请求  获得响应
        CreateIndexResponse createIndexResponse = client.indices()
                .create(dlb_index, RequestOptions.DEFAULT);
        System.out.println("createIndexResponse = " + createIndexResponse);
        //创建索引完成,put
     //删除测试       
     DeleteIndexRequest request = new DeleteIndexRequest("dlb_index");
      AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
      //获取索引测试
      GetIndexRequest request = new GetIndexRequest("dlb_index");
      boolean exists = client.indices().exists(request,RequestOptions.DEFAULT);        
    }

  2.文档操作

 //添加文档
    @Test
    void addDoc() throws IOException {
        User u1 = new User("大萝卜", 10);
        IndexRequest req = new IndexRequest("dlb_index");
        //put test/_doc/1
        req.id("1");
        //超时设置
        req.timeout(TimeValue.timeValueSeconds(1));
        req.timeout("1s");
        //将数据放到请求 json  引入fastJson
        req.source(JSON.toJSONString(u1), XContentType.JSON);
        //客户端发送请求 ,获取响应结果
        IndexResponse index = client.index(req, RequestOptions.DEFAULT);
        System.out.println("index = " + index.toString());
        System.out.println("index = " + index.status());//CREATED 首次状态
        /**
         PUT /test1/type1/1
         {
         "name": "大萝卜",
         "uage" : 30
         }
         */
    }
  @Test//判断文档(这个条数据)是否存在  GetRequest
    void pdDoc() throws IOException {
        GetRequest request = new GetRequest("dlb_index","1");
        //不获取返回的_source上下文,效率更高
        request.fetchSourceContext(new FetchSourceContext(false));
        boolean exists = client.exists(request, RequestOptions.DEFAULT);
        System.out.println("exists = " + exists);
    }
    @Test//获取文档信息(本行数据)
    void getDoc() throws IOException {
        GetRequest request = new GetRequest("dlb_index","1");
        GetResponse documentFields = client.get(request, RequestOptions.DEFAULT);
        //打印文档内容
        //map转字符串:{"age":10,"name":"大萝卜"}
        System.out.println(documentFields.getSourceAsString());
        //这个地方就是kibana右侧显示的巴拉巴拉
        System.out.println("documentFields = " + documentFields);
    }
    @Test//更新文档信息(修改本行数据)
    void upDataDoc() throws IOException {
        UpdateRequest request = new UpdateRequest("dlb_index","1");
        request.timeout("1s");
        User user = new User("啊哈哈",33);
        request.doc(JSON.toJSONString(user),XContentType.JSON);
        UpdateResponse update = client.update(request, RequestOptions.DEFAULT);
        System.out.println(update.status());
    }
    @Test//删除文档记录
    void deleteDoc() throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest("dlb_index","1");
        deleteRequest.timeout("1s");
        DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(delete.status());
    }
  @Test//批量导入数据,本质像循环
    void bigData() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("10s");
        ArrayList<User> list = new ArrayList<>();
        list.add(new User("大萝卜",18));
        list.add(new User("大萝卜",18));
        for (int i = 0; i < list.size(); i++) {
            bulkRequest.add(new IndexRequest("dlb_index")
                    .id(i+1+"")//不写id就会生成随机id
                    .source(JSON.toJSONString(list.get(i)),XContentType.JSON));
        }
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        boolean b = bulk.hasFailures();//成功返回false
        System.out.println(bulk.status());//OK
    }
   @Test//查询
    void cz() throws IOException {
     //建造者模式 SearchRequest searchRequest
= new SearchRequest("dlb_index"); //构建搜索对象---->指挥 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //设置搜索条件---->工人 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("age", "18"); searchSourceBuilder.query(termQueryBuilder);//----->生产产品 searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //放进请求中 searchRequest.source(searchSourceBuilder); //发送请求 SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT); System.out.println(JSON.toJSONString(search.getHits())); System.out.println("------------------"); SearchHits hits = search.getHits(); SearchHit[] hits1 = hits.getHits(); for (SearchHit documentFields : hits1) { System.out.println(documentFields.getSourceAsMap()); } }

4.案例

pojo实体类未写出 Crawler、user

1.爬取数据准备
<!--导包-->
<!--电影音乐用tika包-->
 <!-- 解析网页包--https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.14.3</version>
        </dependency>
2.工具类爬取方法
public List<Crawler> getData(String url) throws Exception {
        ArrayList<Crawler> list = new ArrayList<>();
        //前提:联网 , AJAX 不能获取需要模拟浏览器
        //获取请求
        if ("".equals(url)){
            url="https://XXXX.com/tag/win11-zhuti/";
        }
        //解析网页 , 返回的是前端页面 url不支持中文
        Document document = Jsoup.parse(new URL(url), 3000);
        Element elementById = document.getElementById("post-list");
        Elements li = elementById.getElementsByTag("li");
        for (Element el : li) {
            String img = el.getElementsByTag("img").eq(0).attr("src");
            String name = el.getElementsByClass("post-info").eq(0).text();
            if (!"".equals(img)&&!"".equals(name)){
                list.add(new Crawler(img,name));
                System.out.println(img);
                System.out.println("name:"+name);
            }
        }
        return list;
    }
3.导入数据和查询数据
@Service
public class HtmlService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    //插入到查询引擎中
    public Boolean parseHtml(String url) throws Exception {
        List<Crawler> list = new HtmlUtils().getData(url);
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("3m");

        for (int i = 0; i < list.size(); i++) {
            bulkRequest.add(
                    new IndexRequest("mf")
                //这里没有设置id,可以参考上篇批量添加
                            .source(JSON.toJSONString(list.get(i)),XContentType.JSON));
        }
        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        return !bulk.hasFailures();
    }
    //查询、参数1:查询谁。参数2:从第几页开始。参数3:每页大小
    public ArrayList<Map<String,Object>> searchPage(String keyword,int page,int pageSize)  {
        if (page<=1){
            page=1;
        }
        //查询连接
        SearchRequest mf = new SearchRequest("mf");
        //条件建造者
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //分页
        searchSourceBuilder.from(page);
        searchSourceBuilder.size(pageSize);
        //查询条件组装
        MatchQueryBuilder ztName = QueryBuilders.matchQuery("ztName", keyword);
        searchSourceBuilder.query(ztName);
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        //设置高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("ztName");//哪个字段是高亮
        highlightBuilder.requireFieldMatch(false);//查出的一句话中只有一个词是高亮的
        highlightBuilder.preTags("<p style='color:red'>");//怎么亮起来
        highlightBuilder.postTags("</p>");
        searchSourceBuilder.highlighter(highlightBuilder);
        //把组装好的条件放到请求中
        mf.source(searchSourceBuilder);
        //执行请求,返回结果
        SearchResponse search = null;
        try {
            search = restHighLevelClient.search(mf, RequestOptions.DEFAULT);
        } catch (IOException e) {
            System.out.println("连接异常");
            e.printStackTrace();
            return null;
        }
        ArrayList<Map<String,Object>> list = new ArrayList<>();
        for (SearchHit hit : search.getHits().getHits()) {
            Map<String, Object> map = hit.getSourceAsMap();//原来的结果
            //解析高亮字段
            Map<String, HighlightField> hmap = hit.getHighlightFields();
            HighlightField name = hmap.get("ztName");//从hit里面查找出高亮的字段
            if (name!=null){
                String a="";
                Text[] fragments = name.fragments();//
                System.err.println("fragments = " + fragments[0]);
                for (Text text : fragments) {
                    a+=text;
                }
                System.err.println(a);
                map.put("ztName",a);//替换原来的
            }
            System.out.println(map);
            list.add(map);
        }
        return list;
    }

 

posted @ 2021-12-20 20:33  大萝卜萌萌哒  阅读(593)  评论(0编辑  收藏  举报