ElasticSearch | Mysql |
Index | Table |
Type废弃 | Table废弃 |
Document | Row |
Field | Column |
Mapping | Schema |
Everything is indexed | Index |
Query DSL | SQL |
GET http://... | select * from |
POST http://... | update table set ... |
Aggregations | group by\sum\sum |
cardinality | 去重 distinct |
reindex | 数据迁移 |
#查询当前集群状态 --status字段显示为yellow,表示缺少副本
GET /_cat/health?v
#查询当前节点情况
GET /_cat/nodes?v
#查询索引列表
GET /_cat/indices?v
#查看user索引的分片情况
GET /_cat/shards/user?v
#查询user索引的情况
GET /user
#删除person索引
delete /person
#创建user索引
PUT /user
{
"mappings": {
"properties": {
"name": {
"type": "text",
"index":true
},
"age": {
"type": "integer"
},
"sex": {
"type": "keyword",
"index":false
},
"remark": {
"type": "text"
},
"birthday": {
"type": "date"
},
"parents": {
"properties": {
"name": {
"properties": {
"first": { "type": "text" },
"last": { "type": "text" }
}
}
}
}
}
}
}
#创建索引person
PUT /person
{
"mappings": {
"properties":{
"id":{
"type": "keyword",
"index": false,
"store": true
},
"name":{
"type": "text",
"index": true,
"store": true
},
"age":{
"type": "integer",
"index": true,
"store": true
},
"birthday":{
"type": "date",
"index": false,
"store": true
},
"remark":{
"type": "text",
"index": true
},
"sex":{
"type":"text"
},
"price":{
"type":"float"
}
}
}
}
#修改索引person的别名
POST /_aliases
{
"actions": [
{
"add": {
"index": "person",
"alias": "person_test"
}
}
]
}
#删除person索引别名
POST /_aliases
{
"actions" : [
{
"remove" :
{
"index" : "person",
"alias" : "person_test"
}
}
]
}
#查看user索引映射关系
GET /user/_mapping
# 默认查10条
GET /user/_search
#查询指定ID的索引
GET /user/_doc/1001
#为索引person新建文档
put /person/_doc/1009
{
"id":"1009",
"name":"王小兵",
"age":55,
"birthday":"1985-12-03",
"remark":"我的备注是张九灵",
"sex":"woman",
"price":40,
"desc":"这是啥啊"
}
#为索引user新建文档id为1001
put /user/_doc/1001
{
"id": "1001",
"name": "张三",
"age": 24,
"birthday": "1982-12-03",
"remark": "我的备注是张三",
"price": 20.56,
"parents": {
"name": {
"first": "张",
"last": "三"
}
}
}
#为索引person新建文档
put /person/_doc/1002
{
"id":"1002",
"name":"张九灵",
"age":24,
"birthday":"1982-12-03",
"remark":"我的备注是张九灵",
"price":20.56,
"parents1.name1.first1":"1三",
"parents.name.center":"1张"
}
#修改索引为person的文档(原有的其他字段都会消失)
post /person/_doc/1003
{
"name": "张九灵",
"birthday": "1982-10-03",
"remark": "哈哈",
"sex": "woman"
}
#删除索引为person的文档1003
DELETE person/_doc/1003
#基本查询(不能设置查询多个条件)match_all
GET /person/_search
{
"query":{
"match_all":{
}
}
}
#基本查询-match
GET /person/_search
{
"query":{
"match":{
"desc":"这是啥啊"
}
}
}
#基本查询-match -and
GET /person/_search
{
"query":{
"match":{
"desc":{
"query":"这是啥啊",
"operator":"and"
}
}
}
}
#多字段查询
GET /person/_search
{
"query":{
"multi_match": {
"query": "测试",
"fields": [ "desc", "remark" ]
}
}
}
#词条匹配(term)-查询被用于精确值 匹配
GET /person/_search
{
"query":{
"term":{
"price":20.56
}
}
}
#多词条精确匹配(terms)
GET /person/_search
{
"query":{
"terms":{
"price":[20.56,40]
}
}
}
#结果过滤
GET /person/_search
{
"_source": ["name","price"],
"query":{
"match_all":{
}
}
}
#指定includes和excludes
GET /person/_search
{
"_source": {
"includes":["name","price"],
"excludes": ["name"]
},
"query":{
"match_all":{
}
}
}
#高级查询布尔组合(bool)
GET person/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"desc": "啥啊"
}
}
],
"must": [
{
"match": {
"desc": "我是"
}
},
{
"match": {
"age": 24
}
}
]
}
}
}
#范围查询(range)gt大于/gte大于等于/lt小于/lte小于等于
GET person/_search
{
"query": {
"range": {
"price": {
"gte": 30,
"lte": 50
}
}
}
}
#模糊查询fuzzy ,与term 查询的模糊等价
GET /person/_search
{
"query": {
"fuzzy": {
"desc":"这"
}
}
}
#偏差不能超过2
GET /person/_search
{
"query": {
"fuzzy": {
"desc": {
"value":"测试",
"fuzziness":1
}
}
}
}
#过滤(filter)
GET person/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"desc": "我是"
}
}
],
"filter": {
"range": {
"price": {
"gte": 30,
"lte": 50
}
}
}
}
}
}
#单字段排序(Sort)是对查询后做的,因此在query查询对象外面
GET /person/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"desc": "我是"
}
}
]
}
},
"sort": [
{
"price": {
"order": "desc"
}
},
{
"age": {
"order": "desc"
}
}
]
}
参考文档
ELK官网:https://www.elastic.co/
ELK官网文档:https://www.elastic.co/guide/index.html
ELK中文手册:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
ELK中文社区:https://elasticsearch.cn/
ELK-API :https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html