ES+ik+Kibana(win10安装)

1. es-head安装

1.1. 下载地址:https://github.com/mobz/elasticsearch-head## 1.2. 解压文件夹在这里插入图片描述

1.3. crx下es-head.crx改为es-head.zip在这里插入图片描述

1.4. 解压

在这里插入图片描述

1.5. 谷歌扩展程序引入在这里插入图片描述

1.6. 打开位置在这里插入图片描述

1.7. 测试在这里插入图片描述

2. ik分词器

2.1 下载es对应版本

https://github.com/medcl/elasticsearch-analysis-ik/releases

2.2 解压到 es目录下的plugins下 命名为ik

重新启动-打印出如图的日志,则为安装成功
在这里插入图片描述

2.3 ik分词器模式

细粒度模式 ik_max_word在这里插入图片描述

2.4 es中有默认的分词器

处理非汉字的内容

智能模式 ik_smart
在这里插入图片描述

3. kibana安装

3.1 下载地址

3.2 启动

在这里插入图片描述
启动完成后:http://localhost:5601/app/home#/ 进入页面

3.3 汉化页面

config/kibana.yml 打开
修改为

 i18n.locale: "zh-CN"

保存,重启es

3.4 连接es

config/kibana.yml 打开
修改为

elasticsearch.hosts: ["http://localhost:9200"]

保存,重启es

3.5 使用

在这里插入图片描述

4. es使用

4.1 官方文档

学习文档
https://www.yiibai.com/elasticsearch/elasticsearch-getting-start.html
中文社区
https://learnku.com/elasticsearch

4.2 基本概念

索引库(indices)Database 数据库
类型(type)Table 数据表
文档(Document)Row 行
字段(Field)Columns 列
映射配置(mappings)每个列的约束(类型、长度)

详细说明:

