1.基本概念

1.1._source

存储的原始数据。_source中的内容就是搜索api返回的内容,如:

  1. {
     "query":{
      "term":{
       "title":"test"
      }
     }
    }

结果:

  1. {
  2. "took": ,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": ,
  6. "successful": ,
  7. "skipped": ,
  8. "failed":
  9. },
  10. "hits": {
  11. "total": ,
  12. "max_score": 0.2876821,
  13. "hits": [
  14. {
  15. "_index": "book3",
  16. "_type": "english",
  17. "_id": "3nuFZ2UBYLvVFwGWZHcJ",
  18. "_score": 0.2876821,
  19. "_source": {
  20. "title": "test!"
  21. }
  22. }
  23. ]
  24. }
  25. }

默认情况下,Elasticsearch里面有2份内容,一份是原始文档,也就是_source字段里的内容,我们在Elasticsearch中搜索文档,查看的文档内容就是_source中的内容。另一份是倒排索引,倒排索引中的数据结构是倒排记录表,记录了词项和文档之间的对应关系。

1.2.index:索引

index使用倒排索引存储的是,分析器分析完的词和文档的对应关系。如图:

在搜索排序的时候,查询倒排索引要比快。

那么文档索引到Elasticsearch的时候,默认情况下是对所有字段创建倒排索引的(动态mapping解析出来为数字类型、布尔类型的字段除外),某个字段是否生成倒排索引是由字段的index属性控制的,在Elasticsearch 5之前,index属性的取值有三个:

  1. analyzed:字段被索引,会做分词,可搜索。反过来,如果需要根据某个字段进搜索,index属性就应该设置为analyzed。
  2. not_analyzed:字段值不分词,会被原样写入索引。反过来,如果某些字段需要完全匹配,比如人名、地名,index属性设置为not_analyzed为佳。
  3. no:字段不写入索引,当然也就不能搜索。反过来,有些业务要求某些字段不能被搜索,那么index属性设置为no即可。

ES6.3 index属性支持false和true,false不能搜索相当于no,true可以索引。默认使用standar分词

1.3.store

默认为no,被store标记的fields被存储在和index不同的fragment中,以便于快速检索。虽然store占用磁盘空间,但是减少了计算。store的值可以取yes/no或者true/false,默认值是no或者false。

如果在{"store":yes}的情况下,ES会对该字段单独存储倒排索引,每次根据ID检索的时候,会多走一次IO来从倒排索引取数据。

而如果_source enabled 情况下,ES可以直接根据Client类来解析_source JSON,只需一次IO就将所有字段都检索出来了。

如果需要高亮处理,这里就要说到store属性,store属性用于指定是否将原始字段写入索引,默认取值为no。如果在Lucene中,高亮功能和store属性是否存储息息相关,因为需要根据偏移位置到原始文档中找到关键字才能加上高亮的片段。在Elasticsearch,因为_source中已经存储了一份原始文档,可以根据_source中的原始文档实现高亮,在索引中再存储原始文档就多余了,所以Elasticsearch默认是把store属性设置为no。

注意:如果想要对某个字段实现高亮功能,_source和store至少保留一个。

1.4._all

_all: 在6.0+ 中 , 该字段 默认被禁用,建议使用copy_to。再说_all字段,顾名思义,_all字段里面包含了一个文档里面的所有信息,是一个超级字段。以图中的文档为例,如果开启_all字段,那么title+content会组成一个超级字段,这个字段包含了其他字段的所有内容,空格隔开。当然也可以设置只存储某几个字段到_all属性里面或者排除某些字段。适合一次搜索整个文档。

2.配置

2.1._source配置

_source字段默认是存储的, 什么情况下不用保留_source字段?如果某个字段内容非常多,业务里面只需要能对该字段进行搜索,最后返回文档id,查看文档内容会再次到mysql或者hbase中取数据,把大字段的内容存在Elasticsearch中只会增大索引,这一点文档数量越大结果越明显,如果一条文档节省几KB,放大到亿万级的量结果也是非常可观的。 
如果想要关闭_source字段,在mapping中的设置如下:

  1. {
  2. "yourtype":{
  3. "_source":{
  4. "enabled":false
  5. },
  6. "properties": {
  7. ...
  8. }
  9. }
  10. }

如果只想存储某几个字段的原始值到Elasticsearch,可以通过incudes参数来设置,在mapping中的设置如下:

  1. {
  2. "yourtype":{
  3. "_source":{
  4. "includes":["field1","field2"]
  5. },
  6. "properties": {
  7. ...
  8. }
  9. }
  10. }

