1.es下载&配置

配置JVM

在这里插入图片描述

配置跨域
在这里插入图片描述

配置https和密码

在这里插入图片描述

2.es启动

.\elasticsearch.bat

或 后台启动:
nohup ./bin/elasticsearch&

浏览器访问:https://localhost:9200
输入账户:elastic / 123456
在这里插入图片描述

3.重置es密码

.\elasticsearch-reset-password.bat -u elastic -i

在这里插入图片描述

4.安装图形化插件:elasticsearch-head插件,完成图形化界面的效果,完成索引数据的查看
  1. es核心概念

    面向文档

    可以对文档(而非成行成列的数据)进行索引、搜索、排序、过滤

    Elasticsearch对比传统[关系型数据库]如下:

    Relational DB ‐> Databases ‐> Tables ‐> Rows ‐> Columns
    Elasticsearch ‐> Index ‐> Types ‐> Documents ‐> Fields

集群cluster:一个集群就是由一个或多个节点组织在一起,它们共同持有整个的数据,并一起提供索引和搜索功能。一个集群由 一个唯一的名字标识,这个名字默认就是“elasticsearch”。这个名字是重要的,因为一个节点只能通过指定某个集 群的名字,来加入这个集群。

节点node:一个节点是集群中的一个服务器,作为集群的一部分,它存储数据,参与集群的索引和搜索功能;一个节点可以通过配置集群名称的方式来加入一个指定的集群。默认情况下,每个节点都会被安排加入到一个叫 做“elasticsearch”的集群中

分片和复制 shards&replicas:一个索引可以存储超出单个结点硬件限制的大量数据

ElasticSearch客户端操作:

  • 使用elasticsearch-head插件
  • 使用elasticsearch提供的Restful接口直接访问
  • 使用elasticsearch提供的API进行访问

Elasticsearch的接口语法:

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

查询文档document有三种方式:

  • 根据id查询;
  • 根据关键词查询
  • 根据输入的内容先分词,再查询
  1. elasticsearch集成springboot使用
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
1.创建实体

import lombok.Data;
import org.apache.catalina.Store;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Document(indexName = "article")
@Data
public class Article {
    @Id
    @Field(index = false,type = FieldType.Integer)
    private Integer id;

    /**
     * index:是否设置分词  默认为true
     * analyzer:储存时使用的分词器
     * searchAnalyze:搜索时使用的分词器
     * store:是否存储  默认为false
     * type:数据类型  默认值是FieldType.Auto
     *
     */
    @Field(analyzer = "ik_smart",searchAnalyzer = "ik_smart",store = true,type = FieldType.Text)
    private String title;

    @Field(analyzer = "ik_smart",searchAnalyzer = "ik_smart",store = true,type = FieldType.Text)
    private String context;

    @Field(store = true,type =FieldType.Integer)
    private  Integer hits;
}


2.创建CRUD操作类
import com.cloud.entities.Article;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *  自定义接口需要继承ElasticsearchRepository<实体类型,主键类型>  基本的crud 分页
 */
@Component
public interface ArticalRepository extends ElasticsearchRepository<Article,Integer> {
    List<Article> findByTitle(String title);


    List<Article> findArticleByTitleOrContext(String title,String context);

    /**
     * 根据标题或内存查询(含分页)
      * @param title
     * @param context
     * @param pageable0
     * @return
     */
    List<Article> findByTitleOrContext(String title, String context, Pageable pageable0);

}

3.创建启动入口类
@SpringBootApplication
@MapperScan("com.cloud.mapper")  //import tk.mybatis.spring.annotation.MapperScan
public class Main8001 {
    public static void main(String[] args) {
        SpringApplication.run(Main8001.class,args);
    }
  }
4.配置springboot配置es
  spring:
    elasticsearch:
    	uris: http://localhost:9200
   		connection-timeout: 5s
        #username: elastic
        #password: 123456
  
5.创建单元测试类
import com.cloud.Main8001;
import com.cloud.entities.Article;
import com.cloud.repository.ArticalRepository;
import jakarta.annotation.Resource;
import org.elasticsearch.client.RestClient;
import org.junit.jupiter.api.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.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Main8001.class)
public class ArticleTest {


    @Resource
    private RestClient restClient;

    @Resource
    private ElasticsearchTemplate elasticsearchTemplate;

    @Resource
    private ArticalRepository articalRepository;

    /**1.
     * 使用ElasticsearchTemplate
     */
    @Test
    void insert(){
        Article article=new Article();
        article.setTitle("男乒乓");
        article.setId(1);
        article.setContext("中国赢了");
        article.setHits(100);
        elasticsearchTemplate.save(article);
    }

    /**
     * 2.
     * 使用Repository
     */
    @Test
    void insert2(){
        Article article=new Article();
        article.setTitle("男乒乓2");
        article.setId(2);
        article.setContext("中国赢了2");
        article.setHits(120);
        articalRepository.save(article);
    }

    /**
     * 批量保存
     */
    @Test
    void insert3(){
        Article article=new Article();
        article.setTitle("男乒乓3");
        article.setId(3);
        article.setContext("中国赢了2");
        article.setHits(130);
        articalRepository.saveAll(Arrays.asList(article));
    }


    @Test
    void update(){
        Article article= articalRepository.findById(2).get();
        article.setTitle("篮球");
        articalRepository.save(article);
    }

    @Test
    void update2(){
        Article article=new Article();
        article.setId(2);
        article.setTitle("网球");
        elasticsearchTemplate.update(article);
    }

    /**
     *  查询全部数据
     */

    @Test
    void findAll(){
        Iterable<Article> articles=articalRepository.findAll();
        articles.forEach(System.out::println);
    }


    /**
     * 根据id删除
     */

    @Test
    void deleteById(){
        articalRepository.deleteById(3);
    }

    /**
     * 删除:传入实体类删
     */
    @Test
    void delete(){
        Article article=new Article();
        article.setId(3);
        articalRepository.delete(article);
    }



    /**
     * 删除索引里面所有数据
     */
    @Test
    void deleteAll(){
        articalRepository.deleteAll();
    }


}

  1. 测试工具:postman
  2. es 参考资料:

https://cloud.tencent.com/developer/article/2249187

https://www.hadoopdoc.com/elasticsearch/elasticsearch-begin-tutorial

Logo

助力广东及东莞地区开发者,代码托管、在线学习与竞赛、技术交流与分享、资源共享、职业发展,成为松山湖开发者首选的工作与学习平台

更多推荐