通过命令curl 操作ElasticSearch指南

2,114 阅读4分钟

集群操作

查询集群的名字

⇒  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表示数据可以有也可以没有