同样,可以通过excludes参数排除某些字段:

  1. {
  2. "yourtype":{
  3. "_source":{
  4. "excludes":["field1","field2"]
  5. },
  6. "properties": {
  7. ...
  8. }
  9. }
  10. }

测试,首先创建一个索引:

  1. PUT book2

设置mapping,禁用_source:

  1. POST book2/english/_mapping
  2.  
  3. {
  4. "book2": {
  5. "_source": {
  6. "enabled": false
  7. }
  8. }
  9. }

插入数据:

  1. POST /book2/english/
  2. {
  3. "title":"test!",
  4. "content":"test good Hellow"
  5. }

搜索"test"

  1. POST book2/_search
  2. {
  3. "query":{
  4. "term":{
  5. "title":"test"
  6. }
  7. }
  8. }

结果,只返回了id,没有_suorce,任然可以搜索到。

  1. {
  2. "took": ,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": ,
  6. "successful": ,
  7. "skipped": ,
  8. "failed":
  9. },
  10. "hits": {
  11. "total": ,
  12. "max_score": 0.2876821,
  13. "hits": [
  14. {
  15. "_index": "book2",
  16. "_type": "english",
  17. "_id": "zns1Z2UBYLvVFwGW4Hea",
  18. "_score": 0.2876821
  19. }
  20. ]
  21. }
  22. }

当_source=false,store和index必须有一个为true,原始数据不保存,倒排索引必须要存储,否则去哪里查询呢,验证下:

  1. POST book3/english/_mapping
  2. {
  3. "english":{
  4. "_source": {
  5. "enabled": false
  6. },
  7. "properties": {
  8. "content":{
  9. "type":"text",
  10. "store":"false",
  11. "index":"no"
  12. },
  13. "title":{
  14. "type":"text",
  15. "store":"false",
  16. "index":"no"
  17. }
  18. }
  19. }
  20. }

报错:

  1. {
  2. "error": {
  3. "root_cause": [
  4. {
  5. "type": "illegal_argument_exception",
  6. "reason": "Could not convert [content.index] to boolean"
  7. }
  8. ],
  9. "type": "illegal_argument_exception",
  10. "reason": "Could not convert [content.index] to boolean",
  11. "caused_by": {
  12. "type": "illegal_argument_exception",
  13. "reason": "Failed to parse value [no] as only [true] or [false] are allowed."
  14. }
  15. },
  16. "status":
  17. }

2.2._all配置(copy_to)

  • _all: 在6.0+ 中 , 该字段 默认被禁用,同时在创建index的时候不能 enable;_all 字段能捕获所有字段,它将所有其他字段的值连接成一个大字符串,使用空格作为分隔符,然后 进行分析和索引,但不存储。这意味着它可以被搜索,但不能被检索。 建议使用 copy_to 实现 用户自定义的_all 功能
  1. {
  2. "yourtype": {
  3. "_all": {
  4. "enabled": true
  5. },
  6. "properties": {
  7. ...
  8. }
  9. }
  10. }

也可以通过在字段中指定某个字段是否包含在_all中:

  1. {
  2. "yourtype": {
  3. "properties": {
  4. "field1": {
  5. "type": "string",
  6. "include_in_all": false
  7. },
  8. "field2": {
  9. "type": "string",
  10. "include_in_all": true
  11. }
  12. }
  13. }
  14. }

如果要把字段原始值保存,要设置store属性为true,这样索引会更大,需要根据需求使用。下面给出测试代码。 
创建test索引:

  1. DELETE book2
  2.  
  3. PUT book2

copy_to语法:

  1. POST book2/english/_mapping
  2. {
  3. "english":{
  4.  
  5. "properties": {
  6. "content":{
  7. "type":"text",
  8. "copy_to":"all_text"
  9. },
  10. "title":{
  11. "type":"text",
  12. "copy_to":"all_text"
  13. },
  14. "all_text":{
  15. "type":"text"
  16. }
  17. }
  18. }
  19. }

插入数据:

  1. POST /book2/english/
  2. {
  3. "title":"test!",
  4. "content":"test good Hellow"
  5. }

查询:

  1. POST book2/_search{
        "query":{
            "term":{
                "all_text":"test"
            }
        }
    }

