【零】简介
Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎,分布式,十分高效。
主要的特点:
- 分布式的实时文件存储,每个字段都被索引并可被搜索
- 分布式的实时分析搜索引擎
- 可以扩展到上百台服务器,处理PB级结构化或非结构化数据
【一】下载、安装、启动、安装插件
sudo wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/zip/elasticsearch/2.1.1/elasticsearch-2.1.1.zip sudo unzip elasticsearch-2.1.1.zip sudo mv /home/username/elasticsearch-2.1.1 /usr/lib/elasticsearch #启动 sudo ./bin/elasticsearch
启动报错
[[email protected] elasticsearch]$ sudo ./bin/elasticsearch Exception in thread "main" java.lang.RuntimeException: don't run elasticsearch as root. at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:93) at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:144) at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:285) at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:35) Refer to the log for complete error details.
原因是,ElasticSearch不能作为root启动
sudo chown huanghaifeng elasticsearch
sudo chown huanghaifeng elasticsearch/*
sudo chown huanghaifeng:huanghaifeng elasticsearch/log
./bin/elasticsearch
用自己的账户开启,Permission denied;用sudo,直接不给开起
检查是否开启
curl 'http://localhost:9200/?pretty'
#输出
{
"name" : "Torrent",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "2.1.1",
"build_hash" : "40e2c53a6b6c2972b3d13846e450e66f4375bd71",
"build_timestamp" : "2015-12-15T13:05:55Z",
"build_snapshot" : false,
"lucene_version" : "5.3.1"
},
"tagline" : "You Know, for Search"
}
安装插件
./bin/plugin -h ./bin/plugin install mobz/elasticsearch-head ./bin/plugin list #其他常用的插件安装 http://www.cnblogs.com/wgp13x/p/4859680.html
打开http://10.60.81.183:9200/_plugin/head/出现问题,一直是“无法显示此网页”。
解决办法
#检查监听网络 netstat -an #编辑配置文件,添加network.host和http.port vi elasticsearch.yml # ---------------------------------- Network ----------------------------------- # Set the bind address to a specific IP (IPv4 or IPv6): network.host: 10.60.81.183 # Set a custom port for HTTP: http.port: 9200
重启elasticsearch,即可打开网页
最后给出一个对应关系,让读者理解elasticsearch
Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices -> Types -> Documents -> Fields
【二】新建索引、简单查询、基本使用
API : curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
-
VERB HTTP方法:
GET,POST,PUT,HEAD,DELETE - PROTOCOL http或者https协议(只有在Elasticsearch前面有https代理的时候可用)
- HOST Elasticsearch集群中的任何一个节点的主机名,如果是在本地的节点,那么就叫localhost
- PORT Elasticsearch HTTP服务所在的端口,默认为9200
- PATH API路径(例如_count将返回集群中文档的数量),PATH可以包含多个组件,例如_cluster/stats或者_nodes/stats/jvm
-
QUERY_STRING 一些可选的查询请求参数,例如
?pretty参数将使请求返回更加美观易读的JSON数据 - BODY 一个JSON格式的请求主体(如果请求需要的话)
#插入数据
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
#查询数据量
curl -XGET 'http://localhost:9200/_count' -d '
{
"query":{
"match_all":{}
}
}'
#查询单个文档详情
curl -XGET http://10.60.81.183:9200/megacorp/employee/1?pretty
#简单查询接口调用
GET /megacorp/employee/_search?pretty
#添加条件检索
curl -XGET http://10.60.81.183:9200/megacorp/employee/_search?q=last_name:Smith
【三】DSL(Domain Specific Language特定领域语言)查询(POST)
#模糊查询查询,以空格为区分的单词匹配
#返回结果是包含查询条件里面的一个或多个词
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
#全词匹配,又称为短语匹配,将match关键词改为match_phrase即可
#过滤器
{
"query": {
"filtered": {
"filter": {
"range": {
"age": {
"gt": "20"
}
}
},
"query": {
"match": {
"last_name": "smith"
}
}
}
}
}
elasticsearch的结果默认按照结果相关性分数(_score)排序。
#使用DSL做统计分析
#姓氏为smith的人中,有各种爱好的人平均年龄
{
"query": {
"match": {
"last_name": "smith"
}
},
"aggs": {
"all_interests": {
"terms": {
"field": "interests"
},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
}
}
#附上一篇比较完整的查询语句
{
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"match": {
"IsDel": "0"
}
}
]
}
},
"filter": {
"bool": {
"must": [
{
"range": {
"PostDate": {
"gt": "2015-08-05 09:00:00",
"lt": "2015-09-02 09:00:00"
}
}
}
]
}
}
}
},
"sort": [
{
"PostDate": {
"order": "desc"
},
"ID": {
"order": "desc"
}
}
],
"size": 20,
"from": 0
}
(更多详细内容请关注后续总结文章)