analyzer

分析器,只有text类型的字段才需要配置。在生产环境中,最好先测试分析效果。
一个分析器(analyzer)由分词器(tokenizer)和过滤器(filter)组成

  1. PUT my-index-000001
  2. {
  3. "settings":{
  4. "analysis":{
  5. "analyzer":{
  6. //分析器完整定义
  7. "my_analyzer":{
  8. "type":"custom",
  9. "tokenizer":"standard", //分词器
  10. "filter":[ //过滤器
  11. "lowercase"
  12. ]
  13. },
  14. "my_stop_analyzer":{
  15. "type":"custom",
  16. "tokenizer":"standard",
  17. "filter":[
  18. "lowercase",
  19. "english_stop" //过滤掉a, an, to, on等停止词
  20. ]
  21. }
  22. },
  23. "filter":{
  24. "english_stop":{
  25. "type":"stop",
  26. "stopwords":"_english_"
  27. }
  28. }
  29. }
  30. },
  31. "mappings":{
  32. "properties":{
  33. "title": {
  34. "type":"text",
  35. "analyzer":"my_analyzer", //参数
  36. "search_analyzer":"my_stop_analyzer",
  37. "search_quote_analyzer":"my_analyzer"
  38. }
  39. }
  40. }
  41. }
  42. PUT my-index-000001/_doc/1
  43. {
  44. "title":"The Quick Brown Fox"
  45. }
  46. PUT my-index-000001/_doc/2
  47. {
  48. "title":"A Quick Brown Fox"
  49. }
  50. GET my-index-000001/_search
  51. {
  52. "query":{
  53. "query_string":{
  54. "query":"\"the quick brown fox\""
  55. }
  56. }
  57. }

这里需要特别介绍一下search_quote_analyzer。我们进行短语查询时,一般情况下不希望停止词被忽略掉,比如要查询“I have a dog”,如果不希望a被省略,就想查“I have a dog”,就需要用到search_quote_analyzer,可以像上面的例子一样设置为my_analyzer(不过滤停止词)。

"mappings":{
       "properties":{
          "title": {
             "type":"text",
             "analyzer":"my_analyzer",      //参数
             "search_analyzer":"my_stop_analyzer", 
             "search_quote_analyzer":"my_analyzer" 
         }
      }
   }

analyzer:该字段被索引时使用analyzer配置的分析器
search_analyze**r**:该字段被search时,对search查询条件使用的分析器。注意如果不指定searchanalyzer,那么默认使用analyzer的配置
search_quote_analyzer_: 对该字段进行phrase query时对查询条件使用的分析器

GET my-index-000001/_search
{
   "query":{
      "query_string":{
      //下面的查询条件\"the quick brown fox\",es检测到the quick brown fox是用引号包起来的,就会认为这是一个短语查询phrase query,会将the quick brown fox当做一个整体。如果想精确匹配the quick brown fox,不想停止词the被忽略,这时候需要指定text字段的search_quote_analyzer不能忽略停止词
         "query":"\"the quick brown fox\"" 
      }
   }
}

boost

如果希望某个字段进行查询时计算相关度得分占用的权重更高一点,可以使用boost参数进行调整。boost不指定的情况下,默认是1.0,boost可以是浮点数

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        //title字段在计算相关度得分时占得权重是content字段的2倍
        "boost": 2 
      },
      "content": {
        "type": "text"
      }
    }
  }
}

boost只适用于term query(prefix,range,fuzzy查询不生效)

也可以在查询时指定boost,下面两个查询的效果是一样的

POST _search
{
  "query": {
    "match": {
      "title": {
        "query": "quick brown fox"
      }
    }
  }
}



POST _search
{
  "query": {
    "match": {
      "title": {
        "query": "quick brown fox",
        "boost": 2    //其实mapping中可以不设置boost,直接在查询里设置
      }
    }
  }
}

注意,在5.0.0以前boost会在doc被index时生效。但是5.0.0以后只在query时才会生效。

coerce

