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

1  2  3  4  5  6  7  8  [9]  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13398정성태8/3/20234157스크립트: 55. 파이썬 - pyodbc를 이용한 SQL Server 연결 사용법
13397정성태7/23/20233659닷넷: 2134. C# - 문자열 연결 시 string.Create를 이용한 GC 할당 최소화
13396정성태7/22/20233348스크립트: 54. 파이썬 pystack 소개 - 메모리 덤프로부터 콜 스택 열거
13395정성태7/20/20233311개발 환경 구성: 685. 로컬에서 개발 중인 ASP.NET Core/5+ 웹 사이트에 대해 localhost 이외의 호스트 이름으로 접근하는 방법
13394정성태7/16/20233259오류 유형: 873. Oracle.ManagedDataAccess.Client - 쿼리 수행 시 System.InvalidOperationException
13393정성태7/16/20233424닷넷: 2133. C# - Oracle 데이터베이스의 Sleep 쿼리 실행하는 방법
13392정성태7/16/20233299오류 유형: 872. Oracle - ORA-01031: insufficient privileges
13391정성태7/14/20233370닷넷: 2132. C# - sealed 클래스의 메서드를 callback 호출했을 때 인라인 처리가 될까요?
13390정성태7/12/20233341스크립트: 53. 파이썬 - localhost 호출 시의 hang 현상
13389정성태7/5/20233324개발 환경 구성: 684. IIS Express로 호스팅하는 웹을 WSL 환경에서 접근하는 방법
13388정성태7/3/20233517오류 유형: 871. 윈도우 탐색기에서 열리지 않는 zip 파일 - The Compressed (zipped) Folder '[...].zip' is invalid. [1]파일 다운로드1
13387정성태6/28/20233536오류 유형: 870. _mysql - Commands out of sync; you can't run this command now
13386정성태6/27/20233605Linux: 61. docker - 원격 제어를 위한 TCP 바인딩 추가
13385정성태6/27/20233826Linux: 60. Linux - 외부에서의 접속을 허용하기 위한 TCP 포트 여는 방법
13384정성태6/26/20233569.NET Framework: 2131. C# - Source Generator로 해결하는 enum 박싱 문제파일 다운로드1
13383정성태6/26/20233314개발 환경 구성: 683. GPU 런타임을 사용하는 Colab 노트북 설정
13382정성태6/25/20233357.NET Framework: 2130. C# - Win32 API를 이용한 윈도우 계정 정보 (예: 마지막 로그온 시간)파일 다운로드1
13381정성태6/25/20233746오류 유형: 869. Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
13380정성태6/24/20233195스크립트: 52. 파이썬 3.x에서의 동적 함수 추가
13379정성태6/23/20233210스크립트: 51. 파이썬 2.x에서의 동적 함수 추가
13378정성태6/22/20233098오류 유형: 868. docker - build 시 "CANCELED ..." 뜨는 문제
13377정성태6/22/20236901오류 유형: 867. 파이썬 mysqlclient 2.2.x 설치 시 "Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually" 오류
13376정성태6/21/20233286.NET Framework: 2129. C# - Polly를 이용한 클라이언트 측의 요청 재시도파일 다운로드1
13375정성태6/20/20232986스크립트: 50. Transformers (신경망 언어모델 라이브러리) 강좌 - 2장 코드 실행 결과
13374정성태6/20/20233110오류 유형: 866. 파이썬 - <class 'AttributeError'> module 'flask.json' has no attribute 'JSONEncoder'
13373정성태6/19/20234400오류 유형: 865. 파이썬 - pymssql 설치 관련 오류 정리
1  2  3  4  5  6  7  8  [9]  10  11  12  13  14  15  ...