elasticsearch 是一个全文搜索引擎,支持分布式。搜索速度很是快速。java
本文主要进行了 elasticsearch 的安装 配置 以及初步的使用功能;git
-- 下载 放到 /data/soft 目录github
wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.1.0/elasticsearch-2.1.0.tar.gz
-- 建立用户 esdemo 密码 kinmos1234vim
-- 进入 bin 目录 elasticsearch 启用 发现报错 须要安装jdk浏览器
-- 下载 jdk 放到/data/soft 目录中
加入环境变量
vim /etc/profileapp
JAVA_HOME=/usr/java/jdk1.8.0_60
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATHcurl
source /etc/profileelasticsearch
java -version分布式
再次 /data/soft/elasticsearch-2/bin/elasticsearch
报错 Exception in thread "main" java.lang.RuntimeException: don't run elasticsearch as root.post
发现 不能使用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.