Elasticsearch 7:查询结果中展示 _version 字段


#Elasticsearch 笔记


简介

查询 DSL 中加上"version":true即可,例如:

GET student/_search
{
  "query": {
    "match": {
      "age": "12"
    }
  },
  "version": true
}

示例

创建索引:

PUT student
{
  "mappings" : {
    "properties" : {
      "name" : {
        "type" : "keyword"
      },
      "age" : {
        "type" : "integer"
      }
    }
  }
}

使用 _bulk 创建文档

POST _bulk
{ "index" : { "_index" : "student", "_id" : "1" } }
{ "name" : "张三", "age": 12}
{ "index" : { "_index" : "student", "_id" : "2" } }
{ "name" : "李四", "age": 10 }
{ "index" : { "_index" : "student", "_id" : "3" } }
{ "name" : "王五", "age": 11 }
{ "index" : { "_index" : "student", "_id" : "4" } }
{ "name" : "陈六", "age": 11 }

查询 DSL 中加上"version":true即可 :

GET student/_search
{
  "query": {
    "match": {
      "age": "12"
    }
  },
  "version": true
}

响应:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 1,   # 这里显示了 _version
        "_score" : 1.0,
        "_source" : {
          "name" : "张三",
          "age" : 12
        }
      }
    ]
  }
}



( 本文完 )