<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-elasticsearchartifactId>
dependency>
spring:
elasticsearch:
uris:
- localhost:9200
username: elastic
password: password
@EnableElasticsearchRepositories(basePackages = "com.meta.es.repositories")//repository所在的包路径
@SpringBootApplication
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args);
}
}
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;
import java.util.Date;
@Data
@Document(indexName = "elastic")//indexName索引名称等价于MySQL的表
public class ElasticSearchEntity {
@Id
@Field(type = FieldType.Keyword)//关键字在查询的时候不会被拆分
private String id;
@Field(type = FieldType.Text)//字符串对应文本类型
private String name;
@Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second_millis)//日期类型
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private Date createTime;
@Field(type = FieldType.Boolean)//布尔类型
private boolean isDelete;
}
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
//ElasticsearchRepository<实体类,主键类型>
public interface ElasticSearchRepository extends ElasticsearchRepository<ElasticSearchEntity,String> {
List<ElasticSearchEntity> findByName(String name);
//自定义查询语句
@Query("{\"match\":{\"name\":\"?0\"}}")
List<ElasticSearchEntity> findAllByNameUsingAnnotations(String name);
}
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class ElasticSearchService {
@Resource
private ElasticSearchRepository elasticSearchRepository;
/**
* 新增/修改
*/
public void save(final ElasticSearchEntity elasticSearchEntity) {
this.elasticSearchRepository.save(elasticSearchEntity);
}
/**
* 通过ID查询
*/
public ElasticSearchEntity findById(final String id){
return this.elasticSearchRepository.findById(id).orElse(null);
}
/**
* 通过ID删除
*/
public void deleteById(String id){
this.elasticSearchRepository.deleteById(id);
}
/**
* 通过名称模糊查询
*/
public List<ElasticSearchEntity> findByName(final String name){
return this.elasticSearchRepository.findByName(name);
}
/**
* 通过使用注解名称模糊查询
*/
public List<ElasticSearchEntity> findAllByNameUsingAnnotations(String name){
return this.elasticSearchRepository.findAllByNameUsingAnnotations(name);
}
/**
* 分页
* @param size 数量
*/
public Page<ElasticSearchEntity> findAllByPage(int page,int size){
//Sort sort = Sort.by(Sort.Order.desc("create_date"));//排序
//Pageable pageable =PageRequest.of(Integer.parseInt(page), Integer.parseInt(size), sort);
Sort sort = Sort.by(Sort.Order.desc("createTime"));//根据
Pageable pageable =PageRequest.of(page, size, sort);
return this.elasticSearchRepository.findAll(pageable);
}
}
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("elasticsearch")
public class ElasticSearchController {
@Resource
private ElasticSearchService elasticSearchService;
/**
* 新增/修改,如果id一致就修改,否则新增
* @param elasticSearchEntity 实体类
*/
@PostMapping
public void save(@RequestBody final ElasticSearchEntity elasticSearchEntity) {
elasticSearchService.save(elasticSearchEntity);
}
/**
* 通过ID查询
* @param id ID
* @return
*/
@GetMapping("/{id}")
public ElasticSearchEntity findById(@PathVariable final String id) {
return elasticSearchService.findById(id);
}
/**
* 通过ID删除
* @param id ID
* @return
*/
@DeleteMapping("/{id}")
public boolean deleteById(@PathVariable String id) {
elasticSearchService.deleteById(id);
return true;
}
/**
* 通过名称模糊查询
* @param name 名称
* @return
*/
@GetMapping("/name/{name}")
public List<ElasticSearchEntity> findAllByName(@PathVariable String name) {
return elasticSearchService.findByName(name);
}
/**
* 使用注释的方式名称模糊查询,即:@Query()注解
* @param name 名称
* @return
*/
@GetMapping("/name/{name}/annotations")
public List<ElasticSearchEntity> findAllByNameAnnotations(@PathVariable String name) {
return elasticSearchService.findAllByNameUsingAnnotations(name);
}
/**
* 简单分页查询
* @param page 起始页,默认从0开始
* @param size 每页数量
* @return
*/
@GetMapping("/page")
public Page<ElasticSearchEntity> findAllByPage(@RequestParam(name = "page",defaultValue = "0") int page, @RequestParam(name = "size",defaultValue = "10") int size){
return elasticSearchService.findAllByPage(page,size);
}
}
查看创建的索引(相当于MySQL的表)
method:GET
localhost:9200/_cat/indices
删除索引
method:DELETE
localhost:9200/{索引名称}
查看索引里的全部数据,elastic是实体类@Document定义的indexName
method:GET
localhost:9200/elastic/_search
新增索引里没有ID就是新增,有ID就是修改
修改索引里没有ID就是新增,有ID就是修改
查询
删除
分页
通过name模糊查询