ElasticSearch 23 种映射参数详解

我会带着你远行 2022-12-26 02:29 257阅读 0赞

文章目录

    • 1.ElasticSearch 映射参数
      • 1.1 analyzer
      • 1.2 search_analyzer
      • 1.3 normalizer
      • 1.4 boost
      • 1.5 coerce
      • 1.6 copy_to
      • 1.7 doc_values 和 fielddata
      • 1.8 dynamic
      • 1.9 enabled
      • 1.10 format
      • 1.11 ignore_above
      • 1.12 ignore_malformed
      • 1.13 include_in_all
      • 1.14 index
      • 1.15 index_options
      • 1.16 norms
      • 1.17 null_value
      • 1.18 position_increment_gap
      • 1.19 properties
      • 1.20 similarity
      • 1.21 store
      • 1.22 term_vectors
      • 1.23 fields

ElasticSearch 系列教程我们前面已经连着发了四篇了,今天第五篇,我们来聊一聊 Es 中的 23 种常见的映射参数。

针对这 23 种常见的映射参数,松哥专门录制了一个视频教程:

d463cc6aec9960090fb3f3673b91178f.png

视频链接: https://pan.baidu.com/s/1J23m6oSTeZJU6j6KaogZSg 提取码: 6k2a

本文是松哥所录视频教程的一个笔记,笔记简明扼要,完整内容小伙伴们可以参考视频。

1.ElasticSearch 映射参数

1.1 analyzer

定义文本字段的分词器。默认对索引和查询都是有效的。

假设不用分词器,我们先来看一下索引的结果,创建一个索引并添加一个文档:

  1. PUT blog
  2. PUT blog/_doc/1
  3. {
  4. "title":"定义文本字段的分词器。默认对索引和查询都是有效的。"
  5. }

查看词条向量(term vectors)

  1. GET blog/_termvectors/1
  2. {
  3. "fields": ["title"]
  4. }

