Elasticsearch入门学习重点笔记

释放双眼,带上耳机,听听看~!
  • 必记知识点

  Elasticsearch可以接近实时的搜索和存储大量数据。Elasticsearch是一个近实时的搜索平台。这意味着当你导入一个文档并把它变成可搜索的时间仅会有轻微的延时。

  文档是基本存储单元,以json格式存储,比如说一个用户信息,类似数据库里的单条数据。索引是多个同一类文档的集合,类似数据库里的表。Elasticsearch支持集群部署,一台服务器是一个节点,多个节点组成一个集群,每个节点有唯一节点名,同一集群里的节点有共同的集群名称。索引可以分片和复制,类似数据库的水平分片和主从复制,主索引分片和复制分片需要在不同的节点,来保证高可用。

  • API

  Elasticsearch的api为rest风格,可以通过url增删改查,格式为:<REST Verb>    /<Index>/<Type>/<ID>   

  REST Verb为:PUT、DELETE、GET、POST

  Index为索引

  Type为索引里的文档,如doc

  ID为文档的id,可以在创建文档时定义,和数据库表的id类似,当将文档加入索引时,ID部分并不是必须的。如果没有指定,Elasticsearch将会生产一个随机的ID,然后使用它去索引文档。Elasticsearch生成的ID将会在API调用成功后返回。

  PUT:

  示例: PUT  /customer/doc/1?pretty  请求将会将一个ID为1的文档加入customer索引。curl命令:curl -XPUT \'localhost:9200/customer/doc/1?pretty&pretty\' -H \'Content-Type: application/json\' -d\' { \"name\": \"John Doe\" } \'

  如果我们再次执行上面的请求,Elasticsearch将会根据相同的ID更新之前的文档。

  DELETE:

  删除ID为2的文档: curl -XDELETE \'localhost:9200/customer/doc/2?pretty&pretty\'

  POST:

  当我们没有明确指定ID的时候,我们需要使用POST方法代替PUT来发送请求。curl -XPOST \'localhost:9200/customer/doc?pretty&pretty\' -H \'Content-Type: application/json\' -d\' { \"name\": \"Jane Doe\" } \'

  如下的例子演示如何去更新我们的之前ID为1的文档,

curl -XPOST \'localhost:9200/customer/doc/1/_update?pretty&pretty\' -H \'Content-Type: application/json\' -d\'
{
\"doc\": { \"name\": \"Jane Doe\" }
}
\'

  GET:

  获取ID为2的文档: curl -XGET \'localhost:9200/customer/doc/2?pretty&pretty\'

  批处理_bulk

  在一个批操作中创建了两个文档:

curl -XPOST \'localhost:9200/customer/doc/_bulk?pretty&pretty\' -H \'Content-Type: application/json\' -d\'
{\"index\":{\"_id\":\"1\"}}
{\"name\": \"John Doe\" }
{\"index\":{\"_id\":\"2\"}}
{\"name\": \"Jane Doe\" }
\'

  先更新ID为1的文档,然后删除ID为2的文档:

curl -XPOST \'localhost:9200/customer/doc/_bulk?pretty&pretty\' -H \'Content-Type: application/json\' -d\'
{\"update\":{\"_id\":\"1\"}}
{\"doc\": { \"name\": \"John Doe becomes Jane Doe\" } }
{\"delete\":{\"_id\":\"2\"}}
\'

  查询语言

  执行搜索有两种基础的方式,一种是在请求的URL中加入参数来实现,另一种方式是将请求内容放到请求体中。使用请求体可以让你的JSON数据以一种更加可读和更加富有展现力的方式发送。

curl -XGET \'localhost:9200/bank/_search?pretty\' -H \'Content-Type: application/json\' -d\'
{
  \"query\": { \"match_all\": {} }
}
\'

  query 部分告诉我们我们的查询定义是什么,match_all 部分简单指定了我们想去执行的查询类型,意思就是在索引中搜索所有的文档。

  除了query参数,我们还可以通过其他的参数影响搜索结果。 sort来指定搜索结果的顺序,from 参数(从0开始)指定了从哪个文档索引开始,size 参数指定了从from指定的索引开始返回多少个文档,用来分页。

curl -XGET \'localhost:9200/bank/_search?pretty\' -H \'Content-Type: application/json\' -d\'
{
  \"query\": { \"match_all\": {} },
  \"_source\": [\"account_number\", \"balance\"]
}
\'
 

  _source指定需要返回的属性名。示例演示了如何返回两个属性,account_number 和 balance (在_source中):

  match它可以被认为是基本的属性搜索查询(就是通过特定的一个或多个属性来搜索)。

curl -XGET \'localhost:9200/bank/_search?pretty\' -H \'Content-Type: application/json\' -d\'
{
  \"query\": { \"match\": { \"account_number\": 20 } }
}
\'
 

  

  match_phrase,将返回所有address中包含“mill lane”的文档。

curl -XGET \'localhost:9200/bank/_search?pretty\' -H \'Content-Type: application/json\' -d\'
{
  \"query\": { \"match_phrase\": { \"address\": \"mill lane\" } }
}
\'
 

  bool 查询允许我们使用布尔逻辑将小的查询组成大的查询。bool must 子句指定了所有匹配文档必须满足的条件。

GET /bank/_search
{
  \"query\": {
    \"bool\": {
      \"must\": [
        { \"match\": { \"address\": \"mill\" } },
        { \"match\": { \"address\": \"lane\" } }
      ]
    }
  }
}
 

  如下示例组合两个match查询并且返回所有address属性中包含 “mill” 或 “lane” 的账户文档,bool should 子句指定了匹配文档只要满足其中的任何一个条件即可匹配。

curl -XGET \'localhost:9200/bank/_search?pretty\' -H \'Content-Type: application/json\' -d\'
{
  \"query\": {
    \"bool\": {
      \"should\": [
        { \"match\": { \"address\": \"mill\" } },
        { \"match\": { \"address\": \"lane\" } }
      ]
    }
  }
}
\'
 

  如下示例组合两个match查询并且返回所有address属性中既不包含 “mill” 也不包含 “lane” 的账户文档,bool must_not 子句指定了其中的任何一个条件都不满足时即可匹配。

curl -XGET \'localhost:9200/bank/_search?pretty\' -H \'Content-Type: application/json\' -d\'
{
  \"query\": {
    \"bool\": {
      \"must_not\": [
        { \"match\": { \"address\": \"mill\" } },
        { \"match\": { \"address\": \"lane\" } }
      ]
    }
  }
}
\'
 

  我们可以在一个bool查询中同时指定mustshouldmust_not子句。此外,我们也可以在一个bool子句中组合另一个bool来模拟任何复杂的多重布尔逻辑。

  bool 查询也支持 filter 子句,它允许我们可以在不改变得分计算逻辑的的情况下限制其他子句匹配的查询结果。为了示例说明,让我们介绍一下range 查询,它允许我们通过一个值区间来过滤文档。这个通常用在数值和日期过滤上。

curl -XGET \'localhost:9200/bank/_search?pretty\' -H \'Content-Type: application/json\' -d\'
{
  \"query\": {
    \"bool\": {
      \"must\": { \"match_all\": {} },
      \"filter\": {
        \"range\": {
          \"balance\": {
            \"gte\": 20000,
            \"lte\": 30000
          }
        }
      }
    }
  }
}
\'
 

  执行聚合

  类似SQL的聚合函数,如下的例子将账户按state进行分组,然后按count降序(默认)返回前10组(默认)states,计算每个state分组的平均账户余额

curl -XGET \'localhost:9200/bank/_search?pretty\' -H \'Content-Type: application/json\' -d\'
{
  \"size\": 0,
  \"aggs\": {
    \"group_by_state\": {
      \"terms\": {
        \"field\": \"state.keyword\"
      },
      \"aggs\": {
        \"average_balance\": {
          \"avg\": {
            \"field\": \"balance\"
          }
        }
      }
    }
  }
}
\'
 

  还有很多其它的聚合功能在这里我们就不去详细介绍了。如果你想了解更多,可以参考https://blog.csdn.net/hololens/article/details/78932628

给TA打赏
共{{data.count}}人
人已打赏
随笔日记

Redis源码分析-底层数据结构盘点

2020-11-9 4:53:10

随笔日记

Google Coral Edge TPU USB加速棒上手体验

2020-11-9 4:53:12

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索