ES数据库基础命令(一)

安装:

  ES数据库安装挺麻烦的,安装完数据库之后还要安装各种插件(ik分词,head集群管理工具,bigdesk集群监控工具可以看到cpu,内存,索引,http连接数,搜索等情况,)es数据库并没有悲观察锁的机制,乐观锁是通过文档默认字段_version实现的,分别是两种方式:内部版本控制:指定的version必须要 = 文档目前的_version,否则就报error。如果符合就修改成功,并且把文档version自增1。外部版本控制:搭配version_type=external,指定的version必须 > 文档目前的_version,否则就报error。如果符合就修改成功,并且把文档version直接变成指定的。

Es 概念以及特点

  1、Elasticsearch和MongoDB/Redis/Memcache一样,是非关系型数据库。是一个接近实时的搜索平台,从索引这个文档到这个文档能够被搜索到只有一个轻微的延迟,企业应用定位:采用Restful API标准的可扩展和高可用的实时数据分析的全文搜索工具。

  2、可拓展:支持一主多从且扩容简易,只要cluster.name一致且在同一个网络中就能自动加入当前集群;本身就是开源软件,也支持很多开源的第三方插件。

  3、高可用:在一个集群的多个节点中进行分布式存储,索引支持shards和复制,即使部分节点down掉,也能自动进行数据恢复和主从切换。

  4、采用RestfulAPI标准:通过http接口使用JSON格式进行操作数据。

  5、数据存储的最小单位是文档,本质上是一个JSON 文本。

  6、es采用倒排索引,value>iD,侧重于结构性检索统计,不适合做数据存储,很难修改属性类型,没有用户验证和权限控制,添加字段可以通过添加不同type来做,每次查询的时候,使用doctype-*来搜索,支持

基本操作:

  在这之前先了解下ES的5个请求方式:
    PUT:新增
    POST:修改
    DELETE:删除
    GET或HEAD:查询,区别是HEAD不会返回响应体,只有响应头。

Kibana命令:

  # 添加数据  

    POST order/order_item/
    {
    "id":100,
    "message":"测试",
    "price":10.09,
    "tid":"S215488888"
    }

  # 修改和新增是一样的命令,如果id存在那就是修改,不存在就是新增,需要注意的是如果我们只修改一个字段的值,不能直接像下面这样操作,更改价格。

    POST order/order_item/100

    {
    "price":22.06
    }

    // 因为执行后就只剩下price这个字段了。应该按下面操作

    POST index/type/id/_update
    {
    "doc" : {
    "name" : "new_name"
    }
    }

  # 根据条件批量修改将所有 NICK是体育旗舰店的数据中的ORDERSIGN修改为“222222”,INVOICE_INFO修改为“开具发票”。

    POST index/type/_update_by_query

    {
    "script":{
    "inline":"ctx._source.ORDERSIGN='222222';ctx._source.INVOICE_INFO='开具发票';"
    },
    "query":{
    "term":{
    "NICK": "体育旗舰店"
    }
    }
    }

    或者

    POST index/type/_update_by_query
    {
    "script": {
    "lang": "painless",
    "inline": "if(ctx._source.INVOICE_INFO== '发票'){ctx._source.INVOICE_INFO= '开具发票'} if(ctx._source.DELIVERY_TYPE==

    '任意'){ctx._source.DELIVERY_TYPE= '任意时间 '}"
    }
    }   //多个if之间是或的关系。lang=painless表示使用painless脚本语言来编写script来完成。

   # 删除

    #根据id删除:DELETE /index/type/1

    #路由删除:DELETE /index/type/1?routing=kimchy

    #超时删除:DELETE /index/type/1?timeout=5m

    #删除所有:POST index/type/_delete_by_query

      {
      "query": {
      "match_all": {}
      }
      }
    #按条件删除
POST index/type/_delete_by_query

      {
      "query": {
      "match": {
      "your key": "your value"
      }
      }
      }
  #查询
  #根据id查询
    如果知道id,可以根据id直接查询
GET index/type/id

  #条件查询
    查询总记录数,类似与MySQL中select count(*) from xx

    GET index/type/_count
  #查询所有

    GET index/type/_search
    {
    "query": {
    "match_all": {}
    }
    }
query代表查询对象,match_all是查询对象的属性,代表匹配属性或者匹配级别,除了match_all,还有 multi_match,match,term , range ,bool等。
有时候返回的字段太多,其实我们只需要个别字段,可以使用过滤。
a.直接指定返回字段:

  GET order/order_item/_search
    {
    "_source": ["message","oid"],
    "query": {
    "match_all": {}
    }
    }
