配置文件位于config目录
bin/elasticsearch -E配置名=配置值
输出集群的结点信息
输出集群的详细结点信息,其中master栏有*表示主结点
输出集群的详细信息
es有专门的Index API,用于创建、更新、删除索引配置等
# 创建文档时,如果索引不存在,es 会自动创建对应index、type# request#索引名index_name/类型type/idPUT /test_index/doc/1 { "username":"alfred", "age":1}# response{ "_index":"test_index", "_type":"doc", "_id":"1", "_version":1, # 每次对文档有变化的操作都会更新+1,包含了锁的机制
"result":"created", "_shards":{ "total":2, "successful":1, "failed":0
}, "_seq_no":0, "_primary_term":1}
# requestPOST /test_index/doc
{ "username":"tom", "age":20}# response{ "_index":"test_index", "_type":"doc", "_id":"Mj-H2ABSmWv7ZHR8Oa3", # 自动生成
"_version":1, "result":"created", "_shards":{ "total":2, "successful":1, "failed":0
}, "_seq_no":0, "_promary_term":1}
# request#索引名index_name/类型type/idGET /test_index/doc/1# 200 response{ "_index":"test_index", "_type":"doc", "_id":"1", "_version":1, "found":true, "_source":{ # 文档的原始数据
"username":"alfred", "age":1
}
}# 404 response{ "_index":"test_index", "_type":"doc", "_id":"2", # 不存在的id "found":false}
# request# 用到_search,并把查询语句作为json格式放到http body中发送到 esGET /test_index/doc/_search{ "query":{ "term":{ # 匹配id为1的
"_id":"1"
}
}
}# response{ "took":0, # 查询耗时,单位ms
"timed_out":false, "_shards":{ "total":5, "successful":5, "skipped":0, "failed":0
}, "hits":{ "total":1, # 符合条件的总文档数
"max_score":1, "hits":[
{ # 返回的文档详情数据数组,默认前10个文档
"_index":"test_index", "_type":"doc", "_id":"1", "_version":1, "_score":1, # 文档的得分
"_source":{ # 文档的原始数据
"username":"alfred", "age":1
}
},
{
...
}
]
}
}
es允许一次创建多个文档,从而减少网络传输开销,提升写入速率
# repuestPOST _bulk# action_type支持: # index 创建文档,如果已经存在就覆盖# create 创建文档,如果已经存在就报错# update 更新文档# delete 删除文档{"index":{"_index":"test_index","_type":"doc","_id":3}}
{"username":"alfred","age":10}
{"delete":{"_index":"test_index","_type":"doc","_id":1}}
{"update":{"_id":"2","_index":"test_index"."_type":"doc"}}
{"doc":{"age":"20"}}# response{ "took":33, 耗时,单位ms "errors":false, "items":[ # 每个bulk操作的返回结果
{ "index":{ "_index":"test_index", "_type":"doc", "_id":"1", "_version":1, "result":"created", "_shards":{ "total":2, "successful":1, "failed":0
}, "_seq_no":0, "_primary_term":1, "status":201
}
},
{ "delete":{ "_index":"test_index", "_type":"doc", "_id":"1", "_version":2, "result":"deleted", "_shards":{ "total":2, "successful":1, "failed":0
}, "_seq_no":0, "_primary_term":1, "status":200
}
},
{ "update":{ "_index":"test_index", "_type":"doc", "_id":"1", "_version":2, "result":"updated", "_shards":{ "total":2, "successful":1, "failed":0
}, "_seq_no":0, "_primary_term":1, "status":200
}
}
]
}
# requestGET /_mget
{ "docs":[
{ "_index":"test_index", "_type":"doc", "_id":"1"
},
{ "_index":"test_index", "_type":"doc", "_id":2
}
]
}# response{ "docs":[
{ "index":"test_index", "_type":"doc", "_id":"1", "found":false # 未找到
},
{ "index":"test_index", "_type":"doc", "_id":"2", "_version":2, "found":true, "_source":{ "username":"lee", "age":"20"
}
}
]
}
es提供了一个测试分词的 api 接口,方便验证分词效果,endpoint 是 _analyze
# requestPOST _analyze{ "analyzer": "standard", # 分词器
"text":"hello world!" # 测试文本}# response{ "tokens": [
{ "token":"hello", # 分词结果
"start_offset":0, # 起始偏移
"end_offset":5, # 结束偏移
"type":"<ALPHANUM>", "position":0 # 分词位置
},
{ "token":"world", "start_offset":6, "end_offset":11, "type":"<ALPHANUM>", "position":1
}
]
}
# requestPOST test_index/_analyze{ "field":"username", # 测试字段
"text":"hello world!" # 测试文本}
# requestPOST _analyze{ "tokenizer": "standard", "filter": ["lowercase"], # 自定义 analyzer
"text":"Hello World!"}
类似数据库中的表结构定义:
# requestGET /test_index/_mapping# response{ "test_index": { # 索引
"mappings": { "doc": { # type "properties": { "age": { "type": "integer"
}, "username": { "type": "keyword"
}
}
}
}
}
}
# requestPUT my_index
{ "mappings": { # mappings 关键词
"doc": { # type "properties": { "title": { "type": "text"
}, "name": { "type": "keyword"
}, "age": { "type": "integer"
}
}
}
}
}# response{ "acknowledged": true, "shards_acknowledge": true, "index": "my_index"}
# requestPUT my_index{ "mappings": { "my_type": { "dynamic": false, "properties": { "user": { "properties": { "name": { "type": "text"
}, "social_networds": { "dynamic": true, "properties": {}
}
}
}
}
}
}}
PUT my_index
{ "mappings": { "doc": { "properties":{ "first_name":{ "type": "text", "copy_to": "full_name"
}, "last_name":{ "type": "text", "copy_to": "full_name"
}, "full_name":{ "type":"text"
}
}
}
}
}
PUT my_index/doc/1{ "first_name":"John", "last_name":"Smith"}
GET my_index/_search
{ "query":{ "match": { "full_name":{ "query":"John Smith", "operator": "and"
}
}
}
}
# requestPUT my_index
{ "mappings":{ "doc": { "properties": { "cookie": { "type": "text", "index": false
}
}
}
}
}
PUT my_index/doc/1
{ "cookie":"name=alfred"}GET my_index/_search
{ "query":{ "match": { "cookie":"name"
}
}
}# response{ "error":{ "root_cause":[ ......
"index": "my_index3", "caused_by":{ "type":"illegal_argument_exception", "reason":"Cannot search on field [cookie] since it is not indexed"
}
]
}, "status":400
}
# requestPUT my_index{ "mappings":{ "doc":{ "properties":{ "cookie":{ "type":"text", "index_options":"offsets"
}
}
}
}
}
# requestPUT my_index{ "mappings":{ "my_type":{ "properties": { "status_code":{ "type": "keyword". "null_value":"NULL"
}
}
}
}
}
允许对同一个自动采用不同的配置,比如分词,场景例子如对人名实现拼音搜索,只需要在人名中新增一个子字段为pinyin 即可
# request{ "test_index":{ "mappings":{ "doc":{ "properties":{ "username":{ "type":"text", "fields":{ "pinyin":{ "type":"text", "analyzer":"pinyin"
}
}
}
}
}
}
}
}GET test_index/_search
{ "query":{ "match":{ "username_pinyin":"hanhan"
}
}
}
# requestPUT /test_index/doc/1{ "username":"alfred", "age":1}
GET /test_index/_mapping# response{ "test_index":{ "mappings":{ "doc":{ "properties": { "age":{ "type":"long"
}, "username":{ "type":"test", "fields":{ "keyword":{ "type":"keyword", # es自动识别 age 为long 类型,username 为 text 类型
"ignore_above":256
}
}
}
}
}
}
}
}
JSON 类型 | es 类型 |
---|---|
null | 忽略 |
boolean | boolean |
浮点类型 | float |
整数 | long |
object | object |
array | 由第一个非 null 值的类型决定 |
string | 匹配为日期则设定为date 类型(默认开启),匹配为数组的话设为 float 或 long 类型(默认关闭),设为 text 类型,并附带 keyword 的子字段 |
# requestPUT /test_index/doc/1{ "username":"alfred", "age":14, "birth":"1988-10-10", "married":false, "year":"18", "tags":["boy", "fashion"], "money":100.1}
GET /test_index/_mapping# response{ "test_index":{ "mappings":{ "doc":{ "properties":{ "age":{ "type":"long"
}, "birth":{ "type":"date"
}, "married":{ "type":"boolean"
}, "money":{ "type":"float"
}, "tags":{ "type":"text", "fields":{ "keyword":{ "type":"keyword", "ignore_above":256
}
}
}, "username":{ "type":"text", "fields":{ "keyword":{ "type":"keyword", "ignore_above":256
}
}
}, "year":{ "type":"text", "fields":{ "keyword":{ "type":"keyword", "ignore_above":256
}
}
}
}
}
}
}
}
["strict_date_optional_time", "yyyy/MM/dd HH:mm:ss Z"]
# requestPUT my_index{ "mappings":{ "my_type":{ "dynamic_date_formats":["MM/dd/yyyy"]
}
}
}
PUT my_index/my_type/1
{ "create_date":"09/25/2015"}# 关闭日期自动识别机制PUT my_index{ "mappings":{ "my_type":{ "date_detection":false
}
}
}
# requestPUT my_index{ "mappings":{ "my_type":{ "numeric_detection":true
}
}
}
PUT my_index/my_type/1
{ "my_float":"1.0", "my_integer":"1"}# responseGET my_index/_mapping{ "my_index1":{ "mappings":{ "my_type":{ "numeric_detection":true, "properties":{ "my_float":{ "type":"float"
}, "my_integer":{ "type":"long"
}
}
}
}
}
}
# requestPUT test_index{ "mappings":{ "doc":{ "dynamic_templates":[ # 数组,可指定多个匹配规则
{ "strings":{ # template 的名称
"match_mapping_type":"string", # 匹配规则
"mapping":{ # 设置 mapping 信息
"type":"keyword"
}
}
}
]
}
}
}
# 字符串默认使用 keyword 类型# es默认会为字符串设置 text 类型,并增加一个 keyword 的子字段# requestPUT test_index
{ "mappings":{ "doc":{ "dynamic_templates":[
{ "strings_as_keywords":{ "match_mapping_type":"string", "mapping":{ "type":"keyword"
}
}
}
]
}
}
}
# 以 message 开头的字段都设置为 text 类型# requestPUT test_index
{ "mappings":{ "doc":{ "dynamic_templates":[
{ "message_as_text":{ "match_mapping_type":"string", "match":"message* ", "mapping":{ "type":"text"
}
}
}
]
}
}
}
# double 类型设定为 float,节省空间# requestPUT test_index
{ "mappings":{ "doc": { "dynamic_templates":[
{ "double_as_float":{ "match_mapping_type":"double", "mapping":{ "type":"float"
}
}
}
]
}
}
}
# 请求 /{Index}/{Type}/{id}POST /accouts/person/1{ "name": "John", "lastname": "Doe", "job_description": "Systems administrator and Linux specialit"}# 响应{ "_index": "accounts", "_type": "person", "_id":"1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0
}, "created": true}
和Create不同的是,使用GET
# 请求 /{Index}/{Type}/{id}GET /accouts/person/1
{ "name": "John", "lastname": "Doe", "job_description": "Systems administrator and Linux specialit"}# 响应{ "_index": "accounts", "_type": "person", "_id":"1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0
}, "created": true}
# 请求POST /accounts/person/1/_update
{ "doc":{ "job_description": "Systems administrator and Linux specialist"
}
}# 响应{ "_index": "accounts", "_type": "person", "_id": "1", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful":1, "failed":0
}
}
# 请求DELETE /accounts/person/1DELETE /accounts# 响应{ "found": true, "_index": "acounts", "_type": "person", "_id": "1", "_version":3, "result":"deleted", "_shards":{ "total":2, "successful":1, "failed":0
}
}
# 请求GET /accounts/person/_search?q=john
# 请求GET /accounts/person/_search{ "query": { "match": { "name":"json"
}
}
}
因为 filebeat 缺乏数据转换能力,所以官方新增 Node: Elasticsearch Ingest Node 作为能力补充,在数据写入es前进行数据转换
基于分隔符原理解析数据,解决 grok 解析时消耗过多 cpu 资源的问题
%{clientip} %{ident} %{auth} [%{timestamp}] "%{request}" % {response} %{bytes} "%{referrer}" "%{agent}"
默认分词器
tokenizer:
token filters:
特性:
tokenizer:
特性:
tokenizer:
特性:
按照 stop word 语气助词等修饰性的词语切分,如 the、an、的、这等等
tokenizer:
token filters:
特性:
特性:
tokenizer:
token filters:
特性:
特性:
我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=1y1u52rqoxs5s
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有