第十三节 SpringBoot使用ES进行多条件查询

亮子 2022-02-14 05:51:55 22221 0 0 0

1、添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

2、修改版本

    <properties>
        <java.version>1.8</java.version>
        <elasticsearch.version>7.8.1</elasticsearch.version>
    </properties>

3、实体类定义

package com.shenmazong.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

/**
 * 小说表
 * @TableName tb_story
 */
@TableName(value ="tb_story")
@Data
@Document(indexName = "tb_story", shards = 1, replicas = 1)
public class TbStory implements Serializable {
    /**
     * 小说ID
     */
    @TableId(type = IdType.AUTO)
    @Id
    private Integer id;

    /**
     * 小说名字
     */
    @Field(type = FieldType.Text, analyzer = "ik_smart")
    private String name;

    /**
     * 小说作者
     */
    @Field(type = FieldType.Keyword)
    private String author;

    /**
     * 小说内容
     */
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String content;

    /**
     * 点赞状态:0支持1反对
     */

    @Field(type = FieldType.Integer)
    private Integer status;

    /**
     * 软删除:0正常1删除
     */
    @Field(type = FieldType.Integer)
    private Integer deleted;

    /**
     * 创建时间
     */
    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date createTime;

    /**
     * 修改时间
     */
    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date updateTime;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
}

4、搜索操作

    @Test
    public void testMultiQuery() {
        //-- 创建BoolQueryBuilder
        BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();

        //--2 创建多个查询条件
        TermQueryBuilder termQueryBuilder1 = QueryBuilders.termQuery("content", "会议");
        TermQueryBuilder termQueryBuilder2 = QueryBuilders.termQuery("name", "星辰变");

        //--3 组合条件
        boolQueryBuilder.must(termQueryBuilder1).mustNot(termQueryBuilder2);

        //--4 组合查询
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .build();

        // 进行查询
        SearchHits<TbStory> search = elasticsearchRestTemplate.search(query, TbStory.class);
        long totalHits = search.getTotalHits();
        System.out.println("totalHits="+totalHits);

        List<SearchHit<TbStory>> searchHits = search.getSearchHits();
        searchHits.forEach(row -> {
            // 普通结果
            TbStory tbStory = row.getContent();
            System.out.println(tbStory);
        });
    }

5、组合查询说明

5.1 BoolQuery( ) 用于组合多个叶子或复合查询子句的默认查询

  • must 相当于 与 & =
  • must not 相当于 非 ~ !=
  • should 相当于 或 | or
  • filter 过滤

例如下面代码:

boolQuery().must(termQuery("content", "test1"))
           .must(termQuery("content", "test4")) 
           .mustNot(termQuery("content", "test2"))
           .should(termQuery("content", "test3"))
           .filter(termQuery("content", "test5"));