elasticsearch 磁盘优化-index

 index参数 的作用

当我们不需要一些字段参数搜索和过滤时可以配置index来减轻我们的磁盘空间的消耗。

index 的默认值是true,是能够被索引和过滤的。

index = false 配置那些不需要搜索和过滤,只需要存储展示的字段情况

举个栗子:

创建索引

PUT whl_test
{
  "mappings": {
    "properties": {
      "age": {
        "type": "integer",
        "index": false
      },
      "name": {
        "type": "text",
        "norms": false
      },
      "address": {
        "type": "text",
        "index_options": "freqs"
      }
    }
  }
}

新增数据

PUT whl_test/_doc/1
{
  "age":1,
  "name":"张三",
  "address":"上海浦东"
}

此映射中的age的index被设置了false,这时他就不能被搜索和过滤,我看看效果。

查询语句

GET whl_test/_search
{
  "query": {
    "term": {
      "age": {
        "value": "1"
      }
    }
  }
}

GET whl_test/_search
{
  "query": {
    "match": {
      "age": "1"
    }
  }
}

报错信息

{
  "error" : {
    "root_cause" : [
      {
        "type" : "query_shard_exception",
        "reason" : "failed to create query: Cannot search on field [age] since it is not indexed.",
        "index_uuid" : "qeW607yzSBm3yI1q8DYpVw",
        "index" : "whl_test"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "whl_test",
        "node" : "v0LdTk8GS9ejsZ9k4kWJ8A",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "failed to create query: Cannot search on field [age] since it is not indexed.",
          "index_uuid" : "qeW607yzSBm3yI1q8DYpVw",
          "index" : "whl_test",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "Cannot search on field [age] since it is not indexed."
          }
        }
      }
    ]
  },
  "status" : 400
}

此时age是不能参数搜索和过滤的,但是他可以被返回。

查看索引数据:

GET whl_test/_search
{
  "query": {
   "match_all": {}
  }
}

数据:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "whl_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "age" : 1,
          "name" : "张三",
          "address" : "上海浦东"
        }
      }
    ]
  }
}

可以看到_source中包含age的信息的。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值