Springdataelasticsearch怎么用
这篇文章主要介绍Springdataelasticsearch怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联专注于企业成都全网营销推广、网站重做改版、沙依巴克网站定制设计、自适应品牌网站建设、HTML5、商城系统网站开发、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为沙依巴克等各大城市提供网站开发制作服务。
一、准备
1.添加依赖
org.springframework.boot spring-boot-starter-data-elasticsearch
2.application.yml
spring: application: name: search-service data: elasticsearch: cluster-name: elasticsearch cluster-nodes: 192.168.25.129:9300
3.实体类
@Data@Document(indexName = "goods", type = "_doc", shards = 1, replicas = 0)public class Goods { @Idprivate Long id; @Field(type = FieldType.text, analyzer = "ik_max_word") private String all; @Field(type = FieldType.keyword, index = false) private String subTitle;private Long brandId;private Long cid1;private Long cid2;private Long cid3;private Date createTime;private Listprice; @Field(type = FieldType.keyword, index = false) private String skus;private Map specs;}
@Document 作用在类,标记实体类为文档对象,一般有两个属性
indexName:对应索引库名称 type:对应在索引库中的类型 shards:分片数量,默认5 replicas:副本数量,默认1 @Id 作用在成员变量,标记一个字段作为id主键 @Field 作用在成员变量,标记为文档的字段,并指定字段映射属性: type:字段类型,取值是枚举:FieldType index:是否索引,布尔类型,默认是true store:是否存储,布尔类型,默认是false analyzer:分词器名称
二.、索引操作
首先注入ElasticsearchTemplate
@Resourceprivate ElasticsearchTemplate elasticsearchTemplate;
● 创建索引
elasticsearchTemplate.createIndex(Goods.class);
● 配置映射
elasticsearchTemplate.putMapping(Goods.class);
● 删除索引
//根据类elasticsearchTemplate.deleteIndex(Goods.class);//根据索引名elasticsearchTemplate.deleteIndex("goods");
三、文档操作
1.定义接口。也是SpringData风格
public interface ItemRepository extends ElasticsearchRepository
2.注入
@Autowiredprivate ItemRepository itemRepository;
● 新增文档
Item item = new Item(1L, "小米手机7", " 手机", "小米", 3499.00, "http://image.leyou.com/13123.jpg");itemRepository.save(item);
● 批量新增
List
四、 基本搜索
● 基本查询。
例:
// 查询全部,并安装价格降序排序 Iterable
● 自定义查询
And findByNameAndPrice {"bool" : {"must" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}} Or findByNameOrPrice {"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}} Is findByName {"bool" : {"must" : {"field" : {"name" : "?"}}}} Not findByNameNot {"bool" : {"must_not" : {"field" : {"name" : "?"}}}} Between findByPriceBetween {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : ?,"include_lower" : true,"include_upper" : true}}}}} LessThanEqual findByPriceLessThan {"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}} GreaterThanEqual findByPriceGreaterThan {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}} Before findByPriceBefore {"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}} After findByPriceAfter {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}} Like findByNameLike {"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}} StartingWith findByNameStartingWith {"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}} EndingWith findByNameEndingWith {"bool" : {"must" : {"field" : {"name" : {"query" : "*?","analyze_wildcard" : true}}}}} Contains/Containing findByNameContaining {"bool" : {"must" : {"field" : {"name" : {"query" : "**?**","analyze_wildcard" : true}}}}} In findByNameIn(Collection
例:
public interface ItemRepository extends ElasticsearchRepository
Keyword | Sample | Elasticsearch Query String |
---|
五、高级查询
● 词条查询
MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("all", "小米"); // 执行查询 Iterable
● 自定义查询
// 构建查询条件 NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); // 添加基本的分词查询 queryBuilder.withQuery(QueryBuilders.matchQuery("all", "小米")); // 执行搜索,获取结果 Page
● 分页查询
// 构建查询条件 NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); // 添加基本的分词查询 queryBuilder.withQuery(QueryBuilders.termQuery("all", "手机")); // 初始化分页参数 int page = 0; int size = 3; // 设置分页参数 queryBuilder.withPageable(PageRequest.of(page, size)); // 执行搜索,获取结果 Page
● 排序
// 构建查询条件 NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); // 添加基本的分词查询 queryBuilder.withQuery(QueryBuilders.termQuery("all", "手机")); // 排序 queryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.DESC)); // 执行搜索,获取结果 Page
● 聚合为桶
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); // 不查询任何结果 queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null)); // 1、添加一个新的聚合,聚合类型为terms,聚合名称为brands,聚合字段为brand queryBuilder.addAggregation(AggregationBuilders.terms("brands").field("brandId")); // 2、查询,需要把结果强转为AggregatedPage类型 AggregatedPage
● 嵌套聚合,求平均值
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); // 不查询任何结果 queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null)); // 1、添加一个新的聚合,聚合类型为terms,聚合名称为brands,聚合字段为brand queryBuilder.addAggregation(AggregationBuilders.terms("brands").field("brandId") .subAggregation(AggregationBuilders.avg("priceAvg").field("price"))); // 在品牌聚合桶内进行嵌套聚合,求平均值 // 2、查询,需要把结果强转为AggregatedPage类型 AggregatedPage
附:配置搜索结果不显示为null字段:
spring: jackson: default-property-inclusion: non_null # 配置json处理时忽略空值
以上是“Springdataelasticsearch怎么用”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
网页标题:Springdataelasticsearch怎么用
当前网址:http://azwzsj.com/article/pcsego.html