elastic 常用查询操作
GET http://127.0.0.1:9200/_cat/health?v 健康状况
GET http://127.0.0.1:9200/_cat/indices?v 查看索引
PUT http://127.0.0.1:9200/test_index 创建test_index索引
DELETE http://127.0.0.1:9200/test_index 删除索引
PUT http://127.0.0.1:9200/index/type/id 新增文档
{
"name":"牙膏",
"price":18
}
POST http://127.0.0.1:9200/index/type 自动生成ID
{
"name":"牙膏",
"price":18
}
GET http://127.0.0.1:9200/index/type/id 查询文档
GET http://127.0.0.1:9200/index/type/id?_source=name, price 查询文档只展示name和价格
PUT http://127.0.0.1:9200/index/type/id 替换文档,带上所有的field
{
"name":"牙膏",
"price”:20
}
PUT http://127.0.0.1:9200/index/type/id?version=1 替换文档,只有版本号等于1才更新
{
"name":"牙膏",
"price”:20
}
PUT http://127.0.0.1:9200/index/type/id?version=1&version_type=extertnal 替换文档,只有版本号大于1才更新
{
"name":"牙膏",
"price”:20
}
POST http://127.0.0.1:9200/index/type/id/_update 修改文档
{
"doc":{
"goodsName": "神红瓶skii护肤肤色B"
}
}
DELETE http://127.0.0.1:9200/index/type/id?pretty 修改文档
GET http://127.0.0.1:9200/index/type/_search 查询所有
{
"took": 2, —耗费的时间毫秒
"timed_out": false, 是否超市
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 60, —总数
"max_score": 1, —相关度匹配分数
"hits": [
{
"_index": "ota_inn",
"_type": "inn",
"_id": "1011_ota",
"_score": 1,
"_source": {
"id": "1011_ota",
"region": "秦皇岛市",
"image": "https://img.jiesuyx.com/test/1539938469365.jpg",
"innName": "超假民宿3",
"tags": [
],
"createTime": 1539938472000,
"searchValue": 1,
"price": null,
"marketPrice": null
}
}
}
}
}
]
}
}
GET 127.0.0.1:9200/ota_inn/inn/_search?q=innName:民宿&sort=createTime:desc 查询名字带有民宿并按时间排序
query dsl
GET 127.0.0.1:9200/ota_inn/inn/_search 查询所有
{
"query":{
"match_all":{}
}
}
GET 127.0.0.1:9200/ota_inn/inn/_search 查询名字带有民宿并按时间排序
{
"query": {
"match": {
"innName": "民宿"
}
},
"sort": [
{
"createTime": "desc"
}
]
}
GET 127.0.0.1:9200/ota_inn/inn/_search 分页查找
{
"query": {
"match_all": {
}
},
"from": 1,
"size": 100
}
GET 127.0.0.1:9200/ota_inn/inn/_search 只展示region和innName
{
"query": {
"match_all": {
}
},
"_source": [
"region",
"innName"
]
}
query filter
GET 127.0.0.1:9200/ota_inn/inn/_search 查询名字带有你好民宿并价格大于0
{
"query":{
"bool":{
"must":{
"match":{
"innName":"你好民宿"
}
},
"filter":{
"range":{
"price":{"gt":0}
}
}
}
}
}
GET 127.0.0.1:9200/ota_inn/inn/_search 完全匹配
{
"query":{
"match_phrase":{
"innName":"超假民宿"
}
}
}
GET 127.0.0.1:9200/ota_inn/inn/_search 高亮显示
{
"query":{
"match_phrase":{
"innName":"超假民宿"
}
},"highlight":{
"fields":{
"innName":{}
}
}
}
Fielddata is disabled on text fields by default 解决办法
PUT 127.0.0.1:9200/ota_goods/_mapping/goods
{
"properties":{
"tags":{
"type":"text",
"fielddata":"true"
}
}
}
GET 127.0.0.1:9200/ota_goods/goods/_search 统计标签下的分类
{
"size":0, —不展现现在数据
"aggs":{
"group_by_tags":{
"terms":{
"field":"tags"
}
}
}
}
GET 127.0.0.1:9200/ota_goods/goods/_search 名字含有蜂蜜下统计标签下的分类
{
"query":{
"match":{
"goodName":"蜂蜜"
}
},
"aggs":{
"group_by_tag":{
"terms":{
"field":"tags"
}
}
},
"size":0
}
GET 127.0.0.1:9200/ota_goods/goods/_search 先分组再算平均价
{
"aggs":{
"group_by_tags":{
"terms":{
"field":"tags"
},
"aggs":{
"avg_price":{
"avg":{
"field":"price"
}
}
}
}
},
"size":0
}
GET 127.0.0.1:9200/ota_goods/goods/_search 先分组再算平均价然后按照平均价排序
{
"aggs":{
"group_by_tags":{
"terms":{
"field":"tags",
"order":{"avg_price":"desc"}
},
"aggs":{
"avg_price":{
"avg":{
"field":"price"
}
}
}
}
}
}
GET 127.0.0.1:9200/ota_goods/goods/_search 按照指定的价格区间进行分组,然后在每组内再按照tag进行分组,计算每组的平均价格
{ "size":0,
"aggs":{
"group_by_price":{
"range":{
"field":"price",
"ranges":[
{
"from":0,
"to":20
},{
"from":20,
"to":4000
}
]
},"aggs":{
"group_by_tags":{
"terms":{
"field":"tags"
},"aggs":{
"avg_price":{
"avg":{
"field":"price"
}
}
}
}
}
}
}
}
基于groovy脚本
内置脚本
POST 127.0.0.1:9200/ota_goods/goods/47/_update 价格加1
{
"script":"ctx._source.price+=1"
}
外部脚本
安装目录/config/scripts
存放脚本 test_add_tags.groovy
脚本内容:ctx._source.tags+=new_tags
POST 127.0.0.1:9200/ota_goods/goods/47/_update 标签增加ha ha
{
"script":{
"lang":"groovy", —语言
"file":"test_add_tags", — 脚本名
"params":{
"new_tag":"ha ha" —参数名:参数值
}
}
}
删除脚本
ctx.op = ctx.source.num == count ? ‘delete’:’none’
POST 127.0.0.1:9200/ota_goods/goods/47/_update num为1的删除否则不操作
{
"script":{
"lang":"groovy",
"file":"test_delete_price",
"params":{
"count":1
}
}
}
POST 127.0.0.1:9200/ota_goods/goods/47/_update 有则更新,没则插入
{
"script":"ctx._source.price+=1",
"upsert":{
"num":0,
"tags":[]
}
}
47.105.105.2:9200/_mget 批量查询
{
"docs":[
{
"_index":"ota_goods",
"_type":"goods",
"_id":"47"
},
{
"_index":"ota_goods",
"_type":"goods",
"_id":"37"
}
]
}
47.105.105.2:9200/ota_goods/_mget 批量查询 index相同
{
"docs":[
{
"_type":"goods",
"_id":"47"
},
{
"_type":"goods",
"_id":"36"
}
]
}
47.105.105.2:9200/ota_goods/goods/_mget 批量查询 index和type都一样
{
"ids":["47","36"]
}
POST 47.105.105.2:9200/_bulk 批量操作,每个操作一行,不能换行 中间步骤有错继续执行下一条
{"delete":{ "_index":"ota_goods","_type":"goods","_id":"47_update"}} 删除
{"create":{"_index":"ota_goods","_type":"goods","_id”:”2”}} 增加文档,设置价格
{"price":199}
{"index":{"_index":"ota_goods","_type":"goods","_id":2}} 替换或创建
{"price":299}
{"update":{"_index":"ota_goods","_type":"goods","_id":2}} 更新操作
{"doc":{"price":399}}
GET 47.105.105.2:9200/ota_goods/_mapping/goods 查看mapping
测试mapping
GET 47.105.105.2:9200/ota_goods/_analyze
Term 不分词 必须包含test hello
Term 不分词 必须包含search full_text nodal
仅filter
快速定位不合法
GET 47.105.105.2:9200/ota_goods/goods/_search?scroll=1m scroll 分页查询
{
"query":{
"match_all":{}
},
"sort":["_doc"],
"size":3
}
返回结果
{
"_scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAAdPFnJzUEFqTFdzU2hTYTZDN3hTbWd2ZmcAAAAAAAAHUxZyc1BBakxXc1NoU2E2Qzd4U21ndmZnAAAAAAAAB1EWcnNQQWpMV3NTaFNhNkM3eFNtZ3ZmZwAAAAAAAAdSFnJzUEFqTFdzU2hTYTZDN3hTbWd2ZmcAAAAAAAAHUBZyc1BBakxXc1NoU2E2Qzd4U21ndmZn",
"took": 3,
"timed_out": false,
"_shards": {
"total": 5。。。。
}
下一次查询 自动会记录上次查询的相关信息
47.105.105.2:9200/_search/scroll
{
"scroll":"1m",
"scroll_id":"DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAAeuFnJzUEFqTFdzU2hTYTZDN3hTbWd2ZmcAAAAAAAAHsRZyc1BBakxXc1NoU2E2Qzd4U21ndmZnAAAAAAAAB68WcnNQQWpMV3NTaFNhNkM3eFNtZ3ZmZwAAAAAAAAeyFnJzUEFqTFdzU2hTYTZDN3hTbWd2ZmcAAAAAAAAHsBZyc1BBakxXc1NoU2E2Qzd4U21ndmZn"
}
创建索引
PUT 47.105.105.2:9200/my_index
{
"settings":{
"number_of_shards":1,
"number_of_replicas":0
},
"mappings":{
"my_type":{
"properties":{
"my_field":{
"type":"text"
}
}
}
}
}
修改索引
PUT 47.105.105.2:9200/my_index/_settings
{
"number_of_replicas":1
}
PUT 127.0.0.1:9200/my_index/_alias/goods_index 给my_index起别名
搜索发帖日期在一个月之后的