zl程序教程

您现在的位置是:首页 >  APP

当前栏目

ES mget、bulk、mappings

2023-02-19 12:19:47 时间

文章目录

1. mget 批量查询

  • 批量查询
GET _mget
{
  "docs": [
    {
      "_index": "test_index",
      "_id": 1
    },
    {
      "_index": "kibana_sample_data_flights",
      "_id": "frc8U4UBIo5EnYllKV0t"
    }
  ]
}

输出

{
  "docs": [
    {
      "_index": "test_index",
      "_id": "1",
      "_version": 3,
      "_seq_no": 11,
      "_primary_term": 1,
      "found": true,
      "_source": {
        "name": "test_new",
        "value": 100,
        "alisa": "名称"
      }
    },
    {
      "_index": "kibana_sample_data_flights",
      "_id": "frc8U4UBIo5EnYllKV0t",
      "_version": 1,
      "_seq_no": 8373,
      "_primary_term": 1,
      "found": true,
      "_source": {
        "FlightNum": "5EKI6QP",
        "DestCountry": "CN",
        "OriginWeather": "Rain",
        "OriginCityName": "London",
        "AvgTicketPrice": 1028.3575673252506,
        "DistanceMiles": 5761.935055291024,
        "FlightDelay": true,
        "DestWeather": "Sunny",
        "Dest": "Shanghai Pudong International Airport",
        "FlightDelayType": "Late Aircraft Delay",
        "OriginCountry": "GB",
        "dayOfWeek": 5,
        "DistanceKilometers": 9272.935609622278,
        "timestamp": "2023-01-14T19:36:28",
        "DestLocation": {
          "lat": "31.14340019",
          "lon": "121.8050003"
        },
        "DestAirportID": "PVG",
        "Carrier": "JetBeats",
        "Cancelled": false,
        "FlightTimeMin": 902.3525435444484,
        "Origin": "London Gatwick Airport",
        "OriginLocation": {
          "lat": "51.14810181",
          "lon": "-0.190277994"
        },
        "DestRegion": "SE-BD",
        "OriginAirportID": "LGW",
        "OriginRegion": "GB-ENG",
        "DestCityName": "Shanghai",
        "FlightTimeHour": 15.03920905907414,
        "FlightDelayMin": 240
      }
    }
  ]
}
  • 同 一个 index 可以简写
GET test_index/_mget
{
  "docs": [
    {
      "_id": 1
    },
    {
      "_id": 2
    }
  ]
}

or

GET test_index/_mget
{
  "ids": [
    1,
    2
  ]
}

2. bulk 批量写入

写入一条

POST _bulk
{"create":{"_index":"test_index","_id":3}}  # action, create 可以改成 index(替换doc)
{"name":"test_new1","value":[1,2,3]}  # data 这两行不能分在多行

写入多条,注意 create、index 的区别

POST _bulk
{"create":{"_index":"test_index","_id":3}}
{"name":"test_new3","value":[1,2,3]}
{"create":{"_index":"test_index","_id":4}}
{"name":"test_new4","value":4}
{"index":{"_index":"test_index","_id":5}}
{"name":"test_new5","value":5}

or 同一index可简写

POST test_index/_bulk
{"create":{"_id":3}}
{"name":"test_new3","value":[1,2,3]}
{"create":{"_id":4}}
{"name":"test_new4","value":4}
{"index":{"_id":5}}
{"name":"test_new5","value":5}
{
  "took": 168,
  "errors": true,
  "items": [
    {
      "create": {
        "_index": "test_index",
        "_id": "3",  # 已经存在,create 报错
        "status": 409,
        "error": {
          "type": "version_conflict_engine_exception",
          "reason": "[3]: version conflict, document already exists (current version [2])",
          "index_uuid": "ntM1X5SOTxiz8tRVwdHK6g",
          "shard": "0",
          "index": "test_index"
        }
      }
    },
    {
      "create": {
        "_index": "test_index",
        "_id": "4",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 15,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "test_index",
        "_id": "5",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 16,
        "_primary_term": 1,
        "status": 201
      }
    }
  ]
}

