ElasticSearch 2.1.1 (1) - Getting Start

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"

}

}

}

}

}

}

}

}'

Reference

https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html

ElasticSearch 2 (1) - Getting Start的更多相关文章

  1. Elasticsearch之java的基本操作一

    摘要   接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...

  2. Elasticsearch 5.0 中term 查询和match 查询的认识

    Elasticsearch 5.0 关于term query和match query的认识 一.基本情况 前言:term query和match query牵扯的东西比较多,例如分词器.mapping ...

  3. 以bank account 数据为例,认识elasticsearch query 和 filter

    Elasticsearch 查询语言(Query DSL)认识(一) 一.基本认识 查询子句的行为取决于 query context filter context 也就是执行的是查询(query)还是 ...

  4. Ubuntu 14.04中Elasticsearch集群配置

    Ubuntu 14.04中Elasticsearch集群配置 前言:本文可用于elasticsearch集群搭建参考.细分为elasticsearch.yml配置和系统配置 达到的目的:各台机器配置成 ...

  5. ElasticSearch 5学习(10)——结构化查询(包括新特性)

    之前我们所有的查询都属于命令行查询,但是不利于复杂的查询,而且一般在项目开发中不使用命令行查询方式,只有在调试测试时使用简单命令行查询,但是,如果想要善用搜索,我们必须使用请求体查询(request ...

  6. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  7. .net Elasticsearch 学习入门笔记

    一. es安装相关1.elasticsearch安装  运行http://localhost:9200/2.head插件3.bigdesk插件安装(安装细节百度:windows elasticsear ...

  8. 自己写的数据交换工具——从Oracle到Elasticsearch

    先说说需求的背景,由于业务数据都在Oracle数据库中,想要对它进行数据的分析会非常非常慢,用传统的数据仓库-->数据集市这种方式,集市层表会非常大,查询的时候如果再做一些group的操作,一个 ...

  9. 如何在Elasticsearch中安装中文分词器(IK+pinyin)

    如果直接使用Elasticsearch的朋友在处理中文内容的搜索时,肯定会遇到很尴尬的问题--中文词语被分成了一个一个的汉字,当用Kibana作图的时候,按照term来分组,结果一个汉字被分成了一组. ...

  10. jar hell & elasticsearch ik 版本问题

    想给es 安装一个ik 的插件, 我的es 是 2.4.0, 下载了一个版本是 1.9.5, [2016-10-09 16:56:26,248][INFO ][node ] [node-2] init ...

随机推荐

  1. 安装chrome扩展json-handle

    chrome插件安装 方式一,在线安装 直接插到json-handle地址,添加即可 https://chrome.google.com/webstore/detail/json-handle/iah ...

  2. 在同一台电脑安装python 2 和3,并且怎样安装各自的pip和模块

    安装python2.7 和 3.6不冲突直接安装就行 安装pip 访问https://pip.pypa.io/en/stable/installing/获取地址 curl https://bootst ...

  3. 实现类QQ的编辑框

    第一步,平面效果.Windows系统有几个消息专门用来处理Windows组件的边框部位,那就是WM_NCCALCSIZE和WM_NCPAINT这两个消息,从消息名字看来NC这个就代表着No Clien ...

  4. Python实例讲解 -- wxpython 基本的控件 (按钮)

    使用按钮工作 在wxPython 中有很多不同类型的按钮.这一节,我们将讨论文本按钮.位图按钮.开关按钮(toggle buttons )和通用(generic )按钮. 如何生成一个按钮? 在第一部 ...

  5. InfluxDB(官方使用说明)

    安装InfluxDB OSS 此页面提供有关安装,启动和配置InfluxDB的说明. InfluxDB OSS安装要求 root为了成功完成,需要安装InfluxDB软件包或具有管理员权限. Infl ...

  6. 删除右键菜单中的Git

    在打开的注册表中找到:HKEY_CLASSES_ROOT,并点HKEY_CLASSES_ROOT前面的小三角:找到Directory,点击前面的小三角:找到Background,点击前面的小三角:打开 ...

  7. Mysql 性能优化5【重要】数据库结构优化

    数据库设计的步骤 我们大多使用mysql 设计三范式 设置时区  

  8. ALGO-22_蓝桥杯_算法训练_数的划分(DP)

    问题描述 将整数n分成k份,且每份不能为空,任意两份不能相同(不考虑顺序). 例如:n=,k=,下面三种分法被认为是相同的. ,,; ,,; ,,; 问有多少种不同的分法. 输入格式 n,k 输出格式 ...

  9. windows2008系统缓存修改

    Windwos Server 2008 中经常有物理内存占用较高,但任务管理器中各进程的内存看起来占用并不多情况. 这可能是由于OS为保证服务器性能不限制系统缓存,服务器系统长时间执行I/O,系统文件 ...

  10. C语言中sizeof与strlen的区别

    1.sizeof sizeof为编译时期被替换,不会等到程序运行再来判断,所以sizeof返回的是数组的总字节数 #include<stdio.h> int main() { ]={'a' ...