结果:

  1. {
  2. "took": ,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": ,
  6. "successful": ,
  7. "skipped": ,
  8. "failed":
  9. },
  10. "hits": {
  11. "total": ,
  12. "max_score": 0.39556286,
  13. "hits": [
  14. {
  15. "_index": "book2",
  16. "_type": "english",
  17. "_id": "0HtjZ2UBYLvVFwGWl3f7",
  18. "_score": 0.39556286,
  19. "_source": {
  20. "title": "test!god",
  21. "content": "test good Hellow"
  22. }
  23. },
  24. {
  25. "_index": "book2",
  26. "_type": "english",
  27. "_id": "z3tjZ2UBYLvVFwGWWXd3",
  28. "_score": 0.39556286,
  29. "_source": {
  30. "title": "test!",
  31. "content": "test good Hellow"
  32. }
  33. }
  34. ]
  35. }
  36. }

2.3.index和score配置

"index":false设置不可搜索

  1. POST book3/english/_mapping
  2. {
  3. "english":{
  4. "_source": {
  5. "enabled": false
  6. },
  7. "properties": {
  8. "content":{
  9. "type":"text",
  10. "store":true,
  11. "index":false
  12. },
  13. "title":{
  14. "type":"text",
  15. "store":true,
  16. "index":false
  17. }
  18. }
  19. }
  20. }

查询:

  1. POST book3/_search
  2. {
  3. "query":{
  4. "term":{
  5. "content":"test"
  6. }
  7. }
  8. }
  9.  
  10. 结果:
  11. {
  12. "error": {
  13. "root_cause": [
  14. {
  15. "type": "query_shard_exception",
  16. "reason": "failed to create query: {\n \"term\" : {\n \"content\" : {\n \"value\" : \"test\",\n \"boost\" : 1.0\n }\n }\n}",
  17. "index_uuid": "FvNPHNb7Sa6H757_lKRhpg",
  18. "index": "book3"
  19. }
  20. ],
  21. "type": "search_phase_execution_exception",
  22. "reason": "all shards failed",
  23. "phase": "query",
  24. "grouped": true,
  25. "failed_shards": [
  26. {
  27. "shard": ,
  28. "index": "book3",
  29. "node": "R8t6R20XQritJB_5QVQsvg",
  30. "reason": {
  31. "type": "query_shard_exception",
  32. "reason": "failed to create query: {\n \"term\" : {\n \"content\" : {\n \"value\" : \"test\",\n \"boost\" : 1.0\n }\n }\n}",
  33. "index_uuid": "FvNPHNb7Sa6H757_lKRhpg",
  34. "index": "book3",
  35. "caused_by": {
  36. "type": "illegal_argument_exception",
  37. "reason": "Cannot search on field [content] since it is not indexed."
  38. }
  39. }
  40. }
  41. ]
  42. },
  43. "status":
  44. }

正确配置:

  1. POST book3/english/_mapping
  2. {
  3. "english":{
  4. "_source": {
  5. "enabled": false
  6. },
  7. "properties": {
  8. "content":{
  9. "type":"text",
  10. "store":true,
  11. "index":"true"
  12. },
  13. "title":{
  14. "type":"text",
  15. "store":true,
  16. "index":"true"
  17. }
  18. }
  19. }
  20. }

高亮:

  1. {
  2. "query":{
  3. "term":{
  4. "title":"test"
  5. }
  6. },
  7. "highlight":{
  8. "fields":{
  9. "title":{}
  10. }
  11. }
  12. }

结果:

  1. {
  2. "took": ,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": ,
  6. "successful": ,
  7. "skipped": ,
  8. "failed":
  9. },
  10. "hits": {
  11. "total": ,
  12. "max_score": 0.2876821,
  13. "hits": [
  14. {
  15. "_index": "book3",
  16. "_type": "english",
  17. "_id": "2nt_Z2UBYLvVFwGWfXcn",
  18. "_score": 0.2876821,
  19. "highlight": {
  20. "title": [
  21. "<em>test</em>!"
  22. ]
  23. }
  24. }
  25. ]
  26. }
  27. }

