背景

es部署在了ip为192.168.159.132虚拟机的docker上,端口为9200。还部署了kibana,端口为5601。

es基础知识

不同类型的存储有不同的叫法,对于会mysql的我来说无非是一种映射关系。

mysql

es

mongodb

数据库

数据库

索引

数据库



类型

集合



文档

文档

字段

字段

字段

字段

索引(数据库)操作

创建索引

curl -XPUT 'localhost:9200/foo'

查看索引

curl 'localhost:9200/_cat/indices'
yellow open .kibana fABwVuctSviluUjU_BL1xA 1 1  2 0  5.8kb  5.8kb
yellow open foo     IjJLZLlsShOP1rQfBHirjw 5 1  0 0   810b   810b

删除索引

curl -XDELETE 'localhost:9200/foo'

牛皮,你已经会创建es的数据库,删除数据库,已经查看数据库了

写数据

添加数据

先创建数据库

curl -XPUT 'localhost:9200/foo'

id由es创建文档
向foo索引的hello类型添加文档,id有es生成(向foo数据库的hello表添加一条数据)
不指定id必须使用post请求。

curl  'localhost:9200/foo/hello' -XPOST -d '{"name":"foo","age":"18"}'

指定id创建文档
向foo索引的hello类型添加id为1的文档(向foo数据库的hello表添加一条数据)
如果id存在的话,会删除原来的文档,创建新文档。

curl  'localhost:9200/foo/hello/1' -d '{"name":"foo","age":"18"}'

查看数据

简单的等值查询。
查看foo索引中hello类型的id为1的文档

curl 'localhost:9200/foo/hello/1'

删除数据

等值删除,删除foo数据库hello表id为1的文档

curl 'localhost:9200/foo/hello/1' -XDELETE

更新文档

把foo数据库的hello表中id为1的文档替换为新文档{"weight":"60kg","age":"19"} 注意原始文档会删除。

curl -XPUT 'localhost:9200/foo/hello/1' -d '{"weight":"60kg","age":"19"}'

更新文档的部分字段,保留原始数据

把id为1的文档,age从19更新到18。其他字段不变

curl 'localhost:9200/foo/hello/1/_update' -d '{"doc":{"age":"18"}}'

丰富的查询语法

查询之前先创建es官网提供的样本集
需要当执行命令的目录存在accounts.json文件。 从这儿下载

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

搜索api

查询bank数据库中所有的文档,按account_number字段顺排序

curl 'localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty'

返回的结果如下

{
  "took" : 63,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : null,
    "hits" : [ {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "0",
      "sort": [0],
      "_score" : null,
      "_source" : {"account_number":0,"balance":16623,"firstname":"Bradshaw","lastname":"Mckenzie","age":29,"gender":"F","address":"244 Columbus Place","employer":"Euron","email":"bradshawmckenzie@euron.com","city":"Hobucken","state":"CO"}
    }, {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "1",
      "sort": [1],
      "_score" : null,
      "_source" : {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
    }, ...
    ]
  }
}

对于返回结果,我们可以看出以下几点:

  • took-elasticsearch执行搜索花费的毫秒数
  • timed_out-告诉我们这次搜索是否超时
  • _shards- 告诉我们搜索了多少个分片,以及搜索成功和失败的分片数
  • hits-搜索返回的文档结果
  • hits.total 一共命中了多少结果
  • sort-搜索排序规则,如果没有该字段,则按相关度排序
  • _score和max_score 暂时先忽略这个参数(文档得分,反映相关度)

查询语法

curl 'localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty'

上面这个例子,可以使用更为强大的查询语法实现:

query

curl 'localhost:9200/bank/_search?pretty' -d '{"query":{"match_all":{}},"sort":[{"account_number":"asc"}]}'

query 字段说明查询内容,match_all是执行查询操作的匹配类型。match_all是匹配所有文档的意思
除了query参数,我们也可以传递其他参数去影响查询结果。在上一章节的例子我们传递了一个sort参数,这里我们传递一个size参数:

size

curl 'localhost:9200/bank/_search?pretty' -d '{"query":{"match_all":{}},"sort":[{"account_number":"asc"}],"size":50}'

注意,如果size没有指定,默认值是10.

from

curl 'localhost:9200/bank/_search?pretty' -d '{"query":{"match_all":{}},"sort":[{"account_number":"asc"}],"size":1,"from":999}'

from参数指定从第几个文档开始返回,size参数指定一共返回多少个文档。这个特性是对分页功能是十分重要的。如果from没有指定,默认值是0。

source

只返回指定字段

curl 'localhost:9200/bank/_search?pretty' -d '{"query":{"match_all":{}},"sort":[{"account_number":"asc"}],"size":1,"from":999,"_source":["account_number","firstname"]}'

match

现在,让我们继续学习查询语法。之前,我们已经看到过match_all查询类型使用来匹配所有文档的。现在,我们介绍一种新的查询类型match,他是基于字段搜索的(即,通过匹配一个特定的字段或一组字段执行搜索)

curl 'localhost:9200/bank/_search?pretty' -d '{"query":{"match":{"firstname":"Amber"}}}'

匹配所有地址字段包含mill的账户文档

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

返回地址字段包含“mill”或者“lane”的账户

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

下面的例子是match的变种(match_phrase),它返回所有地址字段包含词组“mill lane”的账户

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

bool查询

and查询

address字段必须包含mill和lane。

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

or查询

curl 'localhost:9200/bank/_search?pretty' -d'
{
  "query": {
    "bool": {
      "should": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}'

not查询

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

合并查询

地址必须包含mill或者lane并且age等于40

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

大于小于查询

帐号内的金额必须大于2000元的

curl 'localhost:9200/bank/_search?pretty' -d{
  "query": {
    "bool": {
      "must": [
        {"match_all": {}}
      ],
      "filter": {
        "range": {
          "balance": {
            "gte": 2000,
            "lte": 3000
          }
        }
      }
    }
    
  }
}