Elasticsearch ,简称为 ES,是一款非常强大的开源的高扩展的分布式全文 检索引擎,可以帮助我们从海量数据中快速找到需要的内容,它可以近乎实时的 存储、检索数据.还可以可以实现日志统计、分析、系统监控等功能.官网:https://www.elastic.co/cn例如京东,淘宝,头条等站内搜索功能.
ES环境搭建
安装数据可视化界面 elasticsearch head
安装可视化 kibana 组件
访问http://localhost:5601
显示界面
SpringBoot使用ES搜索
配置类
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));
return client;
}
}
使用文章来源:https://www.toymoban.com/news/detail-797127.html
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ffyc.news.config.ElasticSearchConfig;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
@RequestMapping(path = "/es")
public class EsController {
@Autowired
ElasticSearchConfig elasticSearchConfig;
@GetMapping(path = "/indexOper")
public String indexOper() throws IOException {
//创建索引库
CreateIndexRequest request = new CreateIndexRequest("users_index");
elasticSearchConfig.restHighLevelClient().indices().create(request, RequestOptions.DEFAULT);
return "success";
//删除库
// DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("users");
// AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
// System.out.println(delete.isAcknowledged());
// 判断库为例
// GetIndexRequest getIndexRequest = new GetIndexRequest("users");
// boolean users = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
// System.out.println(users);
}
/*@GetMapping(path = "/docOper")
public String docOper() throws IOException {
//模拟从前端传递过来的数据
News news = new News();
news.setId(1);
news.setTitle("美国今年要总统选择,拜登着急了");
news.setImg("aaaaasssss.jpg");
news.setContent("少时诵诗书所所所所所所所所所所所所所所所");
news.setCount(300);
//把News中的数据封装到NewsES
NewsES newsES = new NewsES();
//对象数据复制
BeanUtils.copyProperties(news,newsES);
//创建请求对象
IndexRequest indexRequest = new IndexRequest("news").id(newsES.getId().toString());
indexRequest.source(new ObjectMapper().writeValueAsString(newsES), XContentType.JSON);
//执行
restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
return "success";
}*/
/* @GetMapping(path = "/docOper")
public String docOper() throws IOException {
//模拟从前端传递过来的数据
News news = new News();
news.setId(1);
news.setTitle("中国航母开往美国,准备开战,拜登着急了");
news.setImg("ddddddddddddddddddddd.jpg");
news.setContent("少时诵诗书所所所所所所所所所所所所所所所");
news.setCount(30000);
//把News中的数据封装到NewsES
NewsES newsES = new NewsES();
//对象数据复制
BeanUtils.copyProperties(news,newsES);
//创建一个修改的请求
UpdateRequest updateRequest = new UpdateRequest("news",newsES.getId().toString());
updateRequest.doc(new ObjectMapper().writeValueAsString(newsES), XContentType.JSON);
restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);
return "success";
}*/
/* @GetMapping(path = "/docOper")
public String docOper() throws IOException {
//创建一个修改的请求
DeleteRequest deleteIndexRequest = new DeleteRequest("news","1");
restHighLevelClient.delete(deleteIndexRequest,RequestOptions.DEFAULT);
return "success";
}*/
/* @GetMapping(path = "/docOper")
public String docOper() throws IOException {
//创建一个搜索的请求
SearchRequest searchRequest = new SearchRequest("news");
//封装搜索条件
searchRequest.source().query(QueryBuilders.termQuery("title","免费"));
//发送搜索请求, 接收搜索的结果
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
ArrayList<NewsES> arrayList = new ArrayList<>();
SearchHits hits = search.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit searchHit : hits1){
String json = searchHit.getSourceAsString();
NewsES newsES = new ObjectMapper().readValue(json,NewsES.class);
arrayList.add(newsES);
}
return "success";
}*/
// @GetMapping(path = "/docOper")
// public String docOper() throws IOException {
// //创建一个搜索的请求
// SearchRequest searchRequest = new SearchRequest("news");
// //封装搜索条件
// //设置关键字 高亮显示
// searchRequest.source().highlighter(new HighlightBuilder().field("title").requireFieldMatch(false));
// searchRequest.source().query(QueryBuilders.termQuery("title","免费"));
// //发送搜索请求, 接收搜索的结果
// SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//
// ArrayList<NewsES> arrayList = new ArrayList<>();
// SearchHits hits = search.getHits();
// SearchHit[] hits1 = hits.getHits();
// for (SearchHit searchHit : hits1){
// String json = searchHit.getSourceAsString();
// NewsES newsES = new ObjectMapper().readValue(json,NewsES.class);
//
// //获取高亮名称
// Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
// HighlightField titleField = highlightFields.get("title");
// String highttitle = titleField.getFragments()[0].string();
// newsES.setTitle(highttitle);//用添加了高亮的标题替换原始文档标题内容
// arrayList.add(newsES);
// }
//
//
//
// return "success";
// }
}
文章来源地址https://www.toymoban.com/news/detail-797127.html
到了这里,关于Elasticsearch的基本功能和使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!