1引入jar包

  

<!--elasticsearch-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

2实体类

package com.miracle.config.elasticsearch.entity;

import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document; import java.io.Serializable; /**
* Coding makes me happy.
*  ┏┓   ┏┓
* ┏┛┻━━━┛┻┓
* ┃  ☆☆☆  ┃
* ┃   ━   ┃
* ┃ ┳┛ ┗┳ ┃
* ┃       ┃
* ┃   ┻   ┃
* ┗━┓ 史 ┏━┛
*   ┃ 诗 ┃神兽保佑
*   ┃ 之 ┃代码无BUG!
*   ┃ 宠 ┗━━━┓
*   ┃Author:   ┣┓
*   ┃   liu.Q ┏┛
*   ┗┓┓┏━┳┓┏┛
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛
* ----------------------
*
* @Date : 2018/3/7 下午2:35
* @Description :elasticsearch 商品信息库
*/
@Data
@Document(indexName = "goodsindex",type = "goods")
//indexName索引名称 可以理解为数据库名 必须为小写 不然会报org.elasticsearch.indices.InvalidIndexNameException异常
//type类型 可以理解为表名
public class GoodsInfo implements Serializable {
private int id; //商品规格id
private int goods_id;//商品id
private String goods_name;//商品名称
private String goods_sku_name;//商品规格名称
private String goods_class_code;//商品分类编号
private String goods_class_name;//商品分类名称
private double price;//价格
private int sales_num;//销量(初始销量+真实销量)
private int stock;//库存 private String description;//搜索关键字逗号(,)分隔 ==(分类名称,商品名称,规格名称) public GoodsInfo() {
} public GoodsInfo(int id, int goods_id, String goods_name, String goods_sku_name, String goods_class_code, String goods_class_name, double price, int sales_num, int stock, String description) {
this.id = id;
this.goods_id = goods_id;
this.goods_name = goods_name;
this.goods_sku_name = goods_sku_name;
this.goods_class_code = goods_class_code;
this.goods_class_name = goods_class_name;
this.price = price;
this.sales_num = sales_num;
this.stock = stock;
this.description = description;
}
}

3接口

package com.miracle.config.elasticsearch.service;

import com.miracle.config.elasticsearch.entity.GoodsInfo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component; /**
* Coding makes me happy.
*  ┏┓   ┏┓
* ┏┛┻━━━┛┻┓
* ┃  ☆☆☆  ┃
* ┃   ━   ┃
* ┃ ┳┛ ┗┳ ┃
* ┃       ┃
* ┃   ┻   ┃
* ┗━┓ 史 ┏━┛
*   ┃ 诗 ┃神兽保佑
*   ┃ 之 ┃代码无BUG!
*   ┃ 宠 ┗━━━┓
*   ┃Author:   ┣┓
*   ┃   liu.Q ┏┛
*   ┗┓┓┏━┳┓┏┛
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛
* ----------------------
*
* @Date : 2018/3/7 下午2:35
* @Description :商品信息 接口
*/
@Component
public interface GoodsRepository extends ElasticsearchRepository<GoodsInfo,Long> {
}

4 controller测试

package com.miracle.controller.wx;

