Elasticsearch 2.20 文档篇:多文档操作

    在Elasticsearch对文档的操作中,之前介绍的都是对单个文档进行操作,其实Elasticsearch同时可以对多个文档同时操作。下面介绍多文档查询。

多文档查询

    多文档查询可以在同一个接口中查询多个文档,可以分别指定index,type,id来进行多个文档的查询,响应包括所有查询到文档数组,每个元素在结构上类似于单个文档查询,例如:

请求:POST  http://localhost:9200/_mget?pretty

参数:

{
    "docs" : [
        {
            "_index" : "secilog",
            "_type" : "log",
            "_id" : "1"
        },
        {
            "_index" : "secilog",
            "_type" : "log",
            "_id" : "2"
        }
    ]
}

返回结果:

{
  "docs" : [ {
    "_index" : "secilog",
    "_type" : "log",
    "_id" : "1",
    "_version" : 3,
    "found" : true,
    "_source" : {
      "collect_type" : "syslog",
      "collect_date" : "2016-01-11T09:32:12",
      "message" : "Failed password for root from 192.168.21.2 port 50790 ssh2"
    }
  }, {
    "_index" : "secilog",
    "_type" : "log",
    "_id" : "2",
    "_version" : 1,
    "found" : true,
    "_source" : {
      "collect_type" : "syslog",
      "collect_date" : "2016-01-12T09:32:12",
      "message" : "secisland mget test!"
    }
  } ]
}

从中可以看出,一次查询了两个文档。

    在查询的时候,index,type可以在url中直接填写。例如下面两个请求和之前的是等价的。

请求:POST http://localhost:9200/secilog/_mget?pretty

参数:

{
    "docs" : [
        {
            "_type" : "log",
            "_id" : "1"
        },
        {
            "_type" : "log",
            "_id" : "2"
        }
    ]
}

请求:POST http://localhost:9200/secilog/log/_mget?pretty

参数:

{
    "docs" : [
        {
            "_id" : "1"
        },
        {
            "_id" : "2"
        }
    ]
}

对于上一种,可以用更加简化的方式查询:

请求:POST http://localhost:9200/secilog/log/_mget?pretty

参数:

{
    "ids" : ["1","2" ]
}

从上面的例子可以看出,Elasticsearch的多文档查询还是很灵活的。

type参数说明

    在多文档查询中,_type允许为空,当他设置为空或者_all的时候,系统会匹配第一个查询到的结果。如果不设置_type,当有许多文件有相同的_id的时候,系统最终得到的只有第一个匹配的文档。例如:

请求:POST http://localhost:9200/secilog/_mget?pretty

参数:

{
    "ids" : ["1","2" ]
}

上面的查询当有多个type中都有1,2id的时候,系统只会返回第一个找到的文档。如果想要多个,就需要把type在请求参数中指出来。

    默认情况下,_source字段将在每个文件中返回(如果存储)。类似单个文档的查询,可以在url中指定_source, _source_include 或者_source_exclude来对查询的结果进行过滤。例如:

请求:POST http://localhost:9200/secilog/log/_mget?pretty

参数:

{
    "docs" : [
        {
            "_id" : "1",
            "_source" : false
        },
        {
            "_id" : "2",
            "_source" : ["collect_type", "collect_date"]
        }
    ]
}

返回结果:

{
  "docs" : [ {
    "_index" : "secilog",
    "_type" : "log",
    "_id" : "1",
    "_version" : 3,
    "found" : true
  }, {
    "_index" : "secilog",
    "_type" : "log",
    "_id" : "2",
    "_version" : 1,
    "found" : true,
    "_source" : {
      "collect_date" : "2016-01-12T09:32:12",
      "collect_type" : "syslog"
    }
  } ]
}

类似单个文档查询,在请求的url中或者参数的docs中可以指定field,routing参数。

块操作

    快操作可以在一个接口中,处理个文档的内容,包括创建文档,删除文档,和修改文档。用块操作方式操作多个文档可以提高系统的效率。例如:

请求:POST http://localhost:9200/_bulk?pretty

参数:

{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "10" } }
{ "field1" : "value1" }
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "13" } }
{ "field1" : "value3" }
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "12" } }
}

返回结果:

{
  "took" : 1,
  "errors" : false,
  "items" : [ {
    "index" : {
      "_index" : "test",
      "_type" : "type1",
      "_id" : "10",
      "_version" : 6,
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "status" : 200
    }
  }, {
    "index" : {
      "_index" : "test",
      "_type" : "type1",
      "_id" : "13",
      "_version" : 1,
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "status" : 201
    }
  }, {
    "delete" : {
      "_index" : "test",
      "_type" : "type1",
      "_id" : "12",
      "_version" : 2,
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "status" : 404,
      "found" : false
    }
  } ]
}

    和批量查询类似, /_bulk, /{index}/_bulk, and {index}/{type}/_bulk这三种方式都可以执行,只需要在请求的参数中做出相应的对应。

 赛克蓝德(secisland)后续会逐步对Elasticsearch的最新版本的各项功能进行分析,近请期待。也欢迎加入secisland公众号进行关注

转载于:https://my.oschina.net/secisland/blog/614537

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值