Skip to content

ElasticSearch第六篇 API

yangyp8110 edited this page Jan 17, 2018 · 1 revision

Tips:

  • 在请求的查询串参数中加上 pretty 参数,这将会调用 Elasticsearch 的 pretty-print 功能,该功能 使得 JSON 响应体更加可读。eg:http://192.168.74.129:9200/_cluster/health?pretty
  • 我们可以通过传递 -i 参数给 curl 命令,该参数 能够显示响应的http头部。eg:curl -i -XGET http://XXXXX

检索文档是否存在

检索文档是否存在:curl -i -XHEAD http://192.168.74.129:9200/website/blog/123

  • 文档存在时返回:
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
  • 文档不存在时返回:
HTTP/1.1 404 Not Found
es.index_uuid: _na_
es.resource.type: index_expression
es.resource.id: website
es.index: website
content-type: text/plain; charset=UTF-8
content-length: 0

查询

查询字符串 (query-string) 搜索

  • 查询'last_name'为Smith的结果:

    curl -XGET http://host:port/[Index]/[Type]/_search?q=last_name:Smith

查询表达式

  • match 查询
    curl -XGET http://host:port/[Index]/[Type]/_search
    {
        "query" : {
            "match" : {
                "last_name" : "Smith"
            }
        }
    }
    

全文检索

  • 搜索结果有相关性得分,并按得分从高到底排序
    curl -XGET http://host:port/[Index]/[Type]/_search
    {
       "query" : {
           "match" : {
               "about" : "rock climbing"
           }
       }
    }
    

短语搜索

  • 仅匹配同时包含 “rock” 和 “climbing” ,并且二者以短语 “rock climbing” 的形式紧挨着的记录。
    curl -XGET http://host:port/[Index]/[Type]/_search
    {
        "query" : {
            "match_phrase" : {
                "about" : "rock climbing"
            }
        }
    }
    

高亮搜索

聚合

批量查询 _mget

{
  "docs" : [
    {
      "_index" : "index",
      "_type" : "fulltext",
      "_id" : "1",
      "_version" : 6,
      "found" : true,
      "_source" : {
        "curr" : "new"
      }
    },
    {
      "_index" : "index",
      "_type" : "fulltext",
      "_id" : "2",
      "_version" : 1,
      "found" : true,
      "_source" : {
        "context" : "test data 2"
      }
    }
  ]
}

批量查询结果要以found:true作为查找成功的标识,不能以http code作为标准

更新

  • 在 Elasticsearch 中文档是 不可改变 的,不能修改它们。 相反,如果想要更新现有的文档,需要 重建索引 或者进行替换。
    • 从旧文档构建 JSON
    • 更改该 JSON
    • 删除旧文档
    • 索引一个新文档
[root@yyp ~]# curl -XGET http://192.168.74.129:9200/index/fulltext/1
{"_index":"index","_type":"fulltext","_id":"1","_version":1,"found":true,"_source":
{"content":"美国留给伊拉克的是个烂摊子吗"}
[root@yyp ~]# curl -PUT http://192.168.74.129:9200/index/fulltext/1 -d'{"content":"美国总统特朗普有个漂亮的女儿"}'
{"_index":"index","_type":"fulltext","_id":"1","_version":2,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"created":false}
[root@yyp ~]# 
  • version增加为2,created属性为false,更新文档

新增文档

  • 使用自定义id。 eg:PUT http://XXXX/{index}/{type}/{id}

  • 使用自增id。自动生成的 ID 是 URL-safe、 基于 Base64 编码且长度为20个字符的 GUID 字符串。

    请求的结构调整为:不再使用 PUT 谓词,而是使用 POST 谓词。eg:POST http://XXXX/{index}/{type}

  • 如果es不存在文档,则会新增文档;反之,会更新文档。保证新增文档方法:

    1、使用自增id。自增id不会重复,所有每次都会create

    2、使用自定义id

    • 方法1:使用 op_type 查询 -字符串参数:PUT http://XXXX/{index}/{type}/{id}?op_type=create
    • 方法2:在 URL 末端使用 /_create : PUT http://XXXX/{index}/{type}/{id}/_create

新增已经存在的文档会返回错误 HTTP/1.1 409 Conflict

[root@yyp ~]# curl -XGET http://192.168.74.129:9200/index/fulltext/1
{"_index":"index","_type":"fulltext","_id":"1","_version":4,"found":true,"_source":{"txt":"create"}}

op_type=create 指定新增:

[root@yyp ~]# curl -i -XPUT http://192.168.74.129:9200/index/fulltext/1?op_type=create -d '{"txt":"create"}'
HTTP/1.1 409 Conflict
content-type: application/json; charset=UTF-8
content-length: 439

_create 指定新增:

[root@yyp ~]# curl -i -XPUT http://192.168.74.129:9200/index/fulltext/1/_create -d '{"txt":"create"}'
HTTP/1.1 409 Conflict
content-type: application/json; charset=UTF-8
content-length: 439

删除文档

  • 删除命令:DELETE http://XXXX/{index}/{type}/{id}

文档存在:返回HTTP/1.1 200 OK

[root@yyp ~]# curl -i -XDELETE http://192.168.74.129:9200/index/fulltext/1?pretty
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 201

{
  "found" : true,
  "_index" : "index",
  "_type" : "fulltext",
  "_id" : "1",
  "_version" : 2,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}
[root@yyp ~]# 

文档不存在:返回HTTP/1.1 404 Not Found

[root@yyp ~]# curl -i -XDELETE http://192.168.74.129:9200/index/fulltext/1?pretty
HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 204

{
  "found" : false,
  "_index" : "index",
  "_type" : "fulltext",
  "_id" : "1",
  "_version" : 7,
  "result" : "not_found",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}
[root@yyp ~]# 

版本控制

  • 内部版本号控制:PUT http://{host:port}/{index}/{type}/{id}?version=1

    版本等于1才会操作成功

[root@yyp ~]# curl -i -XGET http://192.168.74.129:9200/index/fulltext/1?pretty
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 139

{
  "_index" : "index",
  "_type" : "fulltext",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "curr" : "12"
  }
}
[root@yyp ~]# curl -i -XPUT http://192.168.74.129:9200/index/fulltext/1?version=1 -d '{"curr":"new"}'
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 143

