简单安装
version: "3"
services:
elk:
image: sebp/elk:640
ports:
- "5601:5601"
- "9200:9200"
- "5044:5044"
restart: always
environment:
- ES_JAVA_OPTS=-Xms2G -Xmx2G
volumes:
- ${PWD}/elk/elasticsearch:/var/lib/elasticsearch
安装ik插件
查找版本
https://github.com/medcl/elasticsearch-analysis-ik/releases/
进入容器,执行elasticsearch-plugin
$ ./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysiss
-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-6.3.0.zip
查询所有索引
curl 'localhost:9200/_cat/indices?v'
创建索引
localhost:9200/people (people 索引名)
curl -X PUT \
http://localhost:9200/people \
-H 'content-type: application/json' \
-d '{
"settings": {
"number_of_shards": 1, //分片数默认是5
"number_of_replicas": 1 //备份数
},
"mappings": {
"man": {
"properties": {
"name": {
"type": "text"
},
"country": { //字段名
"type": "keyword" //字段类型
},
"age": {
"type": "integer"
},
"date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}’
——————————结果
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "people"
}
获取索引
curl -X GET \
http://localhost:9200/people \
——————————结果
{
"people": {
"aliases": {},
"mappings": {
"man": {
"properties": {
"age": {
"type": "integer"
},
"country": {
"type": "keyword"
},
"date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"name": {
"type": "text"
}
}
}
},
"settings": {
"index": {
"creation_date": "1587217871713",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "GAULFlq4QnKP__KSwX7kwA",
"version": {
"created": "6040099"
},
"provided_name": "people"
}
}
}
}
插入一条数据,替换一条数据
PUT localhost:9200/people/man/1 对应 people:索引,man:类型,1:文档id
curl -X PUT \
http://localhost:9200/people/man/1 \
-H 'content-type: application/json' \
-d '{
"name":"中华人民共和国,中国",
"country":"china",
"age":"20",
"data":"2020-09-09"
}’
——————————结果
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
插入一条数据自动生成id的数据
POST localhost:9200/people/man/ 自动生成id
curl -X POST \
http://localhost:9200/people/man \
-H 'content-type: application/json' \
-d '{
"name":"测试,搜索",
"country":"china",
"age":"20",
"data":"1999-09-09"
}’
——————————结果
{
"_index": "people",
"_type": "man",
"_id": "CW6WjXEBIKWlyvncBjJU”, //id自动生成
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
更新一条数据
curl -X POST \
http://localhost:9200/people/man/Cm7RjXEBIKWlyvncdzIr/_update \
-H 'content-type: application/json' \
-d '{
"doc": {
"name": "张三"
}
}’
——————————结果
{
"_index": "people",
"_type": "man",
"_id": "Cm7RjXEBIKWlyvncdzIr",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
根据id获取数据
curl http://localhost:9200/people/man/CW6WjXEBIKWlyvncBjJU
-------------结果
{
"_index": "people",
"_type": "man",
"_id": "CW6WjXEBIKWlyvncBjJU",
"_version": 1,
"found": true,
"_source": {
"name": "测试,搜索",
"country": "china",
"age": "20",
"data": "1999-09-09"
}
}
Q1: 创建索引 报黄线
PUT http://localhost:9200/test test 为索引名
{
“settings”:{
“number_of_replincas”: 0 //es默认创建索引会是5个分片一个备份,如果备份没有机器存的话,会报yellow状态,这个设置为不需要备份
},
“mappings”:{
“dynamic”:“false”//索引结构是固定的,不要随时变动
…具体字段与类型
}
}
--------------------------------返回结果
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “outorder”
}
数组 可以用text es会自动识别为数组
删除索引
DELETE http://localhost:9200/test 删除模板
{
"acknowledged": true
}