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)
13072정성태6/10/20226668Linux: 49. Linux - ls 명령어로 출력되는 디렉터리 색상 변경 방법
13071정성태6/9/20227250스크립트: 39. Python에서 cx_Oracle 환경 구성
13070정성태6/8/20227070오류 유형: 813. Windows 11에서 입력 포커스가 바뀌는 문제 [1]
13069정성태5/26/20229295.NET Framework: 2019. C# - .NET에서 제공하는 3가지 Timer 비교 [2]
13068정성태5/24/20227752.NET Framework: 2018. C# - 일정 크기를 할당하는 동안 GC를 (가능한) 멈추는 방법 [1]파일 다운로드1
13067정성태5/23/20227118Windows: 206. Outlook - 1년 이상 지난 메일이 기본적으로 안 보이는 문제
13066정성태5/23/20226414Windows: 205. Windows 11 - Windows + S(또는 Q)로 뜨는 작업 표시줄의 검색 바가 동작하지 않는 경우
13065정성태5/20/20227085.NET Framework: 2017. C# - Windows I/O Ring 소개 [2]파일 다운로드1
13064정성태5/18/20226650.NET Framework: 2016. C# - JIT 컴파일러의 인라인 메서드 처리 유무
13063정성태5/18/20227070.NET Framework: 2015. C# - 인라인 메서드(inline methods)
13062정성태5/17/20227861.NET Framework: 2014. C# - async/await 그리고 스레드 (4) 비동기 I/O 재현파일 다운로드1
13061정성태5/16/20226673.NET Framework: 2013. C# - FILE_FLAG_OVERLAPPED가 적용된 파일의 읽기/쓰기 시 Position 관리파일 다운로드1
13060정성태5/15/20229117.NET Framework: 2012. C# - async/await 그리고 스레드 (3) Task.Delay 재현파일 다운로드1
13059정성태5/14/20227617.NET Framework: 2011. C# - CLR ThreadPool의 I/O 스레드에 작업을 맡기는 방법 [1]파일 다운로드1
13058정성태5/13/20227548.NET Framework: 2010. C# - ThreadPool.SetMaxThreads 사용법
13057정성태5/12/20229226오류 유형: 812. 파이썬 - ImportError: cannot import name ...
13056정성태5/12/20226388.NET Framework: 2009. C# - async/await 그리고 스레드 (2) MyTask의 호출 흐름 [2]파일 다운로드1
13055정성태5/11/20229281.NET Framework: 2008. C# - async/await 그리고 스레드 (1) MyTask로 재현 [11]파일 다운로드1
13054정성태5/11/20226806.NET Framework: 2007. C# - 10진수 숫자를 담은 문자열을 숫자로 변환하는 방법 [11]파일 다운로드1
13053정성태5/10/20226438.NET Framework: 2006. C# - GC.KeepAlive 메서드의 역할
13052정성태5/9/20226446.NET Framework: 2005. C# - 생성한 참조 개체가 언제 GC의 정리 대상이 될까요?
13051정성태5/8/20226385.NET Framework: 2004. C# XingAPI - ACF 검색 결과로 구한 CSV 파일을 통해 퀀트 종목 찾기파일 다운로드1
13050정성태5/6/20226402.NET Framework: 2003. C# - COM 개체의 이벤트 핸들러에서 발생하는 예외에 대한 CLR의 특별 대우파일 다운로드1
13049정성태5/6/20225375오류 유형: 811. GoLand - Error: Cannot find package
13048정성태5/6/20226501오류 유형: 810. "ASUS TUF GAMING B550M-PLUS (WI-FI)" 모델에서 블루투스 장치가 인식이 안 되는 문제
13047정성태5/6/20226485오류 유형: 809. Speech Recognition could not start
... 16  17  18  19  20  21  [22]  23  24  25  26  27  28  29  30  ...