update 更新

POST _bulk
{"update":{"_index":"test_index","_id":3}}
{"doc":{"value":3}}
{"update":{"_index":"test_index","_id":4}}
{"doc":{"name":"test_new44"}}
{"update":{"_index":"test_index","_id":5}}
{"doc":{"name":"test_new55","value":55}}

or 简写

POST test_index/_bulk
{"update":{"_id":3}}
{"doc":{"value":3}}
{"update":{"_id":4}}
{"doc":{"name":"test_new44"}}
{"update":{"_id":5}}
{"doc":{"name":"test_new55","value":55}}

delete 删除

POST _bulk
{"delete": {"_index": "test_index", "_id": 3}}
{"delete": {"_index": "test_index", "_id": 4}}

or

POST test_index/_bulk
{"delete": {"_id": 5}}
{"delete": {"_id": "ZLcgV4UBIo5EnYlljo2Q"}}

3. 条件删除

POST test_index/_delete_by_query
{
  "query": {
    "term": {
      "value": 100
    }
  }
}

输出

{
  "took": 297,
  "timed_out": false,
  "total": 1,
  "deleted": 1,
  "batches": 1,
  "version_conflicts": 0,
  "noops": 0,
  "retries": {
    "bulk": 0,
    "search": 0
  },
  "throttled_millis": 0,
  "requests_per_second": -1,
  "throttled_until_millis": 0,
  "failures": []
}

检查 value = 100 的 doc 已被删除

GET test_index/_search

4. 条件更新

POST test_index/_update_by_query
{
  "query": {
    "term": {
      "value": 99
    }
  },
  "script": {
    "source": "ctx._source['value'] = 199"
  }
}

更新 query 条件下(value=99)的 doc,将其 value 改为 199

输出

{
  "took": 307,
  "timed_out": false,
  "total": 1,
  "updated": 1,
  "deleted": 0,
  "batches": 1,
  "version_conflicts": 0,
  "noops": 0,
  "retries": {
    "bulk": 0,
    "search": 0
  },
  "throttled_millis": 0,
  "requests_per_second": -1,
  "throttled_until_millis": 0,
  "failures": []
}

5. 映射 mappings

查询映射

GET test_index/_mapping

输出

{
  "test_index": {
    "mappings": {
      "properties": {
        "alisa": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "value": {
          "type": "long"
        }
      }
    }
  }
}

查看单个字段的属性

GET test_index/_mapping/field/value

输出

{
  "test_index": {
    "mappings": {
      "value": {
        "full_name": "value",
        "mapping": {
          "value": {
            "type": "long"
          }
        }
      }
    }
  }
}

6. 自动映射

dynamic mappings 建立 index 的时候没有设置 mapping,根据你的 value 自动推断类型

ES 不支持修改字段类型、没有隐式转换(比如 “123” 不会当成 数字 123)、建议指定 mapping

7. 显式映射

PUT test_index1
{
  "mappings": {
    "properties": {
      "price": {
        "type": "float"
      },
      "name": {
        "type": "keyword"
      },
      "desc": {
        "type": "text"
      }
    }
  }
}

查看 mapping

GET test_index1/_mapping

数据类型 见 https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html

映射参数 https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html

  • coerce,是否允许强制类型转换
  • copy_to,把字段的值 copy 到对应字段中,不可见,但是可查
  • doc_values,设置为 false ,不能对该字段进行排序,聚合查询,可以减少磁盘占用
  • enable,index 是否被索引,或者 object 字段是否被索引
  • ignore_above,对超过长度的字段,搜索时无法搜索到
  • index,字段是否可以被索引
  • fields,可以创建多个子字段
  • norms,是否禁用评分(评分可用来排序)
  • store,加速查询 store 过的字段