前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >通过命令curl 操作ElasticSearch指南

通过命令curl 操作ElasticSearch指南

作者头像
爬蜥
发布2019-07-09 19:48:04
1.6K0
发布2019-07-09 19:48:04
举报
文章被收录于专栏:爬蜥的学习之旅

集群操作

查询集群的名字

代码语言:javascript
复制
⇒  curl -XGET 'http://localhost:9200'
复制代码

查询集群的健康状况

代码语言:javascript
复制
⇒  curl -XGET 'http://localhost:9200/_cluster/health?format=yaml'
复制代码

status字段说明:

  1. green 一切正常
  2. yellow replicas没有分配[可能是只有单个节点],集群正常
  3. red 某些数据取不到 format=yaml指定使用yaml格式输出,方便查看

索引操作

获取集群的所有索引

代码语言:javascript
复制
⇒  curl -XGET 'http://localhost:9200/_cat/indices'
复制代码

索引的字段

代码语言:javascript
复制
⇒  curl -XGET 'http://localhost:9200/mytest/_mapping?format=yaml'
复制代码

结果

代码语言:javascript
复制
 mytest:
 mappings:
   external:
  properties:
    addre:
      type: "string"
    name:
      type: "string"
复制代码

它类似于数据库的schema,描述文档可能具有的字段或属性、每个字段的数据类型。

字段对于非string类型,一般只需要设置type。string域两重要属性 index analyzer

index

代码语言:javascript
复制
	1. analyzed 全文索引这个域。首先分析字符串,然后索引

	2. not_analyzed 精确索引 ,不分析			

	3. no 此域不会被搜索
复制代码

analyzer

代码语言:javascript
复制
	将文本分成四核倒排索引的独立词条,后将词条统一化提高可搜索性
复制代码

动态映射: 文档中出现之前从未遇到过的字段,动态确定数据类型,并自动把新的字段添加到类型映射

新建索引

代码语言:javascript
复制
⇒  curl -XPUT 'localhost:9200/mytest'
复制代码

删除索引

代码语言:javascript
复制
⇒  curl -XDELETE 'localhost:9200/mytest?format=yaml'
复制代码

数据查询

插入单条数据

代码语言:javascript
复制
⇒  curl -XPUT 'localhost:9200/mytest/external/1?format=yaml' -d '
quote> { "name":"paxi"}'
复制代码

查询单条数据

代码语言:javascript
复制
⇒  curl -XGET 'localhost:9200/mytest/external/1?format=yaml'
复制代码

删除单条数据

代码语言:javascript
复制
curl -XDELETE 'localhost:9200/mytest/external/3?format=yaml'
复制代码

存储的文本分析

代码语言:javascript
复制
curl -XGET 'localhost:9200/_analyze?format=yaml' -d '
{"papa xixi write"}'
复制代码

结果为

代码语言:javascript
复制
tokens:
- token: "papa"
  start_offset: 3
  end_offset: 7
  type: "<ALPHANUM>"
  position: 1
- token: "xixi"
  start_offset: 8
  end_offset: 12
  type: "<ALPHANUM>"
  position: 2
- token: "write"
  start_offset: 13
  end_offset: 18
  type: "<ALPHANUM>"
  position: 3
复制代码

token 表示实际存储的词条,position表示词条在原始文本中的位置。

可以看出完整的文本会被切割存储成不同的词条

不返回元数据

代码语言:javascript
复制
curl -XGET 'localhost:9200/mytest/_search?filter_path=hits.hits._source&format=yaml' -d '
{ "query":{"match":{"name":"papa xixi write"}}}'
复制代码

低版本无法生效

只返回部分字段

代码语言:javascript
复制
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"match":{"name":"papa xixi write"}},"_source":["name"]}'
复制代码

低版本无效,可以用通配符

match查询

代码语言:javascript
复制
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"match":{"name":"papa xixi write"}}}'
复制代码

查询匹配的结果如下

代码语言:javascript
复制
 hits:
  - _index: "mytest"
    _type: "external"
    _id: "11"
    _score: 0.6532502
    _source:
      name: "papa xixi write"
  - _index: "mytest"
    _type: "external"
    _id: "4"
    _score: 0.22545706
    _source:
      name: "papa xixi"
  - _index: "mytest"
    _type: "external"
    _id: "2"
    _score: 0.12845722
    _source:
      name: "papa"
  - _index: "mytest"
    _type: "external"
    _id: "10"
    _score: 0.021688733
    _source:
      name: "xixi"
复制代码

从查询结果,它获取到了所有包含 papa 、 xixi和write 的词,相当于将原来的词拆开,然后两个单词做了 OR 操作,如果要全部匹配,可以使用AND操作

代码语言:javascript
复制
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"match":{"name":{"query":"papa xixi write","operator":"and"}}}}'
---
  hits:
  total: 1
  max_score: 0.6532502
  hits:
  - _index: "mytest"
    _type: "external"
    _id: "11"
    _score: 0.6532502
    _source:
      name: "papa xixi write"
复制代码

如果只是想提高精度

代码语言:javascript
复制
 curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"match":{"name":{"query":"papa xixi write","minimum_should_match":"75%"}}}}'
---
hits:
  total: 2
  max_score: 0.6532502
  hits:
  - _index: "mytest"
    _type: "external"
    _id: "11"
    _score: 0.6532502
    _source:
      name: "papa xixi write"
  - _index: "mytest"
    _type: "external"
    _id: "4"
    _score: 0.22545706
    _source:
      name: "papa xixi"
复制代码

term查询

代码语言:javascript
复制
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"term":{"name":"papa xixi write"}}}'
复制代码

它的结果是什么也没有查到

代码语言:javascript
复制
 total: 0
  max_score: null
  hits: []
复制代码

换用查询语句

代码语言:javascript
复制
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"term":{"name":"papa"}}}'
复制代码

结果为

代码语言:javascript
复制
 hits:
  - _index: "mytest"
    _type: "external"
    _id: "2"
    _score: 1.0
    _source:
      name: "papa"
  - _index: "mytest"
    _type: "external"
    _id: "4"
    _score: 0.37158427
    _source:
      name: "papa xixi"
  - _index: "mytest"
    _type: "external"
    _id: "11"
    _score: 0.2972674
    _source:
      name: "papa xixi write"
复制代码

match 和 term的区别

match 如果在全文字段上查询,会使用正确的分析器分析查询字符串;如果精确值字段使用,会精确匹配。 term精确匹配,只要包含了对应的文本就可以,不对文本分析(not_analyzed文本会精确匹配,terms 多个值只要有一个匹配就匹配);

从"papa xixi write"的存储文本分析来看,它本身会被切割成不同的词条,所以用 term查询"papa xixi write",无法获取到结果,但是match确能够匹配

filter使用

代码语言:javascript
复制
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"filtered":{"filter":{"range":{"name":{"gt":"w"}}}}}}'
复制代码

或者

代码语言:javascript
复制
 curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"constant_score":{"filter":{"range":{"name":{"gt":"w"}}}}}}'
复制代码

验证语法是否正确

代码语言:javascript
复制
⇒  curl -XGET 'localhost:9200/_validate/query?explain&format=yaml' -d '{ "query":{{"filter":{"range":{"name":{"gt":"w"}}}}}'
---
valid: false
//原因省略
复制代码

bool使用

使用term查询

代码语言:javascript
复制
 curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"term":{"addre":"beijing"}}}'
复制代码

结果为

代码语言:javascript
复制
  hits:
  - _index: "mytest"
    _type: "external"
    _id: "5"
    _score: 0.30685282
    _source:
      addre: "beijing"
  - _index: "mytest"
    _type: "external"
    _id: "6"
    _score: 0.30685282
    _source:
      addre: "beijing"
      name: "px"
复制代码

转换为bool查询,结果一样

代码语言:javascript
复制
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ query:{bool:{must:{match:{addre:"beijing"}}}}}'
复制代码

如果只想要最后一条

代码语言:javascript
复制
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ query:{bool:{must:{match:{addre:"beijing"}},must:{match:{name:"px"}}}}}'
复制代码

想要第一条

代码语言:javascript
复制
 curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ query:{bool:{must:{match:{addre:"beijing"}},must_not:{match:{name:"px"}}}}}'
复制代码

都想要

代码语言:javascript
复制
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ query:{bool:{must:{match:{addre:"beijing"}},should:{match:{name:"px"}}}}}'
复制代码

must的意思是当前值必须是有的,must_not必须没有,should表示数据可以有也可以没有

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年12月31日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 集群操作
  • 索引操作
  • 新建索引
  • 删除索引
  • 数据查询
  • 存储的文本分析
  • 不返回元数据
  • 只返回部分字段
  • match查询
  • term查询
  • match 和 term的区别
  • filter使用
  • 验证语法是否正确
  • bool使用
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档