欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

ElasticSearch、ki、head、kibana安装与基本使用

时间:2023-04-20
ElasticSearch安装与基本使用**

官网地址:https://www.elastic.co/cn/downloads/past-releases#elasticsearch

1.下载与安装

先去官网下载自己需要的的版本这里我用的是7.10.2,Windows版本

还需要下载一个Kibana(可视化软件),要注意的是,版本需要跟上面下载的一样,不然会报错

然后再来一个分词器elasticsearch ik

下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v5.6.16

最后加上一个elasticsearch-head(客户端工具)

下载地址:https://github.com/mobz/elasticsearch-head

最后下载完会得到四个压缩包

首先解压第一个,注意安装路径不能带空格和中文

除了ik,,其他三个建议全部解压缩到磁盘根目录

ik解压到elasticsearch的plugins目录下

最后浏览器需要安装一个插件,以谷歌浏览器为例

访问chrome://extensions/

会得到一个这个

点击右边的就会出现当前页面

2.运行

接下来就可以尝试跑起来了

双击这个目录下的这个文件,启动完成之后访问浏览器localhost:9200端口,看到以下页面就算安装成功了

5601

9200

点击小三角运行之后,去客户端点击连接

3.语法 添加

语法:PUT /索引名

在没有特殊设置的情况下,默认有1个分片,1个备份,也可以通过请求参数的方式来指定

参数格式:

默认:

PUT my_index

明确指定:

