본문으로 바로가기

[Lucene] 루씬 - Document Field types

category 개발이야기/Lucene & Solr 2019. 10. 2. 23:50
반응형


Document를 생성할때, 각각의 field를 type에 맞게 결정해 줍니다.

Value의 저장여부 부터 term vector 생성여부, data type에 따른 field등 각가 다르게 설정하기 때문에 Document가 어떤 Field를 지원하는지에 대한 내용을 정리합니다.

이 문서는 apache 공식 API 문서를 참고하였습니다.

http://lucene.apache.org/core/8_2_0/core/index.html

Lucene version 8.2.0


StringField

index에는 포함가능하지만 tokenize는 하지 않습니다. 따라서 string 전체가 하나의 token이 됩니다.
  • 사용 예시: "국가명" 또는 "id" 값등

생성자의 인자중 value로 String을 넣으면 textual String field가 되지만 BytesRef를 넣어주면 binary String field가 됩니다.



TextField

index에 포함되면 tokenized 됩니다. 하지만 term vector는 생성하지 않습니다.
  • 사용 예시 "문서의 내용(body)"

StoredField

value가 저장 되기 때문에 IndexSearcher.doc(int)와 indexReader.document()로 field와 값을 return 할 수 있습니다.
이 필드는 value 타입으로 다양하게 overloading 되어 있습니다.
주로 숫자를 입력할때 사용합니다. (Text 와 String은 Field가 따로 있으므로)
지원 가능한 value
  • byte[]
  • BeytsRef
  • CharSequence
  • FieldType
  • float
  • int
  • long
  • string
SoteredField의 경우  range search나 sort를 할 수 없습니다. 따라서 검색/정렬을 하려면 같은 이름으로 xxxDocValuesField를 함께 사용해야 합니다.


SortedSetDocValuesField

Document 단위로 BytesRef의 set을 저장합니다. grouping과 joining, faceting을 위해 index 됩니다.
만약 값을 저장하고 싶다면 StoredField를 만들어 따로 저장해야 합니다.
각각의 value는 32766 bytes 까지 가능합니다.
  • 사용 예시
document.add(new SortedSetDocValuesField(name, new BytesRef("hello")));
document.add(new SortedSetDocValuesField(name, new BytesRef("world")));


SortedNumericDocValuesField

Document 단위로 long 값을 저장합니다. scoring, sorting 또는 값 반환을 위해 사용됩니다.
document.add(new SortedNumericDocValuesField(name, 5L));
document.add(new SortedNumericDocValuesField(name, 14L));

만약 float이나 double을 사용할 경우 정상적인 sorting을 위하여 NumericUtils를 사용해야 합니다.

document.add(new SortedNumericDocValuesField(name, NumericUtils.floatToSortableInt(-5.3f)));


SortedDocValuesField

Document 단위로 BytesRef를 저장합니다.sorting을 위해 index 됩니다.
만약 값을 저장하고 싶다면 StoredField를 만들어 따로 저장해야 합니다.
value는 32766 bytes 까지 가능합니다.
document.add(new SortedDocValuesField(name, new BytesRef("hello")));


NumericDocValuesField

Document 단위로 long 값을 저장합니다. scoring, sorting 또는 값 반환을 위해 사용됩니다.
document.add(new NumericDocValuesField(name, 22L));

만약 value의 저장이 필요하다면 StoredField객체이용해서 따로 add해야 합니다.


BinaryDocValuesField

Document 단위로 Binary 값을 저장합니다. 이 값은 공유없이 바로 sort 됩니다.
  • 사용 예시 "Title"

만약 값이 공유되고 sort되어야 한다면 SortedDocValuesField를 사용하는게 낫습니다.

document.add(new BinaryDocValuesField(name, new BytesRef("hello")));

만약 value의 저장이 필요하다면 StoredField객체이용해서 따로 add해야 합니다.


DocValues는 일반 Field와는 다른 특성을 갖습니다.

DocValues의 상세한 개념에 대해서는 아래 링크를 확인하시면 됩니다.

https://tourspace.tistory.com/246 


BinaryPoint

빠르게 범위를 filtering 하기 위해 index된 binary filed 입니다.
만약 value의 저장이 필요하다면 StoredField객체이용해서 따로 add해야 합니다.

N 차원의 shape 이나 범위내의 모든 문서를 찾을때 효율적입니다.
또한 하나의 Document에 동일한 Field에 대한 여러 값들이 허용됩니다.

이 값을 이용하여 쿼리 생성을 위한 static 함수를 아래와 같이 제공 합니다.
  • newExactQuery(String, byte[]) 1차원 값의 exact match
  • newSetQuery(String, byte[]...) 1차원의 여러값의 set match
  • newRangeQuery(String, byte[], byte[]) 1차원 값의 범위 match
  • newRangeQuery(String, byte[][], byte[][]) n차원의 범위 match

IntPoint, LongPoint, FloatPoint

위에 설명한 BinaryPoint와 유사하게 빠르게 범위를 검색하기 위해 index된 field 입니다.
이 field들 역시 BinaryPoint에서 제공하는 query 생성 함수를 동일하게 제공합니다.
ex) IntPoint의 경우
  • newExactQuery(String, int) 1차원 값의 exact match
  • newSetQuery(String, int...) 1차원의 여러값의 set match
  • newRangeQuery(String, int, int) 1차원 값의 범위 match
  • newRangeQuery(String, int[], int[]) n차원의 범위 match


Date

날짜는 기본적으로 StringField로 전환하여 저장합니다. (아니면 Numeric type으로.)
만약 자바의 Date 객체의 값을 저장한다면 DateTools 클래스를 이용하여 쉽게 변환할 수 있습니다.

  • dateToString(Date date, DateTools.Resolution resolution)
  • timeToString(long time, DateTools.Resolution resolution)
  • stringToTime(Date date, DateTools.Resolution resolution)
DateTools.Resolution
  • DateTools.Resolution.YEAR: YYYY 형태
  • DateTools.Resolution.MONTH: YYYYMM 형태
  • DateTools.Resolution.DAY: YYYYMMDD 형태
  • DateTools.Resolution.HOUR: YYYYMMDDHH형태
  • DateTools.Resolution.MINUTE: YYYYMMDDHHMM 형태
  • DateTools.Resolution.SECOND: YYYYMMDDHHMMSS 형태
  • DateTools.Resolution.MILLISECOND: YYYYMMDDHHMMSSsss형태

반응형