標籤:更新 dma pip length fun bool json 代碼 service
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 :商品資訊 介面 */@Componentpublic 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> searchCity(@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