是否将输入文档的特定字段内容转为目标类型,类似类型强转。

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "number_one": {
        "type": "integer"
      },
      "number_two": {
        "type": "integer",
        //coerce为false关闭强转,任何number_two字段非int型值的文档将会在index时被拒绝。
        "coerce": false
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "number_one": "10" 
}

PUT my-index-000001/_doc/2
{
  "number_two": "10" 
}

number_one默认coerce为开,字符串和浮点数类型的输入将会被强转为integer.
number_two字段coerce为false关闭强转,任何number_two字段非int型值的文档将会在index时被拒绝。

coerce配置可以动态更新,默认为true
index.mapping.coerce设置可以在索引级别关闭强转功能。

copy_to

copy_to允许我们将多个字段临时合并到一个新的字段进行查询。只需要在需要被合并的字段上加上参数copy_to。
如果经常需要进行多字段查询,可以使用copy_to属性减少需要查询的字段数量来提高查询效率。

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "first_name": {
        "type": "text",
        "copy_to": "full_name" 
      },
      "last_name": {
        "type": "text",
        "copy_to": "full_name" 
      },
      "full_name": {
        "type": "text"
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "first_name": "John",
  "last_name": "Smith"
}

GET my-index-000001/_search
{
  "query": {
    "match": {
      "full_name": { 
        "query": "John Smith",
        "operator": "and"
      }
    }
  }
}

需要注意:

  1. 被copy的字段仅仅是以拷贝整个值为形式的拷贝,而不是拷贝经过分词后的所有terms。
  2. _souce字段不会包含合并后的字段
  3. 同一个字段的值可以copy to到多个字段
  4. 不能递归copy_to,copy_to的目标字段不能作为copy_to的源字段。

doc_values

大多数类型的字段都会被建立起索引,这样就可以查询到。倒排索引可以在一个有序的term列表上快速找到想要查找的term,并且立即获取到相关联的doc列表。

但是通过scripts去排序、聚合、获取字段的值时需要我们可以查找doc文档内容并且找出具体某个字段中包含的terms。倒排索引不能做到这一点,但可以通过Doc values实现。Doc values是存在磁盘上的一种数据结构,在被index时创建。doc values存储了与文档_source字段一样的内容,但是是以列式存储的方式store的。列式存储的方式决定了利用doc values对字段进行排序和聚合更加高效率。大多数field type支持doc values功能并且默认都是打开的,但有一些特例比如text和annotated text。
如果确定不用对某个字段进行排序、聚合、或者在script中获取值,那么尽量吧doc_values置为false,这样做可以节约磁盘空间。



dynamic

dynamic控制文档中如果出现了新的字段是否会被动态更新到mapping中。默认开

