es基本查询(2)

本文介绍了Elasticsearch的基本概念,包括结构化和非结构化数据存储、索引与文档操作、正向和倒排索引的区别,以及RESTful风格的API接口。内容涵盖了索引创建、文档增删改查、查询方法(如match、match_phrase、bool查询)、统计分析等关键知识点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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或者ID1004,同时价格不为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

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值