Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)
(시리즈 글이 5개 있습니다.)
개발 환경 구성: 392. 윈도우 환경에서 curl.exe를 이용한 elasticsearch 6.x 기본 사용법
; https://www.sysnet.pe.kr/2/0/11663

개발 환경 구성: 393. 윈도우 환경에서 elasticsearch의 한글 형태소 분석기 설치
; https://www.sysnet.pe.kr/2/0/11664

개발 환경 구성: 394. 윈도우 환경에서 elasticsearch의 한글 블로그 검색 인덱스 구성
; https://www.sysnet.pe.kr/2/0/11669

.NET Framework: 791. C# - ElasticSearch를 위한 Client 라이브러리 제작
; https://www.sysnet.pe.kr/2/0/11676

개발 환경 구성: 507. Elasticsearch 6.6부터 기본 추가된 한글 형태소 분석기 노리(nori) 사용법
; https://www.sysnet.pe.kr/2/0/12309




윈도우 환경에서 elasticsearch의 한글 형태소 분석기 설치

지난 글에서 elasticsearch의 간단한 사용법을 알아봤는데요,

윈도우 환경에서 curl.exe를 이용한 elasticsearch 6.x 기본 사용법
; https://www.sysnet.pe.kr/2/0/11663

이번에는 한글 검색을 도와주는 플러그인을 설치해 보겠습니다. 검색해 보면 Linux에 설치한 ElasticSearch에만 적용되는 확장이 있는데,

MeCab 한글 형태소 분석기 플러그인 설치하기
; http://guruble.com/?p=416

여기서는 윈도우에서도 설치할 수 있는 "open-korean-text" 모듈을 사용하겠습니다.

open-korean-text/elasticsearch-analysis-openkoreantext
; https://github.com/open-korean-text/elasticsearch-analysis-openkoreantext

문제는 지난 글에서 설치한 elasticsearch가 6.3.2 버전이었는데 open-korean-text 모듈이 지원하는 버전은 2017년 12월에 릴리스한 6.1.1 버전이라는 점입니다.

Elasticsearch 6.1.1 December 19, 2017
; https://www.elastic.co/downloads/past-releases/elasticsearch-6-1-1

이를 설치하고 다음과 같이 elasticsearch-plugin.bat을 이용해 로컬에 open-korean-text 모듈을 추가할 수 있습니다.

E:\elasticsearch\6.1.1> .\bin\elasticsearch-plugin install https://github.com/open-korean-text/elasticsearch-analysis-openkoreantext/releases/download/6.1.1/elasticsearch-analysis-openkoreantext-6.1.1.2-plugin.zip
-> Downloading https://github.com/open-korean-text/elasticsearch-analysis-openkoreantext/releases/download/6.1.1/elasticsearch-analysis-openkoreantext-6.1.1.2-plugin.zip
[=================================================] 100%??
-> Installed elasticsearch-analysis-openkoreantext

이후, elasticsearch를 재시작하면 로드 도중 다음과 같은 메시지를 볼 수 있습니다.

[2018-08-09T11:24:18,787][INFO ][o.e.p.PluginsService     ] [FGFsQ64] loaded plugin [elasticsearch-analysis-openkoreantext]




설치 후 간단하게 "open-korean-text/elasticsearch-analysis-openkoreantext" 문서에 따라 다음과 같이 테스트할 수 있습니다.

curl -X POST "http://localhost:9200/_analyze" -H "Content-Type: application/json" -d "{
  "analyzer": "openkoreantext-analyzer",
  "text": "한국어를 처리하는 예시입니닼ㅋㅋ"
}"

c:\temp> curl -X POST "http://localhost:9200/_analyze" -H "Content-Type: application/json" -d "{ \"analyzer\": \"openkoreantext-analyzer\", \"text\": \"한국어를 처리하는 예시입니닼ㅋ \" }"

{"tokens":[{"token":"한국어","start_offset":0,"end_offset":3,"type":"Noun","position":0},{"token":"처리","start_offset":5,"end_offset":7,"type":"Noun","position":1},{"token":"하다","start_offset":7,"end_offset":9,"type":"Verb","position":2},{"token":"예시","start_offset":10,"end_offset":12,"type":"Noun","position":3},{"token":"이다","start_offset":12,"end_offset":15,"type":"Adjective","position":4},{"token":"ㅋ","start_offset":15,"end_offset":16,"type":"KoreanParticle","position":5}]}

그러니까, "한국어", "처리", "하다", "예시", "이다", "ㅋ"로 분석이 된 것입니다.

만약 open-korean-text를 설치하지 않았다면 다음과 같이 분석됩니다.