b.使用includes(include)和excludes(exclude),一个是包含,一个是排除。
include只能指定一个字段,所以已经被弃用。

  GET order/order_item/_search
    {
    "_source": {
    "include": "tid"
    },
    "query": {
    "match_all": {}
    }
    }
这个效果和下面是一样的:

  GET index/type/_search
    {
    "_source": "tid",
    "query": {
    "match_all": {}
    }
    }
而includes可以指定多个字段

  GET order/order_item/_search
    {
    "_source": {
    "includes":["tid","message"]
    },
    "query": {
    "match_all": {}
    }
    }
excludes和exclude的区别和使用一样。
c.根据id查询,并且需要指定返回字段

  GET index/type/id?_source=tid,message

multi_match代表多字段匹配
下面示例会在OWNERENAME和PS_C_BRAND_ENAME两个字段中都含有“系统”的查询出来

  GET index/type/_search
    {
    "query":{
    "multi_match": {
    "query":"系统",
    "fields": [ "OWNERENAME", "PS_C_BRAND_ENAME" ]
    }
    }
    }
match代表匹配查询
示例会将字段message中含有系统或者管理员的都查出来,如果没有,检查下ES的分词器。

  GET index/type/_search
    {
    "query":{
    "match":{
    "message":"系统管理员"
    }
    }
    }
如果想精确匹配,值查询messgae是“系统管理员”,使用逻辑符operator绑定查询,如下。

  GET index/type/_search
    {
    "query":{
    "match": {
    "message": {
    "query": "系统管理员",
    "operator": "and"
    }
    }
    }
    }
term 代表精确值匹配
和使用operator绑定查询效果类似,但是可以使用在数字、时间上。

  GET index/type/_search
    {
    "query":{
    "term":{
    "message":"系统管理员"
    }
    }
    }

range代表范围查询
示例将价格在100到1000的数据查询出来。

  GET index/type/_search
    {
    "query":{
    "range": {
    "price": {
    "gte": 100,
    "lt": 1000
    }
    }
    }
    }

bool查询
must(与)、must_not(非)、should(或),说明下,ES中的逻辑与或非是针对分词来说的,比如下面是messge这个字段不能包含“哈哈”,而不是等于。

  GET order/order_item/_search
    {
    "query": {
    "bool": {
    "must": [
    {
    "match": {
    "price":10.09
    }
    }
    ],
    "must_not": [
    {
    "match": {
    "message":"哈哈"
    }
    }
    ],
    "should": [
    {
    "match": {
    "tid":"S215488888"
    }
    }
    ]
    }  
    }
    }
from–size查询
这个类似与MySQL的limit,from是起始位置,size是偏移量。

  GET index/type/_search
    {
    "from": 0,
    "size": 3,
    "query": {
    "match_all": {}
    }
    }
排序查询
根据id倒叙查询所有,多字段排序以逗号分隔。

  GET index/type/_search
    {
    "sort": [
    {
    "ID": "desc",
    "TID": "desc"
    }
    ],
    "query": {
    "match_all": {}
    }
    }
或者这样

  GET index/type/_search
    {
    "sort": [
    {
    "ID": {
    "order": "desc"
    },
    "TID": {
    "order": "desc"
    }
    }
    ],
    "query": {
    "match_all": {}
    }
    }
骚写法比较多,也可以这样

  GET index/type/_search
    {
    "sort": [
    { "ID": { "order": "desc" }},
    { "TID": { "order": "desc" }}
    ],
    "query": {
    "match_all": {}
    }
    }
ES提供的查询比较强大,除了上面的,还有过滤,排序,聚合等,后面用到再补上,这些查询可以任意搭配组合,满足各种情况的查询。

  # 创建索引名为order 的索引:

    PUT order
      {
      "settings": {
      "number_of_shards" : "5",
      "number_of_replicas" : "1"
      }
      }        ////number_of_shards是设置分片数量,number_of_replicas是设置副本数量。当然可以直接PUT order创建,这样两者默认值分别是5和1。

  # 查询索引:GET order       /        GET order/_settings

    _all和 * 都是进行模糊匹配,所以可以使用GET * 或者GET _all 来查询所有的索引,也可使用GET test* 来匹配test开头的。

  # 删除索引:DELETE order   // 谨慎使用通配符