PUT my-index-000001
{
  "mappings": {
    "dynamic": false, 
    "properties": {
      "user": { 
        "properties": {
          "name": {
            "type": "text"
          },
          "social_networks": { 
            "dynamic": true,
            "properties": {}
          }
        }
      }

dynamic取值有三个:

  1. true:任何新出现的字段都会动态更新到mapping中
  2. false: 不会被更新到mapping中也不会被index,所以无法被query,但是在_source原文中被保留
  3. strict: 不会被动态添加到mapping并且如果包含了mappping中没有出现的field会被拒绝进入index.

eger_global_ordinals (热切全局序号)

To support aggregations and other operations that require looking up field values on a per-document basis, Elasticsearch uses a data structure called doc values.
term-based的字段类型(如keyword)使用序号ordinal映射表存储其doc值,以实现更紧凑的表示。每一个term都会分配一个自增的序号或者按照字典序分配序号。doc values中只存储对应term的序号。相当于doc values时在列式存储时,一个列代表一个field,每个列都有一个列表,里面存储了所有文档中该列出现过的值term,但实际上es内部将每一个term都映射到一个序号,doc values中只存储序号就行了。

在聚合时,使用序号会显著提升效率。

如之前提到的index分布在不同的primary shard上,每个shard上又分为若干个segment。每个segment都有自己的专属ordinal->term映射表。但是在shard级别上进行query(比如aggregation)时,需要获取所有的segment的序号映射表,因此对于整个shard来说,需要一个全局序号映射表。这个全局映射表的工作方式类似于OS中的二级页表,先通过全局映射表找到对应segment,再去segment的序号映射表中查找。

Elasticsearch包含多个断路器,用于防止操作导致OutOfMemoryError。每个断路器都指定了它可以使用多少内存的限制。此外,还有一个父级断路器,它指定了可以跨所有断路器使用的内存总量。global ordinal mapping是存在堆内存中的,在管理全局序号映射表的内存时,es将全局映射表消耗的内存算在fielddata上,因此全局映射表内存包含在fielddata断路器中(circuit breaker)。

默认情况下全局映射表的创建以及加载进内存是在查询时第一次需要用到全局序号的时候。为了提高search性能,你可以使用eger_global_ordinals参数来提前加载映射表

PUT my-index-000001/_mapping
{
  "properties": {
    "tags": {
      "type": "keyword",
      "eager_global_ordinals": true
    }
  }
}

当eger_global_ordinals打开时,全部映射表在shard被刷新时被创建,es总是在index被真正改变之前加载全局序号表。因此建立全局序号表的代价被转移到了index-time。

eger_global_ordinals可以通过mapping api动态更新。
eger_global_ordinals不应使用在frozen状态的index上。

通常,全局序数在加载时间和内存使用方面不会带来很大的开销。但是,对于拥有数据量比较大的shard的索引,或者如果字段包含大量不同的term,则加载全局序数可能会很昂贵。因为全局序数为shard上的所有segment提供了统一的映射,所以当新segment可见时,还需要完全重建它们。

对于某些只有一个segment的shard,这个segment的序号表其实就是全局映射表,可以吧eger_global_ordinals关掉。

enabled

enabled的作用是关掉index功能,只做存储。不index也就无法被search。enabled只可以配置到top-level mapping配置和object类型的字段上。

fielddata

大多数类型的field默认会被倒排索引,这样使得文档可以通过关键词被检索到。但是排序、聚合、access field data需要另一种不同的查询方式。

一个普通的文档查找(检索)行为需要解决“那些文档包含了关键词”。而排序、聚合、access field data需要解决的是“一个文档的某个特定字段的值是什么”,这样才能根据每个文档相应字段上的值去对文档进行排序、聚合。大多数类型的字段可以通过doc values解决“一个文档的某个特定字段的值是什么”。

但是text字段不支持doc values,但是text字段可以使用fielddata:一种在查询时创建存储在内存中的数据结构,来实现类似doc_values 的功能,使得text字段可以被sort and aggregation。
fielddata是在一个字段第一次有聚合、排序需求时创建的。创建fielddata时会去遍历一个index的整个倒排索引,反向获取document->term的关系并存储在内存中。
text的fielddata参数默认是关闭的。fielddata会消耗大量的堆内存。加载fielddata也是很昂贵的操作,会导致用户查询有很大延迟。

因此一般情况下请不要用fielddata来实现text字段的聚合功能,如有需要可以使用下面field的方式。

一般来讲对一个text类型的字段来说对它进行排序集合时没有意义的。因为text字段会被分词,比如字段值为“new york”,会被分词为两个term:”new”和”york”。对这个字段进行聚合会返回一个new桶和york桶,但是我们通常希望将“new york”当做一个整体来聚合。可以使用下面的方式实现:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "my_field": { 
        "type": "text",
        "fields": {
          "keyword": { 
            "type": "keyword"
          }
        }
      }
    }
  }
}

用my_field来做搜索文档的操作。
用my_field.keyword来做聚合。

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "tag": {
        "type": "text",
        "fielddata": true,
        "fielddata_frequency_filter": {
          "min": 0.001,
          "max": 0.1,
          "min_segment_size": 500
        }
      }
    }
  }
}

fielddata_frequency_filter可以用来减少被加载进内存的term数量。

format

用来定义时间字符串格式,不多讲,用到就去查

ignore_above

超过ignore_above指定的长度的strings不会被index(加入倒排索引)和store(以doc values方式存储)。如果字段是string数组类型,那么这个限制会应用到每个数组成员上,超过长度的数组成员不会被index和store。但是在文档返回时,在_source字段内还会展示。

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "message": {
        "type": "keyword",
        "ignore_above": 20 
      }
    }
  }
}

