栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Elasticsearch学习笔记(四)

Elasticsearch学习笔记(四)

Spring Data ElasticSearch整合 什么是Spring Data?

Spring Data是一个用于简化数据库访问,并支持云服务的开源框架。其主要目标是使得对数据的访问变得方便快捷,并支持map-reduce框架和云计算数据服务。 Spring Data可以极大的简化JPA的写法,可以在几乎不用写实现的情况下,实现对数据的访问和操作。除了CRUD外,还包括如分页、排序等一些常用的功能。

Spring Data的官网:Redirecting…

什么是Spring Data ElasticSearch?

        Spring Data ElasticSearch 基于 spring data API 简化 elasticSearch操作,将原始操作elasticSearch的客户端JAVA API 进行封装 。Spring Data为Elasticsearch项目提供集成搜索引擎。Spring Data Elasticsearch POJO的关键功能区域为中心的模型与Elastichsearch交互文档和轻松地编写一个存储库数据访问层。官方网站:Redirecting…

Spring boot 集成spring data elasticsearch的方式来开发更加的方便和快捷。

步骤:
1.起步依赖
2.配置文件配置连接服务器
3.创建POJO 建立映射关系  @document @field @id
4.创建dao接口继承elasticsearchRepository

根据需要执行不同的dao的方法。

  创建Maven工程(jar),导入坐标我们已经导入了


    org.springframework.boot
    spring-boot-starter-data-elasticsearch

   创建pojo, 添加注解

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 = "blog04",type = "article")
public class Article {

    @Id //文档唯一标识
    private Long id;
    @Field(type = FieldType.Text,index = true,store = false,analyzer = "ik_smart",searchAnalyzer = "ik_smart")
    private String title;
    @Field(type= FieldType.Text,index = true,store = false,analyzer = "ik_smart",searchAnalyzer = "ik_smart")
    private String content;

    public Article() {
    }

    public Article(Long id, String title, String content) {
        this.id = id;
        this.title = title;
        this.content = content;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

创建Dao接口继承ElasticsearchRepository

import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;

import java.util.List;


public interface ArticleDao extends ElasticsearchCrudRepository  {

    //创建文档
    //更新文档 @Highlight
    //查询文档
    //删除文档

    //自定义查询
    List findByTitle(String title);

    List findByTitleAndContent(String title,String content);

    @Query("{"match": {"title": {"query": "?0"}}}") //dsl语句
    List abcdefg(String acdf);
}
CRUD:
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.ElasticsearchTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;


@SpringBootTest
@RunWith(SpringRunner.class)
public class TestDataEs {

    @Autowired
    private ArticleDao articleDao;

    //核心的类 操作ES的
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    //创建索引和映射
    @Test
    public void createIndAndMapping(){
        elasticsearchTemplate.createIndex(Article.class);
        elasticsearchTemplate.putMapping(Article.class);
    }

    //创建文档
    @Test
    public void createdocument(){
        List articles = new ArrayList<>();
        for (long i = 0; i < 100; i++) {
            Article article = new Article(i, "小米手机棒棒棒" + i, "小米电话可可可" + i);
            articles.add(article);
        }
        articleDao.saveAll(articles);
    }

    //更新文档
    //没有id对应的数据,就是新增; 有当前id对应的数据,就是更新

    //删除文档
    @Test
    public void delete(){
        articleDao.deleteById(1L);
    }

    //查询文档
    @Test
    public void select(){
        

        //获取列表记录
       

        //自定义查询
        List articles = articleDao.findByTitle("手机");
        for (Article article : articles) {
            System.out.println(article.getTitle());
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/701786.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号