본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Multi-get Documents API

원문 보기 위키 갱신

Multi-get Documents API는 하나의 요청으로 하나 이상의 인덱스에서 여러 문서를 한 번에 조회하는 API예요. 개별 GET 요청을 여러 번 보내는 것보다 효율적인데, 네트워크 오버헤드를 줄이고 여러 작업을 클러스터로 가는 한 번의 왕복(round-trip)으로 묶어주기 때문이에요. 이 기능은 OpenSearch 1.0에서 처음 소개되었어요.

이 API는 조회하려는 문서의 ID를 이미 알고 있고, 특정 문서들을 가져와야 할 때 사용해요. 흔한 시나리오는 다음과 같아요:

  • 알려진 ID 목록을 기반으로 사용자 프로필, 상품 상세, 또는 기타 엔티티 배치를 조회할 때
  • 서로 다른 인덱스에서 관련 문서를 한 번에 가져올 때(예: 주문 기록과 그에 연결된 고객 정보를 동시에 조회)

출처: OpenSearch 문서

본문

  • 각 문서에 대해 반환할 필드를 제어하면서 여러 문서를 조회해야 하는 효율적인 데이터 접근 패턴을 구현할 때

부분 응답(Partial responses)

Multi-get Documents API는 빠른 응답을 우선시해요. 작업 중 하나 이상의 샤드가 실패하면 부분 결과를 반환해요. 샤드 실패로 특정 문서를 가져올 수 없거나 문서가 존재하지 않으면, 그 문서에 대한 오류 정보를 응답에 포함하면서도 성공적으로 조회된 문서는 그대로 반환해요. 즉 일시적인 실패나 누락된 문서 때문에 전체 작업이 막히지 않도록 해주는 것이에요.

엔드포인트

GET  /_mget
POST /_mget
GET  /{index}/_mget
POST /{index}/_mget

경로 매개변수

다음 표는 사용 가능한 경로 매개변수 목록이에요. 모든 경로 매개변수는 선택 사항이에요.

| Parameter | Data type | Description | | index | String | The name of the index to retrieve documents from when ids are specified, or when a document in the docs array does not specify an index. |

쿼리 매개변수

다음 표는 사용 가능한 쿼리 매개변수 목록이에요. 모든 쿼리 매개변수는 선택 사항이에요.

| Parameter | Data type | Description | Default | | _source | Boolean or List or String | Set to true or false to return the _source field or not, or a list of fields to return. | N/A | | _source_excludes | List or String | A comma-separated list of source fields to exclude from the response. You can also use this parameter to exclude fields from the subset specified in _source_includes query parameter. | N/A | | _source_includes | List or String | A comma-separated list of source fields to include in the response. If this parameter is specified, only these source fields are returned. You can exclude fields from this subset using the _source_excludes query parameter. If the _source parameter is false, this parameter is ignored. | N/A | | preference | String | Specifies the node or shard the operation should be performed on. Random by default. | random | | realtime | Boolean | If true, the request is real time as opposed to near real time. | N/A | | refresh | Boolean or String | If true, the request refreshes relevant shards before retrieving documents. Valid values are: - false: Do not refresh the affected shards. - true: Refresh the affected shards immediately. - wait_for: Wait for the changes to become visible before replying. | N/A | | routing | List or String | A custom value used to route operations to a specific shard. | N/A | | stored_fields | List or String | If true, retrieves the document fields stored in the index rather than the document _source. | N/A |

요청 본문 필드

요청 본문은 조회할 문서를 지정해요. 요청 경로에 인덱스를 지정하지 않았다면 요청 본문의 각 문서에 인덱스 이름을 반드시 포함해야 해요. 다음 표는 사용 가능한 요청 본문 필드를 보여줘요.

| Field | Data type | Description | | docs | Array of objects | The documents to retrieve. Required if the ids field is not specified. Each object can include the following fields: _index, _id, routing, _source, and stored_fields. | | docs._index | String | The name of the index containing the document. Required if an index is not specified in the request path. | | docs._id | String | The document ID. Required. | | docs.routing | String | The routing value used to route the operation to a specific shard. Required if a custom routing value was used when indexing the document. | | docs._source | Boolean, Array, or Object | Controls which source fields are returned. If false, the _source field is excluded from the response. If an array, specifies the fields to include. If an object, can contain includes and excludes arrays to control field inclusion and exclusion. Default is true. | | docs._source.includes | Array of strings | The source fields to include in the response. For example, ["title", "author"] returns only the title and author fields. | | docs._source.excludes | Array of strings | The source fields to exclude from the response. For example, ["internal_notes"] excludes the internal_notes field. | | docs.stored_fields | Array of strings | The stored fields to retrieve instead of the _source field. Only fields explicitly stored in the index mapping can be retrieved. If specified, the _source field is not returned unless explicitly requested. | | ids | Array of strings | A simplified way to specify document IDs when all documents are in the same index. Can only be used when an index is specified in the request path. If provided, the docs field is not required. |

