使用的版本:
Spring Data Elasticsearch = 5.0.4
弹性搜索 = 8.5.3
Spring Boot = 3.0.5
产品索引:
@AllArgsConstructor
@Data
@Document(indexName = "product", versionType = Document.VersionType.INTERNAL)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Product {
@Id
@NotEmpty
@Field(type = FieldType.Keyword)
private String id;
@NotEmpty
@Field(type = FieldType.Keyword)
private String brand;
@NotEmpty
@Field(type = FieldType.Keyword)
private String name;
@NotNull
@PositiveOrZero
@Field(type = FieldType.Float)
private Float price;
@NotEmpty
@Field(type = FieldType.Keyword)
private String category;
@Field(type = FieldType.Keyword, name = "image_url")
private String imageURL;
@NotNull
@PositiveOrZero
@Field(type = FieldType.Long)
private Long stock;
@NotNull
@Field(type = FieldType.Text, analyzer = "english")
private String description;
}
产品资料库:
public interface ProductRepository extends ElasticsearchRepository<Product, String> {
@Query("{\"bool\":{\"should\":[{\"wildcard\":{\"name\":\"*?0*\"}},{\"term\":{\"category\":\"?1\"}},{\"terms\":{\"brand\": ?2}},{\"range\":{\"price\":{\"gte\":?3,\"lte\":?4}}}]}}")
Page<Product> productSearch(String name, String category, List<String> brands,
Integer priceMin, Integer priceMax, Pageable pageable);
}
产品映射:
{
"product": {
"mappings": {
"properties": {
"_class": {
"type": "keyword",
"index": false,
"doc_values": false
},
"brand": {
"type": "keyword"
},
"category": {
"type": "keyword"
},
"description": {
"type": "text",
"analyzer": "english"
},
"id": {
"type": "keyword"
},
"image_url": {
"type": "keyword"
},
"name": {
"type": "keyword"
},
"price": {
"type": "float"
},
"stock": {
"type": "long"
}
}
}
}
}
我是 Elasticsearch 的新手,我正在通过制作一个小型电子商务网站来学习它。我在执行上述搜索查询时遇到问题。当我通过 Kibana 控制台执行查询时,该查询有效。需要注意的一件事是,我确实传递了品牌、名称和类别的空值,但我相信我在某处读到它会忽略这些值?不确定。这可能是问题所在。我希望有人能帮助我谢谢。
编辑:我发现问题出在搜索查询的品牌条款部分。如果它是空的,我希望搜索忽略品牌字段。我该怎么做?