Elasticsearch常用语句大全

988 阅读3分钟

所有代码可直接复制到命令行查看结果,持续更新中...

集群

查看集群状态
curl -XGET '101.201.34.96:9200/_cluster/health?pretty'
查看节点信息
curl -XGET '101.201.34.96:9200/_nodes/process?pretty'

索引

创建索引,自定义分片数量
curl -XPUT '101.201.34.96:9200/mtestindex?pretty' -H 'Content-Type: application/json' -d '
{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 1
    }
}
'
查看所有分片
curl -XGET '101.201.34.96:9200/_cat/shards?pretty'

# 查看指定索引的分片
# curl -XGET '101.201.34.96:9200/_cat/shards?pretty' | grep mtestindex
创建索引,自定义映射
curl -XPUT '101.201.34.96:9200/mtestindex3?pretty' -H 'Content-Type: application/json' -d '
{
    "mappings": {
        "properties": {
            "name": {"type": "keyword"},
            "age": {"type": "integer"},
            "address": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above" : 256
                    },
                    "english": {
                        "type": "text",
                        "analyzer": "english"
                    }
                }
            }
        }
    }
}
'
增加一个字段到已经存在的映射
# 不能修改已经存在的字段类型

curl -XPUT '101.201.34.96:9200/mtestindex3/_mapping?pretty' -H 'Content-Type: application/json' -d '
{
    "properties": {
        "password": {
            "type": "keyword",
            "index":false
        }
    }
}
'

# "index":false 不对该字段建立索引
查看索引的映射
curl -XGET '101.201.34.96:9200/mtestindex3/_mapping?pretty'
查看索引映射的某个字段的类型
curl -XGET '101.201.34.96:9200/mtestindex3/_mapping/field/password?pretty'
查看所有索引
curl -XGET '101.201.34.96:9200/_cat/indices?pretty'
查看索引信息
curl -XGET '101.201.34.96:9200/mtestindex3?pretty'

文档

查看索引文档数量
curl -XGET '101.201.34.96:9200/mtestindex3/_count?pretty'
创建一个文档,自动生成id
curl -XPOST '101.201.34.96:9200/mtestindex3/_doc?pretty' -H 'Content-Type: application/json' -d '
{
    "name": "zjy",
    "age": 10,
    "address": "北京 霍营",
    "password": "1234567"
}
'
创建一个文档,指定id
curl -XPUT '101.201.34.96:9200/mtestindex3/_doc/2/_create?pretty' -H 'Content-Type: application/json' -d '
{
    "name": "zjy2",
    "age": 20,
    "address": "北京 海淀",
    "password": "2234567"
}
'

# 也可以不加_create,不存在则创建存在则覆盖
# _create也可以用?op_type=create替代
更新一个文档,整个覆盖
curl -XPUT '101.201.34.96:9200/mtestindex3/_doc/2?pretty' -H 'Content-Type: application/json' -d '
{
    "name": "zjy3",
    "age": 30,
    "address": "北京 朝阳"
}
'
更新一个文档,部分更新(伪)

其实还是覆盖,在 Elasticsearch 中文档是不可改变的,他们不能被修改,只能被替换。

非原子操作,存在并发问题,详见ES乐观锁_version

curl -XPOST '101.201.34.96:9200/mtestindex3/_doc/2/_update?pretty' -H 'Content-Type: application/json' -d '
{
    "doc": {
        "age": 31,
        "password": '22222'
    }
}
'
更新一个文档,使用脚本
curl -XPOST '101.201.34.96:9200/mtestindex3/_doc/2/_update?pretty' -H 'Content-Type: application/json' -d '
{
    "script": "ctx._source.age+=2"
}
'
获取一个文档
curl -XGET '101.201.34.96:9200/mtestindex3/_doc/2?pretty'
检查一个文档是否存在
curl -I -XHEAD '101.201.34.96:9200/mtestindex3/_doc/7xkbPm8BCYmbEjHUXwA-?pretty'

# -I 或者 --head 显示HTTP响应头信息
删除一个文档
curl -XDELETE '101.201.34.96:9200/mtestindex3/_doc/7xkbPm8BCYmbEjHUXwA-?pretty'
获取多个文档
curl -XGET '101.201.34.96:9200/mtestindex3/_doc/_mget?pretty' -H 'Content-Type: application/json' -d '
{
    "docs": [
        {"_id":2},
        {"_id":3}
    ]
}
'
批量操作文档
curl -XPOST '101.201.34.96:9200/mtestindex3/_doc/_bulk?pretty' -H 'Content-Type: application/json' -d '
{ "create": { "_id": "21" }}
{ "name": "zzjjyy", "age": 22}
{ "create": { "_id": "22" }}
{ "name": "zzjjyy2", "age": 23}
{ "delete": { "_id": "23" }}
{ "update": { "_id": "2" }}
{"doc": {"age": 333}}
'

# create 创建、index 创建或替换、update 部分更新、delete 删除

搜索

搜索所有文档
curl -XGET '101.201.34.96:9200/mtestindex3/_doc/_search?pretty'
轻量搜索
curl -XGET '101.201.34.96:9200/mtestindex3/_doc/_search?q=name:zjy3&pretty'
表达式搜索
match
curl -XGET '101.201.34.96:9200/mtestindex3/_doc/_search?pretty' -H 'Content-Type: application/json' -d '
{
    "query": {
        "match": {
            "address": "北京 昌平"
        }
    },
    "from": 1,
    "size": 2
}
'

# 通过 from 和 size 进行分页,默认最多10000条数据
match_phrase
curl -XGET '101.201.34.96:9200/mtestindex3/_doc/_search?pretty' -H 'Content-Type: application/json' -d '
{
    "query": {
        "match_phrase": {
            "address": "北京 昌平"
        }
    }
}
'
term
curl -XGET '101.201.34.96:9200/mtestindex3/_doc/_search?pretty' -H 'Content-Type: application/json' -d '
{
    "query": {
        "term": {
            "age": 22
        }
    }
}
'
must、must_not、should、filter
curl -XGET '101.201.34.96:9200/mtestindex3/_doc/_search?pretty' -H 'Content-Type: application/json' -d '
{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "address": "北京 昌平"
                }
            },
            "must_not": {
                "term": {
                    "age": 40
                }
            },
            "should": {
                "term": {
                    "age": 20
                }
            },
            "filter": {
                "range": {
                    "age": {
                        "gt": 12
                    }
                }
            }
        }
    }
}
'

# must 条件必须都满足,会进行打分
# must_not 条件必须都不满足
# should 如果满足任意条件,将增加 _score ,否则无任何影响。主要用于修正每个文档的相关性得分
# filter 条件必须都满足,不进行打分,效率高,还会进行缓存
高亮搜索
curl -XGET '101.201.34.96:9200/mtestindex3/_doc/_search?pretty' -H 'Content-Type: application/json' -d '
{
    "query": {
        "match": {
            "address": "北京 昌平"
        }
    },
    "highlight": {
        "fields" : {
            "address" : {}
        }
    }
}
'
聚合搜索
curl -XGET '101.201.34.96:9200/mtestindex3/_doc/_search?pretty' -H 'Content-Type: application/json' -d '
{
    "aggs": {
        "ages": {
            "terms": {"field": "age"}
        }
    }
}
'

上一页: Elasticsearch(三)——交互方法

下一页: 待续 ...