Index 매핑 파라미터
index 매핑 파라미터는 필드가 역색인에 포함되는지 여부를 제어해요. true로 설정하면 필드가 인덱싱되어 쿼리에서 사용 가능하고, false로 설정하면 필드는 문서에 저장되지만 인덱싱되지 않아 검색이 불가능해져요.
출처: 문서
본문
index 매핑 파라미터는 필드가 역색인에 포함되는지 여부를 제어합니다. true로 설정하면 필드가 인덱싱되어 쿼리에서 사용할 수 있습니다. false로 설정하면 필드는 문서에 저장되지만 인덱싱되지 않으므로 doc_values가 활성화되지 않은 경우 검색할 수 없습니다. 특정 필드를 검색할 필요가 없다면 해당 필드의 인덱싱과 doc_values를 비활성화해 인덱스 크기를 줄이고 인덱싱 성능을 향상시킬 수 있습니다. 예를 들어 표시용으로만 사용되는 큰 text 필드나 메타데이터에서 인덱싱을 비활성화할 수 있습니다.
기본적으로 모든 필드 유형이 인덱싱됩니다.
index와 doc values 파라미터 비교
index 파라미터를 활성화하면 OpenSearch는 용어를 포함하는 문서에 매핑하는 매핑을 생성합니다. 각 새 문서에 대해 인덱싱된 필드의 값이 용어로 분해되고, 각 용어는 매핑에서 문서 ID로 연결됩니다.
doc_values 파라미터를 활성화하면 OpenSearch는 역방향 매핑을 생성합니다. 각 문서가 해당 필드에서 발견된 용어 목록에 연결됩니다. 이는 시스템이 문서의 필드 값에 빠르게 접근해야 하는 정렬 같은 연산에 유용합니다.
다음 표는 index와 doc_values의 조합에 따른 필드 동작을 보여 줍니다.
| index 파라미터 값 | doc_values 파라미터 값 | 동작 | 사용 사례 |
|---|---|---|---|
true |
true |
필드가 검색 가능하며 정렬, 스크립팅, 집계를 지원합니다. | 직접 쿼리하고 복잡한 연산을 수행하려는 필드에 사용합니다. |
true |
false |
필드가 검색 가능하지만 문서-용어 조회를 지원하지 않습니다(따라서 정렬, 스크립팅, 집계가 더 오래 걸립니다). | 쿼리는 하되 정렬이나 집계에는 필요 없는 필드(예: text 필드)에 사용합니다. |
false |
true |
필드가 검색 가능하며(효율성은 낮지만) 정렬, 스크립팅, 집계를 지원합니다. 모든 필드 유형이 doc_values를 지원하는 것은 아닙니다(예: text 필드는 doc_values를 지원하지 않음). | 필터나 쿼리 없이 집계만 하려는 필드에 사용합니다. |
false |
false |
필드가 검색 불가능합니다. 이 필드를 검색하려는 쿼리는 오류를 반환합니다. | 메타데이터 필드처럼 연산을 수행하고 싶지 않은 필드에 사용합니다. |
지원되는 데이터 유형
index 매핑 파라미터는 다음 데이터 유형에 적용할 수 있습니다.
- Text
- Keyword
- Boolean
- IP address
- Date 필드 유형
- Numeric 필드 유형
필드에서 인덱싱 활성화
다음 요청은 인덱싱(기본 동작)되는 description과 name 필드가 있는 products라는 인덱스를 만듭니다.
PUT /products
{
"mappings": {
"properties": {
"description": {
"type": "text"
},
"name": {
"type": "keyword"
}
}
}
}
다음 요청을 사용해 문서를 인덱싱합니다.
PUT /products/_doc/1
{
"description": "This product has a searchable description.",
"name": "doc1"
}
description 필드를 쿼리합니다.
POST /products/_search
{
"query": {
"match": {
"description": "searchable"
}
}
}
다음 응답은 인덱싱된 문서가 쿼리와 성공적으로 매칭되었음을 확인합니다.
{
...
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.13076457,
"hits": [
{
"_index": "products",
"_id": "1",
"_score": 0.13076457,
"_source": {
"description": "This product has a searchable description.",
"name": "doc1"
}
}
]
}
}
필드에서 인덱싱 비활성화
인덱싱되지 않는 description 필드와 name 필드가 있는 products-no-index라는 인덱스를 만듭니다.
PUT /products-no-index
{
"mappings": {
"properties": {
"description": {
"type": "text",
"index": false
},
"name": {
"type": "keyword",
"index": false
}
}
}
}
다음 요청을 사용해 문서를 인덱싱합니다.
PUT /products-no-index/_doc/1
{
"description": "This product has a non-searchable description.",
"name": "doc1"
}
description 필드로 products-no-index를 쿼리합니다.
POST /products-no-index/_search
{
"query": {
"match": {
"description": "non-searchable"
}
}
}
다음 오류 응답은 description 필드가 인덱싱되지 않았기 때문에 검색 쿼리가 실패했음을 나타냅니다.
{
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "failed to create query: Cannot search on field [description] since it is not indexed.",
"index": "products-no-index",
"index_uuid": "yX2F4En1RqOBbf3YWihGCQ"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "products-no-index",
"node": "0tmy2tf7TKW8qCmya9sG2g",
"reason": {
"type": "query_shard_exception",
"reason": "failed to create query: Cannot search on field [description] since it is not indexed.",
"index": "products-no-index",
"index_uuid": "yX2F4En1RqOBbf3YWihGCQ",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Cannot search on field [description] since it is not indexed."
}
}
}
]
},
"status": 400
}
text 필드의 경우 index 파라미터를 false로 설정하면 text 필드가 doc_values를 지원하지 않으므로 필드 검색이 비활성화됩니다. 다른 필드를 검색 불가능하게 만들려면 doc_values도 false로 추가로 설정해야 합니다.
name 필드로 products-no-index를 쿼리합니다.
POST /products-no-index/_search
{
"query": {
"term": {
"name": {
"value": "doc1"
}
}
}
}
다음 응답은 name 필드가 인덱싱되지는 않았지만 doc_values가 활성화되어 있으므로 검색 쿼리가 성공했음을 확인합니다.
{
...
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "products-no-index",
"_id": "1",
"_score": 1.0,
"_source": {
"description": "This product has a non-searchable description.",
"name": "doc1"
}
}
]
}
}