c:\temp> curl -X POST "http://localhost:9200/_analyze" -H "Content-Type: application/json" -d "{ \"text\": \"한국어를 처리하는 예시입니닼ㅋ \" }"

{"tokens":[{"token":"한국어를","start_offset":0,"end_offset":4,"type":"","position":0},{"token":"처리하는","start_offset":5,"end_offset":9,"type":"","position":1},{"token":"예시입니닼ㅋ","start_offset":10,"end_offset":16,"type":"","position":2}]}

"한국어를", "처리하는", "예시입니닼"라고 분석을 했으니 순수하게 공백 문자 기준이라고 보면 됩니다.




지난 글에서 Type을 스키마를 지정해 생성하는 것을 배웠는데요, 따라서 다음과 같이 새롭게 openkoreantext-analyzer를 address 필드에 적용한 타입을 실습 용으로 만듭니다.

curl -XDELETE "http://localhost:9200/my_org/"

curl -XPUT "http://localhost:9200/my_org/" -H "Content-Type: application/json" -d "{ \"my_org\": { \"settings\" : { \"index\" : { } } } }"

curl -XPUT "http://localhost:9200/my_org/member/_mapping" -H "Content-Type: application/json" -d "{ \"member\" : { \"properties\" : {  \"name\" : {\"type\" : \"text\", \"index\" : \"false\"}, \"age\" : {\"type\" : \"integer\"}, \"address\" : {\"type\" : \"text\", \"analyzer\": \"openkoreantext-analyzer\" }, \"registered\" : {\"type\" : \"date\"} } } }"


다음의 명령어로 member 타입의 매핑 상태를 볼 수 있습니다.

curl -XGET "http://localhost:9200/my_org/member/_mapping?pretty"
{
  "my_org" : {
    "mappings" : {
      "member" : {
        "properties" : {
          "address" : {
            "type" : "text",
            "analyzer" : "openkoreantext-analyzer"
          },
          "age" : {
            "type" : "integer"
          },
          "name" : {
            "type" : "text",
            "index" : false
          },
          "registered" : {
            "type" : "date"
          }
        }
      }
    }
  }
}

테스트용 데이터를 심어 주고,

curl -XPUT "http://localhost:9200/my_org/member/1" -H "Content-Type: application/json" -d 
"{ 
    "name" : "tester", 
    "age": 16, 
    "address": "gacutil.exe를 실행해 닷넷 DLL을 GAC에 등록하려 할 때 다음과 같은 식의 오류가 발생한다면?", 
    "registered":"2017-04-29T10:16:00" 
}"

curl -XPUT "http://localhost:9200/my_org/member/1" -H "Content-Type: application/json" -d 
"{ 
    "name" : "tester", 
    "age": 16, 
    "address": "한국어를 처리하는 예시입니닼ㅋㅋ", 
    "registered":"2017-04-29T10:16:00" 
}"

curl -XPUT "http://localhost:9200/my_org/member/3" -H "Content-Type: application/json" -d 
"{ 
    "name" : "tester", 
    "age": 16, 
    "address": "새로운 스테밍을 추가했었다. 그리고 이것은 예시, 또는 예로써, 한국어를 처리하기", 
    "registered":"2017-04-29T10:16:00" 
}"

curl -XPUT "http://localhost:9200/my_org/member/1" -H "Content-Type: application/json" -d "{ \"name\" : \"tester\", \"age\": 16, \"address\": \"gacutil.exe를 실행해 닷넷 DLL을 GAC에 등록하려 할 때 다음과 같은 식의 오류가 발생한다면?\", \"registered\":\"2017-04-29T10:16:00\" }"

{"_index":"my_org","_type":"member","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

curl -XPUT "http://localhost:9200/my_org/member/2" -H "Content-Type: application/json" -d "{ \"name\" : \"tester\", \"age\": 16, \"address\": \"한국어를 처리하는 예시입니닼ㅋㅋ\", \"registered\":\"2017-04-29T10:16:00\" }"

{"_index":"my_org","_type":"member","_id":"2","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":2}

curl -XPUT "http://localhost:9200/my_org/member/3" -H "Content-Type: application/json" -d "{ \"name\" : \"tester\", \"age\": 16, \"address\": \"새로운 스테밍을 추가했었다. 그리고 이것은 예시, 또는 예로써, 한국어를 처리하기\", \"registered\":\"2017-04-29T10:16:00\" }"

{"_index":"my_org","_type":"member","_id":"3","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":2}


검색 쿼리를 날려볼 수 있습니다.

curl -XGET "http://localhost:9200/my_org/member/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"address\": \"gacutil\" } } }"

{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"my_org","_type":"member","_id":"1","_score":0.2876821,"_source":{ "name" : "tester", "age": 16, "address": "gacutil.exe를 실행해 닷넷 DLL을 GAC에 등록하려 할 때 다음과 같은 식의 오류가 발생한다면?", "registered":"2017-04-29T10:16:00" }}]}}

