properties 매핑 파라미터

properties 매핑 파라미터

properties 매핑 파라미터는 객체(object) 내부 또는 문서의 루트에서 필드의 구조와 데이터 타입을 정의하는 데 사용돼요. 이 파라미터는 매핑 정의의 핵심 역할을 하며, 각 필드의 이름과 타입(예: text, keyword, date, float) 및 추가 설정이나 매핑 파라미터를 명시적으로 지정할 수 있게 해줘요.

properties를 사용하면 데이터가 인덱스되고 저장되는 방식을 완전히 제어할 수 있어서, 정밀한 검색 동작, 집계 지원, 데이터 검증이 가능해져요.

출처: 문서

본문

properties로 필드 정의하기

다음 요청은 properties 파라미터를 사용한 구조화된 매핑으로 products 인덱스를 생성해요. 여기에는 하위 필드를 가진 dimensions라는 nested 객체 필드가 포함돼요:

PUT /products
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "sku": {
        "type": "keyword"
      },
      "price": {
        "type": "float"
      },
      "available": {
        "type": "boolean"
      },
      "created_at": {
        "type": "date",
        "format": "yyyy-MM-dd"
      },
      "dimensions": {
        "type": "object",
        "properties": {
          "width": { "type": "float" },
          "height": { "type": "float" },
          "depth": { "type": "float" }
        }
      }
    }
  }
}

문서 인덱싱하기

중첩 필드를 가진 문서를 인덱스하려면 다음 명령을 사용해요:

PUT /products/_doc/1
{
  "name": "Wireless Mouse",
  "sku": "WM-1001",
  "price": 24.99,
  "available": true,
  "created_at": "2024-12-01",
  "dimensions": {
    "width": 6.5,
    "height": 3.2,
    "depth": 1.5
  }
}

점 표기법으로 쿼리하고 집계하기

점 표기법(dot notation)을 사용해 객체 하위 필드를 쿼리하거나 집계할 수 있어요. 다음 명령을 실행하면:

  • dimensions.width 필드로 문서를 필터링해, width가 5에서 10 사이인 문서를 반환해요.
  • dimensions.depth 필드에 대해 히스토그램 집계를 만들어, depth 0.5 간격으로 제품 버킷을 생성해요.
POST /products/_search
{
  "query": {
    "range": {
      "dimensions.width": {
        "gte": 5,
        "lte": 10
      }
    }
  },
  "aggs": {
    "Depth Distribution": {
      "histogram": {
        "field": "dimensions.depth",
        "interval": 0.5
      }
    }
  }
}

다음 응답은 dimensions.width 필드가 지정된 범위에 속하는 매칭 문서를 보여줘요. 또한 dimensions.depth에 대한 히스토그램 집계 결과도 포함돼요:

{
  ...
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "products",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "Wireless Mouse",
          "sku": "WM-1001",
          "price": 24.99,
          "available": true,
          "created_at": "2024-12-01",
          "dimensions": {
            "width": 6.5,
            "height": 3.2,
            "depth": 1.5
          }
        }
      }
    ]
  },
  "aggregations": {
    "Depth Distribution": {
      "buckets": [
        {
          "key": 1.5,
          "doc_count": 1
        }
      ]
    }
  }
}

더 알아보기 (Learn more)