Backend · Infra

[ElasticSearch] nested Type

devhyen 2024. 7. 10. 14:35

ElasticSearch에서 nested 타입은 중첩된 문서 구조를 지원하는 방법이다.

일반적인 Object가 아닌 중첩된 객체 배열을 색인하고 검색하는 데 사용된다.

 

예제

{
  "title": "Book",
  "authors": [
    {
      "name": "Author A",
      "age": 30
    },
    {
      "name": "Author B",
      "age": 35
    }
  ]
}

 

authors 필드는 중첩된 객체 배열이다. 

각 author은 nameage를 가지고 있다.

 

위 데이터를 ES에 인덱싱하기 위해 매핑을 설정한다.

PUT /books
{
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "authors": {
        "type": "nested",
        "properties": {
          "name": { "type": "text" },
          "age": { "type": "integer" }
        }
      }
    }
  }
}

 

데이터를 추가

POST /books/_doc/1
{
  "title": "Sample Book",
  "authors": [
    {
      "name": "Author A",
      "age": 30
    },
    {
      "name": "Author B",
      "age": 35
    }
  ]
}

 

검색

GET /books/_search
{
  "query": {
    "nested": {
      "path": "authors",
      "query": {
        "bool": {
          "must": [
            { "match": { "authors.name": "Author A" } },
            { "match": { "authors.age": 30 } }
          ]
        }
      }
    }
  }
}

 

각 authors는 독립적인 Object로 관리된다. 이는 authors 간의 관계를 명확히 하고, 개별 저자 정보를 업데이트하거나 검색할 수 있다.

nested쿼리를 사용하면 중첩된 객체 배열 내의 필드들을 정확하게 필터링할 수 있다.

ES는 중첩된 객체 배열을 효율적으로 처리할 수 있도록 설계되어있어, 복잡한 데이터 구조도 색인하고 검색할 수 있다.

 

 

'Backend · Infra' 카테고리의 다른 글

[Java] 추상 클래스와 인터페이스의 차이  (0) 2024.07.12
Error 와 Exception  (0) 2024.07.11
[ElasticSearch] Query  (0) 2024.07.09
싱글톤 패턴 Singleton Pattern  (0) 2024.07.09
MSA (Microservice Architecture)  (0) 2024.07.03