Elasticsearch字段属性:doc_values

734 阅读2分钟

字段的 doc_values 属性有两个值, true、false。默认为 true ,即开启。
当 doc_values 为 fasle 时,无法基于该字段排序、聚合、在脚本中访问字段值。
当 doc_values 为 true 时,ES 会增加一个相应的正排索引,这增加的磁盘占用,也会导致索引数据速度慢一些。

1. doc_values设置为false

1.1 创建索引

PUT people
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword",
        "doc_values": false
      }
    }
  }
}

1.2 插入数据

POST _bulk
{"index": {"_index": "people", "_id": "1"}}
{"name": "张三"}
{"index": {"_index": "people", "_id": "2"}}
{"name": "李四"}
{"index": {"_index": "people", "_id": "3"}}
{"name": "王五"}

1.3 仅查询数据

1.3.1 查询数据
GET people/_search
{
  "query": {
    "match_all": {}
  }
}
1.3.2 结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "张三"
        }
      },
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "李四"
        }
      },
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "王五"
        }
      }
    ]
  }
}

1.4 按照name排序

1.4.1 查询数据
GET people/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "name": {
        "order": "desc"
      }
    }
  ]
}
1.4.2 结果
{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Can't load fielddata on [name] because fielddata is unsupported on fields of type [keyword]. Use doc values instead."
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "people",
        "node" : "YgYjTbNlQnq_0s9KO7FOVg",
        "reason" : {
          "type" : "illegal_argument_exception",
          "reason" : "Can't load fielddata on [name] because fielddata is unsupported on fields of type [keyword]. Use doc values instead."
        }
      }
    ],
    "caused_by" : {
      "type" : "illegal_argument_exception",
      "reason" : "Can't load fielddata on [name] because fielddata is unsupported on fields of type [keyword]. Use doc values instead.",
      "caused_by" : {
        "type" : "illegal_argument_exception",
        "reason" : "Can't load fielddata on [name] because fielddata is unsupported on fields of type [keyword]. Use doc values instead."
      }
    }
  },
  "status" : 400
}

可以看到,仅查询数据是没有问题了,但是如果按照name来排序,就会报错。

2. doc_values设置为true

2.1 创建索引

PUT people
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword",
        "doc_values": false
      }
    }
  }
}

2.2 插入数据

POST _bulk
{"index": {"_index": "people", "_id": "1"}}
{"name": "张三"}
{"index": {"_index": "people", "_id": "2"}}
{"name": "李四"}
{"index": {"_index": "people", "_id": "3"}}
{"name": "王五"}

2.3 仅查询数据

2.3.1 查询数据
GET people/_search
{
  "query": {
    "match_all": {}
  }
}
2.3.2 结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "张三"
        }
      },
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "李四"
        }
      },
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "王五"
        }
      }
    ]
  }
}

这里仅查询不排序,是和示例1中的结果一样的。

2.4 按照name排序

2.4.1 查询数据
GET people/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "name": {
        "order": "desc"
      }
    }
  ]
}
2.4.2 结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "王五"
        },
        "sort" : [
          "王五"
        ]
      },
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "李四"
        },
        "sort" : [
          "李四"
        ]
      },
      {
        "_index" : "people",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "张三"
        },
        "sort" : [
          "张三"
        ]
      }
    ]
  }
}

doc_values设置为true后,就可以按照name来排序了。