ES里的其中两个概念理解:索引(数据表)、文档(表里的数据)
- 语句后加
?pretty表示美化JSON输出
索引操作
# 创建某个索引
curl -X PUT http://localhost:9200/<索引>?pretty
# 查询所有索引
curl -X GET http://localhost:9200/_cat/indices?v
# 删除某个索引
curl -X DELETE http://localhost:9200/<索引>?pretty
# 查询某个索引的字段信息
curl -X GET http://localhost:9200/<索引>?pretty
文档操作
# 创建文档-创建某个索引中的文档
curl -X PUT http://localhost:9200/<索引>/_doc/<数据ID>?pretty -H 'Content-Type: application/json' -d
'{"<字段名>":"<字段值>"}'
# 修改文档-通过数据ID修改某个索引中的文档(实际是先删除旧的再新增)
curl -X PUT http://localhost:9200/<索引>/_doc/<数据ID>/_update?pretty -H 'Content-Type: application/json' -d
'{"doc":{"<字段名>":"<字段值>"}}'
# 删除文档-通过数据ID删除某个索引中的文档
curl -X DELETE http://localhost:9200/<索引>/_doc/<数据ID>?pretty
# 查询文档-通过数据ID查询某个索引中的文档
curl -X GET http://localhost:9200/<索引>/_doc/<数据ID>?pretty
检索文档
# 通过指定字段值查询某个索引中的文档,查询类型:match
curl -X GET http://localhost:9200/<索引>/_search?q=<字段名>:<查询的精确值>
# 通过指定字段值查询某个索引中的文档,查询类型:match
curl -X GET http://localhost:9200/<索引>/_search?pretty -H 'Content-Type: application/json' -d
'{"query":{"match":{"<字段名>":"<查询的精确值>"}}}'
# 查询某个索引所有文档并通过指定字段排序(方式一)
curl -X GET http://localhost:9200/<索引>/_search?q=*&sort=<字段名>:asc&pretty
# 查询某个索引所有文档并通过指定字段排序(方式二),查询类型:match_all
curl -X GET http://localhost:9200/<索引>/_search -H 'Content-Type: application/json' -d
'{
"query":{"match_all":{}},
"sort":[{"account_number":"asc"}]
}'
# 查询某个索引从第几条开始查询多少条所有文档的指定字段并通过指定字段排序,查询类型:match_all
curl -X GET http://localhost:9200/<索引>/_search -H 'Content-Type: application/json' -d
'{
"query":{"match_all":{}},
"_source":["<字段名1>","<字段名2>"],
"from":<从第几条开始,默认0>,
"size":<查询多少条>,
"sort":[{"<字段名>":"asc"}]
}'
# 模糊查询某个索引指定字段包含某个值的文档,查询类型:wildcard
# 通配符:*匹配多个字符,?匹配单个字符,类似SQL里LIKE的%和_,避免影响性能不建议以通配符开头
curl -X GET http://localhost:9200/<索引>/_search -H 'Content-Type: application/json' -d
'{
"query":{"wildcard":{"<字段名>":"<模糊查找的值>*"}}
}'
# 模糊查询某个索引指定字段包含某个值的文档,等效wildcard:<模糊查找的值>*,查询类型:prefix
curl -X GET http://localhost:9200/<索引>/_search -H 'Content-Type: application/json' -d
'{
"query":{"prefix":{"<字段名>":"<模糊查找的值>"}}
}'
# 模糊查询某个索引指定字段相似的文档,查询类型:fuzzy
# fuzziness最大编辑距离字符数,默认AUTO
# prefix_length不被模糊化的字符数,默认0
# max_expansions模糊最大术语字符数,默认50
# transpositions是否支持模糊转置,默认false
curl -X GET http://localhost:9200/<索引>/_search -H 'Content-Type: application/json' -d
'{
"query": {
"fuzzy" : {
"<字段名>": {
"value": "<模糊查找的值>",
"fuzziness": 1,
"prefix_length": 1,
"max_expansions": 100,
"transpositions":false
}
}
}
}'
# 查询某个索引中指定字段不为null的文档,查询类型:exsits
curl -X GET http://localhost:9200/<索引>/_search?pretty -H 'Content-Type: application/json' -d
'{"query":{"exsits":{"field":"<字段名>"}}}'
# 查询某个索引中id在指定id列表的文档,查询类型:ids
curl -X GET http://localhost:9200/<索引>/_search?pretty -H 'Content-Type: application/json' -d
'{"query":{"ids":{"type":"_doc","values":["<ID1>","<ID2>","<ID3>"]}}}'
多条件查询(查询类型:bool)
# 查询某个索引中字段名1、字段名2为指定值且字段名3、字段名4不为指定值或字段名5、字段名6为指定值的文档,查询类型:bool
# must等于,must_not不等于,should或者
curl -X GET http://localhost:9200/<索引>/_search -H 'Content-Type: application/json' -d
'{
"query": {
"bool" : {
"must": [
{"match":{"<字段名1>":"<查询的精确值>"}},
{"match":{"<字段名2>":"<查询的精确值>"}}
],
"must_not": [
{"match":{"<字段名3>":"<查询的精确值>"}},
{"match":{"<字段名4>":"<查询的精确值>"}}
],
"should": [
{"match":{"<字段名5>":"<查询的精确值>"}},
{"match":{"<字段名6>":"<查询的精确值>"}}
]
}
}
}'
# 过滤某个索引中字段名大于等于且小于等于指定值的文档,查询类型:bool
# filters过滤,gte大于等于,lte小于等于
curl -X GET http://localhost:9200/<索引>/_search -H 'Content-Type: application/json' -d
'{
"query": {
"bool" : {
"filters": {
"range": {
"<字段名>": {
"gte":"<大于等于的值>",
"lte":"<小于等于的值>"
}
}
}
}
}
}'