ElasticSearch doc_values、index以及_source测试

https://www.jianshu.com/p/33cb5c9a6903

测试index、doc_values以及_source的作用
template

{
  "order": 0,
  "index_patterns": [
    "baiyxtest*"
  ],
  "settings": {
    "index": {
      "number_of_shards": "2",
      "number_of_replicas": "1"
    }
  },
  "mappings": {
    "_source": {
      "enabled": false
    },
    "properties": {
      "aggable": {
        "index": "false",
        "type": "keyword",
        "doc_values": true
      },
      "value": {
        "type": "long",
        "doc_values": false
      },
      "searchable": {
        "type": "keyword",
        "doc_values": false
      }
    }
  },
  "aliases": {}
}
1、测试index为false的字段作为检索条件
{
  "query": {
    "match": {
      "aggable": "aaa"
    }
  }
}

字段如果设置为index 如果设置为false,那么外部就不能使用query或者trem进行查询了,如果进行查询就会报错

"reason": "Cannot search on field [aggable] since it is not indexed."

2、测试index为true的字段作为检索条件
{
  "query": {
    "match": {
      "searchable": "bbb"
    }
  }
}

可以查得出来,但是设置了"_source":"false",所以看不到具体文档,"_source"字段存储了各个字段value组成的一个字符串,不清楚网上去搜索,query或者term查询的时候默认查询的就是"_source"的值。

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 2,
        "successful": 2,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [{
            "_index": "baiyxtest",
            "_type": "_doc",
            "_id": "1gno53ABNKWqmzmcZdA_",
            "_score": 0.2876821
        }]
    }
}
3、测试doc_values为false的字段做聚合维度字段

doc_values主要是用来做数据的聚合、排序等操作的

{
    "query": {
        "match": {
            "searchable": "bbb"
        }
    },
    "aggregations": {
        "group_by_aggable": {
            "terms": {
                "field": "searchable"
            },
            "aggregations": {
                "value": {
                    "sum": {
                        "field": "value"
                    }
                }
            }
        }
    }
}

报错

"reason": "Can't load fielddata on [value] because fielddata is unsupported on fields of type [long]. Use doc values instead."
5、修改mapping,去掉value字段的doc_values(默认值true)
{
  "order": 0,
  "index_patterns": [
    "baiyxtest*"
  ],
  "settings": {
    "index": {
      "number_of_shards": "2",
      "number_of_replicas": "1"
    }
  },
  "mappings": {
    "_source": {
      "enabled": false
    },
    "properties": {
      "aggable": {
        "index": "false",
        "type": "keyword",
        "doc_values": true
      },
      "value": {
        "type": "long"
      },
      "searchable": {
        "type": "keyword",
        "doc_values": false
      }
    }
  },
  "aliases": {}
}

 

posted on 2020-10-10 09:23  luzhouxiaoshuai  阅读(500)  评论(0编辑  收藏  举报

导航