es基本概念
结构化数据:就是二维表格 如mysql oracle
非结构化数据 :图视频excel表格,键值对数据等 存储用redis,mangodb,hbase等
es和sol都是基于lucene,lucene使用复杂,es更简便且基于java开发
正向索引:就是mysql的主键索引
倒排索引:内容分词作为key与主键id关联,与正向索引相反
与mysql类比概念
index索引相当于数据库
type相当于表 es7以上index索引只能有1个type
document文档就是行记录
field就是column列
restful风格 :url+method,put是幂等的,post不是
基础用法
result类型:fund,not_fund,created,updated,deleted
索引操作
PUT http://{{xuniji}}/student 创建索引
GET http://{{xuniji}}/student 查询索引
DELETE http://{{xuniji}}/student 删除索引
GET http://{{xuniji}}/_cat/indices?v 查询所有索引,v显示表头
文档操作-基本新增创建修改删除
POST http://{{xuniji}}/student/_doc 默认会创建新的数据,id自动随机生成
{
"name": "张三",
"age": 18,
"phone": "xxxxavc",
"height": 175.2
}
POST http://{{xuniji}}/student/_doc/123 指定id=123创建或者修改数据,当有指定id时,可以使用PUT代替POST
{
"name": "张三",
"age": 18,
"phone": "xxxxavc",
"height": 175.2
}
GET http://{{xuniji}}/student/_doc/56 根据id=56主键查询
GET http://{{xuniji}}/student/_search 查询索引下所有数据
{
}
POST http://{{xuniji}}/student/_doc/123 指定id=123全量数据更新,或者用PUT
{
"name": "张三1",
"age": 19,
"phone": "yyyxxxxavc",
"height": 185.2
}
POST http://{{xuniji}}/student/_update/123 定id=123 局部字段修改,新增局部字段
{
"doc":{
"name":"lisi111",
"cc":"cc"
}
}
文档操作-其它查询
match 是分词器分词,倒排索引匹配
math_phrase 完全匹配
GET http://{{xuniji}}/student/_search
##匹配字段查询
{
"query": {
"match": {
"cc": "cc1"
}
}
}
##查询所有
{
"query": {
"match_all": {
}
}
}
##匹配字段查询+多字段排序+分页+过滤展示字段
{
"query": {
"match_all": {}
},
"sort": {
"age": {
"order": "desc"
},
"height": {
"order": "desc"
}
},
"from": 4,
"size": 2,
"_source":["age","name","height"]
}
##完全匹配+高亮字段
{
"query": {
"match_phrase": {
"name": "张三1"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
bool可以组合多条件查询
{"bool" : {"must":[],"should":[],"must_not":[] } }
must:必须满足的条件 (相当于and)
should:可以满足也可以不满足的条件 (相当于or)
must_not:不需要满足的条件 (相当于not)
gt:>
lt:<
gte:>=
lte:<=
GET /lib4/items/_search #满足价格是25或者ID是1004,同时价格不为30
{
"query": {
"bool":{
"should": [
{"term":{"price": 25}},
{"term":{"itemID": "id1004"}}
],
"must_not": [
{"term":{"price": 30}}
]
}
}
}
GET /lib4/items/_search #range表示取一定范围的数据
{
"query": {
"bool":{
"filter": {
"range": {
"price": {
"gt": 25,
"lte": 50
}
}
}
}
}
}
统计操作
GET http://{{xuniji}}/student/_search
## 分组,且不要原始数据
{
"aggs":{
"fenzu":{
"terms":{
"field":"age"
}
}
},
"size":0
}
## 求平均值
{
"aggs":{
"fenzu":{
"avg":{
"field":"age"
}
}
},
"size":0
}