elasticsearch的安装配置以及初步使用

elasticsearch 是一个全文搜索引擎,支持分布式。搜索速度非常快速。

本文主要进行了 elasticsearch  的安装 配置 以及初步的使用功能;


-- 下载 放到 /data/soft 目录

wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.1.0/elasticsearch-2.1.0.tar.gz
-- 创建用户 esdemo 密码 kinmos1234

-- 进入 bin 目录 elasticsearch 启用 发现报错 需要安装jdk

-- 下载 jdk 放到/data/soft 目录中
加入环境变量
vim /etc/profile

JAVA_HOME=/usr/java/jdk1.8.0_60
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATH

source /etc/profile

java -version

再次 /data/soft/elasticsearch-2/bin/elasticsearch
报错 Exception in thread "main" java.lang.RuntimeException: don't run elasticsearch as root.

发现 不能使用root用户去使用 ,修改 elasticsearch-2 的所属用户组

chown -R root .
chown -R esdemo .
chgrp -R esdemo .

切换esdemo用户 su esdemo

修改配置文件 vim /data/soft/elasticsearch-2.1.0/config/elasticsearch.yml
network.host: 192.168.2.55
http.port: 9200
再次开启 发现成功

/data/soft/elasticsearch-2.1.0/bin/elasticsearch -d 后台启动

浏览器中访问 http://192.168.2.55:9200/ 得到数据
head监控插件安装
1.https://github.com/mobz/elasticsearch-head下载zip 解压
2.创建 elasticsearch-2.1.0\plugins\head文件夹
3.将解压后的elasticsearch-head-master文件夹下的文件copy到head
4.运行es
5.打开
http://192.168.2.55:9200/_plugin/head/
创建索引库
命令行 curl -XPUT '192.168.2.55:9200/kinmosuser?pretty'

访问索引库
http://192.168.2.55:9200/kinmosuser?pretty

监控各个索引库
http://192.168.2.55:9200/_cat/indices?v

修改索引配置
curl -XPUT '192.168.2.55:9200/kinmosuser/_settings' -d '{"number_of_replicas": 1}'

创建文档 添加数据
curl -XPUT '192.168.2.55:9200/kinmosuser/user/1' -d ' { "name":"kinmos","age":22,"address":"zhongguo中国","likes":["basketball","music"]}'
安装中文分词
下载
cd /data/soft
wget https://github.com/medcl/elasticsearch-analysis-ik/archive/master.zip
解压
unzip master.zip
cd elasticsearch-analysis-ik-master/

https://github.com/medcl/elasticsearch-rtf 中找到 elasticsearch-analysis-ik-5.1.1.jar
放到 /data/soft/elasticsearch-2.1.0/plugins/analysis-ik 中 并将 elasticsearch-analysis-ik-master 中的文件放到 analysis-ik 中
然后编辑配置文件elasticsearch.yml ,在后面加一行:

index.analysis.analyzer.ik.type : "ik"

ik设置
修改config/IKAnalyzer.cfg.xml
<entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic;custom/sougou.dic</entry>
增加:custom/sougou.dic 分词库

====================操作================================
创建 kinmosuser 索引
curl -XPUT 'http://192.168.2.55:9200/kinmosuser?pretty'

也可以设置的方式创建:
curl -XPUT 'http://192.168.2.55:9200/kinmosuser/' -d '
“settings”:{
“number_of_shards”:3,
“number_of_replicas”:2
}
}'

//删除索引
curl -XDELETE 'http://192.168.2.55:9200/kinmosuser'

索引创建API可以接受一个或者一组映射选项
curl -XPOST localhost:9200/test -d ‘{
“settings”:{
“number_of_shards”:3
},
“mappings”:{
“type1”:{
“_source”:{“enabled”:false},
“preperties”:{
“field1”:{
“type”:”string”,
”index”:”not_analyzed”
}
}
}
}
}’
1. create:

