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学习及总结的更多相关文章

  1. (转)开源分布式搜索平台ELK(Elasticsearch+Logstash+Kibana)入门学习资源索引

    Github, Soundcloud, FogCreek, Stackoverflow, Foursquare,等公司通过elasticsearch提供搜索或大规模日志分析可视化等服务.博主近4个月搜 ...

  2. 开源分布式搜索平台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 ...

  3. Elasticsearch核心技术与实战-学习笔记

    学习资源: Elasticsearch中文社区日报https://elasticsearch.cn/article/ Elasticsearch 官网 https://www.elastic.co/ ...

  4. elasticsearch Discovery 发现模块学习

    发现模块和集群的形成 目标 发现节点 Master选举 组成集群,在Master信息发生变化时及时更新. 故障检测 细分为几个子模块 Discovery发现模块 Discover是在集群Master节 ...

  5. elasticsearch 7.5.0 学习笔记

    温馨提示:电脑端看不到右侧目录的话请减小缩放比例. API操作-- 新建或删除查询索引库 新建索引库 新建index,要向服务器发送一个PUT请求,下面是使用curl命令新建了一个名为test的ind ...

  6. Elasticsearch安装、原理学习总结

    ElasticSearch ElasticSearch概念 Elasticsearch是Elastic Stack核心的分布式搜索和分析引擎. 什么是Elastic Stack Elastic Sta ...

  7. 《读书报告 -- Elasticsearch入门 》-- 安装以及简单使用(1)

    <读书报告 – Elasticsearch入门 > 第一章 Elasticsearch入门 Elasticsearch是一个实时的分布式搜索和分析引擎,使得人们可以在一定规模上和一定速度上 ...

  8. 搜索引擎solr和elasticsearch

    刚开始接触搜索引擎,网上收集了一些资料,在这里整理了一下分享给大家. 一.关于搜索引擎 搜索引擎(Search Engine)是指根据一定的策略.运用特定的计算机程序从互联网上搜集信息,在对信息进行组 ...

  9. Elasticsearch源码分析 - 源码构建

    原文地址:https://mp.weixin.qq.com/s?__biz=MzU2Njg5Nzk0NQ==&mid=2247483694&idx=1&sn=bd03afe5a ...

随机推荐

  1. #随笔之java匿名内部类

    随笔之java匿名内部类 从今天起开始每日一篇技术博客,当然这只是我当天所学的一些随笔,里面或多或少会有理解不当的地方,希望大家多多指教,一起进步! 在讲匿名内部类之前,先讲讲内部类的一些概念. 1. ...

  2. 快速下载android源码

    众所周知的原因,android源码被墙了,还好国内有不少镜像,这里使用清华提供的镜像. 以下内容转自: https://wiki.tuna.tsinghua.edu.cn/MirrorUsage/an ...

  3. SSM项目配置文件及其各项使用

    $说明: ·Spring 5  + Mybatis 3.4.5 +SpringMVC  ·使用druid数据库 ·使用log4j输出日志 $Spring 及其配置文件(部分) Spring官方网站:h ...

  4. 「TJOI2013」循环格

    题目链接 戳我 \(Solution\) 我们观察发现循环格要满足每个点的入度都为\(1\) 证明: 我们假设每个点的入读不一定为\(1\),那么必定有一个或多个点的入度为0,那么则不满足循环格的定义 ...

  5. 函数返回值string与返回值bool区别------c++程序设计原理与实践(进阶篇)

    为什么find_from_addr()和find_subject()如此不同?比如,find_from_addr()返回bool值,而find_subject()返回string.原因在于我们想说明: ...

  6. vi/vim使用总结

    第一部份:一般模式可用的按钮说明,光标移动.复制粘贴.搜索取代等 移动光标的方法: h 或 向左箭头键(←) 光标向左移动一个字符 j 或 向下箭头键(↓) 光标向下移劢一个字符 k 或 向上箭头键( ...

  7. 利用keytool工具生成数字证书

    一.制作数字证书  因测试微信小程序, 腾讯要求使用 https协议,所以需要使用证书.使用jdk工具制作数字证书流程如下: 1.查看JDK是否安装,使用命令java -version 2.切换目录至 ...

  8. 高版本sketch文件转成低版本的sketch

    https://pan.baidu.com/s/1htmNERU 下载 该文件然后在放到高版本sketch文件的目录下,执行下面命令 chmod +x ./build.sh ./build.sh 文件 ...

  9. [USACO10MAR]伟大的奶牛聚集 BZOJ 1827 树形dp+dfs

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  10. 多线程 NSOpeartion 的使用

    NSOperation简介 相对于 GCD ,具有面向对象的特征,比 GCD 更简单易用,代码可读性强 NSOperatioin 单独使用时, 不具有开辟新线程的能力, 只是同步执行操作, 需要配合 ...