springboot与检索

在这里插入图片描述
在这里插入图片描述
elasticsearch的安装:

[root@localhost docker]# docker pull elasticsearch

这种方式下载一般会很慢,或者直接卡死,我们可以使用阿里云的镜像加速服务
您可以通过修改 daemon配置文件 /etc/docker/daemon.json 来使用加速器

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https:aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

ES镜像的运行

[root@localhost ~]# docker run -d -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES1 c71178df2dd5
  • -e ES_JAVA_OPTS="-Xms256m -Xmx256m"
  • 因为 elasticsearch是java实现的,默认初始会2G大小的堆内存,虚拟机内存不足的话会启动报错,所以我们通过-e添加参数进行堆内存大小的限制.-Xms是初始的堆内存大小,-Xmx是最大的堆内存使用.
  • 默认进行web通信时使用的是9200端口,分布式的情况下,elasticsearch各个节点之间的饿通信需要使用的是9300端口,所以,我们需要做两个端口映射.

启动成功后,可以在浏览器进行访问:
在这里插入图片描述
ES的快速入门

- 索引: 给ES存储数据的行为,称为索引,其实就是存储的意思
在这里插入图片描述
在这里插入图片描述

springboot整合ES

springboot默认是使用springdata整合ES的.也支持使用jest操作ES
使用jest整合ES
1.导入依赖

 <!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>5.3.3</version>
        </dependency>

2.配置主机和端口

spring:
  elasticsearch:
    jest:
      uris: http://192.168.124.127:9200
  1. 开始测试
  @Autowired
    JestClient jestClient;

    @Test
    public void contextLoads() throws IOException {
        //给ES中索引 (保存) 一个文档
        Article article = new Article();
        article.setAuthor("zhangsan");
        article.setId(1);
        article.setTitle("好消息");
        article.setContent("hello world");
        //创建一个索引,然后把article索引到ES中的atguigu索引中.
        //构建一个索引功能
        Index index = new Index.Builder(article).index("atguigu").type("news").build();

        //执行
        jestClient.execute(index);
    }

    /**
     * 使用jest进行搜索功能,以全文搜索为例
     */
    @Test
    public void search() throws IOException {
        //构建查询规则
        String json = "{\n" +
                "    \"query\" : {\n" +
                "        \"match\" : {\n" +
                "            \"content\" : \"hello\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        //构建搜索
        Search search = new Search.Builder(json).addIndex("atguigu").addType("news").build();

        //执行搜索并且获取搜索结果
        SearchResult result = jestClient.execute(search);

        System.out.println(result.getJsonString());
    }

Jest的文档

使用springdata整合ES

  1. 导入依赖
 <!--spring boot 默认使用的是springdata进行操作ES-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
  1. 配置节点名称和节点通信端口
spring:
   data:
    elasticsearch:
      cluster-name: elasticsearch   # 节点名称
      cluster-nodes: 192.168.124.127:9300  # 节点间进行通信的端口
  1. 更换版本

启动之后会报错

org.elasticsearch.transport.ConnectTransportException: [][192.168.124.127:9300] connect_timeout[30s]

这个错误主要是,由于我们那个springboot的版本和elasticsearch的版本比匹配导致的,解决方案有两种,第一种是 给springboot版本升级,(但是一般升级后也可能还是不匹配),第二种就是更换低版本的elastic.[版本匹配参考],(https://github.com/spring-projects/spring-data-elasticsearch),我们可以对自己的pom问价那种的elastic和springdata的版本分析之后,决定到底是升级还是降级

在这里插入图片描述
运行低版本的ES镜像

[root@localhost ~]# docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9201:9200 -p 9301:9300 --name ES2 5e9d896dc62c
  1. 测试

用法可以参考官方文档
两种用法:
* 1. 编写一个ElasticSearchRepository接口的子接口
* 2. ElasticSearchTemplate 操作ES

1.编写接口

//该接口有两个泛型,ElasticsearchRepository<T, ID extends Serializable>
    //第一个是要存储的数据的类型,第二个是主键的类型
public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
	//也可以在接口中编写自定义的方法,具体的方法的命名可以参照官方文档
	public List<Book> findBookByBookNameLike(String bookName);
}

2,编写实体类

//@Document的作用就是用来指定实体类将要被存储的索引名称和类型
@Document(indexName = "atguigu" , type = "book")
public class Book {
    private Integer id;
    private String author;
    private String bookName;
}

3 测试类

@Autowired
    BookRepository bookRepository;

    @Test
    public void test02(){
        Book book = new Book();
        book.setId(1);
        book.setAuthor("刘猛");
        book.setBookName("我是特种兵");
        bookRepository.index(book);
    }
    
	@Test
    public void test03(){
       for (Book book : bookRepository.findBookByBookNameLike("我")) {
            System.out.println(book);
        }
    }

4 访问结果
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值