ES CURD


API

1.自动生成id

POST users/_doc
{
  "user":"alex",
  "age":18,
  "birth_day":"2021-1-1"
}

结果

{
  "_index" : "users",
  "_type" : "_doc",
  "_id" : "CdD1XX4BWdgdyhkna-of",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

2.指定id

POST users/_doc/123
{
  "user":"alex",
  "age":18,
  "birth_day":"2021-1-1"
}

指定_id=”123”,结果如下

{
  "_index" : "users",
  "_type" : "_doc",
  "_id" : "123",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

3.PUT方式,指定id

PUT users/_doc/1234
{
  "user":"alex",
  "age":18,
  "birth_day":"2021-1-1"
}

结果

{
  "_index" : "users",
  "_type" : "_doc",
  "_id" : "1234",
  "_version" : 1,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 8,
  "_primary_term" : 1
}

批量获取

GET /_mget
{
  "docs":[
      {
        "_index":"users",
        "_id":123
      },{
        "_index":"users",
        "_id":1235
      }
    ]
}

结果

{
  "docs" : [
    {
      "_index" : "users",
      "_type" : "_doc",
      "_id" : "123",
      "_version" : 8,
      "_seq_no" : 14,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "user" : "alex",
        "age" : 18,
        "birth_day" : "2021-1-1"
      }
    },
    {
      "_index" : "users",
      "_type" : null,
      "_id" : "1235",
      "found" : false
    }
  ]
}

分词

// standard
GET _analyze
{
  "analyzer":"standard",
  "text":"i am a Chinese"
}

结果

{
  "tokens" : [
    {
      "token" : "i",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "am",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "a",
      "start_offset" : 5,
      "end_offset" : 6,
      "type" : "<ALPHANUM>",
      "position" : 2
    },
    {
      "token" : "chinese",
      "start_offset" : 7,
      "end_offset" : 14,
      "type" : "<ALPHANUM>",
      "position" : 3
    }
  ]
}

Search API

  • /_search:搜索集群上所以的索引
  • /index1/_search:在index1索引上搜索
  • /index1,index2/_search:在index1,index2索引上搜索
  • /index*/_search:在以index开头的索引上搜索

1.URI Search
通过URI query实现搜索

实例

http://localhost:9200/fenci/_search?q=id:1
或者
http://localhost:9200/fenci/_search?q=86&df=id

结果

{
    "took":1,
    "timed_out":false,
    "_shards":{
        "total":5,
        "successful":5,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":1,
        "max_score":1,
        "hits":[
            {
                "_index":"fenci",
                "_type":"doc",
                "_id":"jdDBXX4BWdgdyhknj9rw",
                "_score":1,
                "_source":{
                    "name":"美国",
                    "id":1
                }
            }
        ]
    }
}
  • 使用”q”指定查询字符串
  • df:默认字段,不指定是,会对所以字段进行查询
  • sort:排序
  • from,size分页
  • profile 可以查看查询是如何被执行的

实例

GET users/_search?q=user:(zhang OR zhan)

结果

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.5404451,
    "hits" : [
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 1.5404451,
        "_source" : {
          "user" : "zhan",
          "age" : 19,
          "birth_day" : "2021-1-2"
        }
      },
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "1234",
        "_score" : 1.5404451,
        "_source" : {
          "user" : "zhang",
          "age" : 19,
          "birth_day" : "2021-1-2"
        }
      },
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 0.6931472,
        "_source" : {
          "user" : "zhang",
          "age" : 19,
          "birth_day" : "2021-1-2"
        }
      }
    ]
  }
}

es还支持范围、大小、通配符、模糊查询(~)

2.Request Body Search

格式:http://localhost:9200/index/_search
参数都放在body中

示例

GET users/_search
{
  "query":{
    "match":{
      "user":"zhan OR zhang"
    }
  },
  "sort":"age",
  "script_fields":{
    "age":{
      "script":{
        "source":"doc['age'].value+'岁'"
      }
    }
  }
}

结果

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : null,
        "fields" : {
          "age" : [
            "19岁"
          ]
        },
        "sort" : [
          19
        ]
      },
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : null,
        "fields" : {
          "age" : [
            "19岁"
          ]
        },
        "sort" : [
          19
        ]
      },
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "1234",
        "_score" : null,
        "fields" : {
          "age" : [
            "19岁"
          ]
        },
        "sort" : [
          19
        ]
      },
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : null,
        "fields" : {
          "age" : [
            "20岁"
          ]
        },
        "sort" : [
          20
        ]
      }
    ]
  }
}

mapping

  • 定义索引中字段名称、类型、分词器
  • 一个mapping属于一个index

字段类型

  • 简单类型:text/keyword,date,integer/floating,boolean,ipv4&ipv6
  • 复杂类型:对象和嵌套对象
  • 特殊类型:geo_point,geo_shape/percolator

Dynamic Mappings

PUT movies
{
    "mappings":{
        "_doc":{
            "dynamic":"false"
        }
    }
}
  • true:新插入的文档如有有新字段,则新字段会在文档中出现、新字段可被索引、mappings被更新
  • false:新插入的文档如有有新字段,则新字段会在文档中出现, 但是新字段不会被索引、mappings不会被更新
  • strict:严格模式,即新插入的文档如有有新字段,会直接报错,写入失败

Index Template

Dynamic Template

Aggregation(聚合)

  • Bucket Aggregation:一系列满足特定条件的文档的集合。类似于sql中的group
  • Metric Aggregation:一些属性运算,可以对文档字段进行统计分析。类似于sql中的count
  • Pipeline Aggregation:对其他的集合结果进行二次聚合
  • Matrix Aggregation:支持对多个字段的操作并提供一个结果矩阵

示例

GET myidx3/_search
{
  "aggs": {
    "aggs_avg_age": {
      "avg": {
        "field": "age"
      }
    },
    "aggs_max_age":{
      "max":{
        "field":"age"
      }
    },
    "aggs_min_age":{
      "min":{
        "field":"age"
      }
    }
  }
}

结果

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "myidx3",
        "_type" : "1",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "wang",
          "age" : 20
        }
      },
      {
        "_index" : "myidx3",
        "_type" : "1",
        "_id" : "fdHSYX4BWdgdyhknJyjF",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhang",
          "age" : 19
        }
      },
      {
        "_index" : "myidx3",
        "_type" : "1",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "li",
          "age" : 21
        }
      }
    ]
  },
  "aggregations" : {
    "aggs_max_age" : {
      "value" : 21.0
    },
    "aggs_min_age" : {
      "value" : 19.0
    },
    "aggs_avg_age" : {
      "value" : 20.0
    }
  }
}

测试题

  • (1)ES支持使用HTTP PUT写入新文档,并自动生成ID
    • 错,需要用POST。PUT只能用于向已经存在的index中插入新文档,如果index不存在,它不会新建,会报错。
  • (2)Update一个文档,需要用HTTP PUT
    • 对。用POST,PUT都可以。
  • (3)index一个已经存在的文档,会先删除旧文档,再新建新文档,同时版本号加1
    • 对。
  • (4)尝试描述创建一个新的文档到一个不存在的索引中,背后会发生什么
    • 默认情况下,会自动创建所以,并且用过Dynamic Mapping自动创建mapping,当然实际情况会看是否有合适的index template。
  • (5)ES7中合法的type是什么
    • _doc
  • (6)精确值匹配和全文匹配的本质是什么
    • 精确值不会被Analyzer分词,它是精确匹配
  • (7)Analyzer由哪几个部分组成
    • Character Filter+Tokenizer+Token Filter

文章作者: Alex
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Alex !
  目录