ELK(二)ElasticSearch

一、下载并安装

下载: wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.2.tar.gz

解压:tar –vzxf elasticsearch-5.2.2.tar.gz

需要配置一些东西:   vi  /etc/security/limits.conf

添加一些内容:

* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

vi     /etc/security/limits.d/90-nproc.conf

修改一下内容

修改如下内容:   * soft nproc 1024

#修改为  :          * soft nproc 2048

vi /etc/sysctl.conf

添加如下配置

vm.max_map_count=655360

并执行命令:
sysctl –p

由于安全问题elasticsearch不允许root用户登录,所以增加个用户单独去启动它

groupadd tend

useradd tend -g tend -p elasticsearch-5.2.2

chown -R tend:tend elasticsearch

su tend

cd elasticsearch-5.2.2

启动elasticsearch

./bin/elasticsearch

ElasticSearch插件head 安装 

下载:wget https://github.com/mobz/elasticsearch-head/archive/master.zip

解压:unzip master.zip

一、Head需要node的支持,所以在此需要安装node(省略安装步骤)

二、安装grunt 在head的目录下执行npm install -g grunt-cli

执行 npm install -g grunt

如果安装过程比较慢,建议临时使用taobao镜像(我就是用镜像安装的)

例如安装grunt-cli命令如下:

npm install -g grunt-cli--registry=https://registry.npm.taobao.org

安装完成后查看版本grunt –version


vi Gruntfile.js
增加hostname属性 设置为”0.0.0.0”
启动head
node_modules/grunt/bin/grunt server

输入http://127.0.0.1:9100即可进行访问

一些API参数

下载测试数据:

wget https://github.com/bly2k/files/raw/master/accounts.zip

上传到elasticsearch

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

检查集群健康与否:

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

获得集群中的节点列表

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

列出所有的索引

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

创建一个索引

curl -XPUT 'localhost:9200/customer?pretty'

将一个简单的客户文档索引到customer索引、“external”类型中,这个文档的ID是1

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?pretty'

使用请求体方法(一旦你取回了搜索结果,elasticsearch就完成了使命它不会维护任何服务器端的资源或者在你的结果中打开游标)

curl -XPOST'localhost:9200/bank/_search?pretty' -d '

           {

             "query": { "match_all":{} }

           }'


- took ——Elasticsearch执行这个搜索的耗时,以毫秒为单位

- timed_out —— 指明这个搜索是否超时

- _shards —指出多少个分片被搜索了,同时也指出了成功/失败的被搜索的shards的数量

hits —— 搜索结果

- hits.total —— 能够匹配我们查询标准的文档的总数目

- hits.hits —— 真正的搜索结果数据(默认只显示前10个文档)

-hists._score  相关度,值越大相关度越高

下面做了一次match_all并只返回第一个文档:

curl -XPOST 'localhost:9200/bank/_search?pretty'-d '

       {

         "query": { "match_all": {} },

         "size": 1

       }'

注:如果没有指定size则默认为10

 curl-XPOST 'localhost:9200/bank/_search?pretty' -d '

       {

         "query": { "match_all": {} },

         "from": 10,    

         "size": 10

       }'

如果from不指定默认为0

下面这个例子返回账户编号为20的文档:

 curl-XPOST 'localhost:9200/bank/_search?pretty' -d '

       {

         "query": { "match": { "account_number": 20} }

       }'

下面这个例子返回地址中包含“mill”的所有账户:

curl -XPOST 'localhost:9200/bank/_search?pretty' -d '

        {

          "query": {"match": { "address": "mill" } }

        }'

或者"query": { "match": {"address": "mill lane" } }  查询包含mill或者lane的账户

"query": {"match_phrase": { "address": "mill lane" } }  查询包含 mill跟lane的账户,它会去匹配短语“mill lane”

现在这个例子组合了两个match查询,这个组合查询返回包含“mill”和“lane”的所有的账户:

   curl -XPOST 'localhost:9200/bank/_search?pretty' -d '

       {

         "query": {

           "bool": {

              "must": [

                { "match": {"address": "mill" } },

                { "match": {"address": "lane" } }

              ]

           }

         }

       }'

其中 must是必须包含(&&),should是或(||)must_not是都不(not no)

balance在2000~3000(闭区间)的账户

curl -XPOST'localhost:9200/bank/_search?pretty' -d '
        {

         "query": {
           "filtered": {
             "query": { "match_all": {} },
             "filter": {
               "range": {
                 "balance": {
                   "gte": 20000,
                   "lte": 30000
                 }
               }
             }
            }
          }
        }'

以上这种方式在2.2版本被弃用。改用bool的方式

curl -XPOST'localhost:9200/bank/_search?pretty' -d '

      {"query":{

    "bool":{

    "filter": {

          "range": {

            "balance": {

                "gte": 20000,

                "lte": 30000

               }

           }

     }

}

}

}_bulk 多条数据一起查询

POST /my_index/posts/_bulk
{ "index": { "_id": "1"              }}
{ "tags" : ["search"]                } 
{ "index": { "_id": "2"              }}
{ "tags" : ["search", "open_source"] } 
{ "index": { "_id": "3"              }}
{ "other_field" : "some data"        } 
{ "index": { "_id": "4"              }}
{ "tags" : null                      } 
{ "index": { "_id": "5"              }}
{ "tags" : ["search", null]          } 

几种过滤器

范围过滤器(range)

{"query":{
     "bool":{
     "filter": {
           "range": {
             "balance": {
                "gte": 20000,
                "lte": 30000
               }
            }
      }
}
}
}

exits过滤器

过滤掉给定字段没有值的文档


{
   "post_filter":{
         "exists":{
             "field":"year"
         }
    }
}

missing过滤器

过滤掉给定字段有值或缺失的文档

{
   "post_filter":{
         "missing":{
             "field":"year",
             "null_value":0,
             "existence":true
         }
    }
}

脚本过滤器

过滤掉发表在一个世纪以前的书

{
   "post_filter":{
         "script":{
             "script":"now - doc['year'].value > 100",
             "params":{"now":2012}
         }
    }
}

类型过滤器

当查询运行在多个索引上时,有用

{
   "post_filter":{
         "type":{
             "value":"book"
         }
    }
}

限定过滤器   限定每个分片返回的文档数

{
   "post_filter":{
         "limit":{
             "value":1
         }
    }
}
 

标识过滤器

指定标识符为1,2,3的文档

{
   "post_filter":{
         "ids":{
             "type":["book"],
             "values":[1,2,3]
         }
    }
}

组合过滤器

{
   "query": {
       "bool": {
           "must": {
                "range": {
                    "year": {
                        "gte": 1930,
                        "lte": 1990
                    }
                }
           },
           "should": {
                "term": {
                    "available": true
                }
           },
           "boost": 1
       }
    }
}

聚合查询

按年龄降序

curl -XPOST'localhost:9200/bank/_search?pretty' -d '
{
 "aggs": {
   "all_age": {
     "terms": {
       "field": "age"
     }
    }
  }
}'


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值