029 elasticsearch文档管理(ElasticsearchRepository、ElasticsearchRestTemplate)

文章目录

文档的管理
ElasticSearchRepository接口
使用方法:
创建一个接口,继承于ElasticSearchRepository,指定使用的Entity类及对应主键数据类型
Springboot自动扫描接口并创建代理对象

  1. 新增、更新数据
    使用repository的save方法实现
  2. 删除数据
    deleteById
    deleteAll
  3. 查询数据
    可以使用repository自带的查询方法
    findById
    findAll
    可以自定义查询方法
    findBy{Title}And{content}(String title, String content);
    按照命名规则定义方法,就可以实现相应的查询

BlogRepository.java

java 复制代码
package com.xd.cubemall.search.repository;

import com.xd.cubemall.search.model.Blog;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.List;

public interface BlogRepository extends ElasticsearchRepository<Blog, Long> {
    List<Blog> findByTitle(String title);
    List<Blog> findByTitleAndContent(String title, String content);
}

BlogRepositoryTest.java

java 复制代码
package com.xd.cubemall.sdes;


import com.xd.cubemall.search.CubemallSearchApplication;
import com.xd.cubemall.search.model.Blog;
import com.xd.cubemall.search.repository.BlogRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Optional;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = CubemallSearchApplication.class)
public class BlogRepositoryTest {
    @Autowired
    private BlogRepository blogRepository;

    @Test
    public void addDocument() {
        Blog blog = new Blog();
        for (int i = 0; i < 50; i++) {
            blog.setId((long) (i+1));
            blog.setTitle("测试文档"+(i+1));
            blog.setContent("测试文档的内容"+(i+1));
            blog.setComment("注释内容");
            blog.setMobile("111111");
            blogRepository.save(blog);
            
        }

    }

    @Test
    public void updateDocument() {
        Optional<Blog> optional = blogRepository.findById(1l);
        if (optional.isPresent()) {
            Blog blog = optional.get();
            blog.setTitle("hello world");
            blogRepository.save(blog);
        }
    }

    @Test
    public void deleteDocument() {
        blogRepository.deleteById(1l);
    }

    @Test
    public void getById() {
//        Optional<Blog> optional = blogRepository.findById(1l);
//        Blog blog = optional.get();
//        System.out.println(blog);
        Iterable<Blog> all = blogRepository.findAll(PageRequest.of(1,10));
        all.forEach(b-> System.out.println(b));


    }
    @Test
    public void testFindByTitle() {
        List<Blog> list = blogRepository.findByTitle("测试");//term
        list.stream().forEach(System.out::println);
    }


    @Test
    public void testFindByTitleAndContent() {
        List<Blog> list = blogRepository.findByTitleAndContent("37", "内容");
        list.forEach(e-> System.out.println(e));
    }

}

BulkTest.java

java 复制代码
package com.xd.cubemall.sdes;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xd.cubemall.search.CubemallSearchApplication;
import com.xd.cubemall.search.model.Blog;
import com.xd.cubemall.search.repository.BlogRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.stream.Collectors;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = CubemallSearchApplication.class)
public class BulkTest {

    @Autowired
    private ElasticsearchRestTemplate template;

    @Autowired
    private BlogRepository blogRepository;


    @Test
    public void bulkBlog() {
        JSONArray jsonArray = JSON.parseArray("");
        List<IndexQuery> list = jsonArray.stream()
                .map(json -> {
                    IndexQuery query = new IndexQuery();
                    query.setId(((JSONObject) json).getString("id"));
                    query.setSource(((JSONObject) json).toJSONString());
                    return query;
                }).collect(Collectors.toList());
        template.bulkIndex(list, IndexCoordinates.of("blog_1"));
    }






    @Test
    public void saveAllBlog() {
        JSONArray jsonArray = JSON.parseArray("");
        List<Blog> list = jsonArray.stream()
                .map(json -> {
                    JSONObject jsonObject = ((JSONObject) json);
                    Blog blog = jsonObject.toJavaObject(Blog.class);
                    return blog;
                }).collect(Collectors.toList());
        blogRepository.saveAll(list);
    }

}
相关推荐
阿里云大数据AI技术9 小时前
阿里云 ES AI 引擎版:面向 Agent 场景,为亿级租户、千亿规模向量设计的搜索引擎
人工智能·elasticsearch·agent
阿里云大数据AI技术10 小时前
FalconSeek 技术解析:阿里云 Elasticsearch 云原生内核如何让查询性能飙升600%
人工智能·elasticsearch
Elastic 中国社区官方博客10 小时前
将你的 Grafana Kubernetes 仪表板迁移到 Elastic Observability:相同的 PromQL,30 倍更快的查询
大数据·人工智能·elasticsearch·搜索引擎·容器·kubernetes·grafana
Elasticsearch12 小时前
使用重新设计的 AutoOps 更快地进行 Elasticsearch 问题排查
elasticsearch
Elastic 中国社区官方博客13 小时前
如何使用 OpenTelemetry 对你的搜索 API 进行埋点,并使用 ES|QL 对其进行查询
大数据·功能测试·elasticsearch·搜索引擎·全文检索·可用性测试
菜地里的小菜鸟1 天前
logstash定时同步elasticsearch数据
elasticsearch·数据同步·logstash定时同步elastics
BerryS3N1 天前
Code Git 工作树:多分支开发的痛点与工作树的曙光
大数据·git·elasticsearch
Elasticsearch1 天前
Elastic 与 Deductive AI 携手合作,加速工程团队的 agent 式事件调查
elasticsearch
Elasticsearch2 天前
将你的 Grafana Kubernetes 仪表板迁移到 Elastic Observability:相同的 PromQL,30 倍更快的查询
elasticsearch
爱莉希雅&&&2 天前
elasticsearch+kibana+logstash+filebeat链路部署流程
大数据·elasticsearch·搜索引擎·kibana·logstash·filebeat