> For the complete documentation index, see [llms.txt](https://hezhiqiang.gitbook.io/elasticsearch/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hezhiqiang.gitbook.io/elasticsearch/search/multi_index_multi_type.md).

# 多索引多类型

你是否注意到了《空白搜索》一章节的文档中包含了很多不同的类型 —— `user`与`tweet`，它们也分别来自`us`、`gb`这两个不同的索引？

当我们没有特别指定一个索引或者类型的时候，我们将会搜索整个集群中的**所有**文档。Elasticsearch会把搜索请求转发给集群中的每一个主从分片，然后按照结果的相关性得到前十名，并将它们返回给我们。

然而，往往我们只需要在某一个特定的索引的几个类型中进行搜索。我们可以通过在URL中定义它来实现这个功能：

| URL                         | 说明                                         |
| --------------------------- | ------------------------------------------ |
| `/_search`                  | 搜索所有的索引和类型                                 |
| `/gb/_search`               | 搜索索引`gb`中的所有类型                             |
| `/gb,us/_search`            | 搜索索引`gb`以及`us`中的所有类型                       |
| `/g*,u*/_search`            | 搜索所有以`g`或`u`开头的索引中的所有类型                    |
| `/gb/user/_search`          | 搜索索引`gb`中类型`user`内的所有文档                    |
| `/gb,us/user,tweet/_search` | 搜索索引`gb`和 索引`us`中类型`user`以及类型`tweet`内的所有文档 |
| `/_all/user,tweet/_search`  | 搜索所有索引中类型为`user`以及`tweet`内的所有文档            |

当你在一个索引中搜索的时候，Elasticsearch或将你的搜索请求转发给相应索引中的所有主从分片，然后收集每一个分片的结果。在多个索引中搜索也是相同的流程，只不过是增加了一些参与分片。

> ## 重要提示

搜索一个拥有五个主分片的索引与搜索五个都只拥有一个主分片是**完全一样**的。

在后面，你将会了解到如何利用这一点，来根据你的需要灵活打造系统。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hezhiqiang.gitbook.io/elasticsearch/search/multi_index_multi_type.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
