一览

tips:x-pack中的类型不做分析

配置项 作用 注意事项 默认值 使用场景 示例
Aggregate metric(xpack) 存储数据的聚合结果,min,max等
Alias 别名,指向另一个资源,查询,聚合时使用 1:不能用于copy_to,多字段类型的字段
2:别名字段,不能在_source中展示
3:不支持对象类型,nested类型需要有同样的scope(?)
无代码改动,搜索一些字段时,可以指定别名,用于搜索另一个字段。
Arrays 数组类型,多值场景
1:字符串数组: [ “one”, “two” ]
2:数字数组: [ 1, 2 ]
3:数组数组: [ 1, [ 2, 3 ]] 等同于[ 1, 2, 3 ]
4:对象数组: [ { “name”: “Mary”, “age”: 12 }, { “name”: “John”, “age”: 10 }]
1:没有具体的定义数组类型
2:数组中数据类型要一致
3:对于对象类型,如果有关联型查询,使用nested
Binary 存储Base64编码字符串的二进制值, 1:不以默认的方式存储, 且不能被搜索
2:不能存储换行符 \n
doc_values:false
store:false
二进制
Boolean 用于true和false的存储 1:true时支持:true,”true”
2:false是支持:false,”false”
3:版本有差异,在7.x官网说明 空文本””代表false,但是在7.1.1测试,存储””时,查询false并不能查询,使用中可严格进行true或false的存储
1:boost默认:1.0
2:doc_value默认true
3:index默认true
只有true或者false的场景
Date 日期类型,es底层存储单位是毫秒 1:小数点类型,点后的数据会被舍弃,应避免
2:允许非负值,及1970年以来的毫秒值
strict_date_optional_time||epoch_millis
Date nanoseconds 日期类型,es底层存储单位是纳秒 date日期类型的增强,存储纳秒值 strict_date_optional_time||epoch_millis 存储纳秒的场景
Dense vector(x-pack) 向量存储
Flattened(7.3) 新的对象类型,默认情况下,一个对象类型字段,其中的所有子字段都会分别进行索引与映射(扁平化),这样可能会导致 映射爆炸ES通过 flattened 类型,提供了一种折衷的解决方法,
(1) flattened类型会将整个对象,映射为一整个字段
(2) 但是, flattened类型,只提供了部分的查询功能
不支持的查询:范围查询,match分词查询,高亮,不支持存储store 此数据类型对于索引具有大量或未知数量的唯一键的对象很有用。仅为整个 JSON 对象创建一个字段映射,这可以帮助防止由于大量不同的字段映射而导致映射爆炸。
Geo-point
Geo-shape(xpack)
1:位置查询,在【速查手册06-QUERY DSL】中会有详细的记录,支持的查询:
1.1):Geo-bounding-box(矩形中的坐标点)
1.2):boxGeo-distance(找出与指定位置在给定距离内的点)
1.3):Geo-polygon(找出落在多边形中的点。这个过滤器使用代价很大。当你觉得自己需要使用它,最好先看看 geo-shapes)
1.4):Geo-shape(地理形状( Geo-shapes )使用一种与地理坐标点完全不同的方法。我们在计算机屏幕上看到的圆形并不是由完美的连续的线组成的)
[

](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/geo-point.html) | 1:geo_point这种数据格式索引时,使用数组和使用字符串标识经纬度,刚好相反;数组【经,纬】,字符串”纬,经”需要特别注意
2:geo_distance聚合时,计算方式有:ark(球体,精度高,查询效率低)和plane(平面,精度稍低,效率高)
3:geo_polygon查询性能极低,不使用倒排
| 1:使用distance时,filter的查询参数中distance_type 有arc和plane两种方式,arc为默认 | 位置查询 | | | Histogram | 直方图类型 | | | | | | IP | 一个ip字段可以索引/存储IPv4或 IPv6地址。
最常用的方法是使用 CIDR 表示法:[ip_address]/[prefix_length] | 引号是query_string查询的特殊字符 ,因此需要对 ipv6 地址进行转义。最简单的方法是在搜索值周围加上引号 | index,store等常用默认值 | ip数据类型场景 | | | Join | 1:父子关系数据
2:多级父子关系
| 1:父子数据需要在同一个分片
2:每个索引只能有一个join类型字段
3:父-子:1-N
4:不建议使用多个级别的关系来复制关系模型。每个级别的关系都会在查询时增加内存和计算方面的开销。如果你关心性能,你应该去规范化你的数据。
5:当该join字段不经常使用且写入频繁发生时,禁用预先加载可能是有意义的 | 全局序数:eager_global_ordinals默认true,当父会更新频繁,可禁用,但会影响查询效率。
| 1:父非常少,且更新不频繁 | | | Keyword | keyword,用于结构化内容,例如 ID、电子邮件地址、主机名、状态代码、邮政编码或标签。 | 1:避免使用关键字字段进行全文搜索。请改用text 字段类型。
2:通过term查询keyword字段比数字类型快,所以如果没有range查询,可将一些数字类型,改为keyword类型,如果不确定是否有range,可以定义keyword和数字,优化查询性能 | 无 | 结构化数据或者一些无range查询的数字 | | | Nested | 该nested类型是object数据类型的特殊版本,它允许对象数组以一种可以相互独立查询的方式进行索引。 | 1:nested查询性能较低,没有倒排,对象中文档会拆分多个Lucene索引段。
2:如果有A.B的关联查询,可考虑使用A和B拼接成一个字段,并定义成object类型,可提高查询效率 | 1:index.mapping.nested_fields.limit 字段限制,默认50
2:index.mapping.nested_objects.limit,单个文档可以包含的所有nested类型的嵌套 JSON 对象的最大数量 。当文档包含太多嵌套对象时,此限制有助于防止出现内存不足错误。默认为10000。
| 1:对象类型的查询,且查询的A.B有关联关系时 | | | Numeric | 数字类型。具体类型和说明如下:
long -263 — 263-1integer -231 — 231-1
short -32,768 — 32,767
byte -128 — 127
double 双精度64位
float 单精度32位
half_float 单精度16位
scaled_float 支持浮点的long
unsigned_long((xpack)) 一个无符号的 64 位整数,最小值为 0,最大值为。 264-1 | 无 | 无 | 数字查询 | | | Object | json对象存储 | 1:如果是json数组,且关联查询,注意扁平化数据倒排对数据的影响,区别nested
2:如果json内不想动态增加字段,可限制:dynamic为false(默认true) | 1:dynamic:true
2:enabled(对象内字段是否索引):true | 对象类型数据的存储和倒排查询 | | | Percolator | 将查询作为文档存储,查询时返回预先设置好查询条件的文档 | 无 | 无 | 1:预警
2:不需要存储原值即可搜索到匹配的文档
3:使用实例参考:percolate | | | Point(xpack) | 点坐标类型,搜索任意索引x, y落在在二维平面坐标系对 | | | | | | Range | integer_range 有符号32位整数范围 2-32 ~ 232-1
float_range 单精度32位IEEE 754 浮点数范围

