package com.lpl;
import org.apache.lucene.util.QueryBuilder;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by Lenovo on 2017/9/17.
*/
@RestController
public class BookController {
@Autowired
private TransportClient client;
/**
* 根据id查询book
* @param id
* @return
*/
@GetMapping("/shop/product")
@ResponseBody
public ResponseEntity getBookById(@RequestParam(value = "id",defaultValue = "") String id){
if(id.isEmpty()){
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
GetResponse result = this.client.prepareGet("shop","product",id).
get();
if(!result.isExists()){
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
return new ResponseEntity(result.getSource(), HttpStatus.OK);
}
/**
* 增加一个book
* @param name
* @param author
* @param price
* @param country
* @return
*/
@PostMapping("shop/addBook")
@ResponseBody
public ResponseEntity addBook(
@RequestParam("name") String name,
@RequestParam("author") String author,
@RequestParam("price") String price,
@RequestParam("country") String country
) {
try {
//构造数据
XContentBuilder content = XContentFactory.jsonBuilder()
.startObject()
.field("name",name)
.field("author",author)
.field("price",price)
.field("country",country)
.endObject();
IndexResponse response = this.client.prepareIndex("shop","product")
.setSource(content)
.get();
return new ResponseEntity(response.getId(),HttpStatus.OK);
} catch (IOException e) {
e.printStackTrace();
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
/**
* 删除book
* @param id
* @return
*/
@DeleteMapping("shop/delProduct")
@ResponseBody
public ResponseEntity delBook(@RequestParam(name="id") String id){
DeleteResponse result = this.client.prepareDelete("shop", "product", id)
.get();
return new ResponseEntity(result.getResult(),HttpStatus.OK);
}
/**
* 修改book 根据Id
* @param id
* @param name
* @param author
* @return
*/
@PutMapping("shop/updateProduct")
@ResponseBody
public ResponseEntity updateBook(
@RequestParam(name="id")String id,
@RequestParam(name="name",required = false) String name,
@RequestParam(name="author",required = false) String author
){
try {
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject();
if(name!=null){
builder.field("name",name);
}
if(author!=null){
builder.field("author",author);
}
builder.endObject();
UpdateResponse updateResponse = this.client.prepareUpdate("shop", "product", id)
.setDoc(builder)
.get();
return new ResponseEntity(updateResponse.getResult().toString(),HttpStatus.OK);
} catch (IOException e) {
e.printStackTrace();
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
// @RequestMapping("testCreateIndex")
// public void testCreateIndex(){
// //创建映射
// try {
// //配置mapping
// XContentBuilder mapping = XContentFactory.jsonBuilder()
// .startObject()
// .startObject("properties")
// // .startObject("m_id").field("type","keyword").endObject()
// .startObject("id").field("type", "keyword").endObject()
// .startObject("catid").field("type", "keyword").endObject()
// .startObject("classify").field("type", "integer").endObject()
// .startObject("title").field("type", "text").field("analyzer", "ik_max_word").endObject()
// .startObject("author").field("type", "text").field("analyzer", "ik_max_word").endObject()
// .startObject("published").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss").endObject()
// .startObject("article").field("type", "text").endObject()
// .endObject()
// .endObject();
// //
// IndexResponse response = this.client.prepareIndex("lpl","lplp")
// .setSource(mapping)
// .get();
// //创建索引
// System.out.print(response.status());
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
@PostMapping("shop/product")
@ResponseBody
public ResponseEntity query(
@RequestParam(name = "author",required = false) String author,
@RequestParam(name ="name",required = false)String name,
@RequestParam(name="gtPrice",defaultValue = "0") int gtPrice,
@RequestParam(name="ltPrice",required = false) Integer ltPrice
){
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
if(author != null){
boolQuery.must(QueryBuilders.matchQuery("author",author));
}
if(name != null){
boolQuery.must(QueryBuilders.matchQuery("name",name));
}
RangeQueryBuilder rangQuery = QueryBuilders.rangeQuery("price")
.from(gtPrice);
if(ltPrice!=null && ltPrice>0){
rangQuery.to(ltPrice);
}
boolQuery.filter(rangQuery);
SearchRequestBuilder buider = this.client.prepareSearch("shop")
.setTypes("product")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(boolQuery)
.setFrom(0)
.setSize(10);
System.out.print(buider);
SearchResponse response = buider.get();
List<Map<String,Object>> result = new ArrayList<>();
for(SearchHit hit:response.g
没有合适的资源?快使用搜索试试~ 我知道了~
elasticsearch5.5.2实现搜索结果高亮显示和搜索建议
共212个文件
xml:122个
png:27个
java:17个
4星 · 超过85%的资源 需积分: 50 33 下载量 134 浏览量
2018-06-22
22:16:28
上传
评论
收藏 6.35MB RAR 举报
温馨提示
elasticsearch5.5.2用javaAPI实现搜索结果高亮显示和搜索建议源代码下载
资源推荐
资源详情
资源评论
收起资源包目录
elasticsearch5.5.2实现搜索结果高亮显示和搜索建议 (212个子文件)
mvnw.cmd 5KB
amazeui.min.css 249KB
bootstrap.min.css 96KB
app.css 47KB
fullcalendar.min.css 15KB
amazeui.datatables.min.css 9KB
fullcalendar.print.css 5KB
admin.css 5KB
css.css 2KB
base.css 580B
fontawesome-webfont.eot 75KB
baidu_jgylogo3.gif 705B
.gitignore 249B
esdemo.iml 13KB
maven-wrapper.jar 46KB
BookController.java 10KB
ItemsController.java 8KB
IndexController.java 7KB
Page.java 3KB
MybatisConfig.java 3KB
Content.java 2KB
RedisConfig.java 2KB
ContentController.java 1KB
MyConfig.java 1KB
EsdemoApplicationTests.java 757B
MapperScannerConfig.java 748B
AdminController.java 696B
ContentServiceImpl.java 601B
EsdemoApplication.java 421B
ContentService.java 216B
ContentDao.java 215B
Items.java 99B
k.jpg 3.31MB
echarts.min.js 527KB
amazeui.min.js 206KB
moment.js 137KB
fullcalendar.min.js 122KB
jquery-1.11.1.min.js 94KB
amazeui.datatables.min.js 85KB
jquery.min.js 82KB
bootstrap.min.js 25KB
app.js 11KB
dataTables.responsive.min.js 11KB
bootstrap-paginator.min.js 9KB
theme.js 656B
form.jsp 34KB
index.jsp 28KB
searchList.jsp 7KB
itemsList.jsp 7KB
index.jsp 2KB
ResultList.jsp 1KB
app.less 48KB
mvnw 6KB
FontAwesome.otf 122KB
a5.png 179KB
user02.png 37KB
user03.png 35KB
user01.png 35KB
user04.png 34KB
user05.png 21KB
user06.png 20KB
user07.png 20KB
logo.png 19KB
logoa.png 13KB
logob.png 11KB
startup-640x1096.png 9KB
app-icon72x72@2x.png 7KB
landing.png 4KB
admin-safari.png 4KB
admin-firefox.png 4KB
admin-ie.png 4KB
admin-opera.png 4KB
1.png 4KB
admin-chrome.png 4KB
adminPage.png 3KB
blogPage.png 3KB
landingPage.png 2KB
sidebarPage.png 2KB
loginPage.png 2KB
favicon.png 1024B
camera_new_5606e8f.png 647B
log4j.properties 238B
jdbc.properties 208B
redis.properties 129B
maven-wrapper.properties 110B
application.properties 67B
taotao.sql 11.24MB
fontawesome-webfont.ttf 149KB
fontawesome-webfont.woff 88KB
fontawesome-webfont.woff2 70KB
workspace.xml 73KB
pom.xml 4KB
mybatis-config.xml 955B
Maven__org_springframework_boot_spring_boot_test_autoconfigure_1_5_7_RELEASE.xml 814B
Maven__org_springframework_boot_spring_boot_starter_logging_1_5_7_RELEASE.xml 793B
Maven__org_springframework_boot_spring_boot_starter_tomcat_1_5_7_RELEASE.xml 786B
Maven__org_springframework_boot_spring_boot_autoconfigure_1_5_7_RELEASE.xml 779B
Maven__org_springframework_boot_spring_boot_starter_test_1_5_7_RELEASE.xml 772B
Maven__org_springframework_boot_spring_boot_starter_web_1_5_7_RELEASE.xml 765B
Maven__com_fasterxml_jackson_dataformat_jackson_dataformat_smile_2_8_10.xml 755B
共 212 条
- 1
- 2
- 3
资源评论
- JavaPub-rodert2021-03-14太贵了,大佬,忍痛下载了
w奔跑的蜗牛
- 粉丝: 270
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功