ignore_malformed

字段传进来的值与mapping定义的类型不符合时,可以使用ignore_malformed=true来忽略exception,这样有问题的字段不会被index,其他字段能够正常被index。

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "number_one": {
        "type": "integer",
        "ignore_malformed": true
      },
      "number_two": {
        "type": "integer"
      }
    }
  }
}

以下mapping type可以设置ignore_malformed参数

  1. numeric
  2. date
  3. date_nanoseconds
  4. geo-point
  5. geo-shape
  6. Ip

index

控制是否field被index,默认为true

index_options

这个参数控制什么信息被加入到倒排索引以便search和highlight, 这个选项是专门为了text而添加的,避免在其他类型的field上配置此参数。

index_options有以下集中取值:

  • docs:

只以term的全局映射序号即doc num进行index, 可以回答“这个field是否包含指定的term”.

  • freqs

doc num和词频会被加入索引,词频在评分时用于提高重复出现的term的权重。

  • positions

doc num,词频,以及termd在值中的位置会被加入索引,positions用于近似查询和phase短语查询。

  • offsets

doc num, freqs, positions, 以及term在值中的第一个字符和最后一个字符的偏移都会被加入index。offsets被unified highlighter来加速高亮操作的速度。

index_options的默认取值是positions.

注意,position和offset的区别,比如一个text的field的值为“i have my feet washed”,其中have这个term的position是2,offset是(2,5)。

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "index_options": "offsets"
      }
    }
  }
}

index_phrases

如果启用这个配置,组合词类似 one-two-three 将被索引到一个单独的字段中。这使得精确短语查询phrase query可以以更大的索引为代价更高效地运行。请注意,当不删除stop word时,这种方法效果最好,因为包含stop word的phrase将不使用辅助字段,并退化到标准短语查询。默认为false。

index_prefixes

index_prefixes 使得term prefixes会被索引用来加速prefix search.

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "full_name": {
        "type": "text",
        //index_prefixes默认不开启,如果要开启直接加上index_prefixes参数即可
        "index_prefixes": {
        //可以不指定min_chars和max_chars
          "min_chars" : 1,    //最小前缀长度,必须大于0,默认为2
          "max_chars" : 10    //最大前缀长度,必须小于20,默认为5
        }
      }
    }
  }
}

meta

附加到field上的metadata,对于es来说不可见,没什么卵用。

fields

field参数经常被用来使一个字段可以用作不同的查询,相当于multi-fields。举个例子,一个string类型的field可以映射为一个text字段以便用于全文搜索,也可以映射为一个keyword字段用于sort和aggregation.

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "text": { 
        "type": "text",
        "fields": {
          "english": { 
            "type":     "text",
            "analyzer": "english"  //text字段使用standard分词器, text.english使用english分词器
          }
        }
      }
    }
  }
}

PUT my-index-000001/_doc/1
{ "text": "quick brown fox" } 

PUT my-index-000001/_doc/2
{ "text": "quick brown foxes" } 

GET my-index-000001/_search
{
  "query": {
    "multi_match": {
      "query": "quick brown foxes",
      "fields": [ 
        "text",
        "text.english"  
      ],
      "type": "most_fields"   //算分时text和text.english一起算分
    }
  }
}

normalizer

normalizer参数是keyword专属属性,作用类似于analyzer,但是normalizer分析结果只会产生一个token(keyword本来就是一个整体,不分词)

normalizer是在keyword被index之前起作用,同样的在使用match查询或者term查询时,normalizer也会先对查询条件进行处理,然后再实施查询。

PUT index
{
  "settings": {
    "analysis": {
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": ["lowercase", "asciifolding"]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "foo": {
        "type": "keyword",
        "normalizer": "my_normalizer"
      }
    }
  }
}


//在_source返回结果中BAR不变。
//文档1进入index之前BAR经过normalizer变成了bar,然后bar被传入analyzer进行分词。因此在聚合的返回结果中桶的名字只有bar了。
PUT index/_doc/1
{
  "foo": "BÀR"
}

