Project/검색 솔루션 개발

ElasticSearch - Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead 에러 해결

둉이 2021. 5. 20. 13:56

어느 날 이클립스로 검색엔진 관리자 기능을 개발하다가 다음과 같은 오류에 직면했다.

 

오류조아~~

오류 전문은 다음과 같다. 간단히 해석을 해보면 text 필드는 sorting이 안되므로 keyword 필드를 사용하라는 것 같다.

Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead.

 

해결법은 다음과 같다.

 

1. 해당 필드의 type을 keyword로 변경

이미 매핑된 필드의 type은 변경이 불가하다. 따라서 인덱스를 삭제하고 다시 만들어야 하므로 2번이나 3번 방법을 쓰자.

 

2. 해당 필드에 서브 필드를 추가하여 해당 필드의 type을 keyword로 선언

PUT 인덱스명
{
  "mappings": {
    "_doc": {
      "properties": {
        "필드명": { 
          "type": "text",
          "fields": {
            "서브필드명": { 
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

fields 옵션을 사용하여 필드의 서브 필드를 선언할 수 있다.

필드명.서브필드명 ← 이런 형식으로 사용하면 된다.

 

3. 해당 필드에 "fielddata": true 옵션 추가

PUT 인덱스명/_mapping/_doc
{
  "properties": {
    "필드명": { 
      "type": "text",
      "fielddata": true
    }
  }
}

위 방법처럼 fielddata 옵션을 추가하면 된다.