一、启动es
1、启动es
bin目录下找到elasticsearch.bat,启动
访问 http://127.0.0.1:9200/ ,启动成功
2、启动es head
输入npm run start
访问 http://localhost:9100 启动成功
3、启动kibana
bin目录下找到kibana.bat,启动
访问 http://localhost:5601
二、ES使用
基本概念
1、索引(indices)、type、documents、fields
分别对应数据库 数据库、表(table)、行(row)、字段(columns)
{index}:索引,根据需要自己设定,下面例子的索引为:customer
{type}:类型,根据需要自己设定,下面例子的索引为:external
{id}:文档id,根据需要自己设定
2、keywords和text
- text取代了string,当一个字段是要被全文搜索的,比如Email内容、产品描述,应该使用text类型。设置text类型以后,字段内容会被分析,在生成倒排索引以前,字符串会被分析器分成一个一个词项。text类型的字段不用于排序,很少用于聚合(termsAggregation除外)
- keyword类型适用于索引结构化的字段,比如email地址、主机名、状态码和标签。如果字段需要进行过滤(比如查找已发布博客中status属性为published的文章)、排序、聚合。keyword类型的字段只能通过精确值搜索到
索引
settings : 索引的设置
mappings : 映射
kibana添加索引
创建索引
PUT /student_001
{
"mappings": {
"dynamic": "true",
"properties": {
"birthday": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"introduce": {
"type": "text"
},
"name": {
"type": "keyword"
},
"sex": {
"type": "keyword"
},
"stu_id": {
"type": "long"
}
}
}
}
查看索引当前的mapping
GET student_001/_mapping
ES索引的mapping在设置之后是不可以更改字段类型的,但是可以给字段添加新的属性
添加新属性语句
PUT student_001/_mapping
{
"properties":{
"tags" : {
"type" : "keyword"
}
}
}
删除索引 DELETE /索引名
查看是否删除成功:HEAD /索引名
示例
HEAD student_001
DELETE student_001
查看所有索引
GET _all
插入数据
- 如果使用 POST+update 方式,则只会更改对应的字段,其它字段不变,是局部更新;
- 否则使用put或post方式将导致其它数据变化,数据全局更新。
使用 PUT /index/type/id 添加数据,修改数据
PUT student_001/_doc/1
{
"name":"abc1",
"birthday":"2023-07-01 00:00:00",
"tags" :"{\"jump_type\":3,\"jump_url\":\"https://www.baidu.com/index.htm\"}"
}
局部更新
POST /test_index/my_type/1/_update
{
"doc":{
"name":" test"
}
}
查询数据
- match查询是一个标准查询,不管你需要全文本查询还是精确查询基本上都要用到它
- term查询是直接通过倒排索引指定的词条进行精确查找
关于分词:
- term 直接查询精确
- match 会使用分词器解析(先分析文档,在通过分析的文档进行查询)
布尔查询 (bool query)
- must 与关系 相当于and
- should 或关系 相当于or
- must not 非关系 相当于not
- filter 过滤条件
- range 条件筛选范围