PUT index/_doc/2
{
  "foo": "bar"
}

PUT index/_doc/3
{
  "foo": "baz"
}

POST index/_refresh

GET index/_search
{
  "query": {
    "term": {
      "foo": "BAR"
    }
  }
}

GET index/_search
{
  "query": {
    "match": {
      "foo": "BAR"
    }
  }
}

norms

norms 存储各种规范化因子,稍后在查询时使用,以便计算文档相对于查询的评分。

尽管对评分很有用,但norms也需要相当多的磁盘(通常是索引中每个字段的每个文档占用一个字节,即使对于没有这个特定字段的文档也是如此)。因此,如果你不需要在某个特定字段得分,你应该禁用该field的norms。特别是,仅用于筛选或聚合的字段。

PUT my-index-000001/_mapping
{
  "properties": {
    "title": {
      "type": "text",
      "norms": false
    }
  }
}

null_value

空值没法被索引或者查询,如果一个字段被设置为null,那么就会跟字段没有值一样处理。
null_value参数可以用指定的值替换显式的null值,使得空值也可以索引和查询。

null_value参数只是会影响数据怎么被index,而不会修改_source文档

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "status_code": {
        "type":       "keyword",
        "null_value": "NULL" 
      }
    }
  }
}


//显式的null会被替换为NULL
PUT my-index-000001/_doc/1
{
  "status_code": null
}

//不会替换,status_code是一个array,并不是null
PUT my-index-000001/_doc/2
{
  "status_code": [] 
}


//此查询会返回doc 1,doc不会返回
GET my-index-000001/_search
{
  "query": {
    "term": {
      "status_code": "NULL" 
    }
  }
}

position_increment_gap

这个是为了text字段准备的,position_increment_gap的作用是为了防止大多数查询在text数组中跨数据元素进行查询。当一个text数组被index时,es会插入一个假的gap在text数组元素的分词结果之间。position_increment_gap默认gap为100

gap的精准定义是:下一个array元素分词结果的第一个term,距离上一个array元素分词结果的最后一个term的距离。

但是有的时候,对于一个实际值为text array的field,我们希望优化近似查询和短语查询的效果。比如下面这个例子:

PUT my-index-000001/_doc/1
{
  "names": [ "John Abraham", "Lincoln Smith"]
}

//这个查询不会匹配到文档1
//因为Abraham 和 Lincoln这两个term属于不同的两个数组元素,它们之间的实际距离为100
//match_phrase短语查询要求精确匹配"Abraham Lincoln"这个短语,也就是说要求Abraham和 Lincoln的position之差为1.显然不会匹配doc 1.
//但是事实上这两个term是依次紧邻的,满足我们想要的匹配要求。
GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "names": {
        "query": "Abraham Lincoln" 
      }
    }
  }
}

//这个查询会匹配doc 1,因为指定了slop=101,大于position_increment_gap的默认距离100
GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "names": {
        "query": "Abraham Lincoln",
        "slop": 101 
      }
    }
  }
}

修改field的mapping参数可以达到同样的效果。

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "names": {
        "type": "text",
        //下一个array元素分词结果的第一个term,距离上一个array元素分词结果的最后一个term的距离被设置为0
        "position_increment_gap": 0 
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "names": [ "John Abraham", "Lincoln Smith"]
}


//此时该查询会匹配doc 1
GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "names": "Abraham Lincoln" 
    }
  }
}

properties

object 和nested包含子字段,子字段统一定义为properties. properties可以为任一类型,包括object和nested。

PUT my-index-000001
{
  "mappings": {
    "properties": { 
      "manager": {
        "properties": { 
          "age":  { "type": "integer" },
          "name": { "type": "text"  }
        }
      },
      "employees": {
        "type": "nested",
        "properties": { 
          "age":  { "type": "integer" },
          "name": { "type": "text"  }
        }
      }
    }
  }
}

PUT my-index-000001/_doc/1 
{
  "region": "US",
  "manager": {
    "name": "Alice White",
    "age": 30
  },
  "employees": [
    {

      "name": "John Smith",
      "age": 34
    },
    {
      "name": "Peter Brown",
      "age": 26
    }
  ]
}

