Elasticsearch Multi Search(_msearch)

Elasticsearch Multi Search(_msearch)一、作用  一次连接,执行多条query语句(单个api请求中执行多个搜索);与_mget指定id查询类似,可缩减网络开销

一、作用

  一次连接,执行多条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
    }
  ]
}

今天的文章Elasticsearch Multi Search(_msearch)分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/69872.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注