1、multi-index和multi-type搜索模式
告诉你如何一次性搜索多个index和多个type下的数据
/_search:所有索引,所有type下的所有数据都搜索出来
/test_index/_search:指定一个index,搜索其下所有type的数据
/index1,index2/_search:同时搜索两个index下的数据
/*1,*2/_search:按照通配符去匹配多个索引
/index1/type1/_search:搜索一个index下指定的type的数据
/index1/type1,type2/_search:可以搜索一个index下多个type的数据
/index1,index2/type1,type2/_search:搜索多个index下的多个type的数据
/_all/type1,type2/_search:_all,可以代表搜索所有index下的指定type的数据
1查询所有索引
GET /_search
响应结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 11,
"successful": 11,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 14,
"max_score": 1,
"hits": [
{
"_index": ".kibana",
"_type": "config",
"_id": "5.6.0",
"_score": 1,
"_source": {
"buildNum": 15523
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_score": 1,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "10",
"_score": 1,
"_source": {
"test_field1": "test1",
"test_field2": "updated test2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "12",
"_score": 1,
"_source": {
"test_field": "test12"
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": 1,
"_source": {
"name": "jiajieshi yagao",
"desc": "youxiao fangzhu",
"price": 25,
"producer": "jiajieshi producer",
"tags": [
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_score": 1,
"_source": {
"name": "special yagao",
"desc": "special meibai",
"price": 50,
"producer": "special yagao producer",
"tags": [
"meibai"
]
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "4",
"_score": 1,
"_source": {
"test_field1": "test field111111"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "6",
"_score": 1,
"_source": {
"test_field": "test test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_score": 1,
"_source": {
"test_field": "replaced test2"
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 1,
"_source": {
"name": "gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
}
}
]
}
}
2查询指定的索引
GET /test_index/_search
响应结果
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_score": 1,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "10",
"_score": 1,
"_source": {
"test_field1": "test1",
"test_field2": "updated test2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "12",
"_score": 1,
"_source": {
"test_field": "test12"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "4",
"_score": 1,
"_source": {
"test_field1": "test field111111"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "6",
"_score": 1,
"_source": {
"test_field": "test test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_score": 1,
"_source": {
"test_field": "replaced test2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "7",
"_score": 1,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_score": 1,
"_source": {
"test_field1": "test field1",
"test_field2": "bulk test1"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "11",
"_score": 1,
"_source": {
"num": 1,
"tags": []
}
}
]
}
}
3同时搜索两个index下的数据
接下来的操作需要多个index,现在再创建一个test_index2
PUT /test_index2/test_type2/1
{
"test_field" : "111"
}
搜索
GET /test_index,test_index2/_search
响应结果
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 10,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_score": 1,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "10",
"_score": 1,
"_source": {
"test_field1": "test1",
"test_field2": "updated test2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "12",
"_score": 1,
"_source": {
"test_field": "test12"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "4",
"_score": 1,
"_source": {
"test_field1": "test field111111"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "6",
"_score": 1,
"_source": {
"test_field": "test test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_score": 1,
"_source": {
"test_field": "replaced test2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "7",
"_score": 1,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_score": 1,
"_source": {
"test_field1": "test field1",
"test_field2": "bulk test1"
}
},
{
"_index": "test_index2",
"_type": "test_type2",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "111"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "11",
"_score": 1,
"_source": {
"num": 1,
"tags": []
}
}
]
}
}
4按照通配符去匹配多个索引
再创建一个test_index3
PUT /test_index3/test_type3/1
{
"test_field" : "333"
}
查询
GET /*2,*3/_search
响应结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test_index2",
"_type": "test_type2",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "111"
}
},
{
"_index": "test_index3",
"_type": "test_type3",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "333"
}
}
]
}
}
5搜索一个index下指定的type的数据
GET /test_index/test_type/_search
响应结果
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_score": 1,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "10",
"_score": 1,
"_source": {
"test_field1": "test1",
"test_field2": "updated test2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "12",
"_score": 1,
"_source": {
"test_field": "test12"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "4",
"_score": 1,
"_source": {
"test_field1": "test field111111"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "6",
"_score": 1,
"_source": {
"test_field": "test test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_score": 1,
"_source": {
"test_field": "replaced test2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "7",
"_score": 1,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_score": 1,
"_source": {
"test_field1": "test field1",
"test_field2": "bulk test1"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "11",
"_score": 1,
"_source": {
"num": 1,
"tags": []
}
}
]
}
}
6可以搜索一个index下多个type的数据
再创建一个/test_index2/test_type3/1
PUT test_index2/test_type3/1
{
"test_filed" : "test_type333"
}
查询
GET /test_index2/test_type2,test_type3/_search
响应结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test_index2",
"_type": "test_type2",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "111"
}
},
{
"_index": "test_index2",
"_type": "test_type3",
"_id": "1",
"_score": 1,
"_source": {
"test_filed": "test_type333"
}
}
]
}
}
7搜索多个index下的多个type的数据
再创建一个/test_index3/test_type2/1
PUT test_index3/test_type2/1
{
"test_filed" : "test_type222"
}
查询
GET /test_index2,test_index3/test_type2,test_type3/_search
响应结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "test_index2",
"_type": "test_type2",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "111"
}
},
{
"_index": "test_index2",
"_type": "test_type3",
"_id": "1",
"_score": 1,
"_source": {
"test_filed": "test_type333"
}
},
{
"_index": "test_index3",
"_type": "test_type3",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "333"
}
},
{
"_index": "test_index3",
"_type": "test_type2",
"_id": "1",
"_score": 1,
"_source": {
"test_filed": "test_type222"
}
}
]
}
}
8搜索所有index下的指定type的数据
GET /_all/test_type2,test_type3/_search
响应结果
{
"took": 28,
"timed_out": false,
"_shards": {
"total": 21,
"successful": 21,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "test_index2",
"_type": "test_type2",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "111"
}
},
{
"_index": "test_index2",
"_type": "test_type3",
"_id": "1",
"_score": 1,
"_source": {
"test_filed": "test_type333"
}
},
{
"_index": "test_index3",
"_type": "test_type3",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "333"
}
},
{
"_index": "test_index3",
"_type": "test_type2",
"_id": "1",
"_score": 1,
"_source": {
"test_filed": "test_type222"
}
}
]
}
}
之前已经了解过,ES 里的 Index 可以看做一个库,而 Types 相当于表 这里 Types 的概念已经被逐渐弱化, Elasticsearch 6.X 中,一个 index 下已经只能包含一个 type, Elasticsearch 7.X 中, Type 的概念已经被删除了。
Elasticsearch 5.X依然可以使用multi-type搜索模式
9搜索指定index下所有type的数据
GET /test_index2/_search
响应结果
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test_index2",
"_type": "test_type2",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "111"
}
},
{
"_index": "test_index2",
"_type": "test_type3",
"_id": "1",
"_score": 1,
"_source": {
"test_filed": "test_type333"
}
}
]
}
}
2、初步图解一下简单的搜索原理
搜索原理初步图解
应用程序发送一个搜索请求,假如每个shard上都包含部分数据,会把请求打到所有的primary shard上,