- query string search
GET /ecommerce/product/_search //查詢所有數據 { "took": 4,//耗費幾毫秒 "timed_out": false,//是否超時 "_shards": {//數據拆分成5個分片,對所有請求都會打到所有primary shared(或者是它的某個replica shared也可以) "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1,//條數 "max_score": 1,//查詢匹配度 "hits": [ { "_index": "ecommerce", "_type": "product", "_id": "2", "_score": 1, "_source": { "name": "jiajieshi yagao", "desc": "jiajieshi meibai", "price": 30, "producer": "jiajieshi producer", "tags": [ "meibai", "fangzhu" ] } } ] } }
GET /ecommerce/product/_search?q=name:yagao&sort=price:desc
- query DSL
- DSL:Domain Specified Language:特定領域的語言
- http request body:請求體,用json格式構建查詢語法
GET /ecommerce/product/_search { "query": { "match": { "name": "yagao" //查詢包含單詞 } }, "_source": ["name","price"],//不寫查詢所有字段 "sort": [ { "price": { "order": "desc"//倒序排序 } } ], "from": 0,//分頁 "size": 1 }
- query filter
GET /ecommerce/product/_search { "query": { "bool": {//多個條件 "must": [ { "match": { "name": "yagao" } } ], "filter": {//過濾條件 "range": { "price": { "gte": 10, "lte": 40 } } } } } }
- full-text search
GET /ecommerce/product/_search { "query": { "match": { "producer": "lishi producer"//會查包含這兩個單詞的所有數據 } } } { "took": 5, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 0.51623213, "hits": [ { "_index": "ecommerce", "_type": "product", "_id": "3", "_score": 0.51623213,//匹配度最高 "_source": { "name": "lishi yagao", "desc": "lishi meibai", "price": 50, "producer": "lishi producer", "tags": [ "meibai", "fangzhu" ] } }, { "_index": "ecommerce", "_type": "product", "_id": "1", "_score": 0.25811607, "_source": { "name": "jiaqiangban gaolujie yagao2", "desc": "gaoxiao meibai", "price": 30, "producer": "gaolujie producer", "tags": [ "meibai", "fangzhu" ] } }, { "_index": "ecommerce", "_type": "product", "_id": "2", "_score": 0.1805489, "_source": { "name": "jiajieshi yagao", "desc": "jiajieshi meibai", "price": 40, "producer": "jiajieshi producer", "tags": [ "meibai", "fangzhu" ] } }, { "_index": "ecommerce", "_type": "product", "_id": "4", "_score": 0.14638957, "_source": { "name": "special yaogao", "desc": "special meibai", "price": 50, "producer": "special yagao producer", "tags": [ "meibai" ] } } ] } }
- phrase search
必須包含一模一樣的串,才會返回(包含短語的意思)
GET /ecommerce/product/_search { "query": { "match_phrase": { "producer": "yagao producer" } } } { "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.70293105, "hits": [ { "_index": "ecommerce", "_type": "product", "_id": "4", "_score": 0.70293105, "_source": { "name": "special yaogao", "desc": "special meibai", "price": 50, "producer": "special yagao producer", "tags": [ "meibai" ] } } ] } }
- highlight search
- 查詢到的結果高亮
GET /ecommerce/product/_search { "query": { "match_phrase": { "producer": "gaolujie producer" } }, "highlight": { "fields": { "producer": {} } } } { "took": 18, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.51623213, "hits": [ { "_index": "ecommerce", "_type": "product", "_id": "1", "_score": 0.51623213, "_source": { "name": "jiaqiangban gaolujie yagao2", "desc": "gaoxiao meibai", "price": 30, "producer": "gaolujie producer", "tags": [ "meibai", "fangzhu" ] }, "highlight": { "producer": [ "<em>gaolujie</em> <em>producer</em>" ] } } ] } }
計算每個tag下的商品數量
- 將文本field的fielddata屬性設置為true
PUT /ecommerce/_mapping/product { "properties": { "tags":{ "type": "text", "fielddata": true } } }
- 再運行下面才能查出來
GET /ecommerce/product/_search { "aggs": { "group_by_tags": { "terms": { "field": "tags" } } } } "aggregations": { "group_by_tags": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "fangzhu", "doc_count": 3 }, { "key": "meibai", "doc_count": 3 }, { "key": "suibian", "doc_count": 1 } ] } }
對名稱中包含yagao的商品,計算每個tag下的商品數量
GET /ecommerce/product/_search { "query": { "match": { "name": "yagao" } }, "aggs": { "group_by_tags": { "terms": { "field": "tags" } } } }
先分組,再計算每組的平均值,計算每個tag下的商品的平均價格
GET /ecommerce/product/_search { "size": 0, "aggs": { "group_by_tags": { "terms": { "field": "tags" }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } }
對上述結果按平均價格排序
GET /ecommerce/product/_search { "size": 0, "aggs": { "group_by_tags": { "terms": { "field": "tags", "order": { "avg_price": "desc" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } }
按照指定的價格范圍區間進行分組,然后在每組內再按照tag進行分組,最后再計算每組的平均價格
GET /ecommerce/product/_search { "size": 0, "aggs": { "group_by_price": { "range": { "field": "price", "ranges": [ { "from": 0, "to": 20 }, { "from": 20, "to": 40 }, { "from": 40, "to": 60 } ] }, "aggs": { "group_by_tags": { "terms": { "field": "tags" }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } } } }
基本結構 { "query":{ "bool":{ "must":{ }, "must_not":{}, "filter":{}, "should":{}, "should_not":{} } } } #數組查詢 { "query":{ "nested":{ "path":"firm_app", "query":{ "match":{ "firm_app.app":"noticias" } } } } } 對象查詢 { "query":{ "term":{ "language.v4.keyword": "Spanish" } } } 存在field##### 亦可判斷寫入字段為空 { "query":{ "exists": { "field": "title" } } } 過濾 { "query":{ "range": { "discovery_time": { "gt": "2018-03-16", "lt": "2018-03-23" } } } } --------------------- 作者:野沐沐 來源:CSDN 原文:https://blog.csdn.net/yanxiaobo1991/article/details/79665590 版權聲明:本文為博主原創文章,轉載請附上博文鏈接!