一起来玩Elasticsearch,加我微信:wx1250134974
Elasticsearch认证复习准备
https://www.elastic.co/guide/cn/elasticsearch/guide/current/getting-started.html
- 安装配置Elasticsearch
https://www.elastic.co/guide/cn/elasticsearch/guide/current/running-elasticsearch.html
##下载软件包
https://www.elastic.co/cn/downloads/elasticsearch
##解压并运行,后台运行加-d选项即可
bin/elasticsearch (or bin\elasticsearch.bat on Windows)
##验证是否运行,类似于下方输出即表示安装成功
curl http://localhost:9200/
##下载安装kibana,注意对应的版本(下载后直接解压)
https://www.elastic.co/cn/downloads/past-releases/
##运行Kibana
bin/kibana &
##验证kibana是否安装成功,浏览器访问下方端口
- 像风一样的体验下Elasticsearch
##Elasticsearch的客户端
如果是JAVA代码,用内置客户端,包括节点客户端(Node client)、传输客户端(Transport client)
如果非JAVA代码,使用RESTful风格的客户端,包括Python、PHP、Go等等
(https://www.elastic.co/guide/en/elasticsearch/client/index.html)
##Elasticsearch的语法
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
##感受下Elasticsearch的请求语句
curl -XGET 'http://localhost:9200/_count?pretty' -d '
{
"query": {
"match_all": {}
}
}'
##索引文档(直接放到Kibana的DevTool中感受下吧)
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-特定雇员的ID
##检索文档
按照ID搜索
GET /megacorp/employee/1
查询下last_name包含Smith的雇员
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
查询下last_name包含smith同时年龄大于30的
GET /megacorp/employee/_search
{
"query" : {
"bool": {
"must": {
"match" : {
"last_name" : "smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}
查询喜欢攀岩的雇员
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
查询喜欢攀岩的雇员,这个查询更精准
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
让搜索结果高亮
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
查看员工的爱好都有哪些
GET /megacorp/employee/_search
{
"aggs": {
"all_interests": {
"terms": { "field": "interests.keyword" }
}
}
}
查看下last_name为smith的,他们的爱好怎么分布的
GET /megacorp/employee/_search
{
"query": {
"match": {
"last_name": "smith"
}
},
"aggs": {
"all_interests": {
"terms": {
"field": "interests.keyword"
}
}
}
}
查看不同爱好的人,他们的平均年龄是多少
GET /megacorp/employee/_search
{
"aggs" : {
"all_interests" : {
"terms" : { "field" : "interests.keyword" },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}
一起来玩Elasticsearch,加我微信:wx1250134974