예제: 여러 인덱스에서 문서 조회

다음 예제는 books 인덱스에서 문서 하나, articles 인덱스에서 문서 하나를 조회해요:

GET /_mget
{
"docs": [
{
"_index": "books",
"_id": "1"
},
{
"_index": "articles",
"_id": "1"
}
]
}
response = client.mget(
body =   {
"docs": [
{
"_index": "books",
"_id": "1"
},
{
"_index": "articles",
"_id": "1"
}
]
}
)

예제: ids 배열 사용

같은 인덱스에서 여러 문서를 조회할 때는 경로에 인덱스를 지정하고 ids 배열을 사용하면 요청을 간단히 할 수 있어요. 다음 예제는 books 인덱스에서 문서 3개를 조회해요:

GET /books/_mget
{
"ids": [
"1",
"2",
"3"
]
}
response = client.mget(
index = "books",
body =   {
"ids": [
"1",
"2",
"3"
]
}
)

예제: 소스 필드 필터링

_source 매개변수를 사용해 각 문서에서 반환할 필드를 제어할 수 있어요. 다음 예제는 다양한 소스 필터링 옵션을 보여줘요: 첫 번째 문서는 소스를 완전히 제외하고, 두 번째 문서는 특정 필드만 반환하며, 세 번째 문서는 includes를 사용해 선택한 필드만 반환해요:

GET /_mget
{
"docs": [
{
"_index": "books",
"_id": "1",
"_source": false
},
{
"_index": "books",
"_id": "2",
"_source": [
"title",
"author"
]
},
{
"_index": "books",
"_id": "3",
"_source": {
"includes": [
"title",
"year"
]
}
}
]
}
response = client.mget(
body =   {
"docs": [
{
"_index": "books",
"_id": "1",
"_source": false
},
{
"_index": "books",
"_id": "2",
"_source": [
"title",
"author"
]
},
{
"_index": "books",
"_id": "3",
"_source": {
"includes": [
"title",
"year"
]
}
}
]
}
)

예제: 저장된 필드(stored fields) 조회

인덱스 매핑에 저장된 필드가 있다면 문서 소스 대신 저장된 필드를 조회할 수 있어요. 다음 예제는 두 사용자 문서에서 서로 다른 저장된 필드를 조회해요:

GET /_mget
{
"docs": [
{
"_index": "users",
"_id": "1",
"stored_fields": [
"name",
"location"
]
},
{
"_index": "users",
"_id": "2",
"stored_fields": [
"email",
"joined_date"
]
}
]
}
response = client.mget(
body =   {
"docs": [
{
"_index": "users",
"_id": "1",
"stored_fields": [
"name",
"location"
]
},
{
"_index": "users",
"_id": "2",
"stored_fields": [
"email",
"joined_date"
]
}
]
}
)

예제: 라우팅 값 지정

문서를 색인할 때 커스텀 라우팅을 사용했다면, 문서를 조회할 때도 반드시 라우팅 값을 제공해야 해요. 다음 예제는 두 주문 문서를 라우팅 값을 사용해 조회해요. 첫 번째 문서는 쿼리 매개변수의 라우팅 값을 사용하고, 두 번째 문서는 자신만의 라우팅 값을 지정해요:

GET /_mget?routing=user123
{
"docs": [
{
"_index": "orders",
"_id": "1"
},
{
"_index": "orders",
"_id": "2",
"routing": "user456"
}
]
}
response = client.mget(
params = { "routing": "user123" },
body =   {
"docs": [
{
"_index": "orders",
"_id": "1"
},
{
"_index": "orders",
"_id": "2",
"routing": "user456"
}
]
}
)

예제 응답

Multi-get Documents API는 요청한 순서와 동일한 순서로 조회된 문서를 담은 docs 배열을 반환해요. 각 문서에는 메타데이터와 소스 데이터가 포함되고, 문서를 조회하지 못했을 경우 오류가 포함돼요.

