版权声明:此博客为个人博客,不涉及商业用途,仅提供学习参考,内容均来自个人原创以及互联网转载和摘录。 --------------------- 本文来自 路西法Lucifer 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/qq_37495786/article/details/84204023
前提:
首先,你的Elasticsearch,kibana已经安装,并且已经启动了。
项目结构:
pom.xml:需要引入elasticsearch与spring boot的整合包,lombok这个包是可以用注解代替get、set、tostring....等方法。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
实体类:
package com.lucifer.elasticsearch.pojo;
import lombok.Data;
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;
/**
* @author: Lucifer
* @create: 2018-11-18 18:59
* @description:
**/
@Data
@Document(indexName = "lucifer",type = "item",shards = 1)
public class Item {
@Field(type = FieldType.Long)
@Id
Long id;
@Field(type = FieldType.Text,analyzer = "ik_smart")
String title; //标题
@Field(type = FieldType.Keyword)
String category;// 分类
@Field(type = FieldType.Keyword)
String brand; // 品牌
@Field(type = FieldType.Double)
Double price; // 价格
@Field(type = FieldType.Keyword)
String images; // 图片地址
}
application.yml:
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 192.168.59.136:9300
测试类: 创建索引库及添加映射关系。
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchApplicationTests {
@Autowired
ElasticsearchTemplate elasticsearchTemplate;
@Test
public void testCreate() {
//创建索引库
elasticsearchTemplate.createIndex(Item.class);
//映射关系
elasticsearchTemplate.putMapping(Item.class);
}
}
启动测试类,然后访问浏览器http://192.168.59.136:5601。
创建接口:ItemRepository
package com.lucifer.elasticsearch;
import com.lucifer.elasticsearch.pojo.Item;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
public interface ItemRepository extends ElasticsearchCrudRepository<Item,Long> {
}
实体类中添加以下两个注解,创建构造方法。
@AllArgsConstructor
@NoArgsConstructor
测试类中,添加批量新增方法。
//批量新增
@Test
public void insertIndex(){
List<Item> list=new ArrayList<>();
list.add(new Item(1L,"小米手机8","手机","小米",2700.00,"http://image.lucifer.com/1111.jpg"));
list.add(new Item(2L,"荣耀手机8","手机","华为",3500.00,"http://image.lucifer.com/2222.jpg"));
list.add(new Item(3L,"苹果手机8","手机","苹果",7500.00,"http://image.lucifer.com/3333.jpg"));
list.add(new Item(4L,"oppoR11","手机","OPPO",2999.00,"http://image.lucifer.com/4444.jpg"));
itemRepository.saveAll(list);
}
启动测试类批量新增方法,然后访问浏览器http://192.168.59.136:5601。
测试查询:
@Test
public void testFind(){
// 查询全部,并安装价格降序排序
Iterable<Item> items = this.itemRepository.findAll(Sort.by(Sort.Direction.DESC, "price"));
items.forEach(item-> System.out.println(item));
}
ps:
items.forEach(item-> System.out.println(item));
是java8的新特性,同:
for(Item item:items){
System.out.println(item );
}
控制台打印结果: 并且是按照价格降序排列。
ItemRepository 接口中自定义方法:
public interface ItemRepository extends ElasticsearchCrudRepository<Item,Long> {
List<Item> findByPriceBetween(Double begin,Double end);
}
测试条件查询:
@Test
public void testFindBy(){
// 查询两价格之间
Iterable<Item> items = this.itemRepository.findByPriceBetween(2000d,5000d);
for(Item item:items){
System.out.println(item );
}
}
控制台打印结果:
附:
Spring Data 的另一个强大功能,是根据方法名称自动实现功能。
比如:你的方法名叫做:findByTitle,那么它就知道你是根据title查询,然后自动帮你完成,无需写实现类。
当然,方法名称要符合一定的约定:
Keyword | Sample | Elasticsearch Query String |
---|---|---|
And |
findByNameAndPrice |
{"bool" : {"must" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}} |
Or |
findByNameOrPrice |
{"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}} |
Is |
findByName |
{"bool" : {"must" : {"field" : {"name" : "?"}}}} |
Not |
findByNameNot |
{"bool" : {"must_not" : {"field" : {"name" : "?"}}}} |
Between |
findByPriceBetween |
{"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : ?,"include_lower" : true,"include_upper" : true}}}}} |
LessThanEqual |
findByPriceLessThan |
{"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}} |
GreaterThanEqual |
findByPriceGreaterThan |
{"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}} |
Before |
findByPriceBefore |
{"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}} |
After |
findByPriceAfter |
{"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}} |
Like |
findByNameLike |
{"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}} |
StartingWith |
findByNameStartingWith |
{"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}} |
EndingWith |
findByNameEndingWith |
{"bool" : {"must" : {"field" : {"name" : {"query" : "*?","analyze_wildcard" : true}}}}} |
Contains/Containing |
findByNameContaining |
{"bool" : {"must" : {"field" : {"name" : {"query" : "**?**","analyze_wildcard" : true}}}}} |
In |
findByNameIn(Collection<String>names) |
{"bool" : {"must" : {"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"name" : "?"}} ]}}}} |
NotIn |
findByNameNotIn(Collection<String>names) |
{"bool" : {"must_not" : {"bool" : {"should" : {"field" : {"name" : "?"}}}}}} |
Near |
findByStoreNear |
Not Supported Yet ! |
True |
findByAvailableTrue |
{"bool" : {"must" : {"field" : {"available" : true}}}} |
False |
findByAvailableFalse |
{"bool" : {"must" : {"field" : {"available" : false}}}} |
OrderBy |
findByAvailableTrueOrderByNameDesc |
{"sort" : [{ "name" : {"order" : "desc"} }],"bool" : {"must" : {"field" : {"available" : true}}}} |