登录    关于
马育民老师的博客

马育民的博客

QQ:65242847

springboot 使用 Spring Data Elasticsearch 操作 Elasticsearch 7.9-创建工程

说明

本文介绍 springboot + Spring Data Elasticsearch方式 操作 Elasticsearch 7.9

注意

版本对应:官网链接

springboot 2.3.x 支持 Elasticsearch 7.6

springboot 2.4.x 支持 Elasticsearch 7.9

由于版本不同,有一些配置也会不同

本文介绍 springboot 2.4.2

创建maven工程

修改 maven 配置

pom.xml

<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.4.2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

application.properties

spring.data.elasticsearch.repositories.enabled = true
spring.elasticsearch.rest.uris=http://hadoop2:9200

主启动类

@SpringBootApplication
public class Main {

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

实体类

package top.malaoshi.es.entity;

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 = "toutiao_v1", shards = 1, replicas = 0)
public class Toutiao {
    //必须有 id,这里的 id 是全局唯一的标识,等同于 es 中的"_id"
    @Id
    private String id;
    /**
     * type : 字段数据类型
     * analyzer : 分词器类型
     * index : 是否索引(默认:true)
     * Keyword : 短语,不进行分词
     */
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String title;
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String content;
    @Field(type = FieldType.Keyword)
    private String type;
    @Field(type = FieldType.Keyword)
    private String author;
    @Field(type = FieldType.Integer)
    private int like;
    @Field(type = FieldType.Date)
    private String date;

    @Override
    public String toString() {
        return "Toutiao{" +
                "id='" + id + '\'' +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", type='" + type + '\'' +
                ", author='" + author + '\'' +
                ", like=" + like +
                ", date=" + date +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setId(String 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;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getLike() {
        return like;
    }

    public void setLike(int like) {
        this.like = like;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
}

dao

package top.malaoshi.es.dao;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
import top.malaoshi.es.entity.Toutiao;

@Repository
public interface ToutiaoDao extends ElasticsearchRepository<Toutiao,String> {
}

controller

这里只写 controller 实现查询,没有通过 service

package top.malaoshi.ctrl;

import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.malaoshi.es.entity.Toutiao;

@RestController
public class TestCtrl {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchTemplate;


    /**
     * term 查询加分页
     */
    @RequestMapping("/test")
    public void termQueryByPage() {
        //term查询
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title.keyword", "JAVA技巧");
        //设置查询分页,从第一页开始,一页显示10条,从0开始
        PageRequest page = PageRequest.of(0, 10);
        //按照点赞数量降序排序
        SortBuilder sort= SortBuilders.fieldSort("like").order(SortOrder.DESC);

        NativeSearchQuery query=new NativeSearchQueryBuilder()
                .withQuery(termQueryBuilder)
                .withPageable(page)
                .withSort(sort)
                .build();

        SearchHits<Toutiao> shs = elasticsearchTemplate.search(query,Toutiao.class);
        System.out.println("共:"+shs.getTotalHits()+" 条记录!");
        for (SearchHit<Toutiao> item : shs) {
            Toutiao toutiao=item.getContent();
            System.out.println(toutiao);
        }
    }
}

原文出处:https://malaoshi.top/show_1IX1tKpTSEXs.html