elasticsearch之多索引查询

一、问题源起

在elasticsearch的查询中,我们一般直接通过URL来设置要search的index; 如果我们需要查询的索引比较多并且没有什么规律的话,就会面临一个尴尬的局面,超过URL的长度限制;

二、测试环境

elasticsearch 6.8.12

测试数据

新增三个测试的index,每个index里边一个document;

PUT test1/_doc/1
{
  "id":1,
  "name":"test1-1"
}


# {
#   "_index" : "test1",
#   "_type" : "_doc",
#   "_id" : "1",
#   "_version" : 1,
#   "result" : "created",
#   "_shards" : {
#     "total" : 2,
#     "successful" : 1,
#     "failed" : 0
#   },
#   "_seq_no" : 0,
#   "_primary_term" : 1
# }

PUT test2/_doc/1
{
  "id":1,
  "name":"test2-1"
}


# {
#   "_index" : "test2",
#   "_type" : "_doc",
#   "_id" : "1",
#   "_version" : 1,
#   "result" : "created",
#   "_shards" : {
#     "total" : 2,
#     "successful" : 1,
#     "failed" : 0
#   },
#   "_seq_no" : 0,
#   "_primary_term" : 1
# }

PUT test3/_doc/1
{
  "id":1,
  "name":"test3-1"
}

# {
#   "_index" : "test3",
#   "_type" : "_doc",
#   "_id" : "1",
#   "_version" : 1,
#   "result" : "created",
#   "_shards" : {
#     "total" : 2,
#     "successful" : 1,
#     "failed" : 0
#   },
#   "_seq_no" : 0,
#   "_primary_term" : 1
# }

三、URL中指定multi index

直接在URL中指定搜索特定的index

POST test1/_search 
{
    "query": {
        "match_all": {}
    }
}


# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 5,
#     "successful" : 5,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 1,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       }
#     ]
#   }
# }

可以通过都好分割同时搜索多个index;

POST test1,test2/_search
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 1,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 10,
#     "successful" : 10,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 2,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       },
#       {
#         "_index" : "test2",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test2-1"
#         }
#       }
#     ]
#   }
# }

我们可以使用关键字_all指定搜索所有的index;

POST _all/_search 
{
    "query": {
        "match_all": {}
    }
}

