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)
12495정성태1/19/202111024웹: 40. IIS의 HTTP/2 지원 여부 - h2, h2c [1]
12494정성태1/19/202110190개발 환경 구성: 522. WSL2 인스턴스와 호스트 측의 Hyper-V에 운영 중인 VM과 네트워크 연결을 하는 방법 [2]
12493정성태1/18/20218590.NET Framework: 1009. .NET 5에서의 네트워크 라이브러리 개선 (1) - HTTP 관련 [1]파일 다운로드1
12492정성태1/17/20217980오류 유형: 695. ASP.NET 0x80131620 Failed to bind to address
12491정성태1/16/20219570.NET Framework: 1008. 배열을 반환하는 C# COM 개체의 메서드를 C++에서 사용 시 메모리 누수 현상 [1]파일 다운로드1
12490정성태1/15/20219153.NET Framework: 1007. C# - foreach에서 열거 변수의 타입을 var로 쓰면 object로 추론하는 문제 [1]파일 다운로드1
12489정성태1/13/202110136.NET Framework: 1006. C# - DB에 저장한 텍스트의 (이모티콘을 비롯해) 유니코드 문자가 '?'로 보인다면? [1]
12488정성태1/13/202110363.NET Framework: 1005. C# - string 타입은 shallow copy일까요? deep copy일까요? [2]파일 다운로드1
12487정성태1/13/20218890.NET Framework: 1004. C# - GC Heap에 위치한 참조 개체의 주소를 알아내는 방법파일 다운로드1
12486정성태1/12/20219809.NET Framework: 1003. x64 환경에서 참조형의 기본 메모리 소비는 얼마나 될까요? [1]
12485정성태1/11/202110510Graphics: 38. C# - OpenCvSharp.VideoWriter에 BMP 파일을 1초씩 출력하는 예제파일 다운로드1
12484정성태1/9/202111175.NET Framework: 1002. C# - ReadOnlySequence<T> 소개파일 다운로드1
12483정성태1/8/20218374개발 환경 구성: 521. dotPeek - 훌륭한 역어셈블 소스 코드 생성 도구
12482정성태1/8/20219787.NET Framework: 1001. C# - 제네릭 타입/메서드에서 사용 시 경우에 따라 CS8377 컴파일 에러
12481정성태1/7/20219525.NET Framework: 1000. C# - CS8344 컴파일 에러: ref struct 타입의 사용 제한 메서드파일 다운로드1
12480정성태1/6/202112108.NET Framework: 999. C# - ArrayPool<T>와 MemoryPool<T> 소개파일 다운로드1
12479정성태1/6/20219486.NET Framework: 998. C# - OWIN 예제 프로젝트 만들기
12478정성태1/5/202111108.NET Framework: 997. C# - ArrayPool<T> 소개파일 다운로드1
12477정성태1/5/202113511기타: 79. github 코드 검색 방법 [1]
12476정성태1/5/202110162.NET Framework: 996. C# - 닷넷 코어에서 다른 스레드의 callstack을 구하는 방법파일 다운로드1
12475정성태1/5/202112752.NET Framework: 995. C# - Span<T>와 Memory<T> [1]파일 다운로드1
12474정성태1/4/202110301.NET Framework: 994. C# - (.NET Core 2.2부터 가능한) 프로세스 내부에서 CLR ETW 이벤트 수신 [1]파일 다운로드1
12473정성태1/4/20219096.NET Framework: 993. .NET 런타임에 따라 달라지는 정적 필드의 초기화 유무 [1]파일 다운로드1
12472정성태1/3/20219388디버깅 기술: 178. windbg - 디버그 시작 시 스크립트 실행
12471정성태1/1/20219848.NET Framework: 992. C# - .NET Core 3.0 이상부터 제공하는 runtimeOptions의 rollForward 옵션 [1]
12470정성태12/30/202010042.NET Framework: 991. .NET 5 응용 프로그램에서 WinRT API 호출 [1]파일 다운로드1
... 31  32  33  34  35  36  37  38  39  40  41  42  43  44  [45]  ...