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 ...
随机推荐
- 吐槽一下wp toolkit ToggleSwitch控件
之前用法: <toolkit:ToggleSwitch> <toolkit:ToggleSwitch.Header> <TextBlock Text="2323 ...
- laravel中firstOrCreate的使用
laravel - firstOrCreate(判断是否存在, 不存在则新增数据) 1, 判断goods_name是否存在YKQ003213_G这个参数 2, 不存在则添加数组的内容 3, 需要设置自 ...
- ping包,支持ip录入
@echo off ::等待用户输入需要监控IP set /p ip=Input the IP required to monitor: echo executing...... :start ech ...
- 以太坊系列之十八: 百行go代码构建p2p聊天室
百行go代码构建p2p聊天室 百行go代码构建p2p聊天室 1. 上手使用 2. whisper 原理 3. 源码解读 3.1 参数说明 3.1 连接主节点 3.2 我的标识 3.2 配置我的节点 3 ...
- NSKeyedArchiver数据归档
前言 在 OC 语言中,归档是一个过程,即用某种格式来保存一个或多个对象,以便以后还原这些对象. 通常,这个过程包括将(多个)对象写入文件中,以便以后读取该对象.可以使用归档的方法进行对象的深复制. ...
- 预定义宏,C语言预定义的宏详解
1.预定义宏 对于预定义宏,相信大家并不陌生.为了方便处理一些有用的信息,预处理器定义了一些预处理标识符,也就是预定义宏.预定义宏的名称都是以"__"(两条下划线)开头和结尾的,如 ...
- Mysql初识数据库《一》下载安装Mysql
#1.下载:MySQL Community Server 5.7.16 http://dev.mysql.com/downloads/mysql/ #2.解压 如果想要让MySQL安装在指定目录,那么 ...
- myeclipse2014 安装maven3.3.9和maven配置本地仓库 及错误修改
结合网上的知识梳理以及自己安装的经验 myeclipse2014 安装maven3.3.9和maven配置本地仓库 及犯的错误修改 成功搞定maven 1,安装 Maven 之前要求先确定你的 J ...
- 更改Linux下的时间
1.使用tzseletect glibc-common-2.12-1.192.el6.x86_64 : Common binaries and locale data for glibc Repo : ...
- 如何查看Centos版本
使用命令 cat /etc/centos-release 查看效果如下图 当然,你也可以查看红帽的版本 cat /etc/redhat-release 郴州软件开发培训 郴州软件培训 郴州java培训 ...