【Elasticsearch】之 创建索引(Index)

一、简介


Elasticsearch 是一个实时分布式搜索和分析引擎

Elasticsearch中,每一个字段的数据都是默认被索引的

也就是说,每个字段专门有一个反向索引用于快速检索

节点 说明
index 文档存储的地方
type 文档代表的对象的类
id 文档的唯一标识

Tips:Elasticsearch 7.xtype

数据被存储在分片(shards)中,索引只是一个把一个或多个分片分组在一起的逻辑空间



二、创建索引


(0)HTTP

直接通过 HTTP 访问

PUT /people
{
   "settings" : {
      "number_of_shards" : 3,
      "number_of_replicas" : 1
   },
   "mappings": {
		"man": {
			"dynamic": "strict",
			"properties": {
				"name": {
					"type": "test"
				},
				"age": {
					"type": "integer"
				},
				"birthday": {
					"type": "date",
					"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
				},
				"address": {
					"dynamic": "true",
					"type": "object"
				}
			}
		}
	}
}

"dynamic": "strict" 说明:
表示:如果有新字段加入则抛出异常,
“true”: 动态添加新的字段–缺省(默认)
“false”: 忽略新的字段(不改变_source的字段内容,新的字段不会被加到映射中也不可搜索)

集群的健康状态 yellow 表示:

  1. 所有的主分片(primary shards)启动并且正常运行了
  2. 复制分片(replica shards)还没有全部可用,状态为:unassigned

即,单节点:
在同一个节点上保存相同的数据副本是没有必要的,如果这个节点故障了,那所有的数据副本也会丢失

扫描二维码关注公众号,回复: 8769178 查看本文章

(1)索引模板

场景:
根据天、月,自动创建索引(index)
上传 document 时候,若没有对应的 index,则会自动创建


1. 编写模板

elasicsearch 版本 6.x 以上

  1. 创建模板
PUT http://localhost:9200/_template/template_1
{
    "index_patterns" : "temp_nba_*",
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "_doc" : {
            "properties": {
            "contry": { "type" : "keyword" },
            "jerse_no": {"type": "keyword"},
            "name": {"type": "text"},
            "play_year": {"type": "keyword"},
            "position": {"type": "keyword"},
            "team_name": {"type":"text"}
            }
        }
    }
}
  1. 删除模板
    DELETE http://localhost:9200/_template/template_1

  2. 查看模板
    GET http://localhost:9200/_template/template_1


2. python 脚本

这里采用编写模板的方式来

python安装 Elasticsearch 模块:

pip install elasticsearch

# -*- coding: utf-8 -*-

import json
import argparse
from elasticsearch import Elasticsearch

def parse():
    parser = argparse.ArgumentParser(description="create es index template...")

    parser.add_argument('--host', help='input es hosts', required=True)

    parser.add_argument('--file', help='input es template json file', required=True)

    return parser.parse_args()


def create_es_client(hosts):

    es = Elasticsearch(hosts)

    return es


def create_indexs_template(es, templates):

    for template in templates:

        create_template(es, template)


def read_json(file_name):

    with open(file_name, 'r') as f:
        data = json.load(f)
        return data

def create_template(es, template):

    # 现只能单模板,不支持数组
    index_pattern = template["index_patterns"]

    template_name = index_pattern[:-1] + "template"

    print ('create index template : %s ' % template_name)

    try:

        result = es.indices.put_template(name = template_name, body = template)

        print ('create index template : %s finished' % template_name)

    except BaseException:

        print ('Error! create index template  : %s error !' % template_name)



if __name__ == '__main__':

    args = parse()

    templates = read_json(args.file)

    es = create_es_client(args.host)

    create_indexs_template(es, templates)

对应模板配置:

[
    {
        "index_patterns": "temp_nba_*",
        "settings": {
            "number_of_shards": 1,
            "number_of_replicas": 1
        },
        "mappings": {
            "_doc": {
                "properties": {
                    "contry": { "type" : "keyword" },
                    "jerse_no": {"type": "keyword"},
                    "name": {"type": "text"},
                    "play_year": {"type": "keyword"},
                    "position": {"type": "keyword"},
                    "team_name": {"type":"text"}
                 }
            }
        }
    }
]

可以命令来运行:
python create_es_template.py --file template.json --host localhost:9200


3. 问题

集群环境下可能出现的问题:
https://elasticsearch.cn/question/4394
https://www.jianshu.com/p/aa7792b519a2

发布了404 篇原创文章 · 获赞 270 · 访问量 42万+

猜你喜欢

转载自blog.csdn.net/fanfan4569/article/details/103201540