指定 ID 来建立新记录。 (貌似PUT, POST都可以)
$ curl -XPOST localhost:9200/films/md/2 -d '
{ "name":"hei yi ren", "tag": "good"}'

返回:
{
"ok" : true,
"_index" : "twitter",
"_type" : "tweet",
"_id" : "1"
}

也可以使用自动生成的 ID 建立新纪录:
$ curl -XPOST localhost:9200/films/md -d '
{"id":"1", "name":"ma da jia si jia3", "tag": "good"}'

每一个被索引的文档都会有一个版本号,被关联的版本号会作为index API的请求的响应信息一部分返回回来。因此,我们可以在对索引操作的时候,指定特定的版本号,操作对应版本的文档。例如
curl -XPUT ‘localhost:9200/twitter/tweet/1?version=2’ -d ‘{
“message”:”elasticsearch now has versioning support,double cool!”
}’

另外的一种create方式
curl -XPUT ‘http://localhost:9200/twitter/tweet/1/_create’ -d ‘{
“user”:”kimchy”,
“post_date”:”2009-11-11T14:12:12”,
“message”:”hello,world”
}’
2. 查询:
2.1 查询所有的 index, type:
$ curl localhost:9200/_search?pretty=true

2.2 查询某个index下所有的type:
$ curl http://192.168.2.55:9200/kinmosuser/_search

2.3 查询某个index 下, 某个 type下所有的记录:
$ curl 'http://192.168.2.55:9200/kinmosuser/user/_search?pretty=true'

2.4 带有参数的查询:
$ curl localhost:9200/films/md/_search?q=tag:good
结果:
{"took":7,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"film","_type":"md","_id":"2","_score":1.0, "_source" :
{ "name":"hei yi ren", "tag": "good"}},{"_index":"film","_type":"md","_id":"1","_score":0.30685282, "_source" :
{ "name":"ma da jia si jia", "tag": "good"}}]}}

http://10.70.40.250:9200/dl_pro_product/product/_search?q=first:zhuang1444

2.5 使用JSON参数的查询: (注意 query 和 term 关键字)
$ curl localhost:9200/film/_search -d '
{"query" : { "term": { "tag":"bad"}}}'

3. update
$ curl -XPUT localhost:9200/films/md/1 -d { ...(data)... }

4. 删除。 删除所有的:
$ curl -XDELETE localhost:9200/films
http://10.70.40.250:9200/dl_pro_product/product/_analyze?field=af_productname&pretty=true

transaction log
每个分片都有一个关联的事务日志文件或者预写日志文件,用来保证索引的写和删除是原子操作,你不需要显式的去提交每个请求(对应于lucene的commit),Flush(“commit”)的触发基于以下几个参数:

配置
说明

index.translog.flush_threshold_ops
设置当累计操作达到多少时就执行flush操作,默认值 5000.

index.translog.flush_threshold_size
一旦你的事务日志文件的大小(translog)达到设置的这个值,则开始执行flush操作,默认值 200mb.

index.translog.flush_threshold_period
每隔多长时间执行一次flush,默认 30m.
注意:这些参数可以在运行时使用索引设置更新API来更新。(例如,当在执行批量更新时,这些数字需要加大,以支持更高的TPS)
每个碎片都有一个事务日志文件,事务日志文件主要是为了保证索引写和删除过程的可靠性,你不需要显式的去提交每个请求(对应于lucene的commit),这一切都是自动的,你也可以显式的执行flush操作来进行请求的提交,还可以使用下面这些参数来进行控制:
配置 说明
index.translog.flush_threshold_ops 设置当累计操作达到多少时就执行flush操作,默认值 5000.
index.translog.flush_threshold_size 一旦你的事务日志文件的大小(translog)达到设置的这个值,则开始执行flush操作,默认值500mb.
index.translog.flush_threshold_period 每隔多长时间执行一次flush,默认 60m.

 

posted on 2017-05-09 11:56  轻浮不韪  阅读(193)  评论(0编辑  收藏  举报