{
"docs": [
{
"_index": "books",
"_id": "1",
"_version": 2,
"_seq_no": 2,
"_primary_term": 3,
"found": true,
"_source": {
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"year": 1925,
"genre": "Fiction",
"pages": 180
}
},
{
"_index": "articles",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"title": "Introduction to OpenSearch",
"author": "Jane Smith",
"published": "2024-01-15",
"category": "Technology",
"views": 1500
}
}
]
}
{
"docs": [
{
"_index": "books",
"_id": "1",
"_version": 2,
"_seq_no": 2,
"_primary_term": 3,
"found": true,
"_source": {
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"year": 1925,
"genre": "Fiction",
"pages": 180
}
},
{
"_index": "books",
"_id": "2",
"_version": 2,
"_seq_no": 3,
"_primary_term": 3,
"found": true,
"_source": {
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"year": 1960,
"genre": "Fiction",
"pages": 324
}
},
{
"_index": "books",
"_id": "3",
"_version": 1,
"_seq_no": 4,
"_primary_term": 3,
"found": true,
"_source": {
"title": "1984",
"author": "George Orwell",
"year": 1949,
"genre": "Dystopian",
"pages": 328
}
}
]
}
{
"docs": [
{
"_index": "books",
"_id": "1",
"_version": 2,
"_seq_no": 2,
"_primary_term": 3,
"found": true
},
{
"_index": "books",
"_id": "2",
"_version": 2,
"_seq_no": 3,
"_primary_term": 3,
"found": true,
"_source": {
"author": "Harper Lee",
"title": "To Kill a Mockingbird"
}
},
{
"_index": "books",
"_id": "3",
"_version": 1,
"_seq_no": 4,
"_primary_term": 3,
"found": true,
"_source": {
"year": 1949,
"title": "1984"
}
}
]
}
{
"docs": [
{
"_index": "users",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"fields": {
"name": [
"Alice Johnson"
],
"location": [
"San Francisco"
]
}
},
{
"_index": "users",
"_id": "2",
"_version": 1,
"_seq_no": 1,
"_primary_term": 1,
"found": true,
"fields": {
"joined_date": [
"2021-06-20T00:00:00.000Z"
],
"email": [
"bob@example.com"
]
}
}
]
}
{
"docs": [
{
"_index": "orders",
"_id": "1",
"_version": 2,
"_seq_no": 3,
"_primary_term": 14,
"_routing": "user123",
"found": true,
"_source": {
"order_id": "ORD-001",
"user_id": "user123",
"product": "Laptop",
"amount": 1299.99
}
},
{
"_index": "orders",
"_id": "2",
"_version": 2,
"_seq_no": 4,
"_primary_term": 14,
"_routing": "user456",
"found": true,
"_source": {
"order_id": "ORD-002",
"user_id": "user456",
"product": "Smartphone",
"amount": 899.99
}
}
]
}

응답 본문 필드

응답은 요청 시 각 문서에 대해 하나의 요소를 가진 docs 배열을 포함하며, 요청 순서와 동일한 순서로 반환돼요. 다음 표는 응답 본문 필드를 보여줘요.

| Field | Data type | Description | | docs | Array of objects | The retrieved documents. Each object represents one document and includes the fields described in this table. | | _index | String | The name of the index containing the document. | | _id | String | The document ID. | | _version | Integer | The document version number. This number is incremented each time the document is updated. | | _seq_no | Integer | The sequence number assigned to the document when it was indexed. Used for optimistic concurrency control. | | _primary_term | Integer | The primary term assigned to the document when it was indexed. Used with _seq_no for optimistic concurrency control. | | found | Boolean | Whether the document was found. If false, the document does not exist, and the _source field is not included. | | _source | Object | The document’s original JSON content. Omitted if found is false, if _source was set to false in the request, or if stored_fields was specified. | | fields | Object | The stored fields for the document. Only included when stored_fields is specified in the request and found is true. Each field value is returned as an array. | | _routing | String | The routing value used to direct the document to a specific shard. Only included if a custom routing value was used. | | error | Object | Error information if the document could not be retrieved due to a failure. Contains details about the error type and reason. |

필요한 권한

Security 플러그인을 사용한다면 적절한 권한이 있는지 확인해 주세요: indices:data/read/mget and indices:data/read/mget*.

더 알아보기 (Learn more)

여러 문서를 일괄로 다루는 더 다양한 기법을 알고 싶다면 원문 OpenSearch 문서를 확인해 주세요.