上一篇 <<<Elasticsearch集群及分片实现原理
下一篇 >>>Linux环境安装Elasticsearch
1.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2.es配置
spring:
data:
elasticsearch:
####集群名称
cluster-name: myes
####地址,集群由逗号隔开
cluster-nodes: 10.211.55.16:9300
3.es配置
@RestController
public class EsController {
@Autowired
private UserReposiory userReposiory;
@RequestMapping("/addUser")
public UserEntity addUser(@RequestBody UserEntity user) {
return userReposiory.save(user);
}
@RequestMapping("/findUser")
public Optional<UserEntity> findUser(String id) {
return userReposiory.findById(id);
}
}
public interface UserReposiory extends CrudRepository<UserEntity, String> {
}
@Document(indexName = "jarye", type = "user")
@Data
public class UserEntity {
@Id
private String id;
private String name;
private int sex;
private int age;
}
Tips:id如果不传的话,会自动生成,如果传了的话,则使用当前传输的id
4.es复杂查询
@RequestMapping("/search")
public List<CloudDiskEntity> search(String name, String describe,int page,int pageSize
/*@PageableDefault(page = 0, value = 2) Pageable pageable*/) {
/**查询条件*/
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
/**模糊查询*/
// boolQuery.filter(QueryBuilders.wildcardQuery("itemDesc", "*手机*"));
//当前关键字在多个字段里查询
// QueryBuilders.multiMatchQuery(name,"name","describe");
if (!StringUtils.isEmpty(name)) {
/**对name字段进行分词模糊匹配*/
MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("name", name);
boolQuery.must(matchQuery);
}
if (!StringUtils.isEmpty(describe)) {
/**对describe字段精准匹配*/
TermQueryBuilder describe1 = QueryBuilders.termQuery("describe", describe);
boolQuery.must(describe1);
}
//构建查询
SearchQuery query = new NativeSearchQueryBuilder()
.withFields("name","describe","shartime")
.withQuery(boolQuery)
.withSort(SortBuilders.fieldSort("shartime").order(SortOrder.DESC))
.withPageable(PageRequest.of(page,pageSize))
.build();
Iterable<CloudDiskEntity> search = cloudDiskDao.search(query);
return Lists.newArrayList(search);
}
推荐阅读:
<<<Elasticsearch入门知识
<<<Elasticsearch快速原因分析及应用场景
<<<Elasticsearch的存储结构端口及版本控制
<<<Elasticsearch文档映射方式
<<<Elasticsearch的基本及复杂数据类型
<<<Elasticsearch的简易版及结构化查询语句
<<<Elasticsearch默认分词器对中文分词不友好
<<<Elasticsearch自定义分词和分词器
<<<正向索引和倒排索引区别
<<<Elasticsearch中的类型区别汇总
<<<Elasticsearch是如何解决高并发问题
<<<Elasticsearch集群相关名词
<<<Elasticsearch集群及分片实现原理
<<<Linux环境安装Elasticsearch
<<<Linux环境安装Elasticsearch集群
<<<Elasticsearch和数据库保持同步的方式及原理
<<<Logstash-input-jdbc实现ES和数据同步操作步骤
网友评论