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)

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