浏览器带接口执行:

  # 创建索引:POST http://localhost:9200/movies

  # 查看索引状态:http://ds0:9200/_cat/indices?v          //浏览器中执行

  # 添加索引信息(更新索引信息=》相同的索引,相同的类型,相同的ID):

    PUT http://ds0:9200/movies/movie/1
      {
      "title": "The Godfather",
      "director": "Francis Ford Coppola",
      "year": 1972
      }

  # 获取文档或索引:GET http://ip:port/索引/类型/ID

  # 更新文档内容:

    http://ds0:9200/secisland/secilog/1/_update/
      {
      "doc":{
      "computer":"secisland",
      "message":"secisland is an security computer.It provides log analysis products"
      }
      }

  # 删除文档或索引:DELETE  http://ip:port/索引名/类型名/ID

  # _search 端点用于搜索: <index>/<type>/_search     //其中index和type都是可以去掉的

聚合查询:

# 没有这个字段的查找
  GET tyjatip-threatbook-text/_search
    {   
    "aggs": {
    "account_missing": {
    "missing": {
    "field": "ioc_type"
    }
    }
    }
    }

# 查询全部ioc_type占多少, 只能查询long 类型的
  POST texttip-nitsc-one/_search
    {
    "track_total_hits": true,
    "query": {
    "bool": {
    "must": [
    {
    "query_string": {
    "query": "*"
    }
    },
    {
    "range": {
    "timestamp": {
    "gte": "2020-06-29T13:29:20Z"
    }
    }
    }
    ]
    }
    },
    "aggs": {
    "ioc_type": {
    "terms": {
    "field": "ioc_type.keyword"
    }
    }
    },
    "size": 0
    }

# 统计confidence字段的10,和75的占比
  GET texttip-nitsc-one/_search
    {
    "size": 0,
    "aggs": {
    "tests_confidence": {
    "percentile_ranks": {
    "field": "confidence",
    "values": [
    1,75
    ]
    }
    }
    }
    }

# percentiles对指定字段(脚本)的值按从小到大累计每个值对应的文档数的占比(占所有命中文档数的百分比),返回指定占比比例对应的值。默认返回[ 1, 5, 25, 50, 75, 95, 99 ]分位上的值

  GET texttip-nitsc-one/_search
    {
    "size": 0,
    "aggs": {
    "tests_confidence":{
    "percentiles": {
    "field": "intelclass",
    "percents": [
    "indicator"
    ]
    }}}
    }

# 计算分数的平均值
  GET tyjatip-threatbook-text/_search
    {
    "size": 0,
    "aggs": {
    "avg_confidence": {
    "avg": {
    "field": "confidence"}}}
    }

# 大于某个数的,然后求平均值,基于范围
  GET tyjatip-threatbook-text/_search
    {
    "size": 0,
    "query": {
    "range": {
    "confidence": {
    "gte": 0,
    "lte": 75}}},
    "aggs": {
    "avg_confidence": {
    "avg": {
    "field": "confidence"}}}
    }

# 多值聚合 存在这个字段的,有多少个这个值
  GET tyjatip-threatbook-two/_search
    {
    "query": {
    "bool": {
    "must": [
    {
    "terms": {
    "ioc": [
    "77.29.120.69",
    "14.186.151.119"
    ]
    }
  },
    {
    "terms": {
    "ioc_type": [
    "ipv4"
    ]
    }
    }
    ]
    }
  },
    "aggs": {
    "raw.ioc": {
    "terms": {
    "field": "row.ioc.keyword"
    }
  },
    "raw.ioc_type": {
    "terms": {
    "field": "row.ioc_type.keyword"
    }
    }
  },
    "size": 0

  }

# 查询前缀
  {
    "query": {
    "bool": {
    "must": [
    {
    "prefix": {
    "raw.id": "campaign"

    }
    }
    ]
    }
  },
    "size": 0
  }

 

# 为索引增加别名
  POST _aliases
    {
    "actions" : [{"add" : {"index" : "texttip-nitsc-one" , "alias" : "nitsc-intel-repo"}}]
    }

# 查看别名
  GET texttip-nitsc-one/_alias/*

在URL后面添加?pretty的意义

  • 在任意的查询字符串中增加pretty参数,会让Elasticsearch美化输出(pretty-print)JSON响应以便更加容易阅读。不包含(_source,这个字段由用户输入时的格式一致)

python-ES:

  ES:插入es.index() 获取es.get() 删除es.delete()搜索:es.search()

  多值的搜索用body中的bool{“must”:[{}{}]}

  切片:from:2,size2从第二页开始,显示两个

  范围:range:gte,lte

  排序:sort

  Script: 脚本,可以用来做更新搜索等等功能,直接在body里面添加{“script”:”脚本”}

  原始查询使用网址链接进行查询的,通过网络的协议(get/post/put/delete...)进行增删改查,这个可以根据不同的语言自己定制

不定期持续更新文档.......

posted @ 2020-05-27 20:19  雅痞六爷  阅读(848)  评论(0)    收藏  举报