ES索引中的字段类型_7_2

1、ES支持的字段类型

核心类型

类型 备注 说明
string 字符串 text和keyword
numeric 数值 long,integer,short,byte,double,float,half_float,scaled_float
date 日期 date,date_nanos
bool 布尔 boolean
bin 二进制 binary
range 范围 integer_range,float_range,long_range,double_range,date_range,ip_range

复杂类型

类型 备注 说明
object 对象 单个对象的json格式
nested 嵌套 对象数组的json格式

地理类型

类型 备注 说明
geo_point 地理位置 表示经纬度的点
geo_shape 地理图形 表示复杂图形,如多边形

特殊类型

类型 备注 说明
ip IP地址 IPv4或IPv6地址格式
token_count 字符串计数类型 统计字符串类型单词数
join 联合操作 在同一个索引当中定义父子关系
aliase 别名 针对已存在的字段定义别名
flattened 将json对象作为字符串处理

2、数组

ES中没有明确的数组类型,任意字段默认可以包含0个到多个值,不过数组中的值的类型必须保证一致,否则索引时将会报错;
因为以上的原因ES针对数组值不可独立进行查询,如果希望能够对数组中值进行独立查询需要将对应字段类型改成nested;
针对索引动态添加字段并存入数组类型值,字段类型默认会映射为数组第一个值对应的类型;
数组可以包含null值,可以通过配置null_value字段进行替换或者直接跳过,字段值为空数组[]表示字段没有值;

//tags为字符串数组,lists为对象数组
PUT arrays_in_es_index/_doc/1
{
  "message": "The stop analyzer is like the simple analyzer.",
  "tags": [
    "elasticsearch",
    "lucene"
  ],
  "lists": [
    {
      "name": "prog_list",
      "desc": "programming list"
    },
    {
      "name": "baby go",
      "desc": "cool boby"
    }
  ]
}

//tags为字符串,lists为对象
PUT arrays_in_es_index/_doc/2
{
  "message": "The keyword analyzer is a “noop” analyzer.",
  "tags": "elasticsearch",
  "lists": {
    "name": "prog_list",
    "desc": "programming list"
  }
}

//请求参数
GET arrays_in_es_index/_mapping
//结果返回
{
  "arrays_in_es_index" : {
    "mappings" : {
      "properties" : {
        "lists" : {
          "properties" : {
            "desc" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "name" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        },
        "message" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "tags" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}


//请求参数
GET arrays_in_es_index/_search
{
  "query": {
    "match": {
      "tags": "elasticsearch"
    }
  }
}
//结果返回
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.21110919,
    "hits" : [
      {
        "_index" : "arrays_in_es_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.21110919,
        "_source" : {
          "message" : "The keyword analyzer is a “noop” analyzer.",
          "tags" : "elasticsearch",
          "lists" : {
            "name" : "prog_list",
            "desc" : "programming list"
          }
        }
      },
      {
        "_index" : "arrays_in_es_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.160443,
        "_source" : {
          "message" : "The stop analyzer is like the simple analyzer.",
          "tags" : [
            "elasticsearch",
            "lucene"
          ],
          "lists" : [
            {
              "name" : "prog_list",
              "desc" : "programming list"
            },
            {
              "name" : "baby go",
              "desc" : "cool boby"
            }
          ]
        }
      }
    ]
  }
}

3、多字段(multi-fields)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章