参考文章:Elasticsearch 字段为空(null)记录查询 - illusioned - 博客园
public void searchNullField() {
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
queryBuilder.withFields("price");
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// 该字段必须为空
boolQueryBuilder.mustNot(QueryBuilders.existsQuery("record"));
// 分页:
queryBuilder.withQuery(boolQueryBuilder);
queryBuilder.withSort(SortBuilders.fieldSort("update_date.keyword").order(SortOrder.DESC));
int pageNum = 0;
int pageSize = 10;
queryBuilder.withPageable(PageRequest.of(pageNum, pageSize));
// 搜索, 获取结果
Page page = itemRepository.search(queryBuilder.build());
// 总条数
long totalNum = page.getTotalElements();
log.info("total_num: {}", totalNum);
log.info("qa_pair_list: {}", page.getContent());
log.info("总页数 = " + page.getTotalPages());
log.info("当前页:" + page.getNumber());
log.info("每页大小:" + page.getSize());
}
ItemEntity.java
package org.fiend.entity;
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;
import java.util.Date;
// indexName索引名称 可以理解为数据库名 必须为小写 不然会报org.elasticsearch.indices.InvalidIndexNameException异常
// type类型 可以理解为表名
// shards:分片数量,默认5
// replicas:副本数量,默认1
// @document(indexName = “test-smq”, type = “test”, refreshInterval = “1s”, createIndex = false)
@document(indexName = "item", type = "docs", shards = 1, replicas = 0)
public class ItemEntity {
@Id // 必须
// @JsonProperty("id")
private Long id;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String title; //标题
@Field(type = FieldType.Keyword)
private String category;// 分类
@Field(type = FieldType.Keyword)
private String brand; // 品牌
@Field(type = FieldType.Double)
private Double price; // 价格
@Field(index = false, type = FieldType.Keyword)
private String images; // 图片地址
// @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
// @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
// @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
private Date myDate;
@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
private Date myDate2;
public ItemEntity() {
}
public ItemEntity(Long id, String title, String category, String brand, Double price, String images) {
this.id = id;
this.title = title;
this.category = category;
this.brand = brand;
this.price = price;
this.images = images;
}
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 getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
}
ItemRepository.java
package org.fiend.repository; import org.fiend.entity.ItemEntity; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Component; import java.util.List; @Component //: ItemEntity的@Id所注解的数据项的数据类型, 必须与Long一致, // 即ItemEntity的@Id如果是String类型, 这里的Long就变为String public interface ItemRepository extends ElasticsearchRepository { List findByPriceBetween(double price1, double price2); }