long_range 有符号64位整数范围 2-64 ~ 264-1

double_range 双精度64位IEEE 754 浮点数范围

date_range 自系统历元以来无符号64位整数范围内的毫秒数范围

ip_range 支持 IPv4 或者 IPv6 (or mixed) 地址的IP范围
| 无 | 无 | range数据类型索引和查询,比如Ip网段 | | | Rank feature,
Rank features | 调整字段相关度分数 | 1:和简单的sort字段不同,是通过查询时调整boost和打分策略调整分数 | “positive_score_impact”: true,默认正相关 | 1:调整分数使用,把文档值作为分数调节因素
2:默认正相关,如果想负相关,比如值越大,分数越低,可设置”positive_score_impact”: false
3:支持的函数:Saturation
Logarithm
Sigmoid
Linear | | | Search-as-you-type | 7.2版本支持,提供类似于text类型的功能,内置了子字段
- 字段._2gram
- 字段._3gram
- 字段._index_prefix
| | | | | | Shape(xpack) | 形状类型,支持点,线,多边形的数据索引和搜索 | | | | | | Sparse vector | 字段存储疏浮点值的向量 | 7.6将变成过期属性且在8版本删除 | | | | | Text | 用于全文内容的传统字段类型 | | | | | | Token count | 记录分词的个数 | 无 | 无 | 貌似没啥使用场景,可能在存储英文句子时,用来查询包含多少个单词 | | | Unsigned long(xpack) | 见上面Number | | | | | | Version(xpack) | 存储字段版本 | | | | |

参考官网位置:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/mapping-types.html

示例

ip示例

  1. PUT my-index-000001
  2. {
  3. "mappings": {
  4. "properties": {
  5. "ip_addr": {
  6. "type": "ip"
  7. }
  8. }
  9. }
  10. }
  11. PUT my-index-000001/_doc/1
  12. {
  13. "ip_addr": "192.168.1.1"
  14. }
  15. GET my-index-000001/_search
  16. {
  17. "query": {
  18. "term": {
  19. "ip_addr": "192.168.0.0/16"
  20. }
  21. }
  22. }
  23. GET my-index-000001/_search
  24. {
  25. "query": {
  26. "query_string" : {
  27. "query": "ip_addr:\"2001:db8::/48\""
  28. }
  29. }
  30. }