查看结果如下:

  1. {
  2. "_index" : "blog",
  3. "_type" : "_doc",
  4. "_id" : "1",
  5. "_version" : 1,
  6. "found" : true,
  7. "took" : 0,
  8. "term_vectors" : {
  9. "title" : {
  10. "field_statistics" : {
  11. "sum_doc_freq" : 22,
  12. "doc_count" : 1,
  13. "sum_ttf" : 23
  14. },
  15. "terms" : {
  16. "义" : {
  17. "term_freq" : 1,
  18. "tokens" : [
  19. {
  20. "position" : 1,
  21. "start_offset" : 1,
  22. "end_offset" : 2
  23. }
  24. ]
  25. },
  26. "分" : {
  27. "term_freq" : 1,
  28. "tokens" : [
  29. {
  30. "position" : 7,
  31. "start_offset" : 7,
  32. "end_offset" : 8
  33. }
  34. ]
  35. },
  36. "和" : {
  37. "term_freq" : 1,
  38. "tokens" : [
  39. {
  40. "position" : 15,
  41. "start_offset" : 16,
  42. "end_offset" : 17
  43. }
  44. ]
  45. },
  46. "器" : {
  47. "term_freq" : 1,
  48. "tokens" : [
  49. {
  50. "position" : 9,
  51. "start_offset" : 9,
  52. "end_offset" : 10
  53. }
  54. ]
  55. },
  56. "字" : {
  57. "term_freq" : 1,
  58. "tokens" : [
  59. {
  60. "position" : 4,
  61. "start_offset" : 4,
  62. "end_offset" : 5
  63. }
  64. ]
  65. },
  66. "定" : {
  67. "term_freq" : 1,
  68. "tokens" : [
  69. {
  70. "position" : 0,
  71. "start_offset" : 0,
  72. "end_offset" : 1
  73. }
  74. ]
  75. },
  76. "对" : {
  77. "term_freq" : 1,
  78. "tokens" : [
  79. {
  80. "position" : 12,
  81. "start_offset" : 13,
  82. "end_offset" : 14
  83. }
  84. ]
  85. },
  86. "引" : {
  87. "term_freq" : 1,
  88. "tokens" : [
  89. {
  90. "position" : 14,
  91. "start_offset" : 15,
  92. "end_offset" : 16
  93. }
  94. ]
  95. },
  96. "效" : {
  97. "term_freq" : 1,
  98. "tokens" : [
  99. {
  100. "position" : 21,
  101. "start_offset" : 22,
  102. "end_offset" : 23
  103. }
  104. ]
  105. },
  106. "文" : {
  107. "term_freq" : 1,
  108. "tokens" : [
  109. {
  110. "position" : 2,
  111. "start_offset" : 2,
  112. "end_offset" : 3
  113. }
  114. ]
  115. },
  116. "是" : {
  117. "term_freq" : 1,
  118. "tokens" : [
  119. {
  120. "position" : 19,
  121. "start_offset" : 20,
  122. "end_offset" : 21
  123. }
  124. ]
  125. },
  126. "有" : {
  127. "term_freq" : 1,
  128. "tokens" : [
  129. {
  130. "position" : 20,
  131. "start_offset" : 21,
  132. "end_offset" : 22
  133. }
  134. ]
  135. },
  136. "本" : {
  137. "term_freq" : 1,
  138. "tokens" : [
  139. {
  140. "position" : 3,
  141. "start_offset" : 3,
  142. "end_offset" : 4
  143. }
  144. ]
  145. },
  146. "查" : {
  147. "term_freq" : 1,
  148. "tokens" : [
  149. {
  150. "position" : 16,
  151. "start_offset" : 17,
  152. "end_offset" : 18
  153. }
  154. ]
  155. },
  156. "段" : {
  157. "term_freq" : 1,
  158. "tokens" : [
  159. {
  160. "position" : 5,
  161. "start_offset" : 5,
  162. "end_offset" : 6
  163. }
  164. ]
  165. },
  166. "的" : {
  167. "term_freq" : 2,
  168. "tokens" : [
  169. {
  170. "position" : 6,
  171. "start_offset" : 6,
  172. "end_offset" : 7
  173. },
  174. {
  175. "position" : 22,
  176. "start_offset" : 23,
  177. "end_offset" : 24
  178. }
  179. ]
  180. },
  181. "索" : {
  182. "term_freq" : 1,
  183. "tokens" : [
  184. {
  185. "position" : 13,
  186. "start_offset" : 14,
  187. "end_offset" : 15
  188. }
  189. ]
  190. },
  191. "认" : {
  192. "term_freq" : 1,
  193. "tokens" : [
  194. {
  195. "position" : 11,
  196. "start_offset" : 12,
  197. "end_offset" : 13
  198. }
  199. ]
  200. },
  201. "词" : {
  202. "term_freq" : 1,
  203. "tokens" : [
  204. {
  205. "position" : 8,
  206. "start_offset" : 8,
  207. "end_offset" : 9
  208. }
  209. ]
  210. },
  211. "询" : {
  212. "term_freq" : 1,
  213. "tokens" : [
  214. {
  215. "position" : 17,
  216. "start_offset" : 18,
  217. "end_offset" : 19
  218. }
  219. ]
  220. },
  221. "都" : {
  222. "term_freq" : 1,
  223. "tokens" : [
  224. {
  225. "position" : 18,
  226. "start_offset" : 19,
  227. "end_offset" : 20
  228. }
  229. ]
  230. },
  231. "默" : {
  232. "term_freq" : 1,
  233. "tokens" : [
  234. {
  235. "position" : 10,
  236. "start_offset" : 11,
  237. "end_offset" : 12
  238. }
  239. ]
  240. }
  241. }
  242. }
  243. }
  244. }

可以看到,默认情况下,中文就是一个字一个字的分,这种分词方式没有任何意义。如果这样分词,查询就只能按照一个字一个字来查,像下面这样:

  1. GET blog/_search
  2. {
  3. "query": {
  4. "term": {
  5. "title": "定"
  6. }
  7. }
  8. }

无意义!!!

所以,我们要根据实际情况,配置合适的分词器。