概念说明
索引库 (indices)indices是index的复数,代表许多的索引,
类型(type类型是模拟mysql中的table概念,一个索引库下可以有不同类型的索引,类似 数据库中的表概念。数据库表中有表结构,也就是表中每个字段的约束信息; 索引库的类型中对应表结构的叫做 映射(mapping) ,用来定义每个字段的约束。
文档 (document)存入索引库原始的数据。比如每一条商品信息,就是一个文档
字段(field)文档中的属性
映射配置 (mappings)字段的数据类型、属性、是否索引、是否存储等特性

4.3 创建 (简单创库)

创建student库

PUT student
{
  "settings": {
    "number_of_shards": 1
  }
}

在这里插入图片描述

查看

GET student

删除

DELETE student

4.4 操作类型及映射(对应数据库中的列)

* type:类型,可以是text、long、short、date、integer、object等
* index:是否索引,默认为true
* store:是否存储,默认为false
* analyzer:分词器,这里的 ik_max_word 即使用ik分词器  
-- 创建库
PUT student
{
  "settings": {
    "number_of_shards": 1
  }
}
-- 创建映射
PUT student/_mapping/_doc?include_type_name=true
{
  "properties": {
    "id":{
      "type": "integer",
      "index": true,
      "store": true
    },
    "name":{
      "type": "text",
      "index": true,
      "store": true
    },
    
    "age":{
      "type": "integer",
      "index": true,
      "store": true
    },
    "sex":{
      "type":"integer",
      "index": true,
      "store": true
    }
  }
}

-- 查看映射
GET student/_mapping
-- 查看多个
GET student,student1/_mapping
-- 查看所有表
GET _all/_mapping

添加映射

POST student1/_mapping
{
  "properties": {
    "id":{
      "type": "integer",
      "index": true,
      "store": true
    },
    "name":{
      "type": "text",
      "index": true,
      "store": true
    },
    "age":{
      "type": "integer",
      "index": true,
      "store": true
    },
    "sex":{
      "type":"integer",
      "index": true,
      "store": true
    }
  }
}

新增数据(不指定id)

POST student/_doc
{
  "id":1,
  "name":"王政",
  "age":"24",
  "sex":"1"
}

在这里插入图片描述

指定id

POST student/_doc/1
{
  "id":1,
  "name":"王政",
  "age":"24",
  "sex":"1"
}

更新

PUT student/_doc/_pLM3nwBwjSyU7ItZlF-
{
  "id":123
}

注意:其他为空的字段内容会被赋予空值

删除

DELETE student/_doc/_ZLL3nwBwjSyU7ItWFHs

4.5 进阶版-》查询

4.5.1 查询所有(match_all)

GET student/_search
{
  "query": {
    "match_all": {}
  }
}

4.5.2 match 条件查询

match 类型查询,会把查询条件进行分词(针对text类型),然后进行查询,多个词条之间默认是or的关系

GET student/_search
{
  "query": {
    "match": {
      "name": "mine"
    }  }
}

and 关系

GET student/_search
{
  "query": {
    "match": {
        "name": {
          "query": "王z",
          "operator": "and"
        } 
    }  }
}

4.5.3 term词条匹配

term 查询被用于精确值 匹配,这些精确值可能是数字、时间、布尔或者那些未分词的字符串

一般是在keyword上进行查询

GET student/_search
{
  "query": {
    "term": {
        "name":  "zz"
        }
    }  
}

4.5.4 bool 布尔组合

bool 把各种其它查询通过 must (与)、 must_not (非)、 should (或)进行组合

GET student/_doc/_search
{
  "query":{
    "bool":{
      "must":{
        "match":{
          "name": "z"
        }
      },
      "must_not":{
        "match":{
          "name":"阿"
        }
      },
      "should":{
        "match":{
          "name":"三"
        }
      }
    }
  }
}

4.5.5 range范围查询

range 查询找出指定区间内的数字或者时间

操作符解释
gt大于
gte大于等于
lt小于
lte小于等于
-- 搜索id小于5
GET student/_doc/_search
{
  "query":{
    "range":{
      "id":{
        "lt":5
      }
    }
  }
}


--搜索小于5大于等于2的
GET student/_doc/_search
{
  "query":{
    "range":{
      "id":{
        "lt":5,
        "gte":"2"
      }
    }
  }
}

4.5.6 fuzzy模糊查询

term的模糊查询,偏差不得超过1个

-- 搜索跟山西相近的
GET student1/_doc/_search
{
  "query":{
    "fuzzy":{
      "address":"山西村"
    }
  }
}

4.5.7 指定返回字段(includes)

-- 显示 id 和 name
GET student/_doc/_search
{
  "_source":{
    "includes":[
    "id",
    "name"
  ]
  },
  "query":{
    "term":{
      "name":"z"
    }
  }
}
-- 同样的效果
GET student/_doc/_search
{
  "_source":[
    "id",
    "name"
  ],
  "query":{
    "term":{
      "name":"z"
    }
  }
}

4.5.8 指定字段不返回(excludes )

GET student/_doc/_search
{
  "_source":{
    "excludes":[
    "id",
    "name"
  ]
  },
  "query":{
    "term":{
      "name":"z"
    }
  }
}

4.5.9 filter过滤

-- 搜索name包含毛,并且id大于3且小于5的数据
GET student/_doc/_search
{
  "query":{
    "bool":{
      "must":{
        "match":{
          "name":"z"
        }
      },
      "filter":{
        "range":{
          "id":{
            "gt": 3,
            "lt": 5
          }
        }
      }
    }
  }
}

4.5.10 sort排序

text类型不可用

-- 升序
GET student/_doc/_search
{
  "query":{
    "match":{
      "name":"z"
      }
    },
  "sort":[
      {
        "id":{"order":"asc"}
      }
    ]
}


--降序
GET student/_doc/_search
{
  "query":{
    "match":{
      "name":"z"
      }
    },
  "sort":[
      {
        "id":{"order":"desc"}
      }
    ]
}

-- 多字段
GET student/_doc/_search
{
  "query":{
    "match":{
      "name":"z"
      }
    },
  "sort":[
      {
        "id":{"order":"desc"}
      },
      {"age":{
        "order":"desc"
      }}
    ]
}

4.5.11 分页

-- 从第1个开始取1个
GET student/_doc/_search
{
  "query":{
    "term":{
      "name":"z"
    }
  },
  "from":0,
  "size":1
}

4.5.12 高亮

-- name中包含毛的字段将会高亮 
GET student/_doc/_search
{
  "query":{
    "term":{
      "name":"z"
    }
  },
  "highlight": {
    "pre_tags": "<em>",
    "post_tags": "</em>",
    "fields": {
      "name": {}
    }
 }
}

4.6 聚合

比较常用的一些度量聚合方式:

  • Avg Aggregation:求平均值
  • Max Aggregation:求最大值
  • Min Aggregation:求最小值
  • Percentiles Aggregation:求百分比
  • Stats Aggregation:同时返回avg、max、min、sum、count等
  • Sum Aggregation:求和
  • Top hits Aggregation:求前几
  • Value Count Aggregation:求总数

5. spring boot集成es

5.1 引入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

5.2 配置

spring:
  elasticsearch:
    rest:
      uris: 127.0.0.1:9200

6. Spring Boot集成es(spring-boot-starter-data-elasticsearch )

6.1引入依赖

! 注意 sprign - boot的版本是2.3.4.RELEASE, spring-boot-starter-data-elasticsearch版本也一样,版本不同使用会有很大的差异

   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

6.2 实体类注解

* @Document:声明索引库配置
  * indexName:索引库名称
  * shards:分片数量,默认5
  * replicas:副本数量,默认1
* @Id:声明实体类的id
* @Field:声明字段属性
  * type:字段的数据类型
  * analyzer:指定分词器类型
  * index:是否创建索引
  * nullValue:默认初始值  

踩坑:type 类型为 FieldType.Date,需要添加格式化

	@Field(type = FieldType.Date,format = DateFormat.basic_date_time)

6.3 代码创建索引库以及映射

    @Test
    void contextLoads() {
        IndexOperations indexOperations = template.indexOps(KnowledgeArticle.class);
//        创建-索引库
        boolean b = indexOperations.create();
       
        System.out.println("新增结果:" + b1);
    }

6.4 增删改查

mapper类

@Mapper
public interface KaMapper extends ElasticsearchRepository<KnowledgeArticle,String>{
}

    @Autowired
    private ElasticsearchRestTemplate template;
    @Autowired
    private TestMapper testMapper;
    @Autowired
    private KaMapper kaMapper;
// 创建
    @Test
    void contextLoads() {
        IndexOperations indexOperations = template.indexOps(KnowledgeArticle.class);
//        创建-索引库
//        boolean b = indexOperations.create();
        //创建字符串映射
        boolean b1 = template.putMapping(KnowledgeArticle.class);
        System.out.println("新增结果:" +b1);
    }
  /*新增*/
    @Test
    void add() {
        KnowledgeArticle knowledgeArticle = testMapper.selectById(629657898296803328L);
        knowledgeArticle.setId(1L);
        KnowledgeArticle save = kaMapper.save(knowledgeArticle);
        System.out.println(save);
//        IndexQuery indexQuery = new IndexQueryBuilder()
//                .withId(knowledgeArticle.getId().toString())
//                .withObject(knowledgeArticle)
//                .build();
//        IndexCoordinates icd = template.getIndexCoordinatesFor(knowledgeArticle.getClass());
//        String index = template.index(indexQuery, icd);
//        System.out.println(index);
    }
/*获取*/
    @Test
    KnowledgeArticle get(){
        return template.queryForObject(GetQuery.getById("629657898296803328"), KnowledgeArticle.class);
    }

    //更新
    @Test
    void update(){
        KnowledgeArticle knowledgeArticle = get();
        knowledgeArticle.setIncId(1);
        KnowledgeArticle save = kaMapper.save(knowledgeArticle);
        System.out.println(save);
    }
//    删除
    @Test
    void delete(){
        kaMapper.deleteById("1");
    }

7 pinyin插件(了解有这个东西)

8 设置es登录密码

es设置

1. 在es安装目录conf下
2. 打开elasticsearch.yml
3. 末尾添加:
	xpack.security.enabled: true 
	xpack.license.self_generated.type: basic
	xpack.security.transport.ssl.enabled: true
4. bin目录下执行:
	elasticsearch-setup-passwords interactive
5. 需要数据6位数密码(123456)
6. 创建的用户:
	elastic 
	apm_system
	kibana_system
	logstash_system
	beats_system
	remote_monitoring_user

kibana设置

在kibana安装目录config下添加:
elasticsearch.username: "kibana_system"
elasticsearch.password: "123456"

登录使用该账户登录:elastic(有权限)

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在 Elasticsearch 上安装 IK 分词器,需要以下几步操作: 1. 下载 IK 分词器插件,并将其复制到 Elasticsearch 插件目录中。 2. 使用 Elasticsearch 的命令行工具安装插件,例如: ``` bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.10.0/elasticsearch-analysis-ik-7.10.0.zip ``` 3. 重启 Elasticsearch 节点,使 IK 分词器生效。 4. 在你的索引的映射中使用 IK 分词器,例如: ``` PUT my_index { "mappings": { "_doc": { "properties": { "message": { "type": "text", "analyzer": "ik_max_word" } } } } } ``` 这样 IK 分词器就安装完成了。 ### 回答2: Elasticsearch是一个广泛应用于全文搜索、日志分析、业务指标和数据分析等领域的开源搜索引擎,对于中文搜索的支持是非常重要的。而IK分词器为其提供了中文分词的支持。本答案将为您解释如何安装IK分词器。 1. 下载IK分词器: 首先需要下载适用于您当前使用的Elasticsearch版本的IK分词器,可以在开源中国上下载。下载后将其解压至任意目录,例如解压到“C:\ik”。 2. 安装IK分词器: 将解压后的IK分词器安装到Elasticsearch。只需在Elasticsearch的根目录下启动终端,通过以下命令安装IK分词器: ``` ./bin/elasticsearch-plugin install file:///PATH_TO_FILE/ik-plugin-x.x.x.zip ``` 其中,PATH_TO_FILE即为解压后保存IK分词器的路径。"x.x.x"为IK分词器的版本号,替换为实际的版本号。 3. 测试IK分词器: 在Elasticsearch中创建索引,指定使用IK分词器: ``` PUT /my_index { "settings": { "analysis": { "analyzer": { "ik_analyzer": { "type": "custom", "tokenizer": "ik_smart" } } } } } ``` 以上示例将IK分词器设置为my_index索引的默认分词器。可通过以下命令来测试IK分词器的效果: ``` GET /my_index/_analyze { "analyzer": "ik_analyzer", "text": "中文分词测试" } ``` 以上即为安装IK分词器的步骤和测试方法,通过以上步骤可以快速、方便地为您的Elasticsearch添加中文搜索的支持。 ### 回答3: Elasticsearch是一个开源的搜索引擎,它可以帮助用户快速和准确地检索大量的数据。而IK分词器则是一款中文分词插件,它可以将中文文本分解为词语,从而提高搜索结果的准确性。那么,如何在Elasticsearch中安装IK分词器呢? 安装方法如下: Step 1:下载ik分词器插件 可以从GitHub下载最新的ik分词器此时版本为7.11.2 wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.11.2/elasticsearch-analysis-ik-7.11.2.zip Step 2:解压ik插件 将下载好的插件解压到elasticsearch的plugins目录中: unzip elasticsearch-analysis-ik-7.11.2.zip -d /path/to/elasticsearch-<version>/plugins/ Step 3:重启ElasticSearch 重启Elasticsearch服务器,使插件生效: ./elasticsearch Step 4:安装分词器 使用curl命令安装中文分词器: curl -H "Content-Type: application/json" -X PUT --data-binary @/path/to/config/location/ik_index_settings.json http://localhost:9200/_settings curl -H "Content-Type: application/json" -X PUT --data-binary @/path/to/config/location/ik_synonyms.json http://localhost:9200/_settings Step 5:创建测试索引 为了测试是否安装成功,可以创建一个测试索引和mapping: curl -H "Content-Type: application/json" -X PUT --data-binary @/path/to/config/location/ik_test_index.json http://localhost:9200/ik_test 注意事项: - 确认Elasticsearch和插件版本是否一致。 - 插件应该安装在每个Elasticsearch节点上的plugins目录中。 - 确认分词器的配置是否正确。 - 在添加文档时,使用正确的字段名和正确的分词器。 总之,以上是在Elasticsearch中安装IK分词器的方法,如果您遇到了什么问题,请详细阅读官方文档或向社区寻求帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值