{"_index":"index","_type":"fulltext","_id":"1","_version":2,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"created":false}

[root@yyp ~]# curl -i -XPUT http://192.168.74.129:9200/index/fulltext/1?version=55 -d '{"curr":"new"}'
HTTP/1.1 409 Conflict
content-type: application/json; charset=UTF-8
content-length: 467

{"error":{"root_cause":[{"type":"version_conflict_engine_exception","reason":"[fulltext][1]: version conflict, current version [3] is different than the one provided [55]","index_uuid":"WBwrEA_LQw-blUsadB7vew","shard":"3","index":"index"}],"type":"version_conflict_engine_exception","reason":"[fulltext][1]: version conflict, current version [3] is different than the one provided [55]","index_uuid":"WBwrEA_LQw-blUsadB7vew","shard":"3","index":"index"},"status":409}
[root@yyp ~]# 
  • 外部版本号控制:PUT http://{host:port}/{index}/{type}/{id}?version=5&version_type=external

    版本大于5才会操作成功

外部版本必须等于当前版本才能查成功

[root@yyp ~]# curl -XGET 'http://192.168.74.129:9200/index/fulltext/1?pretty&version=5'
{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[fulltext][1]: version conflict, current version [6] is different than the one provided [5]",
        "index_uuid" : "WBwrEA_LQw-blUsadB7vew",
        "shard" : "3",
        "index" : "index"
      }
    ],
    "type" : "version_conflict_engine_exception",
    "reason" : "[fulltext][1]: version conflict, current version [6] is different than the one provided [5]",
    "index_uuid" : "WBwrEA_LQw-blUsadB7vew",
    "shard" : "3",
    "index" : "index"
  },
  "status" : 409
}
[root@yyp ~]# curl -XGET 'http://192.168.74.129:9200/index/fulltext/1?pretty&version=6'
{
  "_index" : "index",
  "_type" : "fulltext",
  "_id" : "1",
  "_version" : 6,
  "found" : true,
  "_source" : {
    "curr" : "new"
  }
}
[root@yyp ~]# curl -XGET 'http://192.168.74.129:9200/index/fulltext/1?pretty&version=23&version_type=external'
{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[fulltext][1]: version conflict, current version [6] is different than the one provided [23]",
        "index_uuid" : "WBwrEA_LQw-blUsadB7vew",
        "shard" : "3",
        "index" : "index"
      }
    ],
    "type" : "version_conflict_engine_exception",
    "reason" : "[fulltext][1]: version conflict, current version [6] is different than the one provided [23]",
    "index_uuid" : "WBwrEA_LQw-blUsadB7vew",
    "shard" : "3",
    "index" : "index"
  },
  "status" : 409
}

外部版本必须大于当前版本才能更新成功:

[root@yyp ~]# curl -XPUT 'http://192.168.74.129:9200/index/fulltext/1?pretty&version=6&version_type=external' -d '{"curr":"external version"}'
{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[fulltext][1]: version conflict, current version [6] is higher or equal to the one provided [6]",
        "index_uuid" : "WBwrEA_LQw-blUsadB7vew",
        "shard" : "3",
        "index" : "index"
      }
    ],
    "type" : "version_conflict_engine_exception",
    "reason" : "[fulltext][1]: version conflict, current version [6] is higher or equal to the one provided [6]",
    "index_uuid" : "WBwrEA_LQw-blUsadB7vew",
    "shard" : "3",
    "index" : "index"
  },
  "status" : 409
}
[root@yyp ~]# 

【更新】或【删除】文档ElasticSearch不会立即将文档从磁盘中删除,只是将文档标记为已删除状态。随着你不断的索引更多的数据,Elasticsearch 将会在后台清理标记为已删除的文档。

Clone this wiki locally