目录
1.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2.配置application.yml
spring:
data:
elasticsearch:
cluster-nodes: x.x.x.x:9300
3.SpringBoot如何操作ES
提供了相关注解简化ES操作
- `@Document(indexName = "dangdang",type = "book")`
标识这个类实例是一条文档document
idexName:索引库名字,
type:类型,6.x之后一个索引库只允许有一个类型了,
- ` @Id`
标识文档的主键,作用在类的属性上
- ` @Field(type= FieldType.Text,analyzer = "ik_max_word")`
标识文档属性,type:指定属性的类型,analyzer:使用的分词器
@Data
@Accessors(chain = true)//链式调用
@AllArgsConstructor
@NoArgsConstructor
//操作ES注解
@Document(indexName = "dangdang",type = "book")
public class Book {
@Id
private String id;
@Field(type= FieldType.Text,analyzer = "ik_max_word")
private String name;
@Field(type= FieldType.Double)
private Double price;
@Field(type= FieldType.Date)
private Date pubdata;
@Field(type= FieldType.Text,analyzer = "ik_max_word")
private String content;
@Field(type= FieldType.Keyword)
private String auther;
}
使用使用对象向ES索引文档,删除文档,更新文档
1.使用接口继承ElasticsearchRepository接口
其中已经封装好了一类常用的操作 公共方法接口,
泛型说明:ElasticsearchRepository<Book,String> <当前操作对象的类型,对象中的主键类型>
/**
* 自定义bookRepository继承ElasticsearchRepository<Book,String> <索引类型,索引的主键类型>,
* 其中封装好了常用的一些操作
*/
public interface BookRepository extends ElasticsearchRepository<Book,String> {
}
测试BookRepository
@SpringBootTest(classes = ElaticSearchApp.class)
@RunWith(SpringRunner.class)
public class BookRepositoryTest {
@Autowired
private BookRepository bookRepository;
public void createIndex(){
//保存和更新使用同一api,id存在时为更新,不存在为保存
Book book = new Book("1001","张三历险记",99.89,new Date(),"张三醒来发现自己在一个荒无人烟的小岛上,他四处望了望。。。","李四呀");
bookRepository.save(book);
}
}
执行结果,使用kibana查看
{
"dangdang": {
"mappings": {
"book": {
"properties": {
"auther": {
"type": "keyword"
},
"content": {
"type": "text",
"analyzer": "ik_max_word"
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"analyzer": "ik_max_word"
},
"price": {
"type": "double"
},
"pubdata": {
"type": "date"
}
}
}
}
}
}
///----
GET dangdang/book/_search
{
"query": {
"term": {
"name": {
"value": "历险记"
}
}
}
}
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "dangdang",
"_type": "book",
"_id": "1001",
"_score": 0.2876821,
"_source": {
"id": "1001",
"name": "张三历险记",
"price": 99.89,
"pubdata": 1607701189175,
"content": "张三醒来发现自己在一个荒无人烟的小岛上,他四处望了望。。。",
"auther": "李四呀"
}
}
]
}
}
操作demo
//删除
@Test
public void delBookbyId(){
bookRepository.deleteById("1001");
}
//根据id查询一个
@Test
public void findById(){
//Optional类jdk1.8中新特性
Optional<Book> byId = bookRepository.findById("1001");
System.out.println(byId.get());
}
//查询所有
@Test
public void quertAll(){
Iterable<Book> all = bookRepository.findAll();
all.forEach(book -> System.out.println(book));
}
//创建或更新
@Test
public void createIndex(){
//保存和更新使用同一api,id存在时为更新,不存在为保存
Book book = new Book("1001","张三历险记-被更新了",99.89,new Date(),"张三醒来发现自己在一个荒无人烟的小岛上,他四处望了望。。。","李四呀");
bookRepository.save(book);
}