1 Introducing the JAR package
<!--elasticsearch--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
2 entity classes
Packagecom.miracle.config.elasticsearch.entity;ImportLombok. Data;Importorg.springframework.data.elasticsearch.annotations.Document;Importjava.io.Serializable;/*** Coding makes me happy *┏┓┏┓*┏┛┻━━━┛┻┓*┃☆☆☆┃*┃━┃*┃┳┛┗┳┃*┃┃*┃┻┃*┗━┓ history ┏ ━┛*┃ poem ┃ god beast bless *┃ ┃ code no bug! *┃ Pet ┗━━━┓*┃author:┣┓*┃liu. q┏┛*┗┓┓┏━┳┓┏┛*┃┫┫┃┫┫*┗┻┛┗┻┛*----------------------* * @Date: 2018/3/7 pm 2:35 * @Description: Elastics Earch Commodity Information Base*/@Data @document (IndexName= "Goodsindex", type = "Goods")//IndexName index name can be understood as database name must be lowercase or org.elasticsearch.indices.InvalidIndexNameException exception will be reported//type types can be understood as table names Public classGoodsinfoImplementsSerializable {Private intId//Product specification ID Private intgoods_id;//Product ID PrivateString Goods_name;//Product Name PrivateString Goods_sku_name;//Product Specification Name PrivateString Goods_class_code;//Product Category Number PrivateString Goods_class_name;//Product category name Private DoublePrice//Price Private intSales_num;//Sales (initial sales + real sales) Private intStock//Stock PrivateString description;//search keywords comma (,) delimited = = (category name, product name, specification name) PublicGoodsinfo () {} PublicGoodsinfo (intIdintgoods_id, String goods_name, String goods_sku_name, String Goods_class_code, String goods_class_name,DoublePriceintSales_num,intstock, 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 interface
PackageCom.miracle.config.elasticsearch.service;ImportCom.miracle.config.elasticsearch.entity.GoodsInfo;Importorg.springframework.data.elasticsearch.repository.ElasticsearchRepository;Importorg.springframework.stereotype.Component;/*** Coding makes me happy *┏┓┏┓*┏┛┻━━━┛┻┓*┃☆☆☆┃*┃━┃*┃┳┛┗┳┃*┃┃*┃┻┃*┗━┓ history ┏ ━┛*┃ poem ┃ god beast bless *┃ ┃ code no bug! *┃ Pet ┗━━━┓*┃author:┣┓*┃liu. q┏┛*┗┓┓┏━┳┓┏┛*┃┫┫┃┫┫*┗┻┛┗┻┛*----------------------* * @Date: 2018/3/7 pm 2:35 * @Description: Product Information interface */@Component Public InterfaceGoodsrepositoryextendsElasticsearchrepository<goodsinfo,long> {}
4 Controller Test
Packagecom.miracle.controller.wx;ImportCom.alibaba.fastjson.JSONObject;ImportCom.miracle.config.elasticsearch.entity.GoodsInfo;Importcom.miracle.config.elasticsearch.service.GoodsRepository;ImportCom.miracle.config.redis.RedisService;ImportCom.miracle.controller.wx.util.AesCbcUtil;ImportCom.miracle.controller.wx.util.WXUtil;ImportCom.miracle.mapper.WxUserMapper;ImportCom.miracle.model.WxUser;ImportCom.miracle.util.ReturnVO;Importio.swagger.annotations.*;Importlombok.extern.slf4j.Slf4j;ImportOrg.elasticsearch.index.query.BoolQueryBuilder;ImportOrg.elasticsearch.index.query.QueryBuilder;Importorg.elasticsearch.index.query.QueryBuilders;ImportOrg.elasticsearch.index.query.QueryStringQueryBuilder;ImportOrg.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;Importorg.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;ImportOrg.elasticsearch.search.sort.FieldSortBuilder;Importorg.elasticsearch.search.sort.SortBuilders;ImportOrg.elasticsearch.search.sort.SortOrder;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.beans.factory.annotation.Value;ImportOrg.springframework.data.domain.Page;Importorg.springframework.data.domain.PageRequest;Importorg.springframework.data.domain.Pageable;ImportOrg.springframework.data.elasticsearch.core.query.NativeSearchQuery;ImportOrg.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;ImportOrg.springframework.data.elasticsearch.core.query.SearchQuery;Importorg.springframework.transaction.annotation.EnableTransactionManagement;Importorg.springframework.web.bind.annotation.*;Importjavax.servlet.http.HttpServletRequest;ImportJava.awt.print.Book;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportJava.util.UUID;ImportJava.util.concurrent.TimeUnit;/*** Coding makes me happy *┏┓┏┓*┏┛┻━━━┛┻┓*┃☆☆☆┃*┃━┃*┃┳┛┗┳┃*┃┃*┃┻┃*┗━┓ history ┏ ━┛*┃ poem ┃ god beast bless *┃ ┃ code no bug! *┃ Pet ┗━━━┓*┃author:┣┓*┃liu. q┏┛*┗┓┓┏━┳┓┏┛*┃┫┫┃┫┫*┗┻┛┗┻┛*----------------------* * @Date: 2018/3/7 pm 2:35 * @Description: Elastics Earch Test*/@Slf4j @enabletransactionmanagement//when you need a transaction, add@RestController @api ("Elasticsearch Test") @RequestMapping ("/WX") Public classElasticsearchtestcontroller {@AutowiredPrivategoodsrepository goodsrepository; @ApiOperation (Value= "Add/update", notes = "ID to implement updates") @RequestMapping (value= "/save", method =requestmethod.get) PublicString Save (@ModelAttribute goodsinfo goodsinfo) {goodsrepository.save (goodsinfo); return"Success"; } @ApiOperation (Value= "Search", notes = "Search") @RequestMapping (value= "/getlist", method =requestmethod.get) PublicList<goodsinfo> searchcity (@ApiParam (name = "S", value = "Keyword", defaultvalue = "Commodity") String s) {//Create builder Minimumshouldmatch-match to CountQueryBuilder Builder = Querybuilders.multimatchquery (S, "Goods_name"). Minimumshouldmatch (S.length () + ""); //Builder has must, should, and mustnot equivalent to and, or, and not in SQL//set fuzzy search, Multimatchquery match multiple fields//Builder.must (Querybuilders.multimatchquery (S, "goods_name"));//Builder.must (Querybuilders.termquery (S, "goods_name"));//builder.should (querybuilders.matchquery ("description", s)); //Fuzzy Query//builder.should (Querybuilders.fuzzyquery ("Goods_name", s)); //set name to product//Builder.must (New Querystringquerybuilder ("Commodity"). Field ("Goods_name")); //SortFieldsortbuilder sort = sortbuilders.fieldsort ("id"). order (SORTORDER.DESC); //Set Page Paging//= = Note!es's paging and Hibernate API is the beginning of page No. 0 =========Pagerequest pagerequest =NewPagerequest (0, 10); //Build QueriesNativesearchquerybuilder Nativesearchquerybuilder =NewNativesearchquerybuilder (); //set the search criteria to the buildnativesearchquerybuilder.withquery (builder); //set paging to buildnativesearchquerybuilder.withpageable (pagerequest); //set the sort to buildnativesearchquerybuilder.withsort (sort); //Production NativesearchqueryNativesearchquery query =Nativesearchquerybuilder.build (); //Execute, return pagination of packing resultsPage<goodsinfo> resutllist =goodsrepository.search (query); returnresutllist.getcontent (); } @ApiOperation (Value= "Query All", notes = "Query All") @RequestMapping (value= "/getall", method =requestmethod.get) PublicList<goodsinfo>searchcity () {//Build QueriesNativesearchquerybuilder Nativesearchquerybuilder =NewNativesearchquerybuilder (); //Production NativesearchqueryNativesearchquery query =Nativesearchquerybuilder.build (); //Execute, return pagination of packing resultsPage<goodsinfo> resutllist =goodsrepository.search (query); returnresutllist.getcontent (); } @ApiOperation (Value= "Delete all", notes = "Delete all") @RequestMapping (value= "/deleteall", method =requestmethod.get) PublicString DeleteAll () {goodsrepository.deleteall (); return"Success"; }}
5 Official API Reference address
Https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-full-text-queries.html
Springboot Integrated Elasticsearch