Elasticsearch 7.x 深入【1】索引【二】创建

2020-04-03  本文已影响0人  孙瑞锴

1. 借鉴

极客时间 阮一鸣老师的Elasticsearch核心技术与实战
官方文档 creating_an_index
官方文档 mapping
官方文档 multi-fields

2. 开始

创建索引

在前面,我们知道了什么是es中的索引,接下来我们就来创建一个索引

方式1:通过索引一篇文档创建一个新索引

PUT /hotel/_doc/1
{
  "name": "测试酒店1",
  "grade": 2.5,
  "address": "北京市"
}

突然我想加为这篇文档添加/删除/修改一个属性怎么办呢?好的,多问几个为什么,我们会在后续讲,我会把连接粘贴到这里 // TODO

PUT /hotel/_doc/2
{
  "name": "测试酒店2",
  "grade": 3,
  "address": "上海市"
}

GET /hotel/_mapping

{
  "hotel" : {
    "mappings" : {
      "properties" : {
        "address" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "grade" : {
          "type" : "float"
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}
PUT /hotel_multi_fields
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "ik_smart",
        "fields": {
          "pinyin": {
            "type": "text",
            "analyzer": "pinyin"
          }
        }
      }
    }
  }
}
# 添加文档
# googledjd
PUT /hotel_multi_fields/_doc/1
{
  "name": "google大酒店"
}

PUT /hotel_multi_fields/_doc/2
{
  "name": "微软大酒店"
}

# 查询
# 使用name属性查询
GET /hotel_multi_fields/_search
{
  "query": {
    "term": {
      "name": {
        "value": "google"
      }
    }
  }
}

# 使用name.pinyin属性来查询
GET /hotel_multi_fields/_search
{
  "query": {
    "term": {
      "name.pinyin": {
        "value": "wrdjd"
      }
    }
  }
}

全字段的解析会在后面的章节有详解。额,好像走远了。。。

方式2:通过指定mappings来创建一个新索引

# 先删除已有索引
DELETE /hotel

# 创建hotel索引,并索引id为1的文档[这种就是方式1,一气呵成对吧]
PUT /hotel/_doc/1
{
  "name": "测试酒店1",
  "grade": 2.5,
  "address": "北京市",
  "distance": "25.0"
}

# 我们来看看它的索引结构
GET /hotel/_mapping
{
  "hotel" : {
    "mappings" : {
      "properties" : {
        "address" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "distance" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "grade" : {
          "type" : "float"
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}
# 先删除已有索引
DELETE /hotel

# 指定索引结构
PUT /hotel
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "grade": {
        "type": "double"
      },
      "address": {
        "type": "text"
      },
      "distance": {
        "type": "double"
      }
    }
  }
}

# 再次插入相同的数据
PUT /hotel/_doc/1
{
  "name": "测试酒店1",
  "grade": 2.5,
  "address": "北京市",
  "distance": "25.0"
}

# 看看它的索引结构
GET /hotel/_mapping
{
  "hotel" : {
    "mappings" : {
      "properties" : {
        "address" : {
          "type" : "text"
        },
        "distance" : {
          "type" : "double"
        },
        "grade" : {
          "type" : "double"
        },
        "name" : {
          "type" : "text"
        }
      }
    }
  }
}
# 看下数据
GET /hotel/_search
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "测试酒店1",
          "grade" : 2.5,
          "address" : "北京市",
          "distance" : "25.0"
        }
      }
    ]
  }
}
PUT /hotel/_doc/2
{
  "name": "测试酒店1",
  "grade": 2.5,
  "address": "北京市",
  "distance": "啊哈哈"
}
{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "failed to parse field [distance] of type [double] in document with id '2'. Preview of field's value: '啊哈哈'"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "failed to parse field [distance] of type [double] in document with id '2'. Preview of field's value: '啊哈哈'",
    "caused_by": {
      "type": "number_format_exception",
      "reason": "For input string: \"啊哈哈\""
    }
  },
  "status": 400
}

更多的时候,我们需要确定索引的结构,而非让es自动匹配,同时以便在我们设置错误的类型时es也能给我提示。

PUT /索引的名字
{
  "mappings": {
    "properties": {
      "xxx1属性": {
        "type": "该属性的类型"
      },
     "xxx2属性": {
        "type": "该属性的类型"
      }
    }
  }
}

比如我们创建一个person的索引

PUT /person
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "age": {
        "type": "integer"
      },
      "sex": {
        "type": "boolean"
      }
    }
  }
}

除了以上简单的属性,我们还可参看官方文档有关mapping的更多属性(连接参看借鉴部分 [官方文档 mapping])

3. 大功告成

上一篇下一篇

猜你喜欢

热点阅读