Elasticsearch Multi Search(_msearch)

一、作用

  一次连接,执行多条query语句(单个api请求中执行多个搜索);与_mget指定id查询类似,可缩减网络开销;对于ES服务器端相对于同时收到多个请求,并没有减少太多查询耗时。

二、语法

1、请求格式(JSON)

在这里插入图片描述
如:
{}
{“query”:{}}
{}
{“query”:{}}



注意:

1、严格按照格式组装数据,一条语句一行,最后必须以换行符(\n)结尾
2、header和body是成对出现,即使header无内容也要保留“{}”并与换行结束

2、返回格式

{
“responses”:[,, …]
}

3、详见官网

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html#search-multi-search

三、DEMO

1、数据准备
// 创建两个索引,并添加数据
PUT my_test1
{
  "mappings": {
    "my_doc": {
      "properties": {
        "id": {
          "type": "text"
        },
        "name": {
          "type": "text"
        },
        "age": {
          "type": "integer"
        }
      }
    }
  }
}

PUT my_test2
{
  "mappings": {
    "my_doc": {
      "properties": {
        "id": {
          "type": "text"
        },
        "name": {
          "type": "text"
        },
        "age": {
          "type": "integer"
        }
      }
    }
  }
}

POST /my_test1/my_doc/1
{
  "id": "1",
  "name": "张三",
  "age": "20"
}
POST /my_test1/my_doc/2
{
  "id": "2",
  "name": "李四",
  "age": "21"
}
POST /my_test2/my_doc/3
{
  "id": "3",
  "name": "王五",
  "age": "25"
}
POST /my_test2/my_doc/4
{
  "id": "4",
  "name": "赵六",
  "age": "22"
}
2、指定index
// 查询
POST /my_test1/my_doc/_msearch
{}
{"query":{"match_all":{}}}
{}
{"query":{"match":{"name":"张三"}}}

// 结果:
{
  "responses": [
    {
      "took": 1,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
          {
            "_index": "my_test1",
            "_type": "my_doc",
            "_id": "2",
            "_score": 1,
            "_source": {
              "id": "2",
              "name": "李四",
              "age": "21"
            }
          },
          {
            "_index": "my_test1",
            "_type": "my_doc",
            "_id": "1",
            "_score": 1,
            "_source": {
              "id": "1",
              "name": "张三",
              "age": "20"
            }
          }
        ]
      },
      "status": 200
    },
    {
      "took": 1,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 0.51623213,
        "hits": [
          {
            "_index": "my_test1",
            "_type": "my_doc",
            "_id": "1",
            "_score": 0.51623213,
            "_source": {
              "id": "1",
              "name": "张三",
              "age": "20"
            }
          }
        ]
      },
      "status": 200
    }
  ]
}
3、多个index
// 查询
POST /_msearch
{"index":"my_test1"}
{"query":{"match_all":{}}}
{"index":"my_test2"}
{"query":{"match":{"name":"赵六"}}}

// 结果:
{
  "responses": [
    {
      "took": 1,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
          {
            "_index": "my_test1",
            "_type": "my_doc",
            "_id": "2",
            "_score": 1,
            "_source": {
              "id": "2",
              "name": "李四",
              "age": "21"
            }
          },
          {
            "_index": "my_test1",
            "_type": "my_doc",
            "_id": "1",
            "_score": 1,
            "_source": {
              "id": "1",
              "name": "张三",
              "age": "20"
            }
          }
        ]
      },
      "status": 200
    },
    {
      "took": 1,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 0.51623213,
        "hits": [
          {
            "_index": "my_test2",
            "_type": "my_doc",
            "_id": "4",
            "_score": 0.51623213,
            "_source": {
              "id": "4",
              "name": "赵六",
              "age": "22"
            }
          }
        ]
      },
      "status": 200
    }
  ]
}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值