springboot elasticsearch CRUD+
java版本:1.8
elasticSearch官网是比任何其他博客都正规的学习平台
https://www.elastic.co/cn/elasticsearch/
教学地址
https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html
我的项目是下载了elastic,本地运行自己玩
Staff
@Document(indexName="department")
publicclassStaff{
@Id
privateStringid;
@Field
privateStringfirstName;
@Field
privateStringlastName;
@Field
privateIntegerage=0;
@Field
privateStringabout;
publicStringgetId() {
returnid;
}
publicvoidsetId(Stringid) {
this.id=id;
}
publicStringgetFirstName() {
returnfirstName;
}
publicvoidsetFirstName(StringfirstName) {
this.firstName=firstName;
}
publicStringgetLastName() {
returnlastName;
}
publicvoidsetLastName(StringlastName) {
this.lastName=lastName;
}
publicIntegergetAge() {
returnage;
}
publicvoidsetAge(Integerage) {
this.age=age;
}
publicStringgetAbout() {
returnabout;
}
publicvoidsetAbout(Stringabout) {
this.about=about;
}
}
[@Document(indexName = "department")] index名,文件名设置为department
StaffController
@RestController
@RequestMapping("/elasticSearch")
publicclassStaffController{
@Autowired
privateStaffNewServicestaffNewService;
@ResponseBody
@GetMapping("/saveStaff")
publicStringsaveStaff(@RequestBodyStaffstaff){
staffNewService.saveStaff(staff);
return"saved";
}
@ResponseBody
@RequestMapping("/deleteStaff")
publicStringdeleteStaff(@RequestParamStringid){
staffNewService.deleteStaff(id);
return"deleted";
}
@ResponseBody
@RequestMapping("/updateStaff")
publicStringupdateStaff(@RequestParamStringid,@RequestParamStringfirstName){
staffNewService.updateStaff(id,firstName);
return"updated";
}
//以下都是按照firstName搜索
@ResponseBody
@RequestMapping("/matchAllQuery")//搜索该field(索引)内全部信息
publicStringmatchAllQuery(@RequestParamStringfield)throwsIOException{
SearchResponsesearchResponse=staffNewService.matchAllQuery(field);
return"matchAllQuery"+searchResponse;
}
@ResponseBody
@RequestMapping("/regexpQuery")//以一句话里的单词为关键字查询该句话,不能以一个单词里的几个字母作为关键字搜索该词。这就和mongo里的regex不一样,它能做后者。
publicStringregexpQuery(@RequestParamStringfield,@RequestParamStringkeyword)throwsIOException{
SearchResponsesearchResponse=staffNewService.regexpQuery(field,keyword);
return"regexpQuery"+searchResponse;
}
@ResponseBody
@RequestMapping("/fuzzyQuery")//首先,他会把我正确的keyword全改成小写,两个字符出错
// (漏了,多了,i变j了,只要总数是2以内)就查得出来,多了不行
publicStringfuzzyQuery(@RequestParamStringfield,@RequestParamStringkeyword)throwsIOException{
SearchResponsesearchResponse=staffNewService.fuzzyQuery(field,keyword);
return"fuzzyQuery"+searchResponse;
}
@ResponseBody
@RequestMapping("/termQuery")//如果正确的没空格,也是先会把我正确的keyword变成小写,再搜索,要完全一样才行
// 如果原来的有空格,搜索第一串字符串才能搜出来(还是小写,即使正确的是大写)
publicStringtermQuery(@RequestParamStringfield,@RequestParamStringkeyword)throwsIOException{
SearchResponsesearchResponse=staffNewService.termQuery(field,keyword);
return"termQuery"+searchResponse;
}
@ResponseBody
@RequestMapping("/matchQuery")//大小写随便,空格不能漏
publicStringmatchQuery(@RequestParamStringfield,@RequestParamStringkeyword)throwsIOException{
SearchResponsesearchResponse=staffNewService.matchQuery(field,keyword);
return"matchQuery"+searchResponse;
}
}
StaffRepository(基本用不到)
@Component
publicinterfaceStaffRepositoryextendsElasticsearchRepository<Staff,String>{
/**
* 根据ID 查询员工信息
*
* @param id id
* @return Staff
*/
StaffqueryStaffById(Stringid);
}
StaffNewService
@Service
@Component
publicinterfaceStaffNewService{
/**
* 新增员工测试
*
* @return String
*/
publicvoidsaveStaff(Staffstaff) ;
/**
* 删除测试
*
* @return
*/
publicvoiddeleteStaff(Stringid) ;
/**
* 更新
* @return
*/
publicvoidupdateStaff(Stringid,StringfirstName) ;
/**
* 查询
* @return
*/
publicvoidqueryStaff(Stringid) ;
publicSearchResponsematchAllQuery(Stringfield)throwsIOException;
publicSearchResponseregexpQuery(Stringfield,Stringkeyword)throwsIOException;
publicSearchResponsefuzzyQuery(Stringfield,Stringkeyword)throwsIOException;
publicSearchResponsetermQuery(Stringfield,Stringkeyword)throwsIOException;
publicSearchResponsematchQuery(Stringfield,Stringkeyword)throwsIOException;
}
StaffNewServiceImpl
@Service
@Component
publicclassStaffNewServiceImplimplementsStaffNewService{
@Autowired
privateStaffRepositorystaffRepository;
@Resource
privateElasticsearchRestTemplateelasticsearchRestTemplate;
/**
* 新增员工测试
*
* @return String
*/
publicvoidsaveStaff(Staffstaff) {
staffRepository.save(staff);
}
/**
* 删除测试
*
* @return
*/
publicvoiddeleteStaff(Stringid) {
Staffstaff=staffRepository.queryStaffById(id);
staffRepository.delete(staff);
}
/**
* 更新
* @return
*/
publicvoidupdateStaff(Stringid,StringfirstName) {
Staffstaff=staffRepository.queryStaffById(id);
staff.setFirstName(firstName);
staffRepository.save(staff);
}
/**
* 查询
* @return
*/
publicvoidqueryStaff(Stringid) {
Staffstaff=staffRepository.queryStaffById("1");
}
publicSearchResponsematchAllQuery(Stringfield)throwsIOException{
RestHighLevelClientclient=newRestHighLevelClient(RestClient.builder(
newHttpHost("127.0.0.1",9200,"http")));
SearchSourceBuildersearchSourceBuilder=newSearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
SearchRequestsearchRequest=newSearchRequest();
searchRequest.indices(field);
searchRequest.source(searchSourceBuilder);
SearchResponsesearchResponse=client.search(searchRequest,RequestOptions.DEFAULT);
returnsearchResponse;
}
/**
* 带分页、权重、分域查询
* @param field 索引
* @param keyword 关键字
* @return staff集合
*/
publicSearchResponseregexpQuery(Stringfield,Stringkeyword)throwsIOException{
RestHighLevelClientclient=newRestHighLevelClient(RestClient.builder(
newHttpHost("127.0.0.1",9200,"http")));
SearchSourceBuildersearchSourceBuilder=newSearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.regexpQuery("firstName",keyword));
SearchRequestsearchRequest=newSearchRequest();
searchRequest.indices(field);
searchRequest.source(searchSourceBuilder);
SearchResponsesearchResponse=client.search(searchRequest,RequestOptions.DEFAULT);
returnsearchResponse;
}
publicSearchResponsefuzzyQuery(Stringfield,Stringkeyword)throwsIOException{
RestHighLevelClientclient=newRestHighLevelClient(RestClient.builder(
newHttpHost("127.0.0.1",9200,"http")));
SearchSourceBuildersearchSourceBuilder=newSearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.fuzzyQuery("firstName",keyword));
SearchRequestsearchRequest=newSearchRequest();
searchRequest.indices(field);
searchRequest.source(searchSourceBuilder);
SearchResponsesearchResponse=client.search(searchRequest,RequestOptions.DEFAULT);
returnsearchResponse;
}
publicSearchResponsetermQuery(Stringfield,Stringkeyword)throwsIOException{
RestHighLevelClientclient=newRestHighLevelClient(RestClient.builder(
newHttpHost("127.0.0.1",9200,"http")));
SearchSourceBuildersearchSourceBuilder=newSearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.termQuery("firstName",keyword));
SearchRequestsearchRequest=newSearchRequest();
searchRequest.indices(field);
searchRequest.source(searchSourceBuilder);
SearchResponsesearchResponse=client.search(searchRequest,RequestOptions.DEFAULT);
returnsearchResponse;
}
publicSearchResponsematchQuery(Stringfield,Stringkeyword)throwsIOException{
RestHighLevelClientclient=newRestHighLevelClient(RestClient.builder(
newHttpHost("127.0.0.1",9200,"http")));
SearchSourceBuildersearchSourceBuilder=newSearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("firstName",keyword));
SearchRequestsearchRequest=newSearchRequest();
searchRequest.indices(field);
searchRequest.source(searchSourceBuilder);
SearchResponsesearchResponse=client.search(searchRequest,RequestOptions.DEFAULT);
returnsearchResponse;
}
}
[HttpHost] 程序里生成客户端,这样不需要加什么插件就能读取elastic里的索引,文件了。
DemoApplication
@Service
@SpringBootApplication
publicclassDemoApplication{
publicstaticvoidmain(String[]args){
SpringApplication.run(DemoApplication.class,args);
}
}
application.properties
#ES
spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9200
本地程序运行端口默认是8080,所以postman调用接口时的端口时8080。
但是elasticSearch默认是在9200运行的。
不要搞混!!!
我就主要玩了一下searchSourceBuilder里的各种query,aggregation好像主要做的是数据统计,这个我没玩。