훈훈훈

Spring Data :: Spring Data Elasticsearch _class 필드 자동 생성 비활성화 본문

Spring Framework/개념

Spring Data :: Spring Data Elasticsearch _class 필드 자동 생성 비활성화

훈훈훈 2021. 11. 27. 13:36

이슈


Spring data elasticsearch 를 사용하여 색인을 하면 아래와 같이 _class 라는 필드가 자동 생성이 되는 것을 볼 수 있다.

 

 

해당 필드는 sava 메소드를 호출할 때 사용했던 도큐먼트 객체에 대한 정보를 담고 있지만, Elasticsearch 를 호출하는 클라이언트 입장에서는 불필요한 데이터이다.  따라서 굳이 저 필드를 생성할 필요는 없다고 생각한다. 

 

_class 필드가 존재하는 목적이 궁금하여 스택오버플로우를 검색해보니, spring data elasticsearch 프로젝트를 리드하시는 분의 답변을 찾을 수 있었다.

 

 

100% 이해가 되는 문장은 아니지만 상속 관련 된 객체를 사용할 때 사용하는 필드라고 한다. 

그렇다면 상속을 하지 않는 도큐먼트 객체들에 대해서는 해당 필드는 사용하지 않기 떄문에 제거해도 된다고 결론을 낼 수 있을 것 같다.

(정확하게 어떤 기능을 하는지는 이해가 되지는 않지만, Spring data elasticseach 에서 사용하는 메타데이터인 것 같다.)

 

 

 

해결 방법


해당 필드가 자동 생성되는 것을 막기 위해서 Spring data elasticseach Github 이슈를 확인해보니 spring data elasticsearch 4.3 버전부터 서포트 하는 것을 알 수 있다.

 

@Getter
@ToString
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Document(indexName = "user", createIndex = false, writeTypeHint = WriteTypeHint.FALSE)
public class UserDocument {

  @Id
  private String id;

  @Field(type = FieldType.Text)
  private String name;

  public void setName(String name) {
    this.name = name;
  }
}

@Document 어노테이션에 writeTypeHint 을 False 로 설정하면, 색인할 때 _class 필드 자동 생성을 비활성화하는 것을 볼 수 있다.

 

 

 

 

** 참고

https://github.com/spring-projects/spring-data-elasticsearch/issues/1883

 

How to remove the _class field when automatically creating index and how to specify what fields we want return in query? · Issu

There are two questions: A field named _class will be inserted automatically when spring-data-elasticsearch creates an index, even though it is not desired. I wonder how to avoid that. *Repository ...

github.com

https://stackoverflow.com/questions/66062703/is-there-any-way-to-force-spring-not-to-use-create-class-field-in-the-mapping

 

Is there any way to force spring not to use/create '_class' field in the mapping?

The thing is on production servers we got mapping for Elasticsearch with dynamic set to strict. Currently, we use a rest level client to communicate with Elastisearch, however, we would like to mig...

stackoverflow.com

https://stackoverflow.com/questions/65126545/reading-from-elastic-search-using-spring-boot-where-es-record-without-class-at

 

Reading from elastic search using Spring boot, where ES record without _class attribute

I am trying to read/search the existing records in Elastic Search via Spring Data + Elastic Search.The existing records in the elastic search does not have the _class attribute. { "_index&qu...

stackoverflow.com

 

Comments