ElasticSearch 2.1.1学习及总结
Install & Up
cd elasticsearch-2.1.1/bin
./elasticsearch
./elasticsearch --cluster.name my_cluster_name --node.name my_node_name
Cluster Health
curl 'localhost:9200/_cat/health?v'
curl 'localhost:9200/_cat/nodes?v'
List All Indices
curl 'localhost:9200/_cat/indices?v'
Create an Index
curl -XPUT 'localhost:9200/customer?pretty'
{
"acknowledged" : true
}
curl 'localhost:9200/_cat/indices?v'
health | index | pri | rep | docs.count | docs.deleted | store.size | pri.store.size
yellow | customer | 5 | 1 | 0 |0 | 495b | 495b
Index and Query
Index:
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "John Doe”
}'
Response:
{
"_index" : "customer”,
"_type" : "external”,
"_id" : "1”,
"_version" : 1,
"created" : true
}
Query:
curl -XGET 'localhost:9200/customer/external/1?pretty'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" :
{
"name": "John Doe"
}
}
Delete an Index
curl -XDELETE 'localhost:9200/customer?pretty'
{
"acknowledged" : true
}
curl 'localhost:9200/_cat/indices?v'
health | index | pri | rep | docs.count | docs.deleted | store.size | pri.store.size
curl -X :///
Updating Document
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d
' { "doc": { "name": "Jane Doe" } }'
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d
' { "doc": { "name": "Jane Doe", "age": 20 } }'
Script:
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d
' { "script" : "ctx._source.age += 5" }'
Error:
{
"error" : {
"root_cause" : [ {
"type" : "remote_transport_exception",
"reason" : "[Angelica Jones][127.0.0.1:9300][indices:data/write/update[s]]"
} ],
"type" : "illegal_argument_exception",
"reason" : "failed to execute script",
"caused_by" : {
"type" : "script_exception",
"reason" : "scripts of type [inline], operation [update] and lang [groovy] are disabled"
}
},
"status" : 400
}
Solution:elasticsearch.yml
script.inline: on
script.indexed: on
Deleting Documents
curl -XDELETE 'localhost:9200/customer/external/2?pretty’
The delete-by-query plugin can delete all documents matching a specific query.
Batch Processing
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d
'{"index":{"_id":"1”}}
{"name": "John Doe” }
{"index":{"_id":"2”}}
{"name": "Jane Doe" } ‘
Delete:
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d
' {"update":{"_id":"1”}}
{
"doc": { "name": "John Doe becomes Jane Doe" }
}
{"delete":{"_id":"2"}} ‘
The Search API
curl 'localhost:9200/bank/_search?q=*&pretty’
took –
time in milliseconds for Elasticsearch to execute the search
timed_out –
tells us if the search timed out or not
_shards –
tells us how many shards were searched, as well as a count of the successful/failed searched shards
hits –
search results
hits.total –
total number of documents matching our search criteria
hits.hits –
actual array of search results (defaults to first 10 documents)
_score and max_score -
ignore these fields for now
XPOST:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} } }'
NO CURSOR DON’T LIKE SQL
Introducing the Query Language
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "size": 1 }'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "from": 10, "size": 10 }'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "sort": { "balance": { "order": "desc" } } }’
Executing Searches
Basic Query
Fields:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "_source": ["account_number", "balance"] }'
Returns the account numbered 20:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match": { "account_number": 20 } } }'
Containing the term "mill" in the address:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match": { "address": "mill" } } }'
Containing the term "mill" or "lane" in the address:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match": { "address": "mill lane" } } }'
Containing the phrase "mill lane" in the address:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_phrase": { "address": "mill lane" } } }'
Boolean Query
AND
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }'
OR
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "should": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }'
NOR
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must_not": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }'
Anybody who is 40 years old but don’t live in ID(aho):
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must": [ { "match": { "age": "40" } } ], "must_not": [ { "match": { "state": "ID" } } ] } } }'
Range Query:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must": { "match_all": {} }, "filter": { "range": { "balance": { "gte": 20000, "lte": 30000 } } } } } }'
Executing Aggregations
Groups all the accounts by state, and then returns the top 10 (default) states sorted by count descending (also default):
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state"
}
}
}
}'
SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC
Calculates the average account balance by state:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "size": 0, "aggs": { "group_by_state": { "terms": { "field": "state" }, "aggs": { "average_balance": { "avg": { "field": "balance" } } } } } }'
You can nest aggregations inside aggregations arbitrarily to extract pivoted summarizations that you require from your data.
Sort on the average balance in descending order:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}'Group by age brackets (ages 20-29, 30-39, and 40-49), then by gender, and then finally get the average account balance, per age bracket, per gender:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_age": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"group_by_gender": {
"terms": {
"field": "gender"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
}
}'
ElasticSearch 2.1.1学习及总结的更多相关文章
- (转)开源分布式搜索平台ELK(Elasticsearch+Logstash+Kibana)入门学习资源索引
Github, Soundcloud, FogCreek, Stackoverflow, Foursquare,等公司通过elasticsearch提供搜索或大规模日志分析可视化等服务.博主近4个月搜 ...
- 开源分布式搜索平台ELK(Elasticsearch+Logstash+Kibana)入门学习资源索引
from: http://www.w3c.com.cn/%E5%BC%80%E6%BA%90%E5%88%86%E5%B8%83%E5%BC%8F%E6%90%9C%E7%B4%A2%E5%B9%B ...
- Elasticsearch核心技术与实战-学习笔记
学习资源: Elasticsearch中文社区日报https://elasticsearch.cn/article/ Elasticsearch 官网 https://www.elastic.co/ ...
- elasticsearch Discovery 发现模块学习
发现模块和集群的形成 目标 发现节点 Master选举 组成集群,在Master信息发生变化时及时更新. 故障检测 细分为几个子模块 Discovery发现模块 Discover是在集群Master节 ...
- elasticsearch 7.5.0 学习笔记
温馨提示:电脑端看不到右侧目录的话请减小缩放比例. API操作-- 新建或删除查询索引库 新建索引库 新建index,要向服务器发送一个PUT请求,下面是使用curl命令新建了一个名为test的ind ...
- Elasticsearch安装、原理学习总结
ElasticSearch ElasticSearch概念 Elasticsearch是Elastic Stack核心的分布式搜索和分析引擎. 什么是Elastic Stack Elastic Sta ...
- 《读书报告 -- Elasticsearch入门 》-- 安装以及简单使用(1)
<读书报告 – Elasticsearch入门 > 第一章 Elasticsearch入门 Elasticsearch是一个实时的分布式搜索和分析引擎,使得人们可以在一定规模上和一定速度上 ...
- 搜索引擎solr和elasticsearch
刚开始接触搜索引擎,网上收集了一些资料,在这里整理了一下分享给大家. 一.关于搜索引擎 搜索引擎(Search Engine)是指根据一定的策略.运用特定的计算机程序从互联网上搜集信息,在对信息进行组 ...
- Elasticsearch源码分析 - 源码构建
原文地址:https://mp.weixin.qq.com/s?__biz=MzU2Njg5Nzk0NQ==&mid=2247483694&idx=1&sn=bd03afe5a ...
随机推荐
- 求整数数组(长度为n),出现大于2/n次数的数字
条件:时间复杂度是O(n),空间复杂度是O(1) 方法1:标记法 , , , , , , , , , , , , , }; int len = arr.Length; int[] c = new in ...
- 使用Linq读取资源文件
ResXResourceReader resxr = new ResXResourceReader(txt_WebResourceOpenFile.Text); IEnumerable<Dict ...
- appstore开发者 名称修改
1.前提是开发者账号为企业账号 CSDN大佬写的,应该没错
- 6w5:第六周程序填空题2
描述 下面程序的输出结果是: destructor B destructor A 请完整写出 class A. 限制条件:不得为 class A 编写构造函数. #include <iostre ...
- shell-脚本-作用是发现前10个最占用磁盘空间的文件
#!/bin/bash## 这个脚本的作用是发现前10个最占用磁盘空间的文件################定义这个脚本的全区变量##################################C ...
- 配置Java web的一次经历
最近在完成数据库作业,重新拾起了以前学过的Java,讲下自己的 Java web 配置过程. 1.安装 Tomcat 在官网下载 Tomcat7.0版本:https://tomcat.apache.o ...
- Jenkins持续集成企业实战系列之Jenkins插件下载及邮件配置-----05
注:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任. 最初接触Jenkins也是由于公司需求,根据公司需求Java代码项目升级的.(公司是 ...
- php中的openssl开启方法
windows下开启方法: 1: 首先检查php.ini中:extension=php_openssl.dll是否存在, 如果存在的话去掉前面的注释符‘:', 如果不存在这行,那么添加extensio ...
- TX2 上使用opencv 调用板载mipi摄像头
使用命令测试 gst-launch-1.0 nvcamerasrc ! 'video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, fo ...
- Qt 学习之路 2(30):Graphics View Framework
Qt 学习之路 2(30):Graphics View Framework 豆子 2012年12月11日 Qt 学习之路 2 27条评论 Graphics View 提供了一种接口,用于管理大量自定义 ...