ES系列十一、ES的index、store、_source、copy_to和all的区别的更多相关文章

  1. ES系列目录

    ES系列一.CentOS7安装ES 6.3.1 ES系列二.CentOS7安装ES head6.3.1 ES系列三.基本知识准备 ES系列四.ES6.3常用api之文档类api ES系列五.ES6.3 ...

  2. 十一、.net core(.NET 6)搭建ElasticSearch(ES)系列之ElasticSearch、head-master、Kibana环境搭建

    搭建ElasticSearch+Kibana环境 前提条件:已经配置好JDK环境以及Nodejs环境.如果还未配置,请查看我的上一篇博客内容,有详细配置教程. 先下载ElasticSearch(以下文 ...

  3. ES系列十六、集群配置和维护管理

    一.修改配置文件 1.节点配置 1.vim elasticsearch.yml # ======================== Elasticsearch Configuration ===== ...

  4. ES系列四、ES6.3常用api之文档类api

    1.Index API: 创建并建立索引 PUT twitter/tweet/ { "user" : "kimchy", "post_date&quo ...

  5. ES系列十七、logback+ELK日志搭建

    一.ELK应用场景 在复杂的企业应用服务群中,记录日志方式多种多样,并且不易归档以及提供日志监控的机制.无论是开发人员还是运维人员都无法准确的定位服务.服务器上面出现的种种问题,也没有高效搜索日志内容 ...

  6. OpenGL ES 系列教程

    http://www.linuxgraphics.cn/graphics/opengles_tutorial_index.html 本文收集了一套 OpenGL ES 系列教程. www.play3d ...

  7. [ES]Python查询ES导出数据为Excel

    版本 elasticsearch==5.5.0 python==3.7 说明 用python查询es上存储的状态数据,将查询到的数据用pandas处理成excel code # -*- coding: ...

  8. struts2官方 中文教程 系列十一:使用XML进行表单验证

    在本教程中,我们将讨论如何使用Struts 2的XML验证方法来验证表单字段中用户的输入.在前面的教程中,我们讨论了在Action类中使用validate方法验证用户的输入.使用单独的XML验证文件让 ...

  9. ES scroll(ES游标) 解决深分页

    ES scroll(ES游标) 解决深分页. Why 当Elasticsearch响应请求时,它必须确定docs的顺序,排列响应结果.如果请求的页数较少(假设每页20个docs), Elasticse ...

随机推荐

  1. Helm使用详解

    使用1.helm search 查看charts stable是官方的 local是自己的 2.查看repo helm repo list 3.安装 helm install stable/mysql ...

  2. JDK源码分析(4)HashSet

    JDK版本 HashSet简介 HashSet特点 非线程安全 允许null值 添加值得时候会先获取对象的hashCode方法,如果hashCode 方法返回的值一致,则再调用equals方法判断是否 ...

  3. intent.setFlags方法中的参数值含义

     一. intent.setFlags()方法中的参数值含义: 1.FLAG_ACTIVITY_CLEAR_TOP:例如现在的栈情况为:A B C D .D此时通过intent跳转到B,如果这个int ...

  4. 【51Nod1405】树上距离和 二次扫描与换根法

    题目大意:给定一棵 N 个点的边权均为 1 的树,依次输出每个点到其他各个点的距离和. 题解:首先任意选定一个节点为根节点,比如 1,第一遍 dfs 遍历树求出子树大小.树上前缀和.第二遍 dfs 遍 ...

  5. ST表与树状数组

    ST表  st表可以解决区间最值的问题.可以做到O(nlogn)预处理 ,O(1)查询,但是不支持修改. st表的大概思路就是用st[i][j]来表示从i开始的2的j次方个树中的最值,查询时就从左端点 ...

  6. (转)Java程序员的面试经历和题库

    背景:最近我在找工作,前期就像打了鸡血的一样,隔一段时间没有面试,就又松懈了下来,看到别人写的面经,感觉就像打脸一般,以后要多多总结前人的经验,时刻保持压力状态才是. 作者:nuaazhaofeng2 ...

  7. (转)在Eclipse中创建Maven多模块工程

    背景:以前只总结了怎么在命令行下创建maven的多模块项目,在eclipse下怎么创建不是很清楚.最近需要在git的资源库中上传多模块项目,方便后期的维护,所以将网上的资料进行整理. 原文链接:htt ...

  8. 12: MyBatis之传入参数parameterType

    源链接地址:http://blog.csdn.net/liaoxiaohua1981/article/details/6862764

  9. 通过url传递参数如果汉字乱码采用的方法

    urlCodeDeal 方法把汉字编码, 在Jsp界面通过Escape.unescape方法,将编码反编译成汉字. 下面是urlCodeDeal方法: //UrlCode 处理代码 function ...

  10. eclipse中中文注释乱码怎么解决

    作项目一般都是用UTF-8编码的,eclipse的默认编码是GBK,你在菜单栏Window里,选Preferences选项,第一项General里的Workspace,选定后右面有个Text file ...