PUT /my_index{ "settings": {"number_of_shards": 5, //设置5个片区"number_of_replicas": 1 //设置1个备份 }}

注意:

1:索引不能有大写字母

2:参数格式必须是标准的json格式

查看单个

GET /索引名

查看所有

GET _cat/indices

删除

语法:DELETE /索引名

设置映射(列)

创建

类型跟映射一起创建
语法:
PUT /索引名
{
“mappings”: {
“properties”: {
字段名: {
“type”: 字段类型,
“analyzer”: 分词器类型,
“search_analyzer”: 分词器类型,

},

}
}
}
字段类型就是:数据类型

创建数据

POST /product/_bulk

{“create”:{"_id": 1}}

{“id”:1,“title”:“Apple iPhone XR (A2108) 128GB 白色 移动联通电信4G手机 双卡双待”,“price”:5299,“intro”:"【iPhoneXR限时特惠!】6.1英寸视网膜显示屏,A12仿生芯片,面容识别,无线充电,支持双卡!选【换修无忧版】获 AppleCare 原厂服务,享只换不修!更有快速换机、保值换新、轻松月付!",“brand”:“Apple”}

GET /索引/_search{ "query": {"match": {field: value} }}

GET /product/_search{ "query": {"match":{ "title": "游戏 手机"} }}

multi_match参数格式:GET /索引/_search{ "query": {"multi_match": { "query": value, "fields": [field1, field2, ...]} }}

4.springboot集成 1.依赖

org.springframework.boot spring-boot-starter-parent 2.4.3 org.springframework.boot spring-boot-starter-data-elasticsearch commons-beanutils commons-beanutils 1.9.4 org.projectlombok lombok org.springframework.boot spring-boot-starter-test test

application.properties

配置集群名称,名称写错会连不上服务器,默认elasticsearch

spring.data.elasticsearch.cluster-name=elasticsearch

配置集群节点

spring.data.elasticsearch.cluster-nodes=localhost:9300

是否开启本地存储

spring.data.elasticsearch.repositories.enabled=true
过期原因:
es官方打算在es7设置TransportClient 过期, es8之后抛弃, 直接使用 High Level REST Client 替换TransportClient
https://docs.spring.io/spring-data/elasticsearch/docs/3.2.3.RELEASE/reference/html/#reference
spring.elasticsearch.rest.uris = localhost:9200

domain

@Getter@Setter@ToString@NoArgsConstructor@AllArgsConstructor@document(indexName="es_shop", type="shop_product")public class Product { @Id private String id; @Field(analyzer="ik_smart",searchAnalyzer="ik_smart",type = FieldType.Text) private String title; private Integer price; @Field(analyzer="ik_smart",searchAnalyzer="ik_smart",type = FieldType.Text) private String intro; @Field(type=FieldType.Keyword) private String brand;}

### CRUDpublic interface ProductRepository extends ElasticsearchRepository{}public interface IProductService { void save(Product product); void update(Product product); void delete(String id); Product get(String id); List list();}@Servicepublic class ProductServiceImpl implements IProductService { @Autowired private ProductRepository repository; @Autowired private ElasticsearchTemplate template; @Override public void save(Product product) { repository.save(product); } @Override public void update(Product product) { repository.save(product); } @Override public void delete(String id) { repository.deleteById(id); } @Override public Product get(String id) { return repository.findById(id).get(); } @Override public List list() { Iterable all = repository.findAll(); List p = new ArrayList<>(); all.forEach(a->p.add(a)); return p; }}

@SpringBootTestpublic class ElasticsearchDemoApplicationTests {@Autowiredprivate IProductService productService;@Testpublic void testSave() {Product p = new Product(); p.setId("123")p.setBrand("dafei");p.setIntro("dafei手机");p.setPrice(1000);p.setTitle("全球最帅的手机");productService.save(p);}@Testpublic void testUpate() {Product p = new Product();p.setId("ue5r1m4BXlaPW5P0TegF");p.setBrand("dafei");p.setIntro("dafei手机");p.setPrice(1000);p.setTitle("全球最sb的手机");productService.update(p);}@Testpublic void testDelete() {productService.delete("123");}@Testpublic void testGet() {System.out.println(productService.get("123"));}@Testpublic void testList() {System.err.println(productService.list());}}### 各类查询- match // 查询商品标题中符合"游戏 手机"的字样的商品 @Test public void testQuery4(){ NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder(); builder.withQuery( QueryBuilders.matchQuery("title", "游戏 手机") ); builder.withPageable(PageRequest.of(0, 100)); Page search = repository.search(builder.build()); search.getContent().forEach(System.err::println); }- multi_match // 查询商品标题或简介中符合"蓝牙 指纹 双卡"的字样的商品 @Test public void testQuery7(){ NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder(); builder.withQuery( QueryBuilders.multiMatchQuery("蓝牙 指纹 双卡", "title", "intro") ); builder.withPageable(PageRequest.of(0, 100, Sort.Direction.DESC, "price")); Page search = repository.search(builder.build()); search.getContent().forEach(System.err::println); }### 高亮显示@Testpublic void testHighlight() throws Exception {//定义索引库SearchRequest searchRequest = new SearchRequest("product");SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//定义query查询MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("蓝牙 指纹 双卡","title", "intro");HighlightBuilder highlightBuilder = new HighlightBuilder(); // 生成高亮查询器highlightBuilder.field("title");// 高亮查询字段highlightBuilder.field("intro");// 高亮查询字段highlightBuilder.requireFieldMatch(false); // 如果要多个字段高亮,这项要为falsehighlightBuilder.preTags(""); // 高亮设置highlightBuilder.postTags("");highlightBuilder.fragmentSize(800000); // 最大高亮分片数highlightBuilder.numOfFragments(0); // 从第一个分片获取高亮片段Pageable pageable = PageRequest.of(1, 2); // 设置分页参数NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(queryBuilder) // match查询.withPageable(pageable).withHighlightBuilder(highlightBuilder) // 设置高亮.build();SearchHits searchHits = template.search(searchQuery, Product.class);List list = new ArrayList();for (SearchHit searchHit : searchHits) { // 获取搜索到的数据Product content = searchHit.getContent();// 处理高亮Map> highlightFields = searchHit.getHighlightFields();for (Map.Entry> stringHighlightFieldEntry : highlightFields.entrySet()) {String key = stringHighlightFieldEntry.getKey();if (StringUtils.equals(key, "title")) {List fragments = stringHighlightFieldEntry.getValue();StringBuilder sb = new StringBuilder();for (String fragment : fragments) {sb.append(fragment.toString());}content.setTitle(sb.toString());}if (StringUtils.equals(key, "intro")) {List fragments = stringHighlightFieldEntry.getValue();StringBuilder sb = new StringBuilder();for (String fragment : fragments) {sb.append(fragment.toString());}content.setIntro(sb.toString());}}list.add(content);}Page page = new PageImpl(list, pageable, searchHits.getTotalHits());list.forEach(System.out::println);}

ingBuilder sb = new StringBuilder();

for (String fragment : fragments) {sb.append(fragment.toString());}content.setTitle(sb.toString());}if (StringUtils.equals(key, "intro")) {List fragments = stringHighlightFieldEntry.getValue();StringBuilder sb = new StringBuilder();for (String fragment : fragments) {sb.append(fragment.toString());}content.setIntro(sb.toString());}}list.add(content);}Page page = new PageImpl(list, pageable, searchHits.getTotalHits());list.forEach(System.out::println);

}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。