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   luzhouxiaoshuai  阅读(514)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示