给字段设定分词器:

  1. PUT blog
  2. {
  3. "mappings": {
  4. "properties": {
  5. "title":{
  6. "type":"text",
  7. "analyzer": "ik_smart"
  8. }
  9. }
  10. }
  11. }

存储文档:

  1. PUT blog/_doc/1
  2. {
  3. "title":"定义文本字段的分词器。默认对索引和查询都是有效的。"
  4. }

查看词条向量:

  1. GET blog/_termvectors/1
  2. {
  3. "fields": ["title"]
  4. }

查询结果如下:

  1. {
  2. "_index" : "blog",
  3. "_type" : "_doc",
  4. "_id" : "1",
  5. "_version" : 1,
  6. "found" : true,
  7. "took" : 1,
  8. "term_vectors" : {
  9. "title" : {
  10. "field_statistics" : {
  11. "sum_doc_freq" : 12,
  12. "doc_count" : 1,
  13. "sum_ttf" : 13
  14. },
  15. "terms" : {
  16. "分词器" : {
  17. "term_freq" : 1,
  18. "tokens" : [
  19. {
  20. "position" : 4,
  21. "start_offset" : 7,
  22. "end_offset" : 10
  23. }
  24. ]
  25. },
  26. "和" : {
  27. "term_freq" : 1,
  28. "tokens" : [
  29. {
  30. "position" : 8,
  31. "start_offset" : 16,
  32. "end_offset" : 17
  33. }
  34. ]
  35. },
  36. "字段" : {
  37. "term_freq" : 1,
  38. "tokens" : [
  39. {
  40. "position" : 2,
  41. "start_offset" : 4,
  42. "end_offset" : 6
  43. }
  44. ]
  45. },
  46. "定义" : {
  47. "term_freq" : 1,
  48. "tokens" : [
  49. {
  50. "position" : 0,
  51. "start_offset" : 0,
  52. "end_offset" : 2
  53. }
  54. ]
  55. },
  56. "对" : {
  57. "term_freq" : 1,
  58. "tokens" : [
  59. {
  60. "position" : 6,
  61. "start_offset" : 13,
  62. "end_offset" : 14
  63. }
  64. ]
  65. },
  66. "文本" : {
  67. "term_freq" : 1,
  68. "tokens" : [
  69. {
  70. "position" : 1,
  71. "start_offset" : 2,
  72. "end_offset" : 4
  73. }
  74. ]
  75. },
  76. "有效" : {
  77. "term_freq" : 1,
  78. "tokens" : [
  79. {
  80. "position" : 11,
  81. "start_offset" : 21,
  82. "end_offset" : 23
  83. }
  84. ]
  85. },
  86. "查询" : {
  87. "term_freq" : 1,
  88. "tokens" : [
  89. {
  90. "position" : 9,
  91. "start_offset" : 17,
  92. "end_offset" : 19
  93. }
  94. ]
  95. },
  96. "的" : {
  97. "term_freq" : 2,
  98. "tokens" : [
  99. {
  100. "position" : 3,
  101. "start_offset" : 6,
  102. "end_offset" : 7
  103. },
  104. {
  105. "position" : 12,
  106. "start_offset" : 23,
  107. "end_offset" : 24
  108. }
  109. ]
  110. },
  111. "索引" : {
  112. "term_freq" : 1,
  113. "tokens" : [
  114. {
  115. "position" : 7,
  116. "start_offset" : 14,
  117. "end_offset" : 16
  118. }
  119. ]
  120. },
  121. "都是" : {
  122. "term_freq" : 1,
  123. "tokens" : [
  124. {
  125. "position" : 10,
  126. "start_offset" : 19,
  127. "end_offset" : 21
  128. }
  129. ]
  130. },
  131. "默认" : {
  132. "term_freq" : 1,
  133. "tokens" : [
  134. {
  135. "position" : 5,
  136. "start_offset" : 11,
  137. "end_offset" : 13
  138. }
  139. ]
  140. }
  141. }
  142. }
  143. }
  144. }

