elasticsearch安装与基础用法

来自官网,版本为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":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'
复制代码

导入文件中的数据,当前目录下有个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": "40" } }
      ],
      "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就是进阶用法了;


############################

转自:https://www.cnblogs.com/toSeek/p/6094808.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值