properties属性定义使用PUT mapping API可以更新。

对properties定义的inner filed可以使用outter.inner的方式进行query,aggreegation. Just like this

GET my-index-000001/_search
{
  "query": {
    "match": {
    //用'.'连接field和inner field的方式来查询
      "manager.name": "Alice White"
    }
  },
  "aggs": {
    "Employees": {
      "nested": {
        "path": "employees"
      },
      "aggs": {
        "Employee Ages": {
          "histogram": {
            "field": "employees.age",
            "interval": 5
          }
        }
      }
    }
  }
}

search_analyzer

一般情况下某个字段的analyzer在index和search时是共用的,这是为了在查询时保证查询条件的分词结果与进入倒排索引的分词结果相同。

但是某些特殊情况下在search时使用不同的analyzer是有意义的,比如使用edge_ngram tokenizer做自动补全时。

PUT my-index-000001
{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20
        }
      },
      "analyzer": {
        "autocomplete": { 
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "analyzer": "autocomplete", 
        "search_analyzer": "standard" 
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "text": "Quick Brown Fox" 
}

GET my-index-000001/_search
{
  "query": {
    "match": {
      "text": {
        "query": "Quick Br", 
        "operator": "and"
      }
    }
  }
}

similarity

Elasticsearch允许您为每个字段配置评分算法或相似性。相似性设置提供了一种简单的选择相似性算法的方法,而不是默认的BM25,例如TF/IDF。

相似性主要用于文本字段,但也适用于其他字段类型。
可以通过调整内置相似性的参数来配置自定义相似性。有关此专家选项的更多详细信息,请参阅“相似性”模块。

在不进行任何进一步配置的情况下,可以开箱即用的similarity配置有:

  1. BM25

Okapi BM25算法。Elasticsearch和Lucene中默认使用的算法。

  1. classic

[7.0.0]在7.0.0中已弃用。TF/IDF算法,前者在Elasticsearch和Lucene中是默认的。

  1. boolean

一个简单的布尔相似度,当不需要全文排名时使用,分数只应基于查询词是否匹配。布尔相似性给术语的分数等于它们的查询提升。

store

默认情况下,各个field的值会被分词后索引(倒排索引中存储都是token),但是field的初始值不会单独存储(指存在于文档本身以外的地方)。这意味着字段可以被query,但你无法指定某个field去获取这个field的初始值(能够检索到的都是经过normalizer和analyzer的)。通常情况下,这是没有问题的。所有的field的值其实都会存在_source字段中,如果想获取指定field的值,可以使用source filter过滤出想要的结果就可以。

在特定场景下,额外存储field的原值是有意义的。

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "store": true 
      },
      "date": {
        "type": "date",
        "store": true 
      },
      "content": {
        "type": "text"
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "title":   "Some short title",
  "date":    "2015-01-01",
  "content": "A very long content field..."
}

GET my-index-000001/_search
{
    //只想查询title和date字段的取值
  "stored_fields": [ "title", "date" ] 
}

另外在查询某些不在_source中的字段时,store也很实用,比如copy_to.

term_vector

Term vectors包含了analysis process产生的关于terms的信息,包括:

  • term列表
  • 每个term的position位置信息(如果field value是数组,每个数组元素的term位置信息依次排序,默认情况下是有position_increment_gap的)。
  • 每个term在原始value中的起止偏移量
  • payload:与指定position上的term绑定的用户自定义二进制数据。

这些信息可以被存储起来,以用于我们查询。最主要是用来观察分词结果,调整分词器用的。

term vector取值如下:

no No term vectors are stored. (default)
yes Just the terms in the field are stored.
with_positions Terms and positions are stored.
with_offsets Terms and character offsets are stored.
with_positions_offsets Terms, positions, and character offsets are stored.
with_positions_payloads Terms, positions, and payloads are stored.
with_positions_offsets_payloads Terms, positions, offsets and payloads are stored.

如果设置为with_position_offsets会使索引的size double.