<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.8.1</elasticsearch.version>
</properties>
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;
}
@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);
});
}
例如下面代码:
boolQuery().must(termQuery("content", "test1"))
.must(termQuery("content", "test4"))
.mustNot(termQuery("content", "test2"))
.should(termQuery("content", "test3"))
.filter(termQuery("content", "test5"));