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분
정성태

... 31  32  33  34  35  36  37  38  39  40  41  [42]  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
12570정성태3/19/202112671개발 환경 구성: 555. openssl - CA로부터 인증받은 새로운 인증서를 생성하는 방법
12569정성태3/18/202111543개발 환경 구성: 554. WSL 인스턴스 export/import 방법 및 단축 아이콘 설정 방법
12568정성태3/18/20217253오류 유형: 705. C# 빌드 - Couldn't process file ... due to its being in the Internet or Restricted zone or having the mark of the web on the file.
12567정성태3/17/20218579개발 환경 구성: 553. Docker Desktop for Windows를 위한 k8s 대시보드 활성화 [1]
12566정성태3/17/20218917개발 환경 구성: 552. Kubernetes - kube-apiserver와 REST API 통신하는 방법 (Docker Desktop for Windows 환경)
12565정성태3/17/20216433오류 유형: 704. curl.exe 실행 시 dll not found 오류
12564정성태3/16/20216932VS.NET IDE: 160. 새 프로젝트 창에 C++/CLI 프로젝트 템플릿이 없는 경우
12563정성태3/16/20218880개발 환경 구성: 551. C# - JIRA REST API 사용 정리 (3) jira-oauth-cli 도구를 이용한 키 관리
12562정성태3/15/20219967개발 환경 구성: 550. C# - JIRA REST API 사용 정리 (2) JIRA OAuth 토큰으로 API 사용하는 방법파일 다운로드1
12561정성태3/12/20218596VS.NET IDE: 159. Visual Studio에서 개행(\n, \r) 등의 제어 문자를 치환하는 방법 - 정규 표현식 사용
12560정성태3/11/20219944개발 환경 구성: 549. ssh-keygen으로 생성한 개인키/공개키 파일을 각각 PKCS8/PEM 형식으로 변환하는 방법
12559정성태3/11/20219320.NET Framework: 1028. 닷넷 5 환경의 Web API에 OpenAPI 적용을 위한 NSwag 또는 Swashbuckle 패키지 사용 [2]파일 다운로드1
12558정성태3/10/20218853Windows: 192. Power Automate Desktop (Preview) 소개 - Bitvise SSH Client 제어 [1]
12557정성태3/10/20217488Windows: 191. 탐색기의 보안 탭에 있는 "Object name" 경로에 LEFT-TO-RIGHT EMBEDDING 제어 문자가 포함되는 문제
12556정성태3/9/20216782오류 유형: 703. PowerShell ISE의 Debug / Toggle Breakpoint 메뉴가 비활성 상태인 경우
12555정성태3/8/20218779Windows: 190. C# - 레지스트리에 등록된 DigitalProductId로부터 라이선스 키(Product Key)를 알아내는 방법파일 다운로드2
12554정성태3/8/20218615.NET Framework: 1027. 닷넷 응용 프로그램을 위한 PDB 옵션 - full, pdbonly, portable, embedded
12553정성태3/5/20219088개발 환경 구성: 548. 기존 .NET Framework 프로젝트를 .NET Core/5+ 용으로 변환해 주는 upgrade-assistant, try-convert 도구 소개 [4]
12552정성태3/5/20218348개발 환경 구성: 547. github workflow/actions에서 Visual Studio Marketplace 패키지 등록하는 방법
12551정성태3/5/20217248오류 유형: 702. 비주얼 스튜디오 - The 'CascadePackage' package did not load correctly. (2)
12550정성태3/5/20216969오류 유형: 701. Live Share 1.0.3713.0 버전을 1.0.3884.0으로 업데이트 이후 ContactServiceModelPackage 오류 발생하는 문제
12549정성태3/4/20217415오류 유형: 700. VsixPublisher를 이용한 등록 시 다양한 오류 유형 해결책
12548정성태3/4/20218166개발 환경 구성: 546. github workflow/actions에서 nuget 패키지 등록하는 방법
12547정성태3/3/20218633오류 유형: 699. 비주얼 스튜디오 - The 'CascadePackage' package did not load correctly.
12546정성태3/3/20218301개발 환경 구성: 545. github workflow/actions에서 빌드시 snk 파일 다루는 방법 - Encrypted secrets
12545정성태3/2/202111023.NET Framework: 1026. 닷넷 5에 추가된 POH (Pinned Object Heap) [10]
... 31  32  33  34  35  36  37  38  39  40  41  [42]  43  44  45  ...