curl -XGET "http://localhost:9200/my_org/member/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"address\": \"발생\" } } }"

{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"my_org","_type":"member","_id":"1","_score":0.2876821,"_source":{ "name" : "tester", "age": 16, "address": "gacutil.exe를 실행해 닷넷 DLL을 GAC에 등록하려 할 때 다음과 같은 식의 오류가 발생한다면?", "registered":"2017-04-29T10:16:00" }}]}}

curl -XGET "http://localhost:9200/my_org/member/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"address\": \"이다\" } } }"

{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}

형용사에 대한 분석이 재미있습니다. 이에 대해 openkoreantext-stemmer 토큰 필터가 담당하는데, 가령 "새로운 스테밍을 추가했었다." 글이 포함된 검색을 하려고 다음과 같이 쿼리를 해도 됩니다.

curl -XGET "http://localhost:9200/my_org/member/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"address\": \"새로운\" } } }"
{"took":4,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"my_org","_type":"member","_id":"3","_score":0.2876821,"_source":{ "name" : "tester", "age": 16, "address": "새로운 스테밍을 추가했었다. 그리고 이것은 예시, 또는 예로써, 한국어를 처리하기", "registered":"2017-04-29T10:16:00" }}]}}

curl -XGET "http://localhost:9200/my_org/member/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"address\": \"새로워진\" } } }"
{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"my_org","_type":"member","_id":"3","_score":0.2876821,"_source":{ "name" : "tester", "age": 16, "address": "새로운 스테밍을 추가했었다. 그리고 이것은 예시, 또는 예로써, 한국어를 처리하기", "registered":"2017-04-29T10:16:00" }}]}}

curl -XGET "http://localhost:9200/my_org/member/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"address\": \"새롭게\" } } }"
{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"my_org","_type":"member","_id":"3","_score":0.2876821,"_source":{ "name" : "tester", "age": 16, "address": "새로운 스테밍을 추가했었다. 그리고 이것은 예시, 또는 예로써, 한국어를 처리하기", "registered":"2017-04-29T10:16:00" }}]}}




비교를 위해 한글 형태소 분석기 없이 데이터를 저장해 보겠습니다.

curl -XPUT "http://localhost:9200/my_text/" -H "Content-Type: application/json" -d "{ \"my_text\": { \"settings\" : { \"index\" : { } } } }"

curl -XPUT "http://localhost:9200/my_text/member/_mapping" -H "Content-Type: application/json" -d "{ \"member\" : { \"properties\" : {  \"name\" : {\"type\" : \"text\", \"index\" : \"false\"}, \"age\" : {\"type\" : \"integer\"}, \"address\" : {\"type\" : \"text\" }, \"registered\" : {\"type\" : \"date\"} } } }"

curl -XPUT "http://localhost:9200/my_text/member/1" -H "Content-Type: application/json" -d "{ \"name\" : \"tester\", \"age\": 16, \"address\": \"gacutil.exe를 실행해 닷넷 DLL을 GAC에 등록하려 할 때 다음과 같은 식의 오류가 발생한다면?\", \"registered\":\"2017-04-29T10:16:00\" }"

curl -XPUT "http://localhost:9200/my_text/member/2" -H "Content-Type: application/json" -d "{ \"name\" : \"tester\", \"age\": 16, \"address\": \"한국어를 처리하는 예시입니닼ㅋㅋ\", \"registered\":\"2017-04-29T10:16:00\" }"

curl -XPUT "http://localhost:9200/my_text/member/3" -H "Content-Type: application/json" -d "{ \"name\" : \"tester\", \"age\": 16, \"address\": \"새로운 스테밍을 추가했었다. 그리고 이것은 예시, 또는 예로써, 한국어를 처리하기\", \"registered\":\"2017-04-29T10:16:00\" }"

그다음, 아래와 같이 쿼리하면 검색 결과가 한 건도 없습니다.

C:\temp>curl -XGET "http://localhost:9200/my_text/member/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"address\": \"gacutil\" } } }"

{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}

C:\temp>curl -XGET "http://localhost:9200/my_text/member/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"address\": \"발생\" } } }"

{"took":0,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}

반면 띄어쓰기 기준으로 검색어 조건을 주면 결과가 나옵니다.

