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起別名
搜索發帖日期在一個月之後的
elastic 常用查詢操作