ElasticSearch的常用数据类型

发布于:2024-04-03 ⋅ 阅读:(119) ⋅ 点赞:(0)

常见的数据类型

Text类型(文本数据类型)

用于索引全文值的字段,例如电子邮件的正文或产品的描述。这些字段是analyzed,也就是说,它们通过分析器传递,以便 在被索引之前将字符串转换为单个术语的列表。通过分析过程,Elasticsearch可以在 每个全文字段中搜索单个单词。文本字段不用于排序,很少用于聚合

PUT test-03
{
  "mappings": {
    "properties": {
      "full_name": {
        "type": "text"
      }
    }
  }
}

Keyword (关键字数据类型)

用于索引结构化内容(例如ID,电子邮件地址,主机名,状态代码,邮政编码或标签)的字段。

关键字字段只能按其精确值进行搜索。

PUT test-03
{
  "mappings": {
    "properties": {
      "tags": {
        "type": "keyword"
      }
    }
  }
}

Alias(别名类型)

#

Arrays (集合类型)

在Elasticsearch中,没有专用的array数据类型。默认情况下,任何字段都可以包含零个或多个值,但是,数组中的所有值都必须具有相同的数据类型.

{
  "message": "some arrays in this document…",
  "tags": [
    "elasticsearch",
    "wow"
  ],
  "lists": [
    {
      "name": "knight",
      "description": "programming list"
    },
    {
      "name": "rose",
      "description": "cool stuff list"
    }
  ]
}

Binary (二进制类型)

该binary类型接受二进制值作为 Base64编码的字符串。该字段默认情况下不存储,并且不可搜索

PUT test-03
{
  "mappings": {
    "properties": {
      "tags": {
        "type": "text"
      },
     "blob":"binary"
    }
  }
}

Boolean(布尔类型)

布尔字段接受JSON true和false值,但也可以接受解释为true或false的字符串

PUT test-03
{
  "mappings": {
    "properties": {
      "is_published": {
        "type": "boolean"
      }
    }
  }
}

日期类型

JSON没有日期数据类型,因此Elasticsearch中的日期可以是:

  • 包含格式化日期的字符串,例如"2024-01-01"或"2024/01/01 12:10:30"
  • 时间戳


PUT test-011
{
  "mappings": {
    "properties": {
    "date":{
      "type": "date",
      "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
    }
    }
  }
}

PUT test-011/_doc/1
{ "date": "2024-01-01" }

PUT test-011/_doc/2
{ "date": "2024-01-02" }

GET test-011/_search

返回:

{
  "took" : 558,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test-011",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "date" : "2024-01-01"
        }
      },
      {
        "_index" : "test-011",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "date" : "2024-01-02"
        }
      }
    ]
  }
}

Dense vector(密集矢量数据类型)

Flattened (扁平化的数据类型)

Geo-point(地理位置数据类型)

PUT test-012
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}


###
PUT test-012/_doc/1
{
  "text": "Geo-point as an object",
  "location": {
    "lat": 41.12,
    "lon": -71.34
  }
}

地理形状数据类型

的geo_shape数据类型方便的索引和与任意的地理搜索为矩形和多边形的形状,例如。当正在索引的数据或正在执行的查询包含除点以外的其他形状时,应使用它。

PUT test-012
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

IP数据类型

一个ip字段可以索引/存储IPv4或 IPv6地址

PUT test-013
{
  "mappings": {
    "properties": {
      "ip_addr": {
        "type": "ip"
      }
    }
  }
}

PUT test-013/_doc/1
{
"ip_addr": "192.168.1.1"
}

Join (联接数据类型)

Nested (嵌套数据类型)

Object (对象数据类型)

Numeric (数值数据类型)

Range(范围数据类型)

Token count (令牌计数数据类型)