package cn.itcast.hotel.service.impl;
import cn.itcast.hotel.mapper.HotelMapper;
import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.pojo.PageResult;
import cn.itcast.hotel.pojo.RequestParams;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class HotelService extends ServiceImpl<HotelMapper, Hotel> implements IHotelService {
@Autowired
private RestHighLevelClient restHighLevelClient;
@Override
public PageResult search(RequestParams params) {
try{
// 1.准备Request
SearchRequest request = new SearchRequest("hotel");
// 2.准备请求参数
// 2.1.query
buildBasicQuery(params, request);
// 2.2.分页
int page = params.getPage();
int size = params.getSize();
request.source().from((page - 1) * size).size(size);
// 2.3.距离排序
String location = params.getLocation();
if (StringUtils.isNotBlank(location)) {
request.source().sort(SortBuilders
.geoDistanceSort("location", new GeoPoint(location))
.order(SortOrder.ASC)
.unit(DistanceUnit.KILOMETERS)
);
}
// 3.发送请求
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 4.解析响应
return handleResponse(response);
}catch (IOException e){
throw new RuntimeException("搜索数据失败", e);
}
}
@Override
public Map<String, List<String>> getFilters(RequestParams params) {
try{
//1.准备请求
SearchRequest request=new SearchRequest("hotel");
//2.请求参数
//2.1query
buildBasicQuery(params,request);
//2.2size
request.source().size(0);
//2.3聚合
buildAggregations(request);
//3.发出请求
SearchResponse response=restHighLevelClient.search(request, RequestOptions.DEFAULT);
//4.解析结果
Aggregations aggregations=response.getAggregations();
Map<String,List<String>> filters=new HashMap<>(3);
//4.1解析品牌
List<String> brandList=getAggregationByName(aggregations, "brandAgg");
filters.put("brand",brandList);
//4.2解析城市
List<String> cityList = getAggregationByName(aggregations, "cityAgg");
filters.put("city", cityList);
//4.3解析星级
List<String> starList = getAggregationByName(aggregations, "starAgg");
filters.put("starName", starList);
return filters;
}catch (IOException e){
throw new RuntimeException(e);
}
}
@Override
public List<String> getSuggestions(String key) {
try{
//1.准备请求
SearchRequest request=new SearchRequest("hotel");
//2.请求参数
request.source().suggest(new SuggestBuilder()
.addSuggestion(
"hotelSuggest",
SuggestBuilders
.completionSuggestion("suggestion") //补全字段
.size(10)
.skipDuplicates(true)
.prefix(key) //关键字
));
//3.发出请求
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
//4.解析
Suggest suggest=response.getSuggest();
// 4.1.根据名称获取结果
CompletionSuggestion suggestion = suggest.getSuggestion("hotelSuggest");
// 4.2.获取options
List<String> list = new ArrayList<>();
for (CompletionSuggestion.Entry.Option option : suggestion.getOptions()) {
// 4.3.获取补全的结果
String str = option.getText().toString();
// 4.4.放入集合
list.add(str);
}
return list;
}catch (IOException e){
throw new RuntimeException(e);
}
}
@Override
public void deleteById(Long hotelId) {
try {
// 1.创建request
DeleteRequest request = new DeleteRequest("hotel", hotelId.toString());
// 2.发送请求
restHighLevelClient.delete(request, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new RuntimeException("删除酒店数据失败", e);
}
}
@Override
public void saveById(Long hotelId) {
try {
// 查询酒店数据,应该基于Feign远程调用hotel-admin,根据id查询酒店数据(现在直接去数据库查)
Hotel hotel = getById(hotelId);
// 转换
HotelDoc hotelDoc = new HotelDoc(hotel);
// 1.创建Request
IndexRequest request = new IndexRequest("hotel").id(hotelId.toString());
// 2.准备参数
request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
// 3.发送请求
restHighLevelClient.index(request, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new RuntimeException("新增酒店数据失败", e);
}
}
private List<String> getAggregationByName(Aggregations aggregations, String aggName) {
// 4.1.根据聚合名称,获取聚合结果
Terms terms=aggregations.get(aggName);
// 4.2.获取buckets
List<? extends Terms.Bucket> buckets = terms.getBuckets();
//4.3遍历
List<String> list=new ArrayList<>();
for (Terms.Bucket bucket:buckets){
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
分布式搜索引擎-学习笔记-3 (102个子文件)
HotelService.class 16KB
HotelDoc.class 9KB
HotelSearchTest.class 7KB
Hotel.class 7KB
Hotel.class 7KB
RequestParams.class 5KB
HotelDocumentTest.class 5KB
HotelDemoApplicationTests.class 4KB
HotelController.class 4KB
PageResult.class 2KB
PageResult.class 2KB
MqConfig.class 2KB
HotelController.class 2KB
HotelAdminApplication.class 2KB
CorsConfig.class 1KB
CorsConfig.class 1KB
HotelDemoApplication.class 1KB
HotelConstants.class 1KB
HotelListener.class 1KB
IHotelService.class 897B
HotelService.class 679B
HotelMqConstants.class 625B
HotelMqConstants.class 625B
HotelAdminApplicationTests.class 547B
IHotelService.class 314B
HotelMapper.class 301B
HotelMapper.class 301B
index.css 7KB
index.css 7KB
banner.css 2KB
banner.css 2KB
main.css 275B
main.css 275B
.gitignore 184B
.gitignore 184B
index.html 22KB
index.html 22KB
index.html 8KB
index.html 8KB
HotelService.java 12KB
HotelSearchTest.java 5KB
HotelDocumentTest.java 3KB
HotelController.java 2KB
HotelDemoApplicationTests.java 2KB
HotelConstants.java 2KB
HotelDoc.java 2KB
MqConfig.java 1KB
HotelDemoApplication.java 1007B
HotelAdminApplication.java 975B
HotelController.java 954B
HotelListener.java 908B
CorsConfig.java 711B
CorsConfig.java 711B
Hotel.java 639B
Hotel.java 639B
IHotelService.java 570B
HotelMqConstants.java 407B
HotelMqConstants.java 407B
HotelService.java 394B
RequestParams.java 378B
PageResult.java 346B
PageResult.java 339B
HotelAdminApplicationTests.java 220B
IHotelService.java 198B
HotelMapper.java 191B
HotelMapper.java 191B
vue.js 366KB
vue.js 366KB
vue.js 366KB
vue.js 366KB
amap.min.js 56KB
amap.min.js 56KB
axios.min.js 13KB
axios.min.js 13KB
axios.min.js 13KB
axios.min.js 13KB
heima.png 7KB
heima.png 7KB
ad.png 2KB
ad.png 2KB
ad2.png 1KB
ad2.png 1KB
workspace.xml 8KB
workspace.xml 4KB
pom.xml 3KB
pom.xml 3KB
jarRepositories.xml 880B
jarRepositories.xml 880B
compiler.xml 719B
compiler.xml 717B
misc.xml 478B
misc.xml 478B
logback.xml 418B
logback.xml 418B
encodings.xml 191B
encodings.xml 191B
thriftCompiler.xml 140B
thriftCompiler.xml 140B
application.yaml 563B
application.yaml 563B
共 102 条
- 1
- 2
资源评论
lisus2007
- 粉丝: 2000
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功