栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

elasticsearch _source(springboot集成elasticsearch)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

elasticsearch _source(springboot集成elasticsearch)

文章目录

一、Spring Data?二、Spring Data ElasticSearch三、⼊门案例

1)导⼊Spring Data ElasticSearch坐标2)启动器配置⽂件3) 编写实体Article4)编写Dao6)创建测试类

一、Spring Data?

Spring Data是⼀个⽤于简化数据库访问,并⽀持云服务的开源框架。其主要⽬标是使得对数据的访问变
得⽅便快捷,并⽀持map-reduce框架和云计算数据服务。 Spring Data可以极⼤的简化JPA的写法,可
以在⼏乎不⽤写实现的情况下,实现对数据的访问和操作。除了CRUD外,还包括如分页、排序等⼀些
常⽤的功能。官网入口:
Spring Data的官⽹

Spring Data常⽤的功能模块如下:

二、Spring Data ElasticSearch

Spring Data ElasticSearch 基于 spring data API 简化 elasticSearch操作,将原始操作elasticSearch的客
户端API 进⾏封装 。Spring Data为Elasticsearch项⽬提供集成搜索引擎。Spring Data Elasticsearch
POJO的关键功能区域为中⼼的模型与Elastichsearch交互⽂档和轻松地编写⼀个存储库数据访问层。
官网入口:
官⽅⽹站

三、⼊门案例 1)导⼊Spring Data ElasticSearch坐标


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    
    com.alisinngai
    es_demo2
    0.0.1-SNAPSHOT
    es_demo2
    Demo project for Spring Boot
    
        11
    
    
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            junit
            junit
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


2)启动器配置⽂件
package com.alisinngai.dv;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EsDemo2Application {

    public static void main(String[] args) {
        SpringApplication.run(EsDemo2Application.class, args);
    }

}
# 配置文件
spring:
  data:
    elasticsearch:
      cluster-name: my-elasticsearch
      cluster-nodes: 127.0.0.1:9300

3) 编写实体Article
package com.alisinngai.dv.domain;

import lombok.Data;
import lombok.EqualsAndHashCode;
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;



@Data
@document(indexName = "es_blog",type = "article")
@EqualsAndHashCode
public class Article {
    @Id
    @Field(type = FieldType.Long,store = true)
    private long id;
    @Field(type = FieldType.Text,store = true,analyzer = "ik_smart")
    private String title;
    @Field(type = FieldType.Text,store = true,analyzer = "ik_smart")
    private String content;

}

4)编写Dao

⽅法命名规则查询的基本语法findBy + 属性 + 关键词 + 连接符

package com.alisinngai.dv.repositories;

import com.alisinngai.dv.domain.Article;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;

import java.util.List;

public interface ArticleRepository extends ElasticsearchCrudRepository {
//    ⽅法命名规则查询的基本语法findBy + 属性 + 关键词 + 连接符(属性 + 关键词 + 连接符)

    List
findByTitleOrContent(String title,String content); List
findByTitle(String title); List
findByTitleOrContent(String title, String content, Pageable pageable); List
findByTitleLike(String title); }
6)创建测试类
package com.alisinngai.dv;

import com.alisinngai.dv.domain.Article;
import com.alisinngai.dv.repositories.ArticleRepository;
import org.elasticsearch.index.query.QueryBuilders;
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.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.test.context.junit4.SpringRunner;

import javax.swing.*;
import java.io.Reader;
import java.util.List;

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

    @Autowired
    private ArticleRepository articleRepository;
    @Autowired
    private ElasticsearchTemplate template;

    @Test
    public void contextLoads() {
    }

    @Test
    public void createIndex() throws Exception {
        //配置映射关系
        template.putMapping(Article.class);
        //创建索引,并配置映射关系
        template.createIndex(Article.class);
    }

    @Test
    public void deleteIndex() {
        template.deleteIndex("es_blog");
    }



	//添加测试数据
    @Test
    public void adddocument() throws Exception {
        for (int i = 0; i < 100; i++) {
            //创建对象
            Article article = new Article();
            article.setId(i+1);
            article.setTitle("女护士路遇昏迷男子跪地抢救: 救人是职责更是本能" + i);
            article.setContent("这是一个美丽的女护士妹妹 " + i);
            //把文档写入索引库
            articleRepository.save(article);
        }
    }

    @Test
    public void deleteById(){
        articleRepository.deleteById(4L);
    }

    @Test
    public void findAll(){
        Iterable
all = articleRepository.findAll(); all.forEach(System.out :: println); } @Test public void findByTitle(){ List
list = articleRepository.findByTitle("女护士"); list.forEach(System.out :: println); } @Test public void findByTitleLike(){ List
list = articleRepository.findByTitleLike("美丽的女护士"); list.forEach(System.out :: println); } @Test public void findByTitleOrContent(){ List
byTitleOrContent = articleRepository.findByTitleOrContent("女护士", "女护士"); byTitleOrContent.forEach(System.out :: println); } @Test public void findByTitleOrContentPage(){ PageRequest of = PageRequest.of(0, 10); List
list = articleRepository.findByTitleOrContent("女护士", "女护士", of); list.forEach(System.out :: println); } //原生的dsl语句 @Test public void testNativeSearchQuery(){ NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.queryStringQuery("女护士").defaultField("title")) .withPageable(PageRequest.of(0, 15)) .build(); List
list = template.queryForList(nativeSearchQuery, Article.class); list.forEach(res -> System.out.println(res)); } }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/773507.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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