然后就可以通过词去搜索了:

  1. GET blog/_search
  2. {
  3. "query": {
  4. "term": {
  5. "title": "索引"
  6. }
  7. }
  8. }

1.2 search_analyzer

查询时候的分词器。默认情况下,如果没有配置 search_analyzer,则查询时,首先查看有没有 search_analyzer,有的话,就用 search_analyzer 来进行分词,如果没有,则看有没有 analyzer,如果有,则用 analyzer 来进行分词,否则使用 es 默认的分词器。

1.3 normalizer

normalizer 参数用于解析前(索引或者查询)的标准化配置。

比如,在 es 中,对于一些我们不想切分的字符串,我们通常会将其设置为 keyword,搜索时候也是使用整个词进行搜索。如果在索引前没有做好数据清洗,导致大小写不一致,例如 javaboy 和 JAVABOY,此时,我们就可以使用 normalizer 在索引之前以及查询之前进行文档的标准化。

先来一个反例,创建一个名为 blog 的索引,设置 author 字段类型为 keyword:

  1. PUT blog
  2. {
  3. "mappings": {
  4. "properties": {
  5. "author":{
  6. "type": "keyword"
  7. }
  8. }
  9. }
  10. }

添加两个文档:

  1. PUT blog/_doc/1
  2. {
  3. "author":"javaboy"
  4. }
  5. PUT blog/_doc/2
  6. {
  7. "author":"JAVABOY"
  8. }

然后进行搜索:

  1. GET blog/_search
  2. {
  3. "query": {
  4. "term": {
  5. "author": "JAVABOY"
  6. }
  7. }
  8. }

大写关键字可以搜到大写的文档,小写关键字可以搜到小写的文档。

如果使用了 normalizer,可以在索引和查询时,分别对文档进行预处理。

normalizer 定义方式如下:

  1. PUT blog
  2. {
  3. "settings": {
  4. "analysis": {
  5. "normalizer":{
  6. "my_normalizer":{
  7. "type":"custom",
  8. "filter":["lowercase"]
  9. }
  10. }
  11. }
  12. },
  13. "mappings": {
  14. "properties": {
  15. "author":{
  16. "type": "keyword",
  17. "normalizer":"my_normalizer"
  18. }
  19. }
  20. }
  21. }

在 settings 中定义 normalizer,然后在 mappings 中引用。

测试方式和前面一致。此时查询的时候,大写关键字也可以查询到小写文档,因为无论是索引还是查询,都会将大写转为小写。

1.4 boost

boost 参数可以设置字段的权重。

boost 有两种使用思路,一种就是在定义 mappings 的时候使用,在指定字段类型时使用;另一种就是在查询时使用。

实际开发中建议使用后者,前者有问题:如果不重新索引文档,权重无法修改。

mapping 中使用 boost(不推荐):

  1. PUT blog
  2. {
  3. "mappings": {
  4. "properties": {
  5. "content":{
  6. "type": "text",
  7. "boost": 2
  8. }
  9. }
  10. }
  11. }

另一种方式就是在查询的时候,指定 boost

  1. GET blog/_search
  2. {
  3. "query": {
  4. "match": {
  5. "content": {
  6. "query": "你好",
  7. "boost": 2
  8. }
  9. }
  10. }
  11. }

1.5 coerce

coerce 用来清除脏数据,默认为 true。

例如一个数字,在 JSON 中,用户可能写错了:

  1. {"age":"99"}

或者 :

  1. {"age":"99.0"}

这些都不是正确的数字格式。

通过 coerce 可以解决该问题。

默认情况下,以下操作没问题,就是 coerce 起作用:

  1. PUT blog
  2. {
  3. "mappings": {
  4. "properties": {
  5. "age":{
  6. "type": "integer"
  7. }
  8. }
  9. }
  10. }
  11. POST blog/_doc
  12. {
  13. "age":"99.0"
  14. }

如果需要修改 coerce ,方式如下:

  1. PUT blog
  2. {
  3. "mappings": {
  4. "properties": {
  5. "age":{
  6. "type": "integer",
  7. "coerce": false
  8. }
  9. }
  10. }
  11. }
  12. POST blog/_doc
  13. {
  14. "age":99
  15. }

