ElasticSearch

Elasticsearch第9节 mapping 及数据类型

2019-06-15  本文已影响0人  小超_8b2f

索引的mapping

mapping类似于ddl,创建索引的时候,可以预先指定字段的类型及相关属性。

#索引名/_mapping
GET /lib1/_mapping
{
  "lib1" : {
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "doc" : {
          "properties" : {
            "sex" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "sex" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

1. 核心数据类型

String(text、keyword)
text:
text类型被用来索引长文本,在建立索引前会对这些文本进行分词,转化为词的组合,建立索引。允许es来检索这些词语。text类型不能用来排序和聚合。

keyword:
不需要进行分词。可以被原来检索过滤,排序和聚合。keyword类型字段只能用本身来进行检索。

数字类型:long、integer、short、byte、double、float
日期类型:date
布尔类型:boolean
二进制: binary


2. 复杂数据类型

对象类型(object datatype):_ object _ 用于单个json对象
嵌套类型(nested datatype):_ nested _ 用于对象数组


3. 地理位置类型(Geo datatypes)

地理位置类型(Geo-point datatype):_ geo-point _ 用于经纬度坐标
地理位置类型(Geo-shape datatype):_ geo-shape _ 用于类似多边形的复杂形状


1. 创建不同的基本类型字段文档
#老版本在索引后还有一级type
#POST /myindex/article/1
POST /myindex/_doc/article1
{
  "post_date":"2019-05-10",  //注意,日期只能是年-月-日,不能有时分秒
  "tile":"java",
  "content":"java is the best language",
  "id":119,
  "test":["hello","world"]
}
POST /myindex/_doc/article2
{
  "post_date":"2019-06-10",  //注意,日期只能是年-月-日,不能有时分秒
  "tile":"html",
  "content":"I like html",
  "id":120,
  "test":["html","good"]
}
POST /myindex/_doc/article3
{
  "post_date":"2019-06-15",  //注意,日期只能是年-月-日,不能有时分秒
  "tile":"es",
  "content":"es is a new tool",
  "id":121,
  "test":["elasticsearch","very good"]
}
2. 通过映射 _mapping 查看数据类型
#老版本: GET /索引/type/_mapping
GET /myindex/_mapping
{
  "myindex" : {
    "mappings" : {
      "properties" : {
        "content" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "id" : {
          "type" : "long"
        },
        "post_date" : {
          "type" : "date"
        },
        "test" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "tile" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}
3. 通过 _search 查看索引下的所有文档
//
#老版本: GET /索引/type/_search
GET /myindex/_search
{
  "took" : 4,  #查询耗时4毫秒
  "timed_out" : false,  # 是否超时
  "_shards" : {
    "total" : 1,    #分片数
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "myindex",
        "_type" : "_doc",
        "_id" : "article1",
        "_score" : 1.0,    # 查出来的文档和查询条件的匹配度
        "_source" : {
          "post_date" : "2019-05-10",
          "tile" : "java",
          "content" : "java is the best language",
          "id" : 119,
          "test" : [
            "hello",
            "world"
          ]
        }
      },
      {
        "_index" : "myindex",
        "_type" : "_doc",
        "_id" : "article2",
        "_score" : 1.0,  # 查出来的文档和查询条件的匹配度
        "_source" : {
          "post_date" : "2019-06-10",
          "tile" : "html",
          "content" : "I like html",
          "id" : 120,
          "test" : [
            "html",
            "good"
          ]
        }
      },
      {
        "_index" : "myindex",
        "_type" : "_doc",
        "_id" : "article3",
        "_score" : 1.0,  # 查出来的文档和查询条件的匹配度
        "_source" : {
          "post_date" : "2019-06-15",
          "tile" : "es",
          "content" : "es is a new tool",
          "id" : 121,
          "test" : [
            "elasticsearch",
            "very good"
          ]
        }
      }
    ]
  }
}
4. 按条件检索:_search?q=column:val
//按条件检索
#查不到数据,因为没有对日期类型数据进行分词
GET /myindex/_search?q=post_date:2019
#能查出数据
GET /myindex/_search?q=post_date:2019-06-15
#能查出数据,因为content是text类型,有进行分词,排序
GET /myindex/_search?q=content:html&sort=post_date:desc
5. object类型及底层存储格式
//
PUT /lib5/_doc/person1
{
  "name":"Tom",
  "age":25,
  "birthday":"1985-12-12",
  "address":{
    "country":"china",
    "province":"guangdong",
    "city":"shenzhen"
  }
}
GET /lib5/_mapping
#底层存储格式
{
  "name":["Tom"],
  "age":[25],
  "birthday":["1985-12-12"],
  "address.country":["china"],
  "address.province":["guangdong"],
  "address.city":["shenzhen"]
  }
}
PUT /lib5/_doc/persons1
{
  "persons":[
      {"name":"lisi","age":27},
      {"name":"wangwu","age":26},
      {"name":"zhaoliu","age":23}
    ]
}
GET /lib5/_mapping
#底层存储
{
  "persons.name":["lisi","wangwu","zhaoliu"],
  "persons.age"[27,26,23]
}
6. 手动创建mapping
//手动创建mapping
DELETE lib5
#手动创建mapping
PUT /lib5
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "age" : {"type" : "long"},
      "birthday" : {"type" : "date","index": false},  #不希望建立倒排索引
      "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "content":{"type":"text","analyzer": "standard"},
        "price":{"type": "double"},
        "number":{"type": "integer"}
    }
  }
}
GET /lib5/_mapping
上一篇 下一篇

猜你喜欢

热点阅读