{
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 15,
#     "successful" : 15,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 3,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       },
#       {
#         "_index" : "test2",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test2-1"
#         }
#       },
#       {
#         "_index" : "test3",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test3-1"
#         }
#       }
#     ]
#   }
# }

也可以使用通配符*来匹配一些名字有共同特征的index;

POST test*/_search
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 1,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 15,
#     "successful" : 15,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 3,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       },
#       {
#         "_index" : "test2",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test2-1"
#         }
#       },
#       {
#         "_index" : "test3",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test3-1"
#         }
#       }
#     ]
#   }
# }

还可以使用-来排除某个index;

POST test*,-test2/_search
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 10,
#     "successful" : 10,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 2,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       },
#       {
#         "_index" : "test3",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test3-1"
#         }
#       }
#     ]
#   }
# }

四、URL中multi index的一些控制选项

如果我们显示search一个不存在的或者关闭的index就会报错;

POST test4/_search
{
    "query": {
        "match_all": {}
    }
}


# {
#   "error" : {
#     "root_cause" : [
#       {
#         "type" : "index_not_found_exception",
#         "reason" : "no such index",
#         "resource.type" : "index_or_alias",
#         "resource.id" : "test4",
#         "index_uuid" : "_na_",
#         "index" : "test4"
#       }
#     ],
#     "type" : "index_not_found_exception",
#     "reason" : "no such index",
#     "resource.type" : "index_or_alias",
#     "resource.id" : "test4",
#     "index_uuid" : "_na_",
#     "index" : "test4"
#   },
#   "status" : 404
# }

POST test3/_close
# 
# {
#   "acknowledged" : true
# }

POST test3/_search
{
    "query": {
        "match_all": {}
    }
}


# {
#   "error": {
#     "root_cause": [
#       {
#         "type": "index_closed_exception",
#         "reason": "closed",
#         "index_uuid": "KI7Iv4eGRIOk6MsycXokNQ",
#         "index": "test3"
#       }
#     ],
#     "type": "index_closed_exception",
#     "reason": "closed",
#     "index_uuid": "KI7Iv4eGRIOk6MsycXokNQ",
#     "index": "test3"
#   },
#   "status": 400
# }

我们可以使用ignore_unavailable来忽略不存在或者关闭的index;


POST test4/_search?ignore_unavailable=true
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 0,
#     "successful" : 0,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 0,
#     "max_score" : 0.0,
#     "hits" : [ ]
#   }
# }


POST test3/_search?ignore_unavailable=true
{
    "query": {
        "match_all": {}
    }
}


# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 0,
#     "successful" : 0,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 0,
#     "max_score" : 0.0,
#     "hits" : [ ]
#   }
# }

如果通过通配符、_all隐式的指定search的index,如果不存在则默认不会报错,不过可以通过allow_no_indices=false来让elasticsearch报错;

POST noexist*/_search
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 0,
#     "successful" : 0,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 0,
#     "max_score" : 0.0,
#     "hits" : [ ]
#   }
# }


POST noexist*/_search?allow_no_indices=false
{
    "query": {
        "match_all": {}
    }
}

# {
#   "error" : {
#     "root_cause" : [
#       {
#         "type" : "index_not_found_exception",
#         "reason" : "no such index",
#         "resource.type" : "index_or_alias",
#         "resource.id" : "noexist*",
#         "index_uuid" : "_na_",
#         "index" : "noexist*"
#       }
#     ],
#     "type" : "index_not_found_exception",
#     "reason" : "no such index",
#     "resource.type" : "index_or_alias",
#     "resource.id" : "noexist*",
#     "index_uuid" : "_na_",
#     "index" : "noexist*"
#   },
#   "status" : 404
# }



POST test3*/_search
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 0,
#     "successful" : 0,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 0,
#     "max_score" : 0.0,
#     "hits" : [ ]
#   }
# }

POST test3*/_search?allow_no_indices=false
{
    "query": {
        "match_all": {}
    }
}

# {
#   "error" : {
#     "root_cause" : [
#       {
#         "type" : "index_not_found_exception",
#         "reason" : "no such index",
#         "resource.type" : "index_or_alias",
#         "resource.id" : "test3*"
#       }
#     ],
#     "type" : "index_not_found_exception",
#     "reason" : "no such index",
#     "resource.type" : "index_or_alias",
#     "resource.id" : "test3*"
#   },
#   "status" : 404
# }


我们也可以使用expand_wildcards来控制展开哪些index,可选值open、closed、none、all;

默认只扩展open;

POST test*/_search
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 10,
#     "successful" : 10,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 2,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       },
#       {
#         "_index" : "test2",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test2-1"
#         }
#       }
#     ]
#   }
# }


POST test*/_search?expand_wildcards=all
{
    "query": {
        "match_all": {}
    }
}

# {
#   "error": {
#     "root_cause": [
#       {
#         "type": "index_closed_exception",
#         "reason": "closed",
#         "index_uuid": "KI7Iv4eGRIOk6MsycXokNQ",
#         "index": "test3"
#       }
#     ],
#     "type": "index_closed_exception",
#     "reason": "closed",
#     "index_uuid": "KI7Iv4eGRIOk6MsycXokNQ",
#     "index": "test3"
#   },
#   "status": 400
# }

POST test*/_search?expand_wildcards=all&ignore_unavailable=true
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 10,
#     "successful" : 10,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 2,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       },
#       {
#         "_index" : "test2",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test2-1"
#         }
#       }
#     ]
#   }
# }

五、使用index aliases封装物理index

aliases是物理索引的别名,请求api的时候,elasticsearch会自动将aliases转化为对应的物理index name;

别名既可以映射到某个特定的index,也可以映射到多个index;

别名也可以同时应用过滤条件,实现只对index的局部数据进行搜索;

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test*", "alias" : "all_test_indices" } }
    ]
}

# {
#   "acknowledged" : true
# }

POST all_test_indices/_search
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 10,
#     "successful" : 10,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 2,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       },
#       {
#         "_index" : "test2",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test2-1"
#         }
#       }
#     ]
#   }
# }

六、multi search–通过body指定index

Multi Search API的主要目的是实现在一个API里边实现多个search请求,其通过如下格式分别通过header指定index,body指定查询语句;

header\n
body\n
header\n
body\n

Multi Search API除了与前两者具有相同的指定index name的能力,最大的优势就是通过body传递index name,轻松突破URL的长度限制的局限性;

还有一点就是Multi Search API支持大量的没有特定规律的index name,例如跟时间序列有关的index name等;

GET _msearch
{"index":"test*"}
{"query" : {"match_all" : {}}}

