ELASTICSEARCH

[엘라스틱서치] 6. 한글 형태소, mapping

sejin2 2024. 3. 13. 10:49

한글 형태소 설치
엘라스틱서치 -> bin위치에서 cmd창 열어서 설치하기

합성어(복합어) 처리 3가지 옵션

  • none : 복합어를 분리하지 않음
  • discard : 복합어 분리
  • mixed : none과 discard 모두 저장
PUT my_nori2
{
    "settings":{
        "analysis":{
            "tokenizer":{
                "nori-none":{
                "type":"nori_tokenizer",
                "discompound_mode":"none"
                },
                "nori-discard":{
                "type":"nori_tokenizer",
                "discompound_mode":"discard"
                },
                "nori-mixed":{
                "type":"nori_tokenizer",
                "discompound_mode":"mixed"
                }
            }
        }
    }
}

 

- stoptags : 한글 품사 중 제외 시킬 품사

디폴트 값 = 
"stoptags": [
  "E", "IC", "J", "MAG", "MAJ",
  "MM", "SP", "SSC", "SSO", "SC",
  "SE", "XPN", "XSA", "XSN", "XSV",
  "UNA", "NA", "VSV"
]

my_stoptag인덱스에서 수사( NR )를 제외

PUT my_stoptag
{
  "settings":{
    "index":{
      "analysis":{
        "filter":{
          "my_stop_tag":{
            "type":"nori_part_of_speech",
            "stoptags":["NR"]
          }
        }
      }
    }
  }
}
GET my_stoptag/_analyze
{
  "tokenizer":"nori_tokenizer",
  "filter":["my_stop_tag"],
  "text":"일곱난장이들의 나들이"
}

{
  "tokens": [
    {
      "token": "난장이",
      "start_offset": 2,
      "end_offset": 5,
      "type": "word",
      "position": 1
    },
    {
      "token": "들",
      "start_offset": 5,
      "end_offset": 6,
      "type": "word",
      "position": 2
    },
    {
      "token": "의",
      "start_offset": 6,
      "end_offset": 7,
      "type": "word",
      "position": 3
    },
    {
      "token": "나들이",
      "start_offset": 8,
      "end_offset": 11,
      "type": "word",
      "position": 4
    }
  ]
}

- nori_readingform : 한자를 한글로 바꾸어 저장

GET _analyze
{
  "tokenizer":"nori_tokenizer",
  "filter":["nori_readingform"],
  "text":"立春大吉"
}

{
  "tokens": [
    {
      "token": "립춘",
      "start_offset": 0,
      "end_offset": 2,
      "type": "word",
      "position": 0
    },
    {
      "token": "대길",
      "start_offset": 2,
      "end_offset": 4,
      "type": "word",
      "position": 1
    }
  ]
}

- explain : 품사정보를 같이 볼 수 있다.

GET _analyze
{
  "tokenizer":"nori_tokenizer", 
  "text":["동해물과 백두산이"],
  "explain":true
}

{
  "detail": {
    "custom_analyzer": true,
    "charfilters": [],
    "tokenizer": {
      "name": "nori_tokenizer",
      "tokens": [
        {
          "token": "동해",
          "start_offset": 0,
          "end_offset": 2,
          "type": "word",
          "position": 0,
          "bytes": "[eb 8f 99 ed 95 b4]",
          "leftPOS": "NNP(Proper Noun)",
          "morphemes": null,
          "posType": "MORPHEME",
          "positionLength": 1,
          "reading": null,
          "rightPOS": "NNP(Proper Noun)",
          "termFrequency": 1
        },
        {
          "token": "물",
          "start_offset": 2,
          "end_offset": 3,
          "type": "word",
          "position": 1,
          "bytes": "[eb ac bc]",
          "leftPOS": "NNG(General Noun)",
          "morphemes": null,
          "posType": "MORPHEME",
          "positionLength": 1,
          "reading": null,
          "rightPOS": "NNG(General Noun)",
          "termFrequency": 1
        },
        ....

 

mapping( = 스키마 ) : 자동으로 생성됨. 너무 많은 정보를 담고 있다.

PUT books/_doc/1
{
  "title":"Romeo and Juliet",
  "author":"William Shakespeare",
  "category":"Tragedies",
  "publish_date" : "1597-03-12T00:00:00", 
  "page":200
}

-> 전체 구조 확인
GET books 

-> 전체 구조 중 mappings의 값만 보기
GET books/_mapping

⇒ mapping의 정의 : 기존의 mapping에 추가는 가능하지만, 바꾸지는 못한다.

[표현법]
PUT 인덱스명/_mapping
{
    "properties":{
        "추가할 필드명":{
            "type":"필드 타입"
            }
    }
}

Multi filed : "fields" 항목에 다시 새로운 필드를 정의하고 설정

PUT books {
 "mappings": {
    "properties": {
      "author": {
        "type": "text",
        "fields": {
          "fullname": {
            "type": "keyword",
            "ignore_above": 256
          },
          "nori":{
            "type":"text"
            "analyzer":"nori"
          }
        }
      },
      ...

=> 숫자 타입 : java의 타입과 동일 추가로

  • half_float : 2byte
  • scaled_float : long 형태로 저장, 옵션으로 소수점 위치를 지정

mapping에 integer로 해놓고 실수형을 넣으면 보여줄 때는 실수형을 보여주지만, 실제 저장은 long형으로 저장된다.

coerce 옵션 : 지정한 타입 외에 받을지 안받을지 선택 ( 기본 값 : true )

PUT num_index2
  {
    "mappings":{
      "properties":{
        "value":{
          "type":"integer",
          "coerce":false
        }
      }
    }
  }


=> date : ISO8601형식을 따름. 이외에 다른 형식은 text로 인식

PUT date_index/_doc/1
{
"date":"2024-03-12T16:45:30" // type: date
}


PUT date_index2/_doc/1
{
"date":"12/mar/2024:16:45:30" // type : text
}

mapping 미리 정의

PUT date_index3 
{
  "properties":{
    "date":{
      "type":"date",
      "format":"dd/MMM/yyyy:HH:mm:ss"
    }
  }
}
PUT date_index3/_doc/1
{
  "date":"12/Mar/2024:16:45:30"
}

형태가 맞지 않으면 아예 안 들어감.

PUT date_index3 
{
  "mappings":{
    "properties":{
      "date":{
        "type":"date",
        "format":"dd/MMM/yyyy:HH:mm:ss || epoch_millis || yyyy-MM-dd"
      }
    } 
  }
}

여러 포맷들을 설정할 수도 있다.