美文网首页
ElasticSearch doc_values、index以及

ElasticSearch doc_values、index以及

作者: 白奕新 | 来源:发表于2020-03-17 23:23 被阅读0次

测试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"
    }
  }
}

会报错

"reason": "Cannot search on field [aggable] since it is not indexed."
2、测试index为true的字段作为检索条件
{
  "query": {
    "match": {
      "searchable": "bbb"
    }
  }
}

可以查得出来,但是设置了"_source":"false",所以看不到具体文档。

{
    "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的字段做聚合维度字段
{
    "query": {
        "match": {
            "searchable": "bbb"
        }
    },
    "aggregations": {
        "group_by_aggable": {
            "terms": {
                "field": "searchable"
            },
            "aggregations": {
                "value": {
                    "sum": {
                        "field": "value"
                    }
                }
            }
        }
    }
}

报错

"reason": "Can't load fielddata on [searchable] because fielddata is unsupported on fields of type [keyword]. Use doc values instead."
4、测试doc_values为false的字段做聚合数值字段
{
    "query": {
        "match": {
            "searchable": "bbb"
        }
    },
    "aggregations": {
        "group_by_aggable": {
            "terms": {
                "field": "aggable"
            },
            "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": {}
}

正常,返回aggable的值,且可以返回aggrable的值,说明正排索引跟_source控制的存储不一样。

{
    "took": 5,
    "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": "-bD453ABNKWqmzmc4zq2",
            "_score": 0.2876821
        }]
    },
    "aggregations": {
        "group_by_aggable": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [{
                "key": "aaa",
                "doc_count": 1,
                "value": {
                    "value": 123
                }
            }]
        }
    }
}

相关文章

网友评论

      本文标题:ElasticSearch doc_values、index以及

      本文链接:https://www.haomeiwen.com/subject/kzlfyhtx.html