ElasticSearch
1、网址
文档地址:https://www.elastic.co/guide/index.html(教程加权威指南)
2、版本
1.5(此版本要求jdk 1.7.50及以上)
3、安装
3.1 配置java环境变量(jdk 1.7.50及以上)
3.2 下载压缩包、解压
3.3 运行
./bin/elasticsearch –d
(如果不加-d参数,会前台运行,退出的时候进程也就停止了)
4、配置
4.1配置文件路径config下的elasticsearch.yml(主配置文件)、logging.yml(日志)
4.2 主要配置项说明
cluster.name: xxx #集群名称
node.name: "xxx" #节点名称
index.number_of_shards: 5 #分片数量(每台机器的分片数数据分散到每个片上,减少每个片的总数据量,加快查询速度)
index.number_of_replicas:1 #复制数量(每个分片的备份数,保证数据的完整性)
bootstrap.mlockall: true #锁定内存
network.host: xxx.xxx.xxx.xxx #设置本机地址和发布地址
transport.tcp.port: 9300 #TCP访问端口
http.port: 9200 #HTTP访问端口
#集群间通讯超时以及频次设置
discovery.zen.ping.timeout: 60s
discovery.zen.fd.ping_interval: 5s
discovery.zen.fd.ping_timeout: 60s
discovery.zen.fd.ping_retries: 5
discovery.zen.ping.multicast.enabled: false #不使用广播
discovery.zen.ping.unicast.hosts: ["ip:port"] #其他机器地址,多个用逗号分隔
#以下为慢查询时间设置
index.search.slowlog.threshold.query.warn: 10s
index.search.slowlog.threshold.query.info: 5s
index.search.slowlog.threshold.query.debug: 2s
index.search.slowlog.threshold.query.trace: 500ms
http.cors.enabled: true #允许head插件访问
5、启动文件设置
5.1 打开bin/elasticsearch
5.2保存即可
6、查询语句
6.1增删改查
增索引
$ curl -XPUT 'http://localhost:9200/twitter/'
索引别名(更平顺的迁移数据,不影响线上)
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{ "add" : { "index" : "test1", "alias" : "alias1" } }
]
}'
增Mapping
$ curl -XPUT 'http://localhost:9200/twitter/_mapping/tweet' -d '
{
"tweet" : {
"properties" : {
"message" : {"type" : "string", "store" : true }
}
}
}
'
增加文档
$ curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}'
根据Id查找
curl -XGET 'http://localhost:9200/twitter/tweet/1'
删除
$ curl -XDELETE 'http://localhost:9200/twitter/tweet/1'
更新
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"doc" : {
"name" : "new_name"
}
}'
6.1 query(分词查询,全文检索),
6.2 filter(普通查询)