import com.alibaba.fastjson.JSONObject;
import com.miracle.config.elasticsearch.entity.GoodsInfo;
import com.miracle.config.elasticsearch.service.GoodsRepository;
import com.miracle.config.redis.RedisService;
import com.miracle.controller.wx.util.AesCbcUtil;
import com.miracle.controller.wx.util.WXUtil;
import com.miracle.mapper.WxUserMapper;
import com.miracle.model.WxUser;
import com.miracle.util.ReturnVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest;
import java.awt.print.Book;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit; /**
* Coding makes me happy.
*  ┏┓   ┏┓
* ┏┛┻━━━┛┻┓
* ┃  ☆☆☆  ┃
* ┃   ━   ┃
* ┃ ┳┛ ┗┳ ┃
* ┃       ┃
* ┃   ┻   ┃
* ┗━┓ 史 ┏━┛
*   ┃ 诗 ┃神兽保佑
*   ┃ 之 ┃代码无BUG!
*   ┃ 宠 ┗━━━┓
*   ┃Author:   ┣┓
*   ┃   liu.Q ┏┛
*   ┗┓┓┏━┳┓┏┛
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛
* ----------------------
*
* @Date : 2018/3/7 下午2:35
* @Description :elasticsearch测试
*/
@Slf4j
@EnableTransactionManagement // 需要事务的时候加上
@RestController
@Api("elasticsearch测试")
@RequestMapping("/wx")
public class ElasticsearchTestController { @Autowired
private GoodsRepository goodsRepository; @ApiOperation(value = "添加/更新", notes = "id一样即可实现更新")
@RequestMapping(value="/save",method = RequestMethod.GET)
public String save(@ModelAttribute GoodsInfo goodsInfo){
goodsRepository.save(goodsInfo);
return "success";
} @ApiOperation(value = "搜索", notes = "搜索")
@RequestMapping(value="/getList",method = RequestMethod.GET)
public List<GoodsInfo> getList(@ApiParam(name = "s",value = "关键字",defaultValue = "商品") String s) {
//创建builder minimumShouldMatch - 匹配到次数
QueryBuilder builder = QueryBuilders.multiMatchQuery(s,"goods_name").minimumShouldMatch(s.length()+""); //builder下有must、should以及mustNot 相当于sql中的and、or以及not
//设置模糊搜索,multiMatchQuery 匹配多字段
// builder.must(QueryBuilders.multiMatchQuery(s,"goods_name"));
// builder.must(QueryBuilders.termQuery(s,"goods_name"));
// builder.should(QueryBuilders.matchQuery("description", s));
//模糊查询
// builder.should(QueryBuilders.fuzzyQuery("goods_name", s));
//设置名称为商品
// builder.must(new QueryStringQueryBuilder("商品").field("goods_name")); //排序
FieldSortBuilder sort = SortBuilders.fieldSort("id").order(SortOrder.DESC); //设置分页
//====注意!es的分页和Hibernate一样api是从第0页开始的=========
PageRequest pageRequest = new PageRequest(0, 10); //构建查询
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
//将搜索条件设置到构建中
nativeSearchQueryBuilder.withQuery(builder);
//将分页设置到构建中
nativeSearchQueryBuilder.withPageable(pageRequest);
//将排序设置到构建中
nativeSearchQueryBuilder.withSort(sort);
//生产NativeSearchQuery
NativeSearchQuery query = nativeSearchQueryBuilder.build(); //执行,返回包装结果的分页
Page<GoodsInfo> resutlList = goodsRepository.search(query);
return resutlList.getContent();
} @ApiOperation(value = "查询全部", notes = "查询全部")
@RequestMapping(value="/getAll",method = RequestMethod.GET)
public List<GoodsInfo> searchCity() {
//构建查询
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
//生产NativeSearchQuery
NativeSearchQuery query = nativeSearchQueryBuilder.build();
//执行,返回包装结果的分页
Page<GoodsInfo> resutlList = goodsRepository.search(query); return resutlList.getContent();
}
@ApiOperation(value = "删除全部", notes = "删除全部")
@RequestMapping(value="/deleteAll",method = RequestMethod.GET)
public String deleteAll() {
goodsRepository.deleteAll();
return "success";
}
}

5  官方api参考地址

https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-full-text-queries.html

