ElasticSearch基础语法

索引的创建,更新,删除

  • 使用非结构化方式新建索引
// 创建索引
PUT /emloyee
{
	"settings": {
		"numbers_of_shards": 1,
		"numbers_of_replicas": 1
	}
}
PUT /employee/_doc/1
{
	"name": "张三",
	"age": 30
}

// 执行结果
{
	"_index": "employee", // 索引名
	"_type": "_doc", // 类型,ES7废弃
	"_id": "1", // document主键
	"_version": 1,
	"result": "created", // 执行结果
	"_shards": {
		"total": 2,
		"successful": 2,
		"failed": 0
	},
	"_seq_no": 0,
	"_primary_term": 1
}

// 更新索引
PUT /employee/_doc/1
{
	"name": "张三2",
	"age": 30
}

// 执行结果
{
	"_index": "employee", // 索引名
	"_type": "_doc", // 类型,ES7废弃
	"_id": "1", // document主键
	"_version": 2,
	"result": "updated", // 执行结果
	"_shards": {
		"total": 2,
		"successful": 2,
		"failed": 0
	},
	"_seq_no": 2,
	"_primary_term": 1
}

// 获取索引记录
GET /employee/_doc/1

// 执行结果
{
	"_index": "employee", // 索引名
	"_type": "_doc", // 类型,ES7废弃
	"_id": "1", // document主键
	"_version": 2,
	"_seq_no": 2,
	"_primary_term": 1,
	"found": true,
	"_sourse": {
		"name": "张三2",
		"age": 30
	}
}

// 指定字段修改(只修改需要更新的字段,不会丢失其他未写出的字段)
POST /employee/_update/1
{
	"doc":{
		"name": "张三三"
	}
}

// 强制指定创建,若已存在,则创建失败
POST /employee/_create/2
{
	"name": "123",
	"age": 30
}

// 删除某个文档
DELETE /employee/_doc/2

// 查询全部文档(有默认分页值)
GET /employee/_search
  • 使用结构化方式新建索引
PUT /emloyee
{
	"settings": {
		"numbers_of_shards": 1,
		"numbers_of_replicas": 1
	},
	"mappings":{
		"properties": {
			"name": {"type": "text"},
			"age": {"type": "integer"}
		}
	}
}

POST /employee/_create/2
{
	"name": "123",
	"age": "字符串测试" // 会报错,只能是integer类型
	"gender": "男" // 不会报错,会新生成一个字段
}

索引简单查询



// 不带条件查询所有记录
GET /employee/_search
{
	"query": {
		"match_all": {}
	}
}

// 分页查询
GET /employee/_search
{
	"query": {
		"match_all": {}
	},
	"from": 0,
	"to": 1
}

// 带关键字条件的查询
GET /employee/_search
{
	"query": {
		"match": {
			"name": "兄弟"
		}
	}
}

// 排序查询
GET /employee/_search
{
	"query": {
		"match": {
			"name": "兄弟"
		}
	},
	"sort": [ // 有排序的时候,_score命中打分字段就没有意义了,会返回null
		{"age": {"order": "desc"}}
	]
}

// filter过滤查询
GET /employee/_search
{
	"query": {
		"bool": {
			"filter": [ // 一旦使用filter,并不会计算_score打分的匹配度,默认0.0
				{"term": {"name": "张"}} // 表示name中有“张”的
				// {"match": {"name":"张333"}} // match会对“张333”进行analyze分词操作,能过滤出含有“张”的记录,如果用term,不会进行分词,只能过滤出包含“张333”的记录
			]
		}
	}
}

// 带聚合操作
GET /employee/_search
{
	"query": {
		"match": {
			"name": "兄"
		}
	},
	"sort": [
		{"age": {"order": "desc"}}
	],
	"aggs": {
		"group_by_age": {
			"terms": {
				"field": "age"
			}
		}
	}
}

analyze分析过程

  • 举例:
// 先插入一条数据
PUT /movie/_doc
{
	"name": "Eating an apple a day"
}

// 使用analyze api查看分词状态
GET /movie/_analyze
{
	"field": "name",
	"text": "Eating an apple a day"
}

  • analyze分析过程:字符过滤器 -> 字符处理(标准字符处理,以空格和标点符号分割内容) -> 分词过滤(分词转换,即变小写)
// 使用结构化的方式重新创建索引
PUT /movie
{
	"settings": {
		"numbers_of_shards": 1,
		"numbers_of_replicas": 1
	},
	"mappings":{
		"properties": {
			"name": {"type": "text", "analyzer": "english"} // 使用english做一个分词
		}
	}
}

PUT /movie/_doc
{
	"name": "Eating an apple a day"
}

// 然后再使用analyze api查看分词状态
  • english analyze分析过程:字符过滤器(过滤特殊符号,外加量词,the等等) -> 字符处理(标准字符处理,以空格和标点符号分割内容) -> 分词过滤(分词转换,词干转化,比如去除复数(apples/apple -> appl,daies/day -> dai))

类型

  • Text:被分析索引的字符串类型
  • Keyword:不能被分析只能被精确匹配的字符串类型
  • Date:日期类型,可以配合format一起使用
  • 数字类型:long,integer,short,double等
  • boolean:true/false
  • 数组类型:Array:[“one”, “two”]
  • Object类型:json嵌套
  • Ip类型
  • Geo_point:地理位置
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

✦昨夜星辰✦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值