当 coerce 修改为 false 之后,数字就只能是数字了,不可以是字符串,该字段传入字符串会报错。

1.6 copy_to

这个属性,可以将多个字段的值,复制到同一个字段中。

定义方式如下:

  1. PUT blog
  2. {
  3. "mappings": {
  4. "properties": {
  5. "title":{
  6. "type": "text",
  7. "copy_to": "full_content"
  8. },
  9. "content":{
  10. "type": "text",
  11. "copy_to": "full_content"
  12. },
  13. "full_content":{
  14. "type": "text"
  15. }
  16. }
  17. }
  18. }
  19. PUT blog/_doc/1
  20. {
  21. "title":"你好江南一点雨",
  22. "content":"当 coerce 修改为 false 之后,数字就只能是数字了,不可以是字符串,该字段传入字符串会报错。"
  23. }
  24. GET blog/_search
  25. {
  26. "query": {
  27. "term": {
  28. "full_content": "当"
  29. }
  30. }
  31. }

1.7 doc_values 和 fielddata

es 中的搜索主要是用到倒排索引,doc_values 参数是为了加快排序、聚合操作而生的。当建立倒排索引的时候,会额外增加列式存储映射。

doc_values 默认是开启的,如果确定某个字段不需要排序或者不需要聚合,那么可以关闭 doc_values。

大部分的字段在索引时都会生成 doc_values,除了 text。text 字段在查询时会生成一个 fielddata 的数据结构,fieldata 在字段首次被聚合、排序的时候生成。


























doc_values fielddata
索引时创建 使用时动态创建
磁盘 内存
不占用内存 不占用磁盘
索引速度稍微低一点 文档很多时,动态创建慢,占内存

doc_values 默认开启,fielddata 默认关闭。

doc_values 演示:

  1. PUT users
  2. PUT users/_doc/1
  3. {
  4. "age":100
  5. }
  6. PUT users/_doc/2
  7. {
  8. "age":99
  9. }
  10. PUT users/_doc/3
  11. {
  12. "age":98
  13. }
  14. PUT users/_doc/4
  15. {
  16. "age":101
  17. }
  18. GET users/_search
  19. {
  20. "query": {
  21. "match_all": {}
  22. },
  23. "sort":[
  24. {
  25. "age":{
  26. "order": "desc"
  27. }
  28. }
  29. ]
  30. }

由于 doc_values 默认时开启的,所以可以直接使用该字段排序,如果想关闭 doc_values ,如下:

  1. PUT users
  2. {
  3. "mappings": {
  4. "properties": {
  5. "age":{
  6. "type": "integer",
  7. "doc_values": false
  8. }
  9. }
  10. }
  11. }
  12. PUT users/_doc/1
  13. {
  14. "age":100
  15. }
  16. PUT users/_doc/2
  17. {
  18. "age":99
  19. }
  20. PUT users/_doc/3
  21. {
  22. "age":98
  23. }
  24. PUT users/_doc/4
  25. {
  26. "age":101
  27. }
  28. GET users/_search
  29. {
  30. "query": {
  31. "match_all": {}
  32. },
  33. "sort":[
  34. {
  35. "age":{
  36. "order": "desc"
  37. }
  38. }
  39. ]
  40. }

1.8 dynamic

1.9 enabled

es 默认会索引所有的字段,但是有的字段可能只需要存储,不需要索引。此时可以通过 enabled 字段来控制:

  1. PUT blog
  2. {
  3. "mappings": {
  4. "properties": {
  5. "title":{
  6. "enabled": false
  7. }
  8. }
  9. }
  10. }
  11. PUT blog/_doc/1
  12. {
  13. "title":"javaboy"
  14. }
  15. GET blog/_search
  16. {
  17. "query": {
  18. "term": {
  19. "title": "javaboy"
  20. }
  21. }
  22. }

设置了 enabled 为 false 之后,就可以再通过该字段进行搜索了。

1.10 format