springboot 整合 elasticsearch的更多相关文章

  1. SpringBoot整合ElasticSearch实现多版本的兼容

    前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...

  2. ElasticSearch(2)---SpringBoot整合ElasticSearch

    SpringBoot整合ElasticSearch 一.基于spring-boot-starter-data-elasticsearch整合 开发环境:springboot版本:2.0.1,elast ...

  3. springboot整合elasticsearch入门例子

    springboot整合elasticsearch入门例子 https://blog.csdn.net/tianyaleixiaowu/article/details/72833940 Elastic ...

  4. Springboot整合elasticsearch以及接口开发

    Springboot整合elasticsearch以及接口开发 搭建elasticsearch集群 搭建过程略(我这里用的是elasticsearch5.5.2版本) 写入测试数据 新建索引book( ...

  5. SpringBoot整合Elasticsearch详细步骤以及代码示例(附源码)

    准备工作 环境准备 JAVA版本 java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121 ...

  6. Springboot整合Elasticsearch报错availableProcessors is already set to [4], rejecting [4]

    Springboot整合Elasticsearch报错 今天使用SpringBoot整合Elasticsearch时候,相关的配置完成后,启动项目就报错了. nested exception is j ...

  7. Springboot整合ElasticSearch进行简单的测试及用Kibana进行查看

    一.前言 搜索引擎还是在电商项目.百度.还有技术博客中广泛应用,使用最多的还是ElasticSearch,Solr在大数据量下检索性能不如ElasticSearch.今天和大家一起搭建一下,小编是看完 ...

  8. 😊SpringBoot 整合 Elasticsearch (超详细).md

    SpringBoot 整合 Elasticsearch (超详细) 注意: 1.环境搭建 安装es Elasticsearch 6.4.3 下载链接 为了方便,环境使用Windows 配置 解压后配置 ...

  9. SpringBoot整合elasticsearch

    在这一篇文章开始之前,你需要先安装一个ElasticSearch,如果你是mac或者linux可以参考https://www.jianshu.com/p/e47b451375ea,如果是windows ...

  10. springboot整合elasticsearch(基于es7.2和官方high level client)

    前言 最近写的一个个人项目(传送门:全终端云书签)中需要用到全文检索功能,目前 mysql,es 都可以做全文检索,mysql 胜在配置方便很快就能搞定上线(参考这里),不考虑上手难度,es 在全文检 ...

随机推荐

  1. Rest和WebService的区别

    有好多人问我们在设计底层服务的时候到底是应该选择目前最流行的RestFul架构还是选择老牌的webService呢?今天我就将这两个概念做一下阐述,到底什么情况下选择什么比较合理. 首先需要了解:RE ...

  2. webpack执行中出现 ERROR in Path must be a string. Received undefined

    执行webpack时出现错误信息 ERROR in Path must be a string. Received undefined 原因在于我的node.js版本太高了,目前node版本为6.10 ...

  3. [转]Python的getattr(),setattr(),delattr(),hasattr()

    getattr()函数是Python自省的核心函数,具体使用大体如下: 获取对象引用getattrGetattr用于返回一个对象属性,或者方法 class A: def __init__(self): ...

  4. C++网络通信字节序问题

    htonl(), ntohl(), htons(), ntohs() 函数 在C/C++写网络程序的时候,往往会遇到字节的网络顺序和主机顺序的问题.这是就可能用到htons(), ntohl(), n ...

  5. 代码注释,神兽护体,代码无bug

    /** * * ━━━━━━神兽出没━━━━━━ * ┏┓ ┏┓ * ┏┛┻━━━┛┻┓ * ┃ ┃ * ┃ ━ ┃ * ┃ ┳┛ ┗┳ ┃ * ┃ ┃ * ┃ ┻ ┃ * ┃ ┃ * ┗━┓ ┏━┛ ...

  6. JavaScript--语法3--数组

    JavaScript--语法3--数组 一.心得 二.代码 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "h ...

  7. bzoj1143: [CTSC2008]祭祀river 最长反链

    题意:在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在水面上,奉龙王为神.每逢重大庆典, Y族都会在水面上举办盛大的祭祀活动.我们可以把Y族居住地水系看成一个由岔口和河道组成的网络.每条河道连 ...

  8. hdu3549网络流之最大流

    Ford-Fulkerson方法:dfs实现 dfs  140ms #include<map> #include<set> #include<cmath> #inc ...

  9. UVALive-3487 Duopoly(最小割)

    题目大意:有两家公司都想向政府申请某些资源的使用权,并且他们都提供了一些申请列表,列表中含有申请费用和资源种类,同一家公司的申请列表之间不含有重复的资源.政府只可以完整地接受和拒绝谋一份申请列表,问政 ...

  10. 部分函数依赖 && 完全函数依赖

    部分函数依赖:若x->y 并且,存在X的真子集x1,使得x1->y,则 y部分依赖于x. 完全函数依赖:若x->y并且,对于x的任何一个真子集x1,都不存在x1->y,则称y完 ...