来自官网,版本为2.3

注意elasticsearch依赖jdk,2.3依赖jdk7

下载rpm包并安装

wget -c https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/rpm/elasticsearch/2.3.5/elasticsearch-2.3.5.rpm
rpm -ivh elasticsearch-2.3.5.rpm

配置文件位于/etc/elasticsearch/
日志默认位于/var/log/elasticsearch/,可在配置文件中修改
没有特殊要求,默认即可使用,启动停止等操作如下

/etc/init.d/elasticsearch {start|stop|status|restart|reload}

启动默认占用端口9200,可在配置文件中修改

下面三个命令依次为,检查集群状况、检查节点状况、显示所有的索引(刚开始是没有索引的)

节点名称默认是随机起的,可以在配置文件中指定

curl 'localhost:9200/_cat/health?v'
curl 'localhost:9200/_cat/nodes?v'
curl 'localhost:9200/_cat/indices?v'

创建一个索引 customer(可选,因为直接插入数据的时候若索引名不存在会自动创建);

在索引customer中插入一条 type=external, id=1 的数据并查询这条数据;

删除这条数据,最后删除索引customer;

curl -XPUT 'localhost:9200/customer?pretty'
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '{"name": "John Doe"}'
curl -XGET 'localhost:9200/customer/external/1?pretty'
curl -XDELETE 'localhost:9200/customer/external/1?pretty'
curl -XDELETE 'localhost:9200/customer?pretty'

可以重复执行插入数据的命令来替换整条数据;

插入数据时可以不指定id,这样会自动生成一个(注意这里是post);

curl -XPOST 'localhost:9200/customer/external?pretty' -d '{"name": "Jane Doe"}'

而修改某条数据则应该这样;

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '{"doc": { "name": "John Doe update" }}'
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '{"doc": { "name": "John Doe update", "age": 20 }}'

修改数据还可以用脚本script,需要另外开篇记录;

下面是批量插入和更新数据;

curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
{"index":{"_id":""}}
{"name": "John Doe" }
{"index":{"_id":""}}
{"name": "Jane Doe" }
'
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
{"update":{"_id":""}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":""}}
'

导入文件中的数据,当前目录下有个accounts.json文件(要求格式为上述批量操作的格式);

附上这个官方测试文件的下载:https://raw.githubusercontent.com/bly2k/files/master/accounts.zip

curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"

两种查出所有数据的命令(要注意,匹配结果若超过10则默认只传出前10条,可以通过size指定,见下文);

查询参数和返回数据都是json结构;

curl 'localhost:9200/bank/_search?q=*&pretty'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} }
}'

查询所有数据,但只取出前5条和5~10条,from和size可以随意组合;

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"size": 5
}'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"from": 5,
"size": 5
}'

下面这个是根据字段(这里用的age)排序后取前3条数据(与sql一样,asc升序,desc降序);

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"size": 3,
"sort": { "age": { "order": "asc" } }
}'

查询指定字段(姓名、邮箱);

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },"_source": ["firstname", "lastname", "email"]
}'

上面都是用的match_all,使用match用来匹配一定的条件,下面是取出年龄为30的数据;

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "age": 30 } }
}'

地址中包含mill的数据;

地址中包含mill或者lane的数据;

地址中包含mill lane的数据;

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "address": "mill" } }
}'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "address": "mill lane" } }
}'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_phrase": { "address": "mill lane" } }
}'

bool组合查询条件,如下must是都满足,must_not是都不满足,should则是满足其中一项;

must、must_not、should这三个还可以组合;

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "age": "" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}'

使用filter过滤实现范围查询(年龄20~30);

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"age": {
"gte": 20,
"lte": 30
}
}
}
}
}
}'

聚合(Aggregations),就是sql中的groupby,下面是根据年龄age分组,然后只显示第一条;

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_age": {
"terms": {
"field": "age",
"size": 1
}
}
}
}'

aggs可以嵌套,分组后再计算,下面就是根据年龄分组后(取了前3条)求每个组的评价余额(balance);

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_age": {
"terms": {
"field": "age",
"size": 2
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}'

