1.multi-index&multi-type搜索模式
使用 GET /_search 可以将所有索引,所有type下的所有数据搜索出来;也可以指定在某个索引下,某个type下进行搜索数据 GET /test_index/_search
同时,我们可以进行多个索引下的搜索
1):新建一个索引数据:
PUT /test_index1/test_type/100
{
"name":"hello123"
}
这样,我们就有两个索引了test_index1和test_index
我们可以这样进行查询,这就是multi-index
GET /test_index,test_index1/_search
查询结果:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 15,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "10",
"_score": 1,
"_source": {
"test_field": "test_201712291052",
"test_field2": "test002"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "QIBLoGABN5gu7um0xJGZ",
"_score": 1,
"_source": {
"ids": [
1,
2
]
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_score": 1,
"_source": {
"test_field": "xxx81",
"test_field2": "xxx00000000"
}
},
{
"_index": "test_index1",
"_type": "test_type",
"_id": "100",
"_score": 1,
"_source": {
"name": "hello123"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_score": 1,
"_source": {
"test_field": "xxxxx000"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "Q4BLoGABN5gu7um0_ZEr",
"_score": 1,
"_source": {
"ids": [
1,
2,
10
]
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "RIBLoGABN5gu7um0_pFy",
"_score": 1,
"_source": {
"ids": [
1,
2,
10
]
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "xxx",
"test_field2": "xxx2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "7",
"_score": 1,
"_source": {
"test_field": "xxx71"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "QoBLoGABN5gu7um0-ZGx",
"_score": 1,
"_source": {
"ids": [
1,
2,
10
]
}
}
]
}
}
2):我们可以搜索以test_index开头的所有索引的数据
新增一个test_index3的数据
PUT /test_index3/test_type/99
{
"test":"test123"
}
执行下面的命令
GET /test_*/_search
执行结果:可以看到查询出了test_index,test_index1,test_index3的所有的数据了,(数据太多,删除了一些)
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 15,
"successful": 15,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 17,
"max_score": 1,
"hits": [
{
"_index": "test_index3",
"_type": "test_type",
"_id": "99",
"_score": 1,
"_source": {
"test": "test123"
}
},
{
"_index": "test_index1",
"_type": "test_type",
"_id": "100",
"_score": 1,
"_source": {
"name": "hello123"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_score": 1,
"_source": {
"test_field": "xxxxx000"
}
},
}
]
}
}
3)搜索一个index下指定的type的数据
GET /test_index3/test_type/_search
执行结果:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test_index3",
"_type": "test_type",
"_id": "99",
"_score": 1,
"_source": {
"test": "test123"
}
},
{
"_index": "test_index3",
"_type": "test_type",
"_id": "1",
"_score": 1,
"_source": {
"test": "test123"
}
}
]
}
}
4).搜索一个index下多个type的数据
GET /index/type1,type2/_search
例子:
GET /test_index/test_type,my_testtype/_search
执行结果:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "zhejiang"
}
},
{
"_index": "test_index",
"_type": "my_testtype",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "test1"
}
}
]
}
}
5).搜索一个index匹配的type的数据
GET /index/*1,*2/_search
6).搜索多个index下多个type的数据
GET /index1,index2/type1,type2/_search
例子:
GET /test_index,test_index1/test_type,my_testtype/_search
执行结果:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "zhejiang"
}
},
{
"_index": "test_index",
"_type": "my_testtype",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "test1"
}
},
{
"_index": "test_index1",
"_type": "test_type",
"_id": "1",
"_score": 1,
"_source": {
"name": "hello123"
}
}
]
}
}
7).搜索所有的index下制定type的数据
GET /_all/type1,type2/_search
例子:
GET /_all/test_type,my_testtype/_search
执行结果
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 16,
"successful": 16,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "zhejiang"
}
},
{
"_index": "test_index",
"_type": "my_testtype",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "test1"
}
},
{
"_index": "test_index1",
"_type": "test_type",
"_id": "1",
"_score": 1,
"_source": {
"name": "hello123"
}
},
{
"_index": "text_index",
"_type": "test_type",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "test2"
}
}
]
}
}
8).注意,在新版本6.1.1中,不能再同一个index中插入多个type
2.搜索请求的原理
client发送一个搜索请求,会把请求打到所有的primary shard上去执行,因为每个shard都包含部分数据,所以每个shard上都可能会包含搜索请求的结果
但是如果primary shard有replica shard,那么请求可能打到replica shard上去。
如果有可能命中的shard:R0,P1,P2,也可能是P0,R1,R2等等