# {
#   "responses" : [
#     {
#       "took" : 0,
#       "timed_out" : false,
#       "_shards" : {
#         "total" : 10,
#         "successful" : 10,
#         "skipped" : 0,
#         "failed" : 0
#       },
#       "hits" : {
#         "total" : 2,
#         "max_score" : 1.0,
#         "hits" : [
#           {
#             "_index" : "test1",
#             "_type" : "_doc",
#             "_id" : "1",
#             "_score" : 1.0,
#             "_source" : {
#               "id" : 1,
#               "name" : "test1-1"
#             }
#           },
#           {
#             "_index" : "test2",
#             "_type" : "_doc",
#             "_id" : "1",
#             "_score" : 1.0,
#             "_source" : {
#               "id" : 1,
#               "name" : "test2-1"
#             }
#           }
#         ]
#       },
#       "status" : 200
#     }
#   ]
# }

### 回答1: 要实现 Elasticsearch索引联合查询,可以使用 Elasticsearch 的多索引查询功能。具体来说,可以使用 Elasticsearch 的 Multi-Search API 进行多个查询操作,然后将结果合并起来返回给用户。 以下是一个简单的示例代码,假设有两个索引 index1 和 index2,需要联合查询: ``` POST /_msearch {} {"index": "index1"} {"query": {"match_all": {}}} {} {"index": "index2"} {"query": {"match_all": {}}} ``` 上述代码中,`_msearch` 是 Multi-Search API,`index1` 和 `index2` 是要查询的两个索引,`match_all` 是一个简单的查询语句,表示匹配所有文档。查询结果会按照查询顺序依次返回,需要自行解析和处理。 需要注意的是,多索引联合查询可能会带来一些性能问题,特别是在大数据量场景下。因此,需要根据实际情况进行权衡和优化。 ### 回答2: Elasticsearch是一个分布式搜索引擎,可以用于存储、搜索和分析大规模的数据集合。在Elasticsearch中,我们可以使用多索引联合查询来实现对多个索引中的数据进行查询和分析。 多索引联合查询Elasticsearch中非常常见和重要。当我们有多个索引,每个索引包含不同类型或字段的数据时,我们可以使用多索引联合查询来同时搜索这些索引,并获取跨多个索引的结果。 使用多索引联合查询的步骤如下: 1. 创建索引:首先,我们需要创建多个索引,并将不同类型或字段的数据分别存储在这些索引中。 2. 查询语句:在进行多索引联合查询之前,我们需要构建一个查询语句。查询语句可以使用Elasticsearch提供的查询DSL(Domain Specific Language)来编写,通过指定不同的索引名称、查询条件和过滤条件来实现。 3. 查询执行:一旦查询语句准备好,我们可以将其发送到Elasticsearch服务器进行查询执行。Elasticsearch会同时搜索多个索引,并返回跨多个索引的结果。 4. 结果处理:最后,我们可以对查询结果进行处理和分析。可以根据需要,对结果进行排序、筛选、聚合等操作。 多索引联合查询在实际的应用场景中非常有用。例如,当我们的数据被分散存储在不同的索引中,需要同时查询和分析这些数据时,我们可以使用多索引联合查询来快速获取所需的结果。 总结而言,Elasticsearch提供了多索引联合查询的功能,可以方便地搜索和分析跨多个索引的数据。通过构建查询语句、执行查询并处理结果,我们可以快速获取我们所需的数据。 ### 回答3: Elasticsearch是一种开源的分布式搜索引擎,它可用于实现全文搜索、日志分析、数据可视化和实时数据分析等功能。在Elasticsearch中,可以通过多索引联合查询来同时搜索多个索引并获取结果。 多索引联合查询可以通过以下几种方式实现: 1. 使用多个索引名称:可以在查询语句中指定多个索引名称,用逗号分隔。例如,可以使用以下语句同时查询index1和index2两个索引: GET index1,index2/_search { "query": { "match": { "field": "value" } } } 2. 使用通配符查询多个索引:可以使用通配符在查询语句中匹配多个索引名称。例如,可以使用以下语句查询所有以"index"开头的索引: GET index*/_search { "query": { "match": { "field": "value" } } } 3. 使用别名查询多个索引:在创建索引时,可以为索引设置一个别名,然后在查询中使用别名来查询多个索引。例如,可以使用以下语句创建两个索引并为它们设置别名: PUT index1/_alias/myalias PUT index2/_alias/myalias 然后,可以使用以下语句查询myalias别名所对应的索引: GET myalias/_search { "query": { "match": { "field": "value" } } } 多索引联合查询可以帮助我们在一个请求中同时搜索多个索引,提高查询效率和性能。在进行多索引联合查询时,需要注意索引之间的数据结构和映射是否一致,以保证查询结果的准确性和一致性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值