基础用法至此结束,更多的各种api就是进阶用法了;

elasticsearch安装与基础用法的更多相关文章

  1. Docker 安装和基础用法

    理解Docker(1):Docker 安装和基础用法 本系列文章将介绍Docker的有关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 ...

  2. logstash安装与基础用法

    若是搭建elk,建议先安装好elasticsearch 来自官网,版本为2.3 wget -c https://download.elastic.co/logstash/logstash/packag ...

  3. 理解Docker(1):Docker 安装和基础用法

    本系列文章将介绍Docker的有关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...

  4. filebeat安装与基础用法

    来自官网,版本为1.2 下载rpm包并安装 wget -c https://download.elastic.co/beats/filebeat/filebeat-1.2.3-x86_64.rpm r ...

  5. kibana安装与基础用法

    来自官网,版本为4.5 下载rpm包并安装 wget -c https://download.elastic.co/kibana/kibana/kibana-4.5.4-1.x86_64.rpm rp ...

  6. git安装及基础用法

    1.安装GitGit-2.9.3-64-bit.exe 2.打开Git Bash,设置用户名,Email $ git config --global user.name "Your Name ...

  7. 全文检索-Elasticsearch (一) 安装与基础概念

    ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口 Elasticsearch由java开发,所以在搭建时,需先安 ...

  8. 前端自动化测试神器-Katalon的基础用法

    前言 最近由于在工作中需要通过Web端的功能进行一次大批量的操作,数据量大概在5000左右,如果手动处理, 完成一条数据的操作用时在20秒左右的话,大概需要4-5个人/天的工作量(假设一天8小时的工作 ...

  9. mySQL的安装和基础使用及语法教程

    mySQL的安装和基础使用及语法指南 一.MySQL的安装.配置及卸载 1.安装 2.配置 3.mySQL5.1的完全卸载 4.MYSQL环境变量的配置 二.MySQL控制台doc窗口的操作命令 1. ...

随机推荐

  1. 演示一个导致ora-01555错误的场景

    1创建一个undo表空间 2查看当前undo配置 3更该默认undo表空间 4确认更改的配置 5创建一张测试表 6模拟批量操作 7 查询2分钟前的数据 从这里可以到当查询2分钟前的数据时候,系统报出O ...

  2. hdu1201-18岁生日

    Gardon的18岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达18岁生日时所经过的天数都是一样的呢?似乎并不全都是这样,所以他想请你帮忙计算一下他和他的几个朋友从 ...

  3. PHP中文函数顺序排列一数组且其序数不变

    函数Abs() 描述: mixed abs (mixed number); Returns the absolute value of number. If the argument number i ...

  4. IOS第18天(9,核心动画-动画组)

    ****动画组 // 核心动画都是假象,不能改变layer的真实属性的值// 展示的位置和实际的位置不同.实际位置永远在最开始位置 #import "HMViewController.h&q ...

  5. 【iCore3 双核心板】例程二十:LAN_TCPC实验——以太网数据传输

    实验指导书及代码包下载: http://pan.baidu.com/s/1pJY5uXH iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  6. 领导者/追随者(Leader/Followers)模型和半同步/半异步(half-sync/half-async)模型都是常用的客户-服务器编程模型

    领导者-追随者(Leader/Followers)模型的比喻 半同步/半异步模型和领导者/追随者模型的区别: 半同步/半异步模型拥有一个显式的待处理事件队列,而领导者-追随者模型没有一个显式的队列(很 ...

  7. ExtJS4笔记 Data

    The data package is what loads and saves all of the data in your application and consists of 41 clas ...

  8. How to bind data to a user control

    http://support.microsoft.com/kb/327413 Create a user control  by inheriting from the System.Windows. ...

  9. LR之Java Vuser

    虽然LR对C有较好的支持,但有时使用Java Vuser会更方便,以下描述通过LR来编写Java脚本以及调用jar包的基本步骤. 1.安装配置java环境  LR的java vuser脚本的执行依赖于 ...

  10. Why MySQL could be slow with large tables ?

    https://www.percona.com/blog/2006/06/09/why-mysql-could-be-slow-with-large-tables/