ES基础知识-笔记

elasticsearch-head启动:

npm run start

基本概念

索引
含有相同属性的文档的集合。
索引是由英文小写字母组成,且不含中划线。
类型
索引可以定义一个或多个类型,文档必须属于一个类型。
文档
文档是可以被索引的基本数据单位。
分片
每个索引都有多个分片,每个分片都是一个lucene索引。
备份
拷贝一份分片就完成了分片的备份。

基本用法

api的基本格式

http://<ip>:<port>/<索引>/<类型>/<文档id>

使用put方法创建索引:

PUT http://localhost:9200/people

创建索引的参数:

{
	"settings":{
		"number_of_shards":3,
		"number_of_replicas":1
	},
	"mappings":{
		"man":{
			"properties":{
				"name":{
					"type":"text"
				},
				"country":{
					"type":"keyword"
				},
				"age":{
					"type":"integer"
				},
				"date":{
					"type":"date",
					"format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
				}
			}
		}
	}
}

注意的是elasticsearch6只支持一个索引创建一种类型,不支持一个索引创建多种类型。

插入
指定文档id插入:

PUT  http://localhost:9200/people/man/1
{
	"name":"Robb",
	"country":"wolf",
	"age":19,
	"date":"1987-03-29"
}

返回结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

不指定文档id插入:

POST  http://localhost:9200/people/man/
{
	"name":"Talisha",
	"country":"free",
	"age":18,
	"date":"1989-03-29"
}

返回结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "hDT0q2YB6miiGVIFvpyC",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

修改
直接修改,指定id

POST http://localhost:9200/people/man/1/_update
{
	"doc":{
		"name":"fire"
	}
}

返回结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 3
}

脚本修改:
年龄+10

POST http://localhost:9200/people/man/1/_update
{
	"script":{
		"lang":"painless",
		"inline":"ctx._source.age += 10"
	}
}

返回结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 3
}

在修改部分还可以使用外部参数:

{
	"script":{
		"lang":"painless",
		"inline":"ctx._source.age = params.age",
		"params":{
			"age":19
		}
	}
}

删除

删除索引people中类型为man中id为1的文档。

DELETE http://localhost:9200/people/man/1

返回结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 5,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 4
}

删除索引book

DELETE http://localhost:9200/book

返回结果:

{
    "acknowledged": true
}

查询

简单查询
新建索引:

PUT http://localhost:9200/novel
{
	"mappings":{
		"swords":{
			"properties":{
				"title":{
					"type":"text"
				},
				"author":{
					"type":"keyword"
				},
				"word_count":{
					"type":"integer"	
				},
				"publish_date":{
					"type":"date",
					"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
				}
			}	
		}	
	}
}

插入数据:

PUT http://localhost:9200/novel/swords/11
{
	"title":"七十二变",
	"author":"孙悟空",
	"word_count":1000,
	"publish_date":"2000-10-01"
}

插入结果:
在这里插入图片描述查询全部

POST http://localhost:9200/novel/_search
{
	"query":{
		"match_all":{
			
		}	
	}
}

分页:

{
	"query":{
		"match_all":{
			
		}	
	},
	"from":1,
	"size":2
}

条件查询

聚合查询

猜你喜欢

转载自blog.csdn.net/YaxiongJiang/article/details/82729839