- 浏览: 146066 次
文章分类
最新评论
-
x_looking:
Client client = new TransportCl ...
ELASTICSEARCH常见问题 -
辣de冷wmyes:
ElasticSearch视频教程百度网盘地址:http:// ...
一、Elasticsearch安装使用教程
环境准备:
1、预先安装jdk7(Elasticsearch 官方推荐使用 Oracle JDK 7)
Elasticsearch是一个全文搜索引擎。
2、安装
第一步:安装es
wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.zip
unzip elasticsearch-1.7.2.zip
cd elasticsearch
测试正常启动:./bin/elasticsearch
haow@Lenovo:/haow/es/elasticsearch-1.7.2$ ./bin/elasticsearch
[2015-09-16 16:58:48,128][INFO ][node ] [Ebon Seeker] version[1.7.2], pid[19814], build[e43676b/2015-09-14T09:49:53Z]
[2015-09-16 16:58:48,129][INFO ][node ] [Ebon Seeker] initializing ...
[2015-09-16 16:58:48,226][INFO ][plugins ] [Ebon Seeker] loaded [], sites []
[2015-09-16 16:58:48,420][INFO ][env ] [Ebon Seeker] using [1] data paths, mounts [[/ (/dev/sda1)]], net usable_space [337.1gb], net total_space [450.5gb], types [ext4]
[2015-09-16 16:58:50,850][INFO ][node ] [Ebon Seeker] initialized
[2015-09-16 16:58:50,850][INFO ][node ] [Ebon Seeker] starting ...
[2015-09-16 16:58:51,036][INFO ][transport ] [Ebon Seeker] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/172.20.5.14:9300]}
[2015-09-16 16:58:51,063][INFO ][discovery ] [Ebon Seeker] elasticsearch/vpM2w98XRr-IXA5FttPVGA
[2015-09-16 16:58:54,834][INFO ][cluster.service ] [Ebon Seeker] new_master [Ebon Seeker][vpM2w98XRr-IXA5FttPVGA][Lenovo][inet[/172.20.5.14:9300]], reason: zen-disco-join (elected_as_master)
[2015-09-16 16:58:54,846][INFO ][http ] [Ebon Seeker] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/172.20.5.14:9200]}
[2015-09-16 16:58:54,847][INFO ][node ] [Ebon Seeker] started
[2015-09-16 16:58:55,865][INFO ][gateway ] [Ebon Seeker] recovered [0] indices into cluster_state
没报错表示安装成功,Ctrl+c 停掉
elasticsearchservicewrapper安装
这个是对elasticsearch执行命令的包装服务,安装之后,方便elasticsearch的启动,停止等等操作。
(1)下载elasticsearchservicewrapper
git clone https://github.com/elasticsearch/elasticsearch-servicewrapper,然后将目录下的service目录拷贝至ES_HOME/bin目录下。
(2)简单配置jvm的内存
修改ES_HOME/bin/service/elasticsearch.conf,set.default.ES_HEAP_SIZE=1024,该值根据机器的配置可自定义。
(3)安装启动服务
执行命令:ES_HOME/bin/service/elasticsearch install
(4)启动/停止/重启服务
执行命令:ES_HOME/bin/service/elasticsearch start/stop/restart
第二步、安装plugin(部分可选)
集群安装成功之后,需要对集群中的索引数据、运行情况等信息进行查看,索引需要安装一些插件,方面后续工作。工欲善其事必先利其器,下面介绍几个实用的插件:
Marvel
Marvel是一个管理和监控Elasticsearch的工具。它提供一个叫Sense的交互式接口方便通过浏览器与Elasticsearch交互。(建议安装)
命令:bin/plugin -i elasticsearch/marvel/latest
如果不想使用Marvel监控本地集群,可以使用如下方式关闭Marvel监控
echo 'marvel.agent.enabled: false' >> ./config/elasticsearch.yml可以通过前台的方式启动Elasticsearch
bin]$ sudo ./elasticsearch
使用-d参数可以将Elasticsearch放到后台运行
bin]$ sudo ./elasticsearch -d
head
和插件名字一样,首推这个插件。通过head,可以查看集群几乎所有信息,还能进行简单的搜索查询,观察自动恢复的情况等等。
执行下面的命令开始安装:sudo ./bin/plugin -install mobz/elasticsearch-head
记得两台机器都安装一下哦。安装完成之后,
可以通过浏览器输入:http://ip:9200/_plugin/head/ ,可以查看显示效果。如下图:
还有很多功能,请读者自己去体验。
bigdesk
bigdesk是elasticsearch的一个集群监控工具,可以通过它来查看es集群的各种状态,如:cpu、内存使用情况,索引数据、搜索情况,http连接数等。项目git地址: https://github.com/lukas-vlcek/bigdesk。和head一样,它也是个独立的网页程序,使用方式和head一样。
插件安装运行:
方法1:
1.bin/plugin -install lukas-vlcek/bigdesk
2.运行es
3.打开http://localhost:9200/_plugin/bigdesk/
当然,也可以直接下载源码运行index.html
方法2:
1.https://github.com/lukas-vlcek/bigdesk下载zip 解压
2.建立elasticsearch-1.0.0\plugins\bigdesk\_site文件
3.将解压后的bigdesk-master文件夹下的文件copy到_site
4.运行es
5.打开http://localhost:9200/_plugin/bigdesk/
kopf
Kopf是一个ElasticSearch的管理工具,它也提供了对ES集群操作的API。
./elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf/{branch|version}
open http://localhost:9200/_plugin/kopf
./plugin --install lmenezes/elasticsearch-kopf/v1.6.1
3、简要配置和访问命令
通过config/elasticsearch.yml设置cluster.name和node.name
可以通过以下方式关闭Elasticsearch
curl -XPOST 'http://localhost:9200/_shutdown'
Talking to Elasticsearch
根据是否使用Java语言,与Elasticsearch交互有几种方法,如果是Java API参见文档
http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index.html
如果使用其他语言,则使用Elasticsearch提供的RESTFUL API,或者可以直接使用linux命令curl访问
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/?<QUERY_STRING>' -d '<BODY>'
VER HTTP请求方式,GET,POST,PUT,HEAD或DELETE
PROTOCOL 使用HTTP或者HTTPS
HOST es集群中的任意一个node的主机名,如果是在node本机就直接使用localhost
PORT Elasticsearch运行HTTP服务的端口,默认是9200
QUERY_STRING 查询参数
BODY JSON格式的请求数据
4、测试
检查es:
查看Elasticsearch中的数据
$ curl "http://localhost:9200/?pretty"
{
"status" : 200,
"name" : "xxx",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "1.4.2",
"build_hash" : "927caff6f05403e936c20bf4529f144f0c89fd8c",
"build_timestamp" : "2014-12-16T14:11:12Z",
"build_snapshot" : false,
"lucene_version" : "4.10.2"
},
"tagline" : "You Know, for Search"
}
通过config/elasticsearch.yml设置cluster.name和node.name
$ curl -XGET 'http://localhost:9200/_count?pretty' -d '
{
"query": {
"match_all": {}
}
}
'
{
"count" : 22692,
"_shards" : {
"total" : 3,
"successful" : 3,
"failed" : 0
}
}
$ curl -i -XGET 'localhost:9200/'
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 334
{
"status" : 200,
"name" : "jidong",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "1.4.2",
"build_hash" : "927caff6f05403e936c20bf4529f144f0c89fd8c",
"build_timestamp" : "2014-12-16T14:11:12Z",
"build_snapshot" : false,
"lucene_version" : "4.10.2"
},
"tagline" : "You Know, for Search"
}
Relational DB Databases Tables Rows Columns
Elasticsearch Indices Types Documents Fields
测试Marvel
通过Marvel的Sense接口访问Elasticsearch
http://xxxx.com:9200/_plugin/marvel/sense/index.html
以下直接使用GET或PUT的简略形式,都是直接使用Marvel的Sense接口访问Elasticsearch,可以点击“Copy as cURL”查看对应的curl命令写法
Elasticsearch可以横向扩展到几百台服务器,处理PB以上的数据。
a、存放数据
PUT /megacorp/employee/1
{
"first_name":"John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests":["sports","music"]
}
/megacorp/employee/1
这个路径包含三个信息
megacorp 索引名称,类似关系型数据库的数据库名称
employee 类型名称,类似关系型数据库的表名称
1 特定employee的ID
PUT /megacorp/employee/2
{
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests": ["music"]
}
PUT /megacorp/employee/3
{
"first_name": "Douglas",
"last_name": "Fir",
"age": 35,
"about": "I like to build cabinets",
"interests": ["forestry"]
}
2、查询数据
在Sense中输入
GET /megacorp/employee/1
显示结果
{
"_index": "megacorp",
"_type": "employee",
"_id": "1",
"_version":1,
"found": true,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": ["sports","music"]
}
}
GET /megacorp/employee/_search
{
"took": 6,
"timed_out":false,
"_shards":{...},
"hits":{
"total": 3,
"max_score": 1,
"hits":[
{
"_index": "megacorp",
"_type": "employee",
"_id": "3",
"_score": 1,
"_source":{
"first_name": "Douglas",
"last_name": "Fir",
"age": 35,
"about": "I like to build cabinets",
"interests":["forestry"]
}
},
{
"_index": "megacorp",
"_type": "employee",
"_id": "1",
"_score": 1,
"_source":{
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests":["sports","music"]
}
},
{
"_index": "megacorp",
"_type": "employee",
"_id": "2",
"_score": 1,
"_source":{
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests":["music"]
}
}
]
}
}
GET /megacorp/employee/_search?q=last_name:Smith
{
...
"hits":{
"total": 2,
"max_score": 0.30685282,
"hits":[
{
...
"_source":{
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests":["sports","music"]
}
},
{
...
"_source":{
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests":["music"]
}
}
]
}
}
Elasticsearch提供了一个丰富的,灵活的查询语言,叫做DSL.Domain-specific language(DSL)使用特定的JSON请求。
GET /megacorp/employee/_search
{
"query":{
"match":{
"last_name":"Smith"
}
}
}
这里没有使用查询参数,使用match匹配查询条件。输出结果和上个例子相同。
查找所有last name为Smith,年龄大于30的员工
[object Object][object Object]
{
...
"hits":{
"total": 1,
"max_score": 0.30685282,
"hits":[
{
...
"_source":{
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests":["music"]
}
}
]
}
}
Full-text search 全文搜索
搜索所有喜欢rock climbing的员工
在Sense中输入
GET /megacorp/employee/_search
{
"query":{
"match":{
"about":"rock climbing"
}
}
}
查看查询结果
[object Object][object Object]
默认情况下,Elasticsearch根据匹配结果的relevance score进行排序,表示匹配程度。可以看到第二个匹配结果只包含rock也被显示出来。
如果想要完全匹配查询条件,可以使用短语搜索phrase search
使用match_phrase进行条件匹配
GET /megacorp/employee/_search
{
"query":{
"match_phrase":{
"about":"rock climbing"
}
}
}
{
...
"hits":{
"total": 1,
"max_score": 0.23013961,
"hits":[
{
...
"_score": 0.23013961,
"_source":{
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests":["sports","music"]
}
}
]
}
}
现在就只有一条搜索结果
Highlight our searches高亮显示查询结果
GET /megacorp/employee/_search
{
"query":{
"match_phrase":{
"about":"rock climbing"
}
},
"highlight":{
"fields":{
"about":{}
}
}
}
{
...
"hits":{
"total": 1,
"max_score": 0.23013961,
"hits":[
{
...
"_score": 0.23013961,
"_source":{
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests":["sports","music"]
},
"highlight":{
"about":[
"I love to go <em>rock</em> <em>climbing</em>"
]
}
}
]
}
}
使用Elasticsearch的聚合函数可以对数据进行复杂的分析。类似SQL语言的GROUP BY语句。
GET /megacorp/employee/_search
{
"aggs":{
"all_interests":{
"terms":{"field":"interests"}
}
}
}
{
...
"hits":{...},
"aggregations":{
"all_interests":{
"buckets":[
{
"key": "music",
"doc_count":2
},
{
"key": "forestry",
"doc_count":1
},
{
"key": "sports",
"doc_count":1
}
]
}
}
}
GET /megacorp/employee/_search
{
"query":{
"match":{
"last_name":"smith"
}
},
"aggs":{
"all_interests":{
"terms":{
"field":"interests"
}
}
}
}
...
"all_interests":{
"buckets":[
{
"key":"music",
"doc_count":2
},
{
"key":"sports",
"doc_count":1
}
]
}
Elasticsearch可以横向扩展到几百台服务器,处理PB以上的数据。
1、预先安装jdk7(Elasticsearch 官方推荐使用 Oracle JDK 7)
Elasticsearch是一个全文搜索引擎。
2、安装
第一步:安装es
wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.zip
unzip elasticsearch-1.7.2.zip
cd elasticsearch
测试正常启动:./bin/elasticsearch
haow@Lenovo:/haow/es/elasticsearch-1.7.2$ ./bin/elasticsearch
[2015-09-16 16:58:48,128][INFO ][node ] [Ebon Seeker] version[1.7.2], pid[19814], build[e43676b/2015-09-14T09:49:53Z]
[2015-09-16 16:58:48,129][INFO ][node ] [Ebon Seeker] initializing ...
[2015-09-16 16:58:48,226][INFO ][plugins ] [Ebon Seeker] loaded [], sites []
[2015-09-16 16:58:48,420][INFO ][env ] [Ebon Seeker] using [1] data paths, mounts [[/ (/dev/sda1)]], net usable_space [337.1gb], net total_space [450.5gb], types [ext4]
[2015-09-16 16:58:50,850][INFO ][node ] [Ebon Seeker] initialized
[2015-09-16 16:58:50,850][INFO ][node ] [Ebon Seeker] starting ...
[2015-09-16 16:58:51,036][INFO ][transport ] [Ebon Seeker] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/172.20.5.14:9300]}
[2015-09-16 16:58:51,063][INFO ][discovery ] [Ebon Seeker] elasticsearch/vpM2w98XRr-IXA5FttPVGA
[2015-09-16 16:58:54,834][INFO ][cluster.service ] [Ebon Seeker] new_master [Ebon Seeker][vpM2w98XRr-IXA5FttPVGA][Lenovo][inet[/172.20.5.14:9300]], reason: zen-disco-join (elected_as_master)
[2015-09-16 16:58:54,846][INFO ][http ] [Ebon Seeker] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/172.20.5.14:9200]}
[2015-09-16 16:58:54,847][INFO ][node ] [Ebon Seeker] started
[2015-09-16 16:58:55,865][INFO ][gateway ] [Ebon Seeker] recovered [0] indices into cluster_state
没报错表示安装成功,Ctrl+c 停掉
elasticsearchservicewrapper安装
这个是对elasticsearch执行命令的包装服务,安装之后,方便elasticsearch的启动,停止等等操作。
(1)下载elasticsearchservicewrapper
git clone https://github.com/elasticsearch/elasticsearch-servicewrapper,然后将目录下的service目录拷贝至ES_HOME/bin目录下。
(2)简单配置jvm的内存
修改ES_HOME/bin/service/elasticsearch.conf,set.default.ES_HEAP_SIZE=1024,该值根据机器的配置可自定义。
(3)安装启动服务
执行命令:ES_HOME/bin/service/elasticsearch install
(4)启动/停止/重启服务
执行命令:ES_HOME/bin/service/elasticsearch start/stop/restart
第二步、安装plugin(部分可选)
集群安装成功之后,需要对集群中的索引数据、运行情况等信息进行查看,索引需要安装一些插件,方面后续工作。工欲善其事必先利其器,下面介绍几个实用的插件:
Marvel
Marvel是一个管理和监控Elasticsearch的工具。它提供一个叫Sense的交互式接口方便通过浏览器与Elasticsearch交互。(建议安装)
命令:bin/plugin -i elasticsearch/marvel/latest
如果不想使用Marvel监控本地集群,可以使用如下方式关闭Marvel监控
echo 'marvel.agent.enabled: false' >> ./config/elasticsearch.yml可以通过前台的方式启动Elasticsearch
bin]$ sudo ./elasticsearch
使用-d参数可以将Elasticsearch放到后台运行
bin]$ sudo ./elasticsearch -d
head
和插件名字一样,首推这个插件。通过head,可以查看集群几乎所有信息,还能进行简单的搜索查询,观察自动恢复的情况等等。
执行下面的命令开始安装:sudo ./bin/plugin -install mobz/elasticsearch-head
记得两台机器都安装一下哦。安装完成之后,
可以通过浏览器输入:http://ip:9200/_plugin/head/ ,可以查看显示效果。如下图:
还有很多功能,请读者自己去体验。
bigdesk
bigdesk是elasticsearch的一个集群监控工具,可以通过它来查看es集群的各种状态,如:cpu、内存使用情况,索引数据、搜索情况,http连接数等。项目git地址: https://github.com/lukas-vlcek/bigdesk。和head一样,它也是个独立的网页程序,使用方式和head一样。
插件安装运行:
方法1:
1.bin/plugin -install lukas-vlcek/bigdesk
2.运行es
3.打开http://localhost:9200/_plugin/bigdesk/
当然,也可以直接下载源码运行index.html
方法2:
1.https://github.com/lukas-vlcek/bigdesk下载zip 解压
2.建立elasticsearch-1.0.0\plugins\bigdesk\_site文件
3.将解压后的bigdesk-master文件夹下的文件copy到_site
4.运行es
5.打开http://localhost:9200/_plugin/bigdesk/
kopf
Kopf是一个ElasticSearch的管理工具,它也提供了对ES集群操作的API。
./elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf/{branch|version}
open http://localhost:9200/_plugin/kopf
./plugin --install lmenezes/elasticsearch-kopf/v1.6.1
3、简要配置和访问命令
通过config/elasticsearch.yml设置cluster.name和node.name
可以通过以下方式关闭Elasticsearch
curl -XPOST 'http://localhost:9200/_shutdown'
Talking to Elasticsearch
根据是否使用Java语言,与Elasticsearch交互有几种方法,如果是Java API参见文档
http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index.html
如果使用其他语言,则使用Elasticsearch提供的RESTFUL API,或者可以直接使用linux命令curl访问
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/?<QUERY_STRING>' -d '<BODY>'
VER HTTP请求方式,GET,POST,PUT,HEAD或DELETE
PROTOCOL 使用HTTP或者HTTPS
HOST es集群中的任意一个node的主机名,如果是在node本机就直接使用localhost
PORT Elasticsearch运行HTTP服务的端口,默认是9200
QUERY_STRING 查询参数
BODY JSON格式的请求数据
4、测试
检查es:
查看Elasticsearch中的数据
$ curl "http://localhost:9200/?pretty"
{
"status" : 200,
"name" : "xxx",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "1.4.2",
"build_hash" : "927caff6f05403e936c20bf4529f144f0c89fd8c",
"build_timestamp" : "2014-12-16T14:11:12Z",
"build_snapshot" : false,
"lucene_version" : "4.10.2"
},
"tagline" : "You Know, for Search"
}
通过config/elasticsearch.yml设置cluster.name和node.name
$ curl -XGET 'http://localhost:9200/_count?pretty' -d '
{
"query": {
"match_all": {}
}
}
'
{
"count" : 22692,
"_shards" : {
"total" : 3,
"successful" : 3,
"failed" : 0
}
}
$ curl -i -XGET 'localhost:9200/'
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 334
{
"status" : 200,
"name" : "jidong",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "1.4.2",
"build_hash" : "927caff6f05403e936c20bf4529f144f0c89fd8c",
"build_timestamp" : "2014-12-16T14:11:12Z",
"build_snapshot" : false,
"lucene_version" : "4.10.2"
},
"tagline" : "You Know, for Search"
}
Relational DB Databases Tables Rows Columns
Elasticsearch Indices Types Documents Fields
测试Marvel
通过Marvel的Sense接口访问Elasticsearch
http://xxxx.com:9200/_plugin/marvel/sense/index.html
以下直接使用GET或PUT的简略形式,都是直接使用Marvel的Sense接口访问Elasticsearch,可以点击“Copy as cURL”查看对应的curl命令写法
Elasticsearch可以横向扩展到几百台服务器,处理PB以上的数据。
a、存放数据
PUT /megacorp/employee/1
{
"first_name":"John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests":["sports","music"]
}
/megacorp/employee/1
这个路径包含三个信息
megacorp 索引名称,类似关系型数据库的数据库名称
employee 类型名称,类似关系型数据库的表名称
1 特定employee的ID
PUT /megacorp/employee/2
{
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests": ["music"]
}
PUT /megacorp/employee/3
{
"first_name": "Douglas",
"last_name": "Fir",
"age": 35,
"about": "I like to build cabinets",
"interests": ["forestry"]
}
2、查询数据
在Sense中输入
GET /megacorp/employee/1
显示结果
{
"_index": "megacorp",
"_type": "employee",
"_id": "1",
"_version":1,
"found": true,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": ["sports","music"]
}
}
GET /megacorp/employee/_search
{
"took": 6,
"timed_out":false,
"_shards":{...},
"hits":{
"total": 3,
"max_score": 1,
"hits":[
{
"_index": "megacorp",
"_type": "employee",
"_id": "3",
"_score": 1,
"_source":{
"first_name": "Douglas",
"last_name": "Fir",
"age": 35,
"about": "I like to build cabinets",
"interests":["forestry"]
}
},
{
"_index": "megacorp",
"_type": "employee",
"_id": "1",
"_score": 1,
"_source":{
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests":["sports","music"]
}
},
{
"_index": "megacorp",
"_type": "employee",
"_id": "2",
"_score": 1,
"_source":{
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests":["music"]
}
}
]
}
}
GET /megacorp/employee/_search?q=last_name:Smith
{
...
"hits":{
"total": 2,
"max_score": 0.30685282,
"hits":[
{
...
"_source":{
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests":["sports","music"]
}
},
{
...
"_source":{
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests":["music"]
}
}
]
}
}
Elasticsearch提供了一个丰富的,灵活的查询语言,叫做DSL.Domain-specific language(DSL)使用特定的JSON请求。
GET /megacorp/employee/_search
{
"query":{
"match":{
"last_name":"Smith"
}
}
}
这里没有使用查询参数,使用match匹配查询条件。输出结果和上个例子相同。
查找所有last name为Smith,年龄大于30的员工
[object Object][object Object]
{
...
"hits":{
"total": 1,
"max_score": 0.30685282,
"hits":[
{
...
"_source":{
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests":["music"]
}
}
]
}
}
Full-text search 全文搜索
搜索所有喜欢rock climbing的员工
在Sense中输入
GET /megacorp/employee/_search
{
"query":{
"match":{
"about":"rock climbing"
}
}
}
查看查询结果
[object Object][object Object]
默认情况下,Elasticsearch根据匹配结果的relevance score进行排序,表示匹配程度。可以看到第二个匹配结果只包含rock也被显示出来。
如果想要完全匹配查询条件,可以使用短语搜索phrase search
使用match_phrase进行条件匹配
GET /megacorp/employee/_search
{
"query":{
"match_phrase":{
"about":"rock climbing"
}
}
}
{
...
"hits":{
"total": 1,
"max_score": 0.23013961,
"hits":[
{
...
"_score": 0.23013961,
"_source":{
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests":["sports","music"]
}
}
]
}
}
现在就只有一条搜索结果
Highlight our searches高亮显示查询结果
GET /megacorp/employee/_search
{
"query":{
"match_phrase":{
"about":"rock climbing"
}
},
"highlight":{
"fields":{
"about":{}
}
}
}
{
...
"hits":{
"total": 1,
"max_score": 0.23013961,
"hits":[
{
...
"_score": 0.23013961,
"_source":{
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests":["sports","music"]
},
"highlight":{
"about":[
"I love to go <em>rock</em> <em>climbing</em>"
]
}
}
]
}
}
使用Elasticsearch的聚合函数可以对数据进行复杂的分析。类似SQL语言的GROUP BY语句。
GET /megacorp/employee/_search
{
"aggs":{
"all_interests":{
"terms":{"field":"interests"}
}
}
}
{
...
"hits":{...},
"aggregations":{
"all_interests":{
"buckets":[
{
"key": "music",
"doc_count":2
},
{
"key": "forestry",
"doc_count":1
},
{
"key": "sports",
"doc_count":1
}
]
}
}
}
GET /megacorp/employee/_search
{
"query":{
"match":{
"last_name":"smith"
}
},
"aggs":{
"all_interests":{
"terms":{
"field":"interests"
}
}
}
}
...
"all_interests":{
"buckets":[
{
"key":"music",
"doc_count":2
},
{
"key":"sports",
"doc_count":1
}
]
}
Elasticsearch可以横向扩展到几百台服务器,处理PB以上的数据。
发表评论
-
使用 Scripted Metric Aggregation 遇到的问题
2016-02-19 14:12 2404使用Scripted Metric Aggregation进行 ... -
ElasticSearch性能优化策略
2016-02-17 19:42 3489ElasticSearch性能优化主要分为4个方面的优化。 一 ... -
Elasticsearch 数据建模 - 处理关联关系
2016-02-17 14:40 1022数据建模(Modeling Your Data ... -
数据类型转换错误
2016-02-15 18:28 1556异常: Invalid shift value (64) in ... -
elasticsearch 排序异常,关键字冲突
2016-02-03 12:35 5806org.elasticsearch.action.search ... -
ElasticSearch不同类型下同名字段排序错误
2016-02-03 11:18 1896虽然之前知道elasticsearch不同类型下同名字段要慎用 ... -
ElasticSearch immense term错误
2016-02-02 19:07 2570在使用ElasticSearch的过程中遇到了一个immens ... -
ELASTICSEARCH常见问题
2015-09-22 15:39 31581. ELASTICSEARCH建索引过程中崩溃问题追查 by ... -
routing实例1
2015-09-21 18:51 0package com.eg.part1; import j ... -
routing实例
2015-09-21 18:49 0创建索引: PUT /useraudit_v1 创建索引别名: ... -
elasticsearch 精确,模糊查询实例
2015-09-19 23:55 62518实例 http://www.bubuko.com/infode ... -
elasticsearch java调用实例
2015-09-19 23:55 3499http://outofmemory.cn/code-snip ... -
[维护]Elasticsearch零停机时间更新索引配置或迁移索引
2015-09-18 10:16 751另外一篇文章:http://blog.csdn.net/dm_ ... -
elasticsearch 的mapping定义
2015-09-18 10:16 1165elasticsearch 的mapping 例子一: 订单 ... -
ElasticSearch基础杂烩-配置-索引-优化
2015-09-19 23:56 601http://blog.csdn.net/huwei2003/ ... -
Elasticsearch安装中文分词插件ik
2015-09-18 10:15 1987安装步骤: 1、到github ... -
ElasticSearch的各种服务的URL
2015-09-18 10:15 492前言 elasticsearch 将各种功能、配置、服务都以A ... -
ElasticSearch集群搭建
2015-09-19 23:56 541http://www.linuxidc.com/Linux/2 ... -
elasticsearch 配置
2015-09-18 10:16 665elasticsearch.conf 主要是设置一些java运 ... -
elasticsearch中文分词集成
2015-09-17 09:35 565elasticsearch官方只提供smartcn这个中文分词 ...
相关推荐
ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索...
ElasticSearch是一个开源分布式搜索服务器。特点:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,节点数据均等,自动搜索负载等。它提供了一个分布式多用户的全文搜索引擎,...
elasticsearch 6.x 实战教程 ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTfulweb接口。ElasticSearch是用Java开发的,并作为Apache许可条款下的开放源码发布...
分享一套完整版视频课程——分布式搜索引擎Elasticsearch开发实战基础篇 (ElasticSearch、ELK、搜索引擎、Lucene),本教程旨在带领大家进入搜索引擎领域,从无到有,深入浅出的讲解了什么是搜索引擎,搜索引擎的...
课程内容通过ElasticSearch集群的构建,各种相关插件的安装,Netty服务器构建,集群健康状况,各个节点状态,索引状态查看,结合es的备份和恢复,以及如何与hadoop,spark等整合,非常强悍的高级课程。 视频大小:...
elasticsearch是使用比较广泛的分布式搜索引擎,es提供了一个的单字分词工具,还有一个分词插件ik使用比较广泛,hanlp是一个自然语言处理包,能更好的根据上下文的语义,人名,地名,组织机构名等来切分词
ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是开源软件。新增了一个FileBeat,它是一个轻量级的日志收集处理工具(Agent),Filebeat占用资源少,适合于在各个服务器上搜集日志后...
这是一个用 Node JS 编写的 Elasticsearch 教程。 在本教程中,cron 从用户时间线中检索推文并将其保存在 elasticsearch 中。 用户可以通过关键字搜索这些文档。 安装 Elasticsearch Elasticsearch到您的计算机/...
编译后的 elasticsearch-head 源码包,建议配合安装教程《如何安装 elasticsearch-head 插件?》一同使用
Search-Guard 是Elasticsearch的一个免费安全插件,它提供身份验证和授权 ,这里上传的是居于elasticsearch2.3的安装详细教程,包括java中如何连接都有完整的例子,网上很多都不完整,这是作者踩过了无数的坑后整理...
gralog日志管理工具:收集,解析,可视化。 包含mongodb Elasticsearch graylog的安装教程
KingbaseES数据库是一个大型通用的跨平台系统,可以安装和运行于Linux等多种操作系统平台下。KingbaseES数据库在各种操作系统平台上都很容易安装和配置。用户可以参考相应平台下的安装指南,安装KingbaseES数据库。
ElasticSearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elastic...
Kibana 是一个基于浏览器页面的Elasticsearch前端展示工具,也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助您汇总、分析和搜索重要数据日志。...
本文档包含了FIX ES2007 JAVA版本运行环境,安装步骤,DTS工具安装步骤等,按照图文结合步骤可独立完成平台的安装和环境的配置。 方正飞鸿智能信息平台(Fix ES2007)是方正集团推出的国内领先企业基础架构中间件...
Search-Guard 是Elasticsearch的一个免费安全插件,它提供身份验证和授权 ,这里上传的是居于elasticsearch2.3的安装详细教程,包括java中如何连接都有完整的例子,网上很多都不完整,这是作者踩过了无数的坑后整理...
非常详尽的一次关于ElasticSearch构建专业级...通过ElasticSearch集群的构建,各种相关插件的安装,Netty服务器构建,集群健康状况,各个节点状态,索引状态查看,结合es的备份和恢复,以及如何与hadoop,spark等整合。
本教程特点: 1.更适合零基础学员: ·自Java语言起源始,循序渐进,知识点剖析细致且每章配备大量随堂练习,让你步步为营,学得透彻、练得明白 ·拒绝晦涩难懂的呆板教学,宋老师语言生动幽默,举例形象生动深入浅...
Elasticsearch是一个分布式、Restful的搜索及分析服务器,Apache Solr一样,它也是基于Lucence的索引服务器,但我认为Elasticsearch对比Solr的优点在于: 轻量级:安装启动方便,下载文件之后一条命令就可以启动; ...