join类型示例

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "my_id": {
        "type": "keyword"
      },
      "my_join_field": { 
        "type": "join",
        "relations": {
          "question": "answer" 
        }
      }
    }
  }
}
PUT my-index-000001/_doc/1?refresh
{
  "my_id": "1",
  "text": "This is a question",
  "my_join_field": {
    "name": "question" 
  }
}

PUT my-index-000001/_doc/2?refresh
{
  "my_id": "2",
  "text": "This is another question",
  "my_join_field": {
    "name": "question"
  }
}
PUT my-index-000001/_doc/3?routing=1&refresh
{
  "my_id": "3",
  "text": "This is an answer",
  "my_join_field": {
    "name": "answer", 
    "parent": "1" 
  }
}

PUT my-index-000001/_doc/4?routing=1&refresh
{
  "my_id": "4",
  "text": "This is another answer",
  "my_join_field": {
    "name": "answer",
    "parent": "1"
  }
}

#搜索 has_parent
GET my-index-000001/_search
{
  "query": {
    "has_parent": {
      "parent_type": "question",
      "query": {
        "match_all": {}
      }
    }
  }
}
#搜索 has_child
GET my-index-000001/_search
{
  "query": {
    "has_child": {
      "type": "answer",
      "query": {
        "term": {
          "my_id": {
            "value": "4"
          }
        }
      }
    }
  }
}

#聚合
GET my-index-000001/_search
{
  "query": {
    "parent_id": { 
      "type": "answer",
      "id": "1"
    }
  },
  "aggs": {
    "parents": {
      "terms": {
        "field": "my_join_field#question", 
        "size": 10
      }
    }
  }
}
#多级父子关系
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "my_join_field": {
        "type": "join",
        "relations": {
          "question": ["answer", "comment"],  
          "answer": "vote" 
        }
      }
    }
  }
}
存储结构如下 ↓
  question
    /    \
   /      \
comment  answer
           |
           |
          vote

range field 示例

PUT range_index
{
  "settings": {
    "number_of_shards": 2
  },
  "mappings": {
    "properties": {
      "expected_attendees": {
        "type": "integer_range"
      },
      "time_frame": {
        "type": "date_range", 
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      }
    }
  }
}

PUT range_index/_doc/1?refresh
{
  "expected_attendees" : { 
    "gte" : 10,
    "lt" : 20
  },
  "time_frame" : {
    "gte" : "2015-10-31 12:00:00", 
    "lte" : "2015-11-01"
  }
}

GET range_index/_search
{
  "query": {
    "term": {
      "time_frame": {
        "value": "2015-10-31 13:00:00"
      }
    }
  }
}

range_feature示例

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "pagerank": {
        "type": "rank_feature" 
      },
      "url_length": {
        "type": "rank_feature"
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "pagerank": 2,
  "url_length": 23
}
PUT my-index-000001/_doc/2
{
  "pagerank": 5,
  "url_length": 5
}
PUT my-index-000001/_doc/3
{
  "pagerank": 105,
  "url_length": 22
}

GET my-index-000001/_search
{
  "explain": true, 
  "query": {
    "bool": {
      "should": [
        {
          "rank_feature": {
            "field": "pagerank",
            "boost":0.1
          }
        },
         {
          "rank_feature": {
            "field": "url_length",
            "boost": 0.2
          }
        }
      ]
    }
  }
}
#输出结果 3,1,2
简单分析:
以文档3的得分为例:
1:字段(pagerank)
w = boost自定义设置 = 0.1
k =  6.359375
S = 文档值 = 105
分数 = w * S / (S + k) = 10.5/(105+6.359375) =  0.094289325
2:同理:字段url_length得分:0.12137931 
总得分:0.21566863

token count使用场景

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "name": { 
        "type": "text",
        "fields": {
          "length": { 
            "type":     "token_count",
            "analyzer": "standard"
          }
        }
      }
    }
  }
}

PUT my-index-000001/_doc/1
{ "name": "John Smith" }

PUT my-index-000001/_doc/2
{ "name": "Rachel Alice Williams good" }

GET my-index-000001/_search
{
  "query": {
    "term": {
      "name.length": 4 
    }
  }
}