Elasticsearch整理之Field datatype

本文详细介绍了Elasticsearch中的各种数据类型,包括text、keyword、数字类型、date、boolean等,并提供了具体示例说明每种类型的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Field datatype

1.  text类型

ES的新版本不再支持string,而是将string分为text和keyword。

text 数据类型被用来索引长文本,比如说电子邮件的主体部分或者一款产品的介绍。这些文本会被分析,在建立索引前会将这些文本进行分词,转化为词的组合,建立索引。允许 ES来检索这些词语。text 数据类型不能用来排序和聚合。

2. keyword类型

keyword类型适用于索引结构化的字段,比如email地址、主机名、状态码和标签。如果字段需要进行过滤(比如查找已发布博客中status属性为published的文章)、排序、聚合。keyword类型的字段只能通过精确值搜索到。

3. 数字类型

                  类型取值范围
long-2^63至2^63-1
integer-2^31至2^31-1
short-32,768至32768
byte-128至127
double64位双精度
float32位单精度
half_float16位半精度
scaled_float缩放类型的的浮点数(比如价格只需要精确到小数点后两位,price为57.34的字段缩放因子为100,存起来就是5734)

其中,使用scaled_float时需要指定缩放因子

"price": {
      "type": "scaled_float",
      "scaling_factor": 100
 }

4. date类型

日期格式可以是以下三种类型:

(1)包含日期格式的字符串:"2015-01-01" or "2015/01/01 12:10:30".

(2)用long表示的毫秒数 milliseconds-since-the-epoch.

(3)用integer表示的秒数

推荐自己设定日期格式,更多的格式可参照(https://www.elastic.co/guide/en/elasticsearch/reference/6.3/mapping-date-format.html

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

5. boolean类型

值为true、false或"true"、"false"

6. binary类型

binary类型接受base64编码的字符串,默认不存储也不可搜索。

7. ip类型

用于存储IPv4或IPv6的地址

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "ip_addr": {
          "type": "ip"
        }
      }
    }
  }
}

PUT my_index/my_type/1
{
  "ip_addr": "192.168.1.1"
}

GET my_index/_search
{
  "query": {
    "term": {
      "ip_addr": "192.168.0.0/16"
    }
  }
}

8. geo point 类型

用于存储地理位置的经纬度

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "location": {
          "type": "geo_point"
        }
      }
    }
  }
}

PUT my_index/my_type/1
{
  "text": "Geo-point as an object",
  "location": { 
    "lat": 41.12,
    "lon": -71.34
  }
}

PUT my_index/my_type/2
{
  "text": "Geo-point as a string",
  "location": "41.12,-71.34" 
}

PUT my_index/my_type/3
{
  "text": "Geo-point as a geohash",
  "location": "drm3btev3e86" 
}

PUT my_index/my_type/4
{
  "text": "Geo-point as an array",
  "location": [ -71.34, 41.12 ] 
}

GET my_index/_search
{
  "query": {
    "geo_bounding_box": { 
      "location": {
        "top_left": {
          "lat": 42,
          "lon": -72
        },
        "bottom_right": {
          "lat": 40,
          "lon": -74
        }
      }
    }
  }
}

 

 

### Spring Boot 中集成 Elasticsearch 进行时间格式化的配置 在 Spring Boot 应用程序中集成了 Elasticsearch 后,对于日期字段的时间格式化可以通过多种方式来实现。一种常见的方式是在实体类中标记 `@Field` 注解,并设置相应的格式属性。 #### 使用注解定义时间格式 当创建映射到 Elasticsearch 文档的 Java 实体时,可以在相应字段上应用 `@Field(type = FieldType.Date, format = DateFormat.dateOptionalTime)` 来指定日期格式[^2]: ```java import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; @Document(indexName = "articles") public class Article { @Id private String id; @Field(type = FieldType.Text) private String title; @Field(type = FieldType.Date, format = DateFormat.date_optional_time) private Date publishDate; // getters and setters... } ``` 上述代码片段展示了如何声明一个名为 `publishDate` 的字段作为带有可选时间部分的标准 ISO8601 日期字符串处理。 #### 自定义 Jackson Serializer/Deserializer 另一种更灵活的方法涉及自定义序列化器和反序列化器以控制 JSON 转换过程中的具体行为。这允许精确调整输入输出格式而不依赖于默认解析逻辑。为了达到这个目的,可以利用 Jackson 提供的功能扩展点——即编写自己的模块并向其中注册新的组件实例[^4]: ```java import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import java.time.format.DateTimeFormatter; @Configuration public class AppConfig { @Bean public ObjectMapper objectMapper() { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(formatter); JavaTimeModule module = new JavaTimeModule(); module.addSerializer(LocalDateTime.class, localDateTimeSerializer); return new ObjectMapper().registerModule(module); } } ``` 此段配置使得应用程序能够按照预设模式(例如:"yyyy-MM-dd HH:mm:ss")读写 `LocalDateTime` 类型的数据项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值