Lucene을 바탕으로 개발한 분산 검색엔진인 ElasticSearch를 정리 합니다.
curl localhost:9200 #--- 서비스 확인 http://node201.hadoop.com:9200/ #--- 서비스 확인 http://node201.hadoop.com:9200/_status http://node201.hadoop.com:9200/_plugin/head/ #--- elasticsearch-head plugin이 설치된 경우
#### ElasticSearch 로드밸런서 설치
- ElasticSearch 로드밸런서 환경 설정
- vi /nas/appl/elasticsearch/config/elasticsearch.yml
node.master: false node.data: false network.bind_host: 192.168.0.1
- 로드밸런서용 plugin 설치
plugin -install mobz/elasticsearch-head plugin -install lukas-vlcek/bigdesk
#### 환경 설정
- 환경 변수
- bin/elasticsearch 환경변수
- JAVA_OPTS
- ES_JAVA_OPTS, ES_HEAP_SIZE
- ES_MIN_MEM=256m, ES_MAX_MEM=1gb
- 환경 설정 방법
- 환경 설정 파일로 설정
vi /nas/appl/elasticsearch/config/elasticsearch.yml index: store: type: memory
- 명령행 옵션으로 설정
elasticsearch -Des.index.store.type=memory
- REST API로 설정
curl -XPUT 'node201.hadoop.com:9200/customer/ -d ' index: store: type: memory '
- file descriptors 확인
- max_file_descriptors
curl 'node201.hadoop.com:9200/_nodes/process?pretty'
- memory settings : disable swap
- 한번만 적용
swapoff -a
- 항상 적용
vi /etc/fstab #--- swap을 주석 처리
- ElasticSearch 설정으로 처리
ulimit -l unlimited #--- root 사용자로 실행 mkdir /tmp/tmpJna vi config/elasticsearch.yml bootstrap.mlockall: true elasticsearch -Djna.tmpdir=/tmp/tmpJna
#### Service로 실행
- 환경 설정 변수
- ES_USER, ES_GROUP
- ES_HEAP_SIZE, ES_HEAP_NEWSIZE, ES_DIRECT_SIZE
- MAX_OPEN_FILES
- MAX_LOCKED_MEMORY, MAX_MAP_COUNT
- LOG_DIR, DATA_DIR, WORK_DIR
- CONF_DIR, CONF_FILE
- ES_JAVA_OPTS, RESTART_ON_UPGRADE
#--- /etc/init.d/elasticsearch #--- /etc/sysconfig/elasticsearch /sbin/chkconfig --add elasticsearch
### Windows에서 ElasticSearch 설치
'''ElasticSearch 설치'''
ElasticSearch는 JDK 7 이상에서 실행되는 Java 기반의 애플리케이션으로 별도의 설치 과정 없이 소스를 다운로드 받아 실행하면 됩니다. [다운로드 사이트(http://www.elasticsearch.org/download/)](http://www.elasticsearch.org/download/)에서 최신 버전(elasticsearch-1.3.2.zip)의 ElasticeSearch를 다운로드 합니다. 압축을 풀어 c:/appl/elasticsearch/ 폴더를 생성 합니다.
'''실행 및 확인'''
bin/ 폴더에서 elasticsearch.bat 파일을 실행 합니다.
브라우저에서 [http://localhost:9200/](http://localhost:9200/) 로 접속하여 확인 합니다.
http://www.jopenbusiness.com/mediawiki/images/e/e1/ElasticSearch_Install_Windows_001.png
Cluster 정보 확인
- [http://localhost:9200/_cluster/health?pretty=true](http://localhost:9200/_cluster/health?pretty=true)
Node 정보 확인
- [http://localhost:9200/_nodes?pretty=true](http://localhost:9200/_nodes?pretty=true)
- [http://localhost:9200/_nodes/settings?pretty=true](http://localhost:9200/_nodes/settings?pretty=true)
## ElasticSearch 설정
### ElasticSearch 폴더 구조
{| border="1" cellspacing="0" cellpadding="2" style="width: 100%;"
|-
| style="text-align: center; background-color: rgb(204, 204, 204);" | 폴더
| style="text-align: center; background-color: rgb(204, 204, 204);" | 설정 변수
| style="text-align: center; background-color: rgb(204, 204, 204);" | 상세
|-
| style="text-align: center;" | bin
| style="text-align: center;" |
|
윈도우용 실행 파일
- elasticsearch.bat : ElasticSearch 실행 프로그램
- service.bat : Service 형태로 ElasticSearch 실행
service.bat install | remove | start | stop | manager [SERVICE_ID](SERVICE_ID.md)
- plugin.bat : 플러그인 설치 프로그램 (org.elasticsearch.plugins.PluginManager 프로그램이 실행됨)
Linux용 실행 파일
- elasticsearch : ElasticSearch 실행 프로그램
- plugin : 플러그인 설치 프로그램 (org.elasticsearch.plugins.PluginManager 프로그램이 실행됨)
플러그인명/ : Plugin 설치 파일의 bin/ 폴더가 여기로 이동됨
|-
| style="text-align: center;" | config
| style="text-align: center;" | path.conf
|
설정 파일 폴더
- elasticsearch.yml : ElasticSearch 설정 파일
- *path.plugins : 플러그인 설치 폴더 (Default. plugins/)
- logging.yml : 로그 설정 파일
플러그인명/ : Plugin 설치 파일의 config/ 폴더가 여기로 이동됨
|-
| style="text-align: center;" | data
| style="text-align: center;" | path.data
|
데이터와 인덱스를 저장하는 폴더 (elasticsearch/nodes/)
- path.data: /path/to/data1,/path/to/data2
|-
| style="text-align: center;" | lib
|
|
ElasticSearch용 라이브러리
- [Lucene](Lucene.md) 검색 엔진 라이브러리
- [Sigar](Sigar.md) 라이브러리 : CPU, Memory, Disk 등을 모니터링
|-
| style="text-align: center;" | logs
| style="text-align: center;" | path.logs
|
로그 파일 저장 폴더
|-
| style="text-align: center;" | plugins
| style="text-align: center;" | path.plugins
|
ElasticSearch 플러그인 설치 폴더
플러그인명/ : Plugin 설치 파일에서 bin/ 폴더와 config/ 폴더를 제외한 파일이 설치되는 폴더
- _site/ : [http://node111.jopenbusiness.com:9200/_plugin/head/](http://node111.jopenbusiness.com:9200/_plugin/head/) URL로 호출
|-
| style="text-align: center;" | work
| style="text-align: center;" | path.work
|
임시 작업용 폴더
|}
- path.home : ElasticSearch가 설치된 폴더를 지정하는 설정 변수
### elasticsearch.yml 설정
[YAML](http://ko.wikipedia.org/wiki/YAML) 문법에 따라 elasticsearch.yml 파일에서 설정 변수를 구성 합니다.
{| border="1" cellspacing="0" cellpadding="2" style="width: 100%;"
|-
| style="text-align: center; background-color: rgb(204, 204, 204);" | 설정 변수
| style="text-align: center; background-color: rgb(204, 204, 204);" | Default
| style="text-align: center; background-color: rgb(204, 204, 204);" | 상세
|-
| style="text-align: center;" | cluster.name
| style="text-align: center;" | elasticsearch
|
Cluster 이름
data/Cluster 이름/ 폴더가 생성됨
|-
| style="text-align: center;" | node.name
| style="text-align: center;" | 0, 1, 2, ...
(자동 생성)
|
Node 이름
data/Cluster 이름/nodes/Node 이름/ 폴더가 생성됨
|-
| style="text-align: center;" |
node.master
node.data
node.client
| style="text-align: center;" |
true
true
false
|
Node 종류
- Master node
- *node.master: true
- *Cluster와 Node의 상태 정보를 관리
- *Index와 Shard의 조정자 역할
- Data node
- *node.data: true
- *색인 데이터를 저장
- Load Balance node
- *node.master: false, node.data: false
- *검색 요청을 받아 분산 처리
- Client node
- *node.client: true, node.master: false
- *Master node로 사용하지 않고 Client node로 사용하고자 할 경우
|-
| style="text-align: center;" | index.number_of_shards
| style="text-align: center;" | 5
| Shard 개수
|-
| style="text-align: center;" | index.number_of_replicas
| style="text-align: center;" | 1
| Replica 개수
|-
| style="text-align: center;" | http.enabled
| style="text-align: center;" | true
| http 서비스를 활성화 합니다.
|-
| style="text-align: center;" | http.port
| style="text-align: center;" | 9200
| http 서비스에서 사용하는 port
|-
| style="text-align: center;" | transport.tcp.port
| style="text-align: center;" | 9300
| [netty](Netty.md)의 Transport에서 사용하는 port
|-
| style="text-align: center;" | transport.tcp.compress
| style="text-align: center;" |
| true이면 Transport에서 압축 허용
|-
| style="text-align: center;" | network.bind_host
| style="text-align: center;" |
| Client의 요청을 접수할 IP 주소
|-
| style="text-align: center;" | network.host
| style="text-align: center;" |
| ElasticSearch Node의 IP 주소
|-
| style="text-align: center;" | gateway.type
| style="text-align: center;" | local
|
Cluster의 메타 정보와 Index 설정, Mapping 정보 등을 어디서 관리할 것인지 지정
Gateway 종류
- local
- shared fs
- hadoop
- s3
|-
| style="text-align: center;" | discovery.zen.minimum_master_nodes
| style="text-align: center;" | 1
| 최소 Master node 개수 (2개 이상 권장)
|-
| style="text-align: center;" | discovery.zen.ping.timeout
| style="text-align: center;" | 3s
|
|-
| style="text-align: center;" | discovery.zen.ping.multicast.enabled
| style="text-align: center;" | true
|
|-
| style="text-align: center;" | discovery.zen.ping.unicast.hosts
| style="text-align: center;" |
| Unicast 사용시 검색할 서버와 포트
예) ["host2:port"]("host1",)
|}
### Schema 설계
curl -XPUT "[http://localhost:9200/aaa?pretty=true](http://localhost:9200/aaa?pretty=true)" -d @aaa.json
{| border="1" cellspacing="0" cellpadding="2" style="width: 100%;"
|-
| style="text-align: center; background-color: rgb(241, 241, 241);" | 단계
| style="text-align: center; background-color: rgb(241, 241, 241);" | 상세
|-
| style="text-align: center;" | Entity 정의
|
- Entity와 Entity Field 정의
- Entity간의 관계 정의
|-
| style="text-align: center;" | Field 정의
|
- 검색 필드 정의
- 통합 검색 필드 정의
- 정렬 필드 정의
- 패싯 필드 정의
- 강조 필드 정의
|}
## Module과 Service
'''ElasticSearch 실행 순서'''
- ElasticSearch
- Bootstrap
- org.elasticsearch.node.NodeBuilder
- org.elasticsearch.node.internal.InternalNode : 생성자 -> start()
- *PluginsService 생성
- **Site Plugin 로딩
- **JVM Plugin 등록
- ***onModuleReferences에 onModule(AnyModule module) 함수 등록
- *Module 등록 : 여기외에 Service 등에서도 Module을 등록할 수 있음
- **[Google Guice](Guice.md)의 DI (Dependency Injection) 사용
- ***org.elasticsearch.common.inject.Module
- ***org.elasticsearch.common.inject.AbstractModule
- **
- **Version, PageCacheRecyclerModule, CircuitBreakerModule, BigArraysModule, PluginsModule
- **SettingsModule, NodeModule, NetworkModule, ScriptModule, EnvironmentModule
- **NodeEnvironmentModule, ClusterNameModule, ThreadPoolModule, discoveryModule, ClusterModule
- **RestModule, TransportModule, HttpServerModule, RiversModule, IndicesModule
- **SearchModule, ActionModule, MonitorModule, GatewayModule, NodeClientModule
- **BulkUdpModule, ShapeModule, PercolatorModule, ResourceWatcherModule, RepositoriesModule
- **TribeModule, BenchmarkModule
- *Service 시작 <- LifecycleComponent (start(), stop(), close(), lifecycleState())
- **AllocationService, Discovery
- **PluginsService를 사용하여 Plugin에 등록된 Service 시작
- **MappingUpdatedAction, IndicesService, IndexingMemoryController, IndicesClusterStateService, IndicesTTLService
- **RiversManager, SnapshotsService, ClusterService, RoutingService, SearchService
- **MonitorService, RestController, TransportService, DiscoveryService, GatewayService
- **HttpServer, BulkUdpService, ResourceWatcherService, TribeService
## Plugin
[ElasticSearch - Plugin](ElasticSearch - Plugin.md)
## Java 개발 환경 구성
### ElasticSearch Java 환경 구성
- [ElasticSearch 다운로드](http://www.elasticsearch.org/download/) 사이트에서 elasticsearch-1.2.1.zip 파일을 다운로드 합니다.
- lib/elasticsearch-1.2.1.jar
- [ElasticSearch github](https://github.com/elasticsearch/elasticsearch/) 사이트에서 elasticsearch-master.zip 파일을 다운로드 합니다.
- src/main/java/ 폴더 아래의 소스 파일을 사용 합니다.
### Lucene Java 환경 구성
- [Lucene](http://lucene.apache.org/) 사이트에서 "DOWNLOAD" 버튼을 눌러 lucene-4.8.1.zip 파일을 다운로드 합니다.
- core/lucene-core-4.8.1.jar
- [Lucene](http://lucene.apache.org/) 사이트에서 "DOWNLOAD" 버튼을 눌러 lucene-4.8.1-src.tgz 파일을 다운로드 합니다.
- core/src/java/ 폴더 아래의 소스 파일을 사용 합니다.
### Arirang Java 환경 구성
- [SVN 저장소](https://lucenekorean.svn.sourceforge.net/svnroot/lucenekorean/)에서 소스를 다운로드 합니다.
- arirang.morph 소스를 먼저 받아 mvn install 진행
- [http://cafe.naver.com/korlucene/1102](http://cafe.naver.com/korlucene/1102)
- [http://svn.apache.org/repos/asf/lucene/dev/branches/lucene4956/lucene/analysis/arirang/](http://svn.apache.org/repos/asf/lucene/dev/branches/lucene4956/lucene/analysis/arirang/)
- 사전 구성 및 사용법
- [http://cafe.naver.com/korlucene/6](http://cafe.naver.com/korlucene/6)
- [http://cafe.naver.com/korlucene/877](http://cafe.naver.com/korlucene/877)
[Arirang 사전.zip](File:Arirang 사전.zip.md) [http://www.jopenbusiness.com/mediawiki/images/5/54/Arirang_사전.zip http://www.jopenbusiness.com/mediawiki/images/5/54/Arirang_사전.zip]
## REST API
- [ElasticSearch - REST API](ElasticSearch - REST API.md)
## JAVA API
### Client
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.node.Node;
private Boolean getTransportClient() {
Settings settings = null;
settings = ImmutableSettings.settingsBuilder().put("cluster.name", CLUSTER_NAME).build();
client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(HOST, PORT));
return true;
}
//--- elasticsearch.yml
//--- cluster.name=~
private Boolean getNodeClient() {
node = nodeBuilder().clusterName(CLUSTER_NAME).client(true).local(true).node();
client = node.client();
return true;
}
### index java api
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import org.elasticsearch.action.index.IndexResponse;
json = jsonBuilder().startObject() .field("name", "value") .endObject().string(); res = client.prepareIndex("index", "type", "id").setSource(json).execute().actionGet();
UtilLogger.info.print(logCaller, "_index : " + res.getIndex()); UtilLogger.info.print(logCaller, "_type : " + res.getType()); UtilLogger.info.print(logCaller, "_id : " + res.getId()); UtilLogger.info.print(logCaller, "_version : " + res.getVersion()); UtilLogger.info.print(logCaller, "_index : " + res.getIndex());
### get java api
## 관리자 매뉴얼
### 사전 구성
1. 국어 사전
1. 온라인 사전을 통해서 주기적으로 갱신
1. 위키에 등록된 명사로 주기적으로 갱신
### elasticsearch.yml
- index.query.bool.max_clause_count
### 오류 처리
- Heap 메모리 부족시
- vi /nas/appl/elasticsearch/bin/elasticsearch.in.sh
#ES_MIN_MEM=256m #ES_MAX_MEM=1g
ES_MIN_MEM=4g ES_MAX_MEM=4g
- 많은 Client에서 접속하여, 파일 개수 부족으로 오류 발생시
- 오류 메시지
org.elasticsearch.common.netty.channel.ChannelException: Failed to create a selector. Caused by: java.io.IOException: Too many open files
- 조치 방법
ulimit -n vi /etc/security/limits.conf hduser soft nofile 999999 hduser hard nofile 999999
```