Elasticsearch 基本操作 DSL高級查詢 進階查詢
#解析案列demo
POST _analyze
{
"analyzer": "standard",
"text": "我是中國人"
}
#查詢所有索引
GET _search
{
"query": {
"match_all": {}
}
}
#創建索引
PUT /my_name/
#查詢所有索引
GET /_cat/indices?v
#查詢單個索引
GET /my_name
#刪除索引
DELETE /my_name
#創建文檔
PUT /my_index/_doc/1
{
"title":"小米手機",
"category":"北京",
"price":9999
}
#查看文檔
GET /my_index/_doc/1
#修改文檔
PUT /my_index/_doc/1
{
"title":"華為手機",
"category":"北京公司",
"price":99990.0
}
#修改局部屬性
POST /my_index/_update/1
{
"doc":{
"price":88888
}
}
POST /my_index/_update/1
{
"doc":{
"price":4232
}
}
#刪除文檔
DELETE /my_index/_doc/1
#批量添加
POST _bulk
{"create":{"_index":"my_index","_id":1}}
{"title":"小米","price":999}
{"create":{"_index":"my_hua","_id":2}}
{"title":"華為","price":8888}
GET /my_index/_doc/1
GET /my_hua/_doc/2
#批量刪除
POST _bulk
{"delete":{"_index":"my_index","_id":1}}
{"delete":{"_index":"my_hua","_id":2}}
#查看映射
GET /my_index/_mapping
#動態映射
#靜態映射
PUT /my_index
{
"mappings": {
"properties": {
"title":{
"type": "text",
"index": true,
"store": true,
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"category":{
"type": "keyword",
"index": true,
"store": true
},
"image":{
"type": "keyword",
"index": true,
"store": true
},
"price":{
"type": "long",
"index": true,
"store": true
}
}
}
}
#查看創建的映射
GET /my_index/_mapping
#刪除索引
DELETE /my_index
#DSL高級查詢
POST _bulk
{"create":{"_index":"my_index","_id":1}}
{"id":1,"title":"華為筆記本電腦","category":"華為","images":"http://www.gulixueyuan.com/xm.jpg","price":5388}
{"create":{"_index":"my_index","_id":2}}
{"id":2,"title":"華為手機","category":"華為","images":"http://www.gulixueyuan.com/xm.jpg","price":5500}
{"create":{"_index":"my_index","_id":3}}
{"id":3,"title":"VIVO手機","category":"vivo","images":"http://www.gulixueyuan.com/xm.jpg","price":3600}
GET /my_index/_doc/3
#分析倒排索引
POST _analyze
{
"analyzer": "ik_smart",
"text": "華為筆記本電腦"
}
POST _analyze
{
"analyzer": "ik_smart",
"text": "華為手機"
}
POST _analyze
{
"analyzer": "ik_smart",
"text": "VIVO手機"
}
#華為 筆記本電腦
#華為 手機
#vivo 手機
#查詢所有文檔
GET /my_index/_search
{
"query": {
"match_all": {}
}
}
#匹配查詢
GET /my_index/_search
{
"query": {
"match": {
"title": "華為智能手機"
}
}
}
#條件刪除
POST /my_index/_delete_by_query
{
"query":{
"match":{
"title":"vivo"
}
}
}
#多字段匹配
#multi_match
POST /my_index/_search
{
"query": {
"multi_match": {
"query":"華為智能手機",
"fields":["title","category"]
}
}
}
#前綴匹配
POST /my_index/_search
{
"query": {
"prefix": {
"category": {
"value": "華為"
}
}
}
}
#關鍵字精確查詢
#term關鍵字不會進行分詞
POST /my_index/_search
{
"query": {
"term": {
"title": {
"value": "華為筆記本電腦"
}
}
}
}
#多關鍵字查詢
POST /my_index/_search
{
"query": {
"terms": {
"title": [
"華為",
"華為手機"
]
}
}
}
#範圍查詢
POST /my_index/_search
{
"query": {
"range": {
"price": {
"gte": 3000,
"lte": 6000
}
}
}
}
#指定返回字段查詢
POST /my_index/_search
{
"query": {
"range": {
"price": {
"gte": 1000,
"lte": 20000
}
}
},
"_source": ["title","category","price"]
}
#組合查詢
#must
POST /my_index/_search
{
"query": {
"bool": {
"must": [
{"match": {
"title": "華為"
}},
{
"range": {
"price": {
"gte": 5500,
"lte": 6000
}
}
}
]
}
}
}
#組合查詢
#should
POST /my_index/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "華為"
}
},
{
"range": {
"price": {
"gte": 1000,
"lte": 5000
}
}
}
]
}
}
}
#組合查詢
#should and must
POST /my_index/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "華為"
}
},
{
"range": {
"price": {
"gte": 100,
"lte": 20000
}
}
}
],
"must": [
{
"match": {
"title": "華為"
}
},
{
"range": {
"price": {
"gte": 5500,
"lte": 6000
}
}
}
]
}
}
}
#組合查詢
#must_not
POST /my_index/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"title": "華為"
}
}
]
}
}
}
#組合查詢
#filter
POST /my_index/_search
{
"query": {
"bool": {
"filter": [
{
"match":{
"title":"華為"
}
}
]
}
}
}
#聚合查詢
#max
POST /my_index/_search
{
"query": {
"match_all": {}
},
"size": 0,
"from": 0,
"aggs": {
"max_price": {
"max": {
"field": "price"
}
}
}
}
#min
POST /my_index/_search
{
"query": {
"match_all": {}
},
"size": 0,
"from": 0,
"aggs": {
"min_price": {
"min": {
"field": "price"
}
}
}
}
#avg
POST /my_index/_search
{
"query": {
"match_all": {}
},
"size": 0,
"from": 0,
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
#sum
POST /my_index/_search
{
"query": {
"match_all": {}
},
"size": 0,
"from": 0,
"aggs": {
"sum_price": {
"sum": {
"field": "price"
}
}
}
}
#stats
POST /my_index/_search
{
"query": {
"match_all": {}
},
"size": 0,
"from": 0,
"aggs": {
"stats_price": {
"stats": {
"field": "price"
}
}
}
}
#terms 桶聚合相當於group by
POST /my_index/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"groupby_category": {
"terms": {
"field": "category",
"size": 10
}
}
}
}
#進階查詢
POST /my_index/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
#分頁查詢
POST /my_index/_search
{
"query": {
"match_all": {}
},
"from": 1,
"size": 2
}
#高亮查詢
POST /my_index/_search
{
"query": {
"match_all": {}
},
"highlight": {
"pre_tags": "<b style='color:red'>",
"post_tags": "</b>",
"fields": {
"title": {}
}
}
}
#近似查詢
PUT /test
PUT /test/_doc/1
{
"title":"hello world"
}
#fuzzy查詢
GET /test/_search
{
"query": {
"fuzzy": {
"title": {
"value": "word"
}
}
}
}
本文章為轉載內容,我們尊重原作者對文章享有的著作權。如有內容錯誤或侵權問題,歡迎原作者聯繫我們進行內容更正或刪除文章。