日期格式。format 可以规范日期格式,而且一次可以定义多个 format。

  1. PUT users
  2. {
  3. "mappings": {
  4. "properties": {
  5. "birthday":{
  6. "type": "date",
  7. "format": "yyyy-MM-dd||yyyy-MM-dd HH:mm:ss"
  8. }
  9. }
  10. }
  11. }
  12. PUT users/_doc/1
  13. {
  14. "birthday":"2020-11-11"
  15. }
  16. PUT users/_doc/2
  17. {
  18. "birthday":"2020-11-11 11:11:11"
  19. }
  • 多个日期格式之间,使用 || 符号连接,注意没有空格。
  • 如果用户没有指定日期的 format,默认的日期格式是 strict_date_optional_time||epoch_mills

另外,所有的日期格式,可以在 https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html 网址查看。

1.11 ignore_above

igbore_above 用于指定分词和索引的字符串最大长度,超过最大长度的话,该字段将不会被索引,这个字段只适用于 keyword 类型。

  1. PUT blog
  2. {
  3. "mappings": {
  4. "properties": {
  5. "title":{
  6. "type": "keyword",
  7. "ignore_above": 10
  8. }
  9. }
  10. }
  11. }
  12. PUT blog/_doc/1
  13. {
  14. "title":"javaboy"
  15. }
  16. PUT blog/_doc/2
  17. {
  18. "title":"javaboyjavaboyjavaboy"
  19. }
  20. GET blog/_search
  21. {
  22. "query": {
  23. "term": {
  24. "title": "javaboyjavaboyjavaboy"
  25. }
  26. }
  27. }

1.12 ignore_malformed

ignore_malformed 可以忽略不规则的数据,该参数默认为 false。

  1. PUT users
  2. {
  3. "mappings": {
  4. "properties": {
  5. "birthday":{
  6. "type": "date",
  7. "format": "yyyy-MM-dd||yyyy-MM-dd HH:mm:ss"
  8. },
  9. "age":{
  10. "type": "integer",
  11. "ignore_malformed": true
  12. }
  13. }
  14. }
  15. }
  16. PUT users/_doc/1
  17. {
  18. "birthday":"2020-11-11",
  19. "age":99
  20. }
  21. PUT users/_doc/2
  22. {
  23. "birthday":"2020-11-11 11:11:11",
  24. "age":"abc"
  25. }
  26. PUT users/_doc/2
  27. {
  28. "birthday":"2020-11-11 11:11:11aaa",
  29. "age":"abc"
  30. }

1.13 include_in_all

这个是针对 _all 字段的,但是在 es7 中,该字段已经被废弃了。

1.14 index

index 属性指定一个字段是否被索引,该属性为 true 表示字段被索引,false 表示字段不被索引。

  1. PUT users
  2. {
  3. "mappings": {
  4. "properties": {
  5. "age":{
  6. "type": "integer",
  7. "index": false
  8. }
  9. }
  10. }
  11. }
  12. PUT users/_doc/1
  13. {
  14. "age":99
  15. }
  16. GET users/_search
  17. {
  18. "query": {
  19. "term": {
  20. "age": 99
  21. }
  22. }
  23. }
  • 如果 index 为 false,则不能通过对应的字段搜索。

1.15 index_options

index_options 控制索引时哪些信息被存储到倒排索引中(用在 text 字段中),有四种取值:


























index_options 备注
docs 只存储文档编号,默认即此
freqs 在 docs 基础上,存储词项频率
positions 在 freqs 基础上,存储词项偏移位置
offsets 在 positions 基础上,存储词项开始和结束的字符位置

1.16 norms

norms 对字段评分有用,text 默认开启 norms,如果不是特别需要,不要开启 norms。

1.17 null_value

在 es 中,值为 null 的字段不索引也不可以被搜索,null_value 可以让值为 null 的字段显式的可索引、可搜索:

  1. PUT users
  2. {
  3. "mappings": {
  4. "properties": {
  5. "name":{
  6. "type": "keyword",
  7. "null_value": "javaboy_null"
  8. }
  9. }
  10. }
  11. }
  12. PUT users/_doc/1
  13. {
  14. "name":null,
  15. "age":99
  16. }
  17. GET users/_search
  18. {
  19. "query": {
  20. "term": {
  21. "name": "javaboy_null"
  22. }
  23. }
  24. }

