前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一步一步了解索引存储,inverted index,doc_value,store,_source

一步一步了解索引存储,inverted index,doc_value,store,_source

原创
作者头像
周银辉
修改2024-07-24 15:40:30
1730
修改2024-07-24 15:40:30
举报
文章被收录于专栏:ElasticSearch

问题一、如何验证:字段索引时,到底存了哪些数据

  1. text类型存储:默认存储到inverted index,_source
  2. 非text类型存储:默认存储到inverted index,_source,doc_value

如何验证存储了这些数据呢?我们可以通过一些参数,打开和关闭这些选项,并通过查询数据来验证

以下实例,我们都通过一个text类型和一个keyword类型来验证以上两个内容

1、enabled:false

1、text类型存储:不存储到inverted index,doc_value,存储到_source。

2、keyword类型存储:不存储i到nverted index,doc_value,存储到_source,

我们通过搜索字段内容来验证inverted index,通过脚本访问doc对象来验证doc_store,通过访问params或ctx来验证_source

代码语言:txt
复制
DELETE message
##1、建立mapping
PUT message
{
  "mappings": {
    "enabled": false,
    "properties": {
      "msg1": {
        "type": "text"
      },
      "msg2": {
        "type": "keyword"
      }
    }
  }
}

GET message/_doc/1
GET message/_mapping
##2、插入多条数据
PUT message/_doc/1
{
  "msg1":"学习好",
  "msg2":"大家好"
}

PUT message/_doc/2
{
  "msg1":"学习好",
  "msg2":"身体好"
}

PUT message/_doc/3
{
  "msg1":"学习好",
  "msg2":"你好"
}

POST message/_refresh
GET message/_doc/1
##3、无法通过索引查询,因为没有建立倒排索引
GET message/_search
{
  "query": {
    "match": {
      "msg1": "学习"
    }
  }
}

GET message/_search
{
  "query": {
    "match": {
      "msg2": "好"
    }
  }
}

##4、无法分组统计,因为不存储doc_value
GET message/_search
{
  "size": 0, 
  "aggs": {
    "msg2_group": {
      "terms": {
        "field": "msg2"
      }
    }
  }
}
##5、无法从doc_value中读取数据,因为不存储doc_value
GET message/_search
{
 "query": {
   "script_score": {
     "script": {
       "lang": "painless",
       "source": "doc['msg1'].value.length()"
     },
     "query": {
       "match_all": {}
     }
   }
 }
}

GET message/_search
{
 "query": {
   "script_score": {
     "script": {
       "lang": "painless",
       "source": "doc['msg2'].value.length()"
     },
     "query": {
       "match_all": {}
     }
   }
 }
}
##6、可以读取数据,这个数据从_source中读取
GET message/_search
{
 "query": {
   "script_score": {
     "script": {
       "lang": "painless",
       "source": "params['_source']['msg1'].length()"
     },
     "query": {
       "match_all": {}
     }
   }
 }
}




GET message/_search
{
 "query": {
   "script_score": {
     "script": {
       "lang": "painless",
       "source": "params['_source']['msg2'].length()"
     },
     "query": {
       "match_all": {}
     }
   }
 }
}

如果我只想对单个字段不存储倒索引可以吗?也是可以的,如下:

代码语言:txt
复制
PUT message
{
  "mappings": {
    "properties": {
      "msg1": {
        "enabled": false
      },
      "msg2": {
        "enabled": false
      }
    }
  }
}

2、index:false

这个选项的作用是,不建立倒排索引,但是存储doc_value,_source选项,同样通过上面的脚本可以进行测试

代码语言:txt
复制
PUT message
{
  "mappings": {
    "properties": {
      "msg1": {
        "type": "text"
      },
      "msg2": {
        "type": "keyword",
        "index":false
      }
    }
  }
}

问题2:如果我要实现对text类型分组如何操作呢

代码语言:txt
复制
# 在text类型启用fielddata:true即可,但是这将导致堆内存增加
PUT message
{
  "mappings": {
    "properties": {
      "msg1": {
        "type":"text",
       "fielddata":true
      },
      "msg2": {
        "enabled": false
      }
    }
  }
}

问题3:还需要store的内容干什么?

已经有了倒排索引,也有正排索引doc_value用于排序和分组,还有原始数据_source,那还需要store的内容干什么?

代码语言:txt
复制
#其中的一个场景就是,单个文档内容很多,比如有title标题,description描述,还有body内容,
平时只想通过title和description查询和展示,不需要整个_source的内容,如何解决这个问题。
这个时候可以禁用_source,通过单独存储每个字段的内容store属性,通过以下脚本测试
PUT message
{
  "mappings": {
    "_source": {
      "enabled": false
    },
    "properties": {
      "msg1": {
        "type": "text",
        "store": true
      },
      "msg2": {
        "type":"keyword", 
        "store": true
      }
    }
  }
}


PUT message/_doc/1
{
  "msg1":"学习好",
  "msg2":"大家好"
}

PUT message/_doc/2
{
  "msg1":"学习好",
  "msg2":"身体好"
}

PUT message/_doc/3
{
  "msg1":"学习好",
  "msg2":"你好"
}

POST message/_refresh
GET message/_doc/1

GET message/_search
{
  "query": {
    "match": {
      "msg1": "学习"
    }
  }
}

GET message/_search
{
  "query": {
    "match": {
      "msg2": "好"
    }
  }
}

GET message/_search
{
  "stored_fields": ["msg1","msg2"], 
  "query": {
    
    "match": {
      "msg1": "学习"
    }
  }
}

结论:

1、数据存储在哪里,如何通过实验来验证。

2、每个存储参数选项的作用是要解决什么问题,需要理清楚。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题一、如何验证:字段索引时,到底存了哪些数据
    • 1、enabled:false
      • 2、index:false
      • 问题2:如果我要实现对text类型分组如何操作呢
      • 问题3:还需要store的内容干什么?
      相关产品与服务
      Elasticsearch Service
      腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档