ElasticSearch 常用查询语句

# 查看指定index、type和id的文档,返回结构如下,found字段表示是否找到对应的文档

curl -XGET http://localhost:9200/_index/_type/_id?pretty

# 返回
{
  "_index": "_index",
  "_type": "_type",
  "_id": "_id",
  "_version": 1,
  "found": true,
  "_source": {}
}

# 查看指定index、type和id的文档的对应字段,返回结构如下,found字段表示是否找到对应的文档

curl -XGET http://localhost:9200/_index/_type/_id?_source=title

# 返回

{
  "_index": "_index",
  "_type": "_type",
  "_id": "_id",
  "_version": 1,
  "found": true,
  "_source": {
       "title": "0735ee78-8bd9-59ef-9031-1fd1c647a0a5"
   }
}

# 查看指定index、type和id的文档的_source而不查看其他字段,返回结构如下

curl -XGET http://localhost:9200/_index/_type/_id/_source

# 返回结构
{
    "title": "0735ee78-8bd9-59ef-9031-1fd1c647a0a5"
}

# 查看指定index、type和id的文档是否存在

curl -i -XHEAD http://localhost:9200/_index/_type/_id/_source
# 如果存在则返回如下结构
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 903
# 不存在返回如下格式
HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 98

# 删除文档

curl -XDELETE http://localhost:9200/_index/_type/_id

# 更新文档

curl -XPOST 127.0.0.1:9200/_index/_type/_id/_update -d '{"doc":{"event_type":"005"}}'

或

curl -XPOST 127.0.0.1:9200/_index/_type/_id/_update -d '{"script":"ctx._source.event_type=006"}'

# 增加文档

curl -XPUT 127.0.0.1/_index/_type/_id -d '{"title": "My first blog entry","text": "Just trying this out...","date": "2014/01/01"}'

如果不指定ID,ID为默认的UUID

# 查询索引

curl -XGET 127.0.0.1:9200/_index/_search

返回前十条数据

# 分页查询

curl -XGET 127.0.0.1:9200/_index/_search?size=5&from=5

返回前5到第十条数据

size : 结果数,默认 10
from : 跳过开始的结果数,默认 0

# 查看mapping

curl -XGET 127.0.0.1:9200/_index/_mapping/(_type)

小括号括起来的表示可选

#  query查询

curl -XPOST 127.0.0.1:9200/_index/_search -d '{"query":{}}'

# 空查询 等同于

curl -XPOST 127.0.0.1:9200/_index/_search -d '{"query":{"match_all":{}}}'



# 过滤

# 查询特定匹配

curl -XPOST 127.0.0.1:9200/_index/_search -d '{"query":{"match":{}}}'

# bool过滤 
# must :: 多个查询条件的完全匹配,相当于 and 。
# must_not :: 多个查询条件的相反匹配,相当于 not 。
# should :: 至少有一个查询条件匹配, 相当于 or 。
# 但是当should和must共用时should不生效

curl -XPOST 127.0.0.1:9200/_index/_search -d '{"query":{"bool":{"must":{"match":{}}}}}'

#multi_match 允许你做 match 查询的基础上同时搜索多个字段:
curl -XPOST 127.0.0.1:9200/_index/_search-d '{"query":{"multi_match":{"query":"","fields":[""]}}}'

curl -XPOST 127.0.0.1:9200/_index/_search -d '{"query":{"bool":{"must_not":{"match":{}}}}}'

curl -XPOST 127.0.0.1:9200/_index/_search -d '{"query":{"bool":{"shold":{"match":{}}}}}'

#term过滤

curl -XPOST 127.0.0.1:9200/_index/_search -d '{"query":{"term":{"tag":"1"}}}'

#terms允许查询多个

curl -XPOST 127.0.0.1:9200/_index/_search -d '{"query":{"terms":{"tag":["1","2"]}}}'

#range 过滤 gt:大于 gte:大于等于 lt:小于 lte:小于等于

curl -XPOST 127.0.0.1:9200/_index/_search -d '{"query":{"range":{"age":{"gte":20,"lt":30}}}}'
 
#exists 过滤字段是否存在,key为field,value为字段名

curl -XPOST 127.0.0.1:9200/_index/_search -d '{"query":{"exists":{"field":"tag"}}}'

curl -XPOST 127.0.0.1:9200/_index/_search -d '{"query":{"missing":{"field":"tag"}}}'

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值