索引字段与属性都属于静态设置,若后期变更历史数据需要重建索引才能生效

常用字段属性应用

能否查询

index 概念介绍
字段是否可以被搜索
取值true/false,默认true

  1. DELETE test-102
  2. PUT test-102
  3. {
  4. "mappings": {
  5. "properties": {
  6. "name":{
  7. "type": "keyword"
  8. , "index": false
  9. },
  10. "username":{
  11. "type": "keyword"
  12. , "index": true
  13. }
  14. }
  15. }
  16. }
  17. POST test-102/_doc
  18. {
  19. "name":"liwenjun",
  20. "username":"wenjun00"
  21. }
  22. GET test-102/_search
  23. {
  24. "query": {
  25. "term": {
  26. "name": {
  27. "value": "liwenjun"
  28. }
  29. }
  30. }
  31. }
  32. "reason" : "Cannot search on field [name] since it is not indexed."

是否存储

store
概念介绍:
ES默认原始数据存储在_source里,其实也可以在lucene里面存储一份,通过此关键词空置取值范围,true/flase默认false不存储
应用场景:
原始数据_source被禁用,也需要修改原始数据

  1. DELETE test-102
  2. PUT test-102
  3. {
  4. "mappings": {
  5. "properties": {
  6. "name":{
  7. "type": "keyword"
  8. , "index": true
  9. , "store": false
  10. },
  11. "username":{
  12. "type": "keyword"
  13. , "index": true
  14. , "store": true
  15. }
  16. }
  17. }
  18. }
  19. POST test-102/_doc
  20. {
  21. "name":"liwenjun",
  22. "username":"wenjun00"
  23. }
  24. ## 能查出来
  25. GET test-102/_search
  26. {
  27. "query": {
  28. "term": {
  29. "username": {
  30. "value": "wenjun00"
  31. }
  32. }
  33. }
  34. }
  35. ## 查不出来
  36. GET test-102/_search
  37. {
  38. "stored_fields": ["name"]
  39. }
  40. ## 能查出来
  41. GET test-102/_search
  42. {
  43. "stored_fields": ["username"]
  44. }

能否启用

enable
概念解释
设置字段是否需要被检索,类似index属性,用于对于未知object类型的设置,取值范围true/false
应用领域
部分应用领域,一些数据仅仅需要数据字段,不需要被检索与创建索引,一般用于object类型设置,免于设置太多
对于对象类型的字段属性,若业务上不存在检索的条件,则可以设置为 “enabled”:”false”

  1. PUT test-102
  2. {
  3. "mappings": {
  4. "properties": {
  5. "area":{
  6. "type": "object",
  7. "enabled":"false"
  8. }
  9. }
  10. }
  11. }
  12. GET test-102
  13. {
  14. "test-102" : {
  15. "aliases" : { },
  16. "mappings" : {
  17. "properties" : {
  18. "area" : {
  19. "type" : "object",
  20. "enabled" : false
  21. }
  22. }
  23. },
  24. "settings" : {
  25. "index" : {
  26. "creation_date" : "1658304053408",
  27. "number_of_shards" : "1",
  28. "number_of_replicas" : "1",
  29. "uuid" : "XDDGR6HGQ3WBvlPqtkZmIA",
  30. "version" : {
  31. "created" : "7090199"
  32. },
  33. "provided_name" : "test-102"
  34. }
  35. }
  36. }
  37. }

能够聚合排序

doc_value
概念解释
列式数据存储,ES数据存储原始数据一份,列式存储一份,默认有2份数据,取值范围true/false,默认true
非text字段默认开启,text字段是分词使用,做不了聚合排序操作
应用领域
基于该字段做聚合分析,基于该字段做排序

  1. DELETE test-102
  2. PUT test-102
  3. {
  4. "mappings": {
  5. "properties": {
  6. "name":{
  7. "type": "keyword",
  8. "doc_values": true
  9. },
  10. "area":{
  11. "type": "object",
  12. "enabled":"true"
  13. }
  14. }
  15. }
  16. }
  17. GET test-102
  18. POST test-102/_doc
  19. {
  20. "name":"liwenjun3",
  21. "area":{
  22. "pro":"HUN",
  23. "city":"ZJJ"
  24. }
  25. }
  26. GET test-102/_search
  27. {
  28. "sort": [
  29. {
  30. "name": {
  31. "order": "desc"
  32. }
  33. }
  34. ]
  35. }

空值默认

null_value
概念介绍
控制默认值,ES容许可以不设置字段,没有数据库的必须填充选项
此处与数据库里面的默认值有区别,ES并不会存储这个值,也就是_source中不会存储,仅仅用来做索引检索
应用用途
程序性能提升
避免程序数据异常错误

一般不要用这个属性,并不是插入数据会给默认值,而是给null时转换,和数据库的理解不一样

子对象控制

properties
概念解释
数据模型多种多样,有的需要严格的设置

子对象动态扩展

dynamic
概念解释
是否容许对象下面的属性自由扩展
取值范围true/false/strict/runtime ,默认true
应用领域
严格限制子对象下面的字段行为;
strict 不允许动态扩展字段,插入数据时会报错
false 可以插入数据,但是不能做搜索,不能做聚合排序功能;

字符串长度限定

ignore_above
概念解释
keyword类型下,字符串过长,检索意义不大,索引会被禁用,数据不可被检索
应用领域
基于keyword类型检索应用,超过限定长度就无需检索

多字段

概念解释
多字段属性
应用领域
同一个数据字段,需要满足同时多种业务应用

数值强制性约束

coerce
概念解释
数值类数据并非一直附合标准,有的会带一些格式之类的
取值范围true/false,默认false
应用场景
数据类数据写入强制限制,避免数据格式出错

日期格式化

format
概念解释

  1. Date类型默认支持多种输入格式,非常灵活自由,稍微不注意,容易造成脏数据
  2. Format可以限制格式,设定自定义格式

    高级数据字段属性应用

    分词器

    analyzer
    概念解释
    全文检索分词器
    默认算法
    BM25,TF/IDF,Bool

    字段数据

    fielddata
    概念解释
    全文分词字段做聚合统计,需要将一些分词数据存储在内存中,加快聚合效率