1.18 position_increment_gap

被解析的 text 字段会将 term 的位置考虑进去,目的是为了支持近似查询和短语查询,当我们去索引一个含有多个值的 text 字段时,会在各个值之间添加一个假想的空间,将值隔开,这样就可以有效避免一些无意义的短语匹配,间隙大小通过 position_increment_gap 来控制,默认是 100。

  1. PUT users
  2. PUT users/_doc/1
  3. {
  4. "name":["zhang san","li si"]
  5. }
  6. GET users/_search
  7. {
  8. "query": {
  9. "match_phrase": {
  10. "name": {
  11. "query": "sanli"
  12. }
  13. }
  14. }
  15. }
  • sanli 搜索不到,因为两个短语之间有一个假想的空隙,为 100。

    GET users/_search
    {
    “query”: {

    1. "match_phrase": {
    2. "name": {
    3. "query": "san li",
    4. "slop": 101
    5. }
    6. }

    }
    }

可以通过 slop 指定空隙大小。

也可以在定义索引的时候,指定空隙:

  1. PUT users
  2. {
  3. "mappings": {
  4. "properties": {
  5. "name":{
  6. "type": "text",
  7. "position_increment_gap": 0
  8. }
  9. }
  10. }
  11. }
  12. PUT users/_doc/1
  13. {
  14. "name":["zhang san","li si"]
  15. }
  16. GET users/_search
  17. {
  18. "query": {
  19. "match_phrase": {
  20. "name": {
  21. "query": "san li"
  22. }
  23. }
  24. }
  25. }

1.19 properties

1.20 similarity

similarity 指定文档的评分模型,默认有三种:






















similarity 备注
BM25 es 和 lucene 默认的评分模型
classic TF/IDF 评分
boolean boolean 模型评分

1.21 store

默认情况下,字段会被索引,也可以搜索,但是不会存储,虽然不会被存储的,但是 _source 中有一个字段的备份。如果想将字段存储下来,可以通过配置 store 来实现。

1.22 term_vectors

term_vectors 是通过分词器产生的信息,包括:

  • 一组 terms
  • 每个 term 的位置
  • term 的首字符/尾字符与原始字符串原点的偏移量

term_vectors 取值:






























取值 备注
no 不存储信息,默认即此
yes term 被存储
with_positions 在 yes 的基础上增加位置信息
with_offset 在 yes 的基础上增加偏移信息
with_positions_offsets term、位置、偏移量都存储

1.23 fields

fields 参数可以让同一字段有多种不同的索引方式。例如:

  1. PUT blog
  2. {
  3. "mappings": {
  4. "properties": {
  5. "title":{
  6. "type": "text",
  7. "fields": {
  8. "raw":{
  9. "type":"keyword"
  10. }
  11. }
  12. }
  13. }
  14. }
  15. }
  16. PUT blog/_doc/1
  17. {
  18. "title":"javaboy"
  19. }
  20. GET blog/_search
  21. {
  22. "query": {
  23. "term": {
  24. "title.raw": "javaboy"
  25. }
  26. }
  27. }
  • https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html

最后,松哥还搜集了 50+ 个项目需求文档,想做个项目练练手的小伙伴不妨看看哦~

e219fd4cd10e143d33b27336e29bed0a.png
b8fc793dbdc8fda1f3954b002764502f.png
d59d5cc147ae46000f08cb1ffdda204e.png

需求文档地址:https://github.com/lenve/javadoc

发表评论

表情:
评论列表 (有 0 条评论,257人围观)

还没有评论,来说两句吧...

相关阅读

    相关 23 设计模式详解

    设计模式是对大家实际工作中写的各种代码进行高层次抽象的总结,其中最出名的当属 Gang of Four (GoF) 的分类了,他们将设计模式分类为 23 种经典的模式,根据用途