首先来个映射类
package com.czxy.changgou3.vo;
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;
/**
* Created by liangtong.
*/
@Document(indexName = "czxy56",type="book",shards = 4,replicas = 2)
public class ESBook {
@Id
private Long id;
@Field(type= FieldType.Text, analyzer = "ik_max_word")
private String title;
@Field(type=FieldType.Keyword , index = true)
private String images;
@Field(type=FieldType.Float)
private Float price;
}
•步骤一:测试类中,直接注入ElasticsearchTemplate
•步骤二:调用对应的API进行操作
代码实现
package com.czxy.changgou3.elasticsearch;
import com.czxy.changgou3.TestApplication;
import com.czxy.changgou3.vo.ESBook;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
/**
* Created by liangtong.
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class TestES {
@Resource
private ElasticsearchTemplate elasticsearchTemplate;
@Test
public void demo01(){
//创建索引
elasticsearchTemplate.createIndex(ESBook.class);
}
@Test
public void demo02(){
//添加映射
elasticsearchTemplate.putMapping(ESBook.class);
}
@Test
public void demo03(){
//删除映射
elasticsearchTemplate.deleteIndex(ESBook.class);
}
}
顶级接口repository
•Spring Data Elasticsearch提供了一个顶级接口 Repository,用于对“配置类”进行增删改查操作的。顶级类没有完成具体的功能,具体的功能通过子类完成
–PagingAndSortingRepository:(第二代)完成分页和排序功能
–ElasticsearchCrudRepository:(第三代)增删改查功能
–ElasticsearchRepository:(第四代)完成所有功能
•使用:只需要编写子接口,继承 ElasticsearchRepository即可,且spring data自动加载该类。
–注意:接口在使用时,需要确定2个泛型信息
•第一个泛型:映射类,ESBook
•第二个泛型:映射类唯一标识的类型,ID的类型 Long
package com.czxy.changgou3.repository;
import com.czxy.changgou3.vo.ESBook;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* Created by liangtong.
*/
public interface ESBookRepository extends ElasticsearchRepository<ESBook,Long>{
}
CRUD(直接上代码)
package com.czxy.java56.elasticsearch;
import com.czxy.java56.TestApplication;
import com.czxy.java56.repository.ESRepository;
import com.czxy.java56.vo.ESBook;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.ArrayList;
/**
* @author 1392373019@qq.com
* @version 1.0
* @date 2020/4/13
*/
//基于springboot的测试类配置
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class ESData {
//注入我们的顶级接口
@Resource
private ESRepository esRepository;
/**
* 增
*/
@Test
public void demo01(){
ESBook esBook = new ESBook(1L,"xdm","verygood",1f);
esRepository.save(esBook);
}
/**
* 增多条
*/
@Test
public void demo02(){
ArrayList<ESBook> elist = new ArrayList<>();
elist.add( new ESBook(1L,"小东","verygood",1f));
elist.add( new ESBook(2L,"小明","verygood",1f));
elist.add( new ESBook(3L,"小花","verygood",1f));
elist.add( new ESBook(4L,"阿呆","verygood",1f));
esRepository.saveAll(elist);
}
/**
* 修改
*/
@Test
public void demo03(){
ESBook esBook = new ESBook(1L,"xdm","verygood",1f);
esRepository.save(esBook);
}
/**
* 删除
*/
@Test
public void demo04(){
esRepository.deleteById(1L);
esRepository.deleteAll();
}
}