C:\temp>curl -XGET "http://localhost:9200/my_text/member/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"address\": \"gacutil.exe를\" } } }"
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"my_text","_type":"member","_id":"1","_score":0.2876821,"_source":{ "name" : "tester", "age": 16, "address": "gacutil.exe를 실행해 닷넷 DLL을 GAC에 등록하려 할 때 다음과 같은 식의 오류가 발생한다면?", "registered":"2017-04-29T10:16:00" }}]}}

C:\temp>curl -XGET "http://localhost:9200/my_text/member/_search" -H "Content-Type: application/json" -d "{ \"query\": { \"match\": { \"address\": \"발생한다면\" } } }"
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"my_text","_type":"member","_id":"1","_score":0.2876821,"_source":{ "name" : "tester", "age": 16, "address": "gacutil.exe를 실행해 닷넷 DLL을 GAC에 등록하려 할 때 다음과 같은 식의 오류가 발생한다면?", "registered":"2017-04-29T10:16:00" }}]}}

따라서, 한글이 포함된 문서를 elasticsearch에서 사용하고 싶다면 한글 형태소 분석기는 선택이 아닌 필수입니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 7/10/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2022-08-12 10시32분
정성태

... 16  17  18  19  20  21  22  23  24  [25]  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
12996정성태3/9/202215600VS.NET IDE: 175. Visual Studio - 인텔리센스에서 오버로드 메서드를 키보드로 선택하는 방법
12995정성태3/8/20227897.NET Framework: 1173. .NET에서 Producer/Consumer를 구현한 BlockingCollection<T>
12994정성태3/8/20227204오류 유형: 798. WinDbg - Failed to load data access module, 0x80004002
12993정성태3/4/20226984.NET Framework: 1172. .NET에서 Producer/Consumer를 구현하는 기초 인터페이스 - IProducerConsumerCollection<T>
12992정성태3/3/20228365.NET Framework: 1171. C# - BouncyCastle을 사용한 암호화/복호화 예제파일 다운로드1
12991정성태3/2/20227599.NET Framework: 1170. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 transcode_aac.c 예제 포팅
12990정성태3/2/20227192오류 유형: 797. msbuild - The BaseOutputPath/OutputPath property is not set for project '[...].vcxproj'
12989정성태3/2/20226764오류 유형: 796. mstest.exe - System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.QualityTools.Tips.WebLoadTest.Tip
12988정성태3/2/20225710오류 유형: 795. CI 환경에서 Docker build 시 csproj의 Link 파일에 대한 빌드 오류
12987정성태3/1/20227149.NET Framework: 1169. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 demuxing_decoding.c 예제 포팅
12986정성태2/28/20228002.NET Framework: 1168. C# -IIncrementalGenerator를 적용한 Version 2 Source Generator 실습 [1]
12985정성태2/28/20227892.NET Framework: 1167. C# -Version 1 Source Generator 실습
12984정성태2/24/20226991.NET Framework: 1166. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 filtering_video.c 예제 포팅
12983정성태2/24/20227087.NET Framework: 1165. .NET Core/5+ 빌드 시 runtimeconfig.json에 설정을 반영하는 방법
12982정성태2/24/20227024.NET Framework: 1164. HTTP Error 500.31 - ANCM Failed to Find Native Dependencies
12981정성태2/23/20226664VC++: 154. C/C++ 언어의 문자열 Literal에 인덱스 적용하는 구문 [1]
12980정성태2/23/20227371.NET Framework: 1163. C# - 윈도우 환경에서 usleep을 호출하는 방법 [2]
12979정성태2/22/20229950.NET Framework: 1162. C# - 인텔 CPU의 P-Core와 E-Core를 구분하는 방법 [1]파일 다운로드2
12978정성태2/21/20227266.NET Framework: 1161. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 resampling_audio.c 예제 포팅
12977정성태2/21/202210998.NET Framework: 1160. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 qsv 디코딩
12976정성태2/21/20226644VS.NET IDE: 174. Visual C++ - "External Dependencies" 노드 비활성화하는 방법
12975정성태2/20/20228398.NET Framework: 1159. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 qsvdec.c 예제 포팅파일 다운로드1
12974정성태2/20/20226538.NET Framework: 1158. C# - SqlConnection의 최소 Pooling 수를 초과한 DB 연결은 언제 해제될까요?
12973정성태2/16/20228758개발 환경 구성: 639. ffmpeg.exe - Intel Quick Sync Video(qsv)를 이용한 인코딩 [3]
12972정성태2/16/20228026Windows: 200. Intel CPU의 내장 그래픽 GPU가 작업 관리자에 없다면? [4]
12971정성태2/15/20229678.NET Framework: 1157. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 muxing.c 예제 포팅 [7]파일 다운로드2
... 16  17  18  19  20  21  22  23  24  [25]  26  27  28  29  30  ...