通过命令curl 操作ElasticSearch指南

栏目: 后端 · 发布时间: 6年前

内容简介:查询集群的名字查询集群的健康状况获取集群的所有索引

查询集群的名字

  curl -XGET 'http://localhost:9200'
复制代码

查询集群的健康状况

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

status字段说明:

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

索引操作

获取集群的所有索引

  curl -XGET 'http://localhost:9200/_cat/indices'
复制代码

索引的字段

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

结果

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

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

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

index

1. analyzed 全文索引这个域。首先分析字符串,然后索引

	2. not_analyzed 精确索引 ,不分析			

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

analyzer

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

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

新建索引

  curl -XPUT 'localhost:9200/mytest'
复制代码

删除索引

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

数据查询

插入单条数据

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

查询单条数据

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

删除单条数据

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

存储的文本分析

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

结果为

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表示词条在原始文本中的位置。

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

不返回元数据

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

低版本无法生效

只返回部分字段

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

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

match查询

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

查询匹配的结果如下

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操作

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"
复制代码

如果只是想提高精度

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查询

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

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

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

换用查询语句

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

结果为

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使用

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

或者

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

验证语法是否正确

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

bool使用

使用term查询

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

结果为

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查询,结果一样

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

如果只想要最后一条

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

想要第一条

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

都想要

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

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


以上所述就是小编给大家介绍的《通过命令curl 操作ElasticSearch指南》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Python Web开发:测试驱动方法

Python Web开发:测试驱动方法

Harry J.W. Percival / 安道 / 人民邮电出版社 / 2015-10 / 99

本书从最基础的知识开始,讲解Web开发的整个流程,展示如何使用Python做测试驱动开发。本书由三个部分组成。第一部分介绍了测试驱动开发和Django的基础知识。第二部分讨论了Web开发要素,探讨了Web开发过程中不可避免的问题,及如何通过测试解决这些问题。第三部分探讨了一些高级话题,如模拟技术、集成第三方插件、Ajax、测试固件、持续集成等。本书适合Web开发人员阅读。一起来看看 《Python Web开发:测试驱动方法》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换