zoukankan      html  css  js  c++  java
  • Elasticsearch 7 : doc_values 属性

    字段的 doc_values 属性有两个值, true、false。默认为 true ,即开启。

    当 doc_values 为 fasle 时,无法基于该字段排序、聚合、在脚本中访问字段值。

    当 doc_values 为 true 时,ES 会增加一个相应的正排索引,这增加的磁盘占用,也会导致索引数据速度慢一些。

    示例1: 关闭 doc_values 属性

    创建索引

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

    插入数据

    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 }

    查询数据

    POST student/_search
    {
      "query": {
        "match_all": {}
      }
    }

    执行结果:

    {
      "took" : 2,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 3,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "student",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "name" : "张三",
              "age" : 12
            }
          },
          {
            "_index" : "student",
            "_type" : "_doc",
            "_id" : "2",
            "_score" : 1.0,
            "_source" : {
              "name" : "李四",
              "age" : 10
            }
          },
          {
            "_index" : "student",
            "_type" : "_doc",
            "_id" : "3",
            "_score" : 1.0,
            "_source" : {
              "name" : "王五",
              "age" : 11
            }
          }
        ]
      }
    }

    查询数据并按照age排序,会报错

    POST student/_search
    {
      "query": {
        "match_all": {}
      },
      "sort" : [
        {"age": {"order": "desc"}}
      ],
      "from": 0,
      "size": 1
    }

    报错如下:

    {
      "error": {
        "root_cause": [
          {
            "type": "illegal_argument_exception",
            "reason": "Can't load fielddata on [age] because fielddata is unsupported on fields of type [integer]. Use doc values instead."
          }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
          {
            "shard": 0,
            "index": "student",
            "node": "wFhSfuLwR3OX21eldbRIHg",
            "reason": {
              "type": "illegal_argument_exception",
              "reason": "Can't load fielddata on [age] because fielddata is unsupported on fields of type [integer]. Use doc values instead."
            }
          }
        ],
        "caused_by": {
          "type": "illegal_argument_exception",
          "reason": "Can't load fielddata on [age] because fielddata is unsupported on fields of type [integer]. Use doc values instead.",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Can't load fielddata on [age] because fielddata is unsupported on fields of type [integer]. Use doc values instead."
          }
        }
      },
      "status": 400
    }

    查询数据并按照 name 排序,会报错

    和基于 age 的排序报错类似。

    获取 age 平均值,会报错

    POST student/_search
    {
      "aggs":{
        "age_stat": {
          "avg": {"field": "age"}
        }
      },
      "from": 0
    }

    和上面的排序报错类似。

    示例2: 开启 doc_values 属性

    创建索引

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

    插入数据

    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 }

    查询数据

    POST student/_search
    {
      "query": {
        "match_all": {}
      }
    }

    执行结果:

    {
      "took" : 2,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 3,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "student",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "name" : "张三",
              "age" : 12
            }
          },
          {
            "_index" : "student",
            "_type" : "_doc",
            "_id" : "2",
            "_score" : 1.0,
            "_source" : {
              "name" : "李四",
              "age" : 10
            }
          },
          {
            "_index" : "student",
            "_type" : "_doc",
            "_id" : "3",
            "_score" : 1.0,
            "_source" : {
              "name" : "王五",
              "age" : 11
            }
          }
        ]
      }
    }

    查询数据并按照age排序,正常执行

    POST student/_search
    {
      "query": {
        "match_all": {}
      },
      "sort" : [
        {"age": {"order": "desc"}}
      ],
      "from": 0,
      "size": 1
    }

    响应:

    {
      "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" : "student",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : null,
            "_source" : {
              "name" : "张三",
              "age" : 12
            },
            "sort" : [
              12
            ]
          }
        ]
      }
    }

    查询数据并按照 name 排序,正常执行

    POST student/_search
    {
      "query": {
        "match_all": {}
      },
      "sort" : [
        {"name": {"order": "desc"}}
      ],
      "from": 0,
      "size": 1
    }

    响应:

    {
      "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" : "student",
            "_type" : "_doc",
            "_id" : "3",
            "_score" : null,
            "_source" : {
              "name" : "王五",
              "age" : 11
            },
            "sort" : [
              "王五"
            ]
          }
        ]
      }
    }

    获取 age 平均值,正常执行

    POST student/_search
    {
      "aggs":{
        "age_stat": {
          "avg": {"field": "age"}
        }
      },
      "from": 0,
      "size": 0
    }

    响应:

    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 3,
          "relation" : "eq"
        },
        "max_score" : null,
        "hits" : [ ]
      },
      "aggregations" : {
        "age_stat" : {
          "value" : 11.0
        }
      }
    }

    原文链接:https://www.letianbiji.com/elasticsearch/es7-doc-values.html

  • 相关阅读:
    Java实现 蓝桥杯 历届试题 斐波那契
    Java实现 蓝桥杯 历届试题 斐波那契
    Java实现 蓝桥杯 历届试题 斐波那契
    Java实现 LeetCode 552 学生出勤记录 II(数学转换?还是动态规划?)
    linux下查看动态链接库so文件的依赖的相关组建
    linux 查看 *.a *.so 符号表(zt)
    linux下查看动态链接库依赖关系的命令 x86: ldd *.so arm: arm-linux-readelf -d *.so 实际例子: 以项目中用到的库librtsp.so分析: lijun@ubuntu:~/workspace$ arm-hisiv100nptl-linux-ld -d librtsp.so arm-hisiv100nptl-linux-ld:
    ldd 查看程序/动态库 的依赖
    修改SVN中文件的可执行属性
    widow下svn上传项目时的文件可执行权限问题
  • 原文地址:https://www.cnblogs.com/chong-zuo3322/p/14037214.html
Copyright © 2011-2022 走看看