http://192.168.15.38:9200/film/_mapping/dongzuosql
請求方式:postapp
{"properties":{"title":{"type":"keyword"},"publishDate":{"type":"date"},"director":{"type":"keyword"},"price":{"type":"float"},"desc":{"type":"text"}}}
1.查詢指定索引指定類型下全部數據post
http://192.168.15.38:9200/[索引名]/[類型]/_search/
例如:code
http://192.168.15.38:9200/film/dongzuo/_search/ 查詢film索引下類型爲dongzuo的全部數據
2.分頁查詢排序
http://192.168.15.38:9200/[索引名]/[類型]/_search/
請求方式:POST索引
參數:get
{ "from": [起始位置], "size": [每頁數量] }
例如:it
查詢前兩條數據 http://192.168.15.38:9200/film/dongzuo/_search/ { "from": 0, "size": 2 }
3.排序查詢配置
http://192.168.15.38:9200/[索引名]/[類型]/_search/
請求方式:POSTdate
參數:
{ "sort": [ { "[排序字段]": { "order": "[排序類型]" //desc降序 asc升序 } } ] }
例如:
按照發布日期降序 http://192.168.15.38:9200/film/dongzuo/_search/ { "sort": [ { "publishDate": { "order": "desc" } } ] }
4.數據列的過濾
http://192.168.15.38:9200/[索引名]/[類型]/_search/
請求方式:POST
參數:
{ "_source": { "include": [ [查詢包括的列] ] } }
例如:
查詢前三行數據,只查詢標題、導演和價格 { "from": 0, "size": 3, "_source": { "include": [ "title", "director", "price" ] } }
5.簡單條件查詢
請求方式:POST
http://192.168.15.38:9200/[索引名]/[類型]/_search/
參數: { "query": { "match": { "[配置的字段]": "[匹配的值]" } } }
例如
匹配地址帶有山東的數據
注意問題:假如匹配"山"是匹配不到的,由於默認分詞器把「山東」分爲一個詞,而不是"山"
http://192.168.15.38:9200/film/dongzuo/_search/ { "query": { "match": { "address": "山東" } } }
6.查詢結果高亮顯示 請求方式:POST
http://192.168.15.38:9200/[索引名]/[類型]/_search/
參數: { "highlight": { "fields": { "[高亮顯示的字段]": {} } } }
例如:
標題title高亮顯示
注:高亮顯示的只能是在查詢中的字段
http://192.168.15.38:9200/film/dongzuo/_search/ { "query": { "match": { "title": "戰狼2" } }, "highlight": { "fields": { "title": {} } } }
7.多條件組合查詢
首先了解bool過濾器: 主要有三部分:
{ "bool" : { "must" : [], "should" : [], "must_not" : [], } }
must:全部語句必須匹配 和and等價
must_not:全部語句必須不能匹配 和not等價
should:至少一個匹配 和or等價
(1)單個條件模糊匹配
{ "query": { "bool": { "must": { "match": { "name": "李四" } } } }, "_source": { "include": [ "name", "age", "address" ] }, "highlight": { "fields": { "name": {} } } }
(2)多個條件匹配
{ "query": { "bool": { "must": [ { "match": { "name": "李四" } }, { "match": { "address": "山東" } } ] } }, "_source": { "include": [ "name", "age", "address" ] }, "highlight": { "fields": { "name": {} } } }
(3)must與must_not組合使用
查詢地址含有山東,姓名不含有張三的用戶 { "query": { "bool": { "must": [ { "match": { "address": "山東" } } ], "must_not": { "match": { "name": "張三" } } } }, "_source": { "include": [ "name", "age", "address" ] }, "highlight": { "fields": { "name": {} } } }
(4)使用should查詢
should查詢至關於sql裏面的or條件,在ES裏面還有個功能就是提升_score參數,是的匹配更精確
{ "query": { "bool": { "must": [ { "match": { "address": "山東" } } ], "should": [ { "match": { "name": "張三" } }, { "range": { "age": { "gte": 21 } } } ] } }, "_source": { "include": [ "name", "age", "address" ] }, "highlight": { "fields": { "name": {} } } }
(5)filter過濾 fileter做用是過濾符合條件的查詢
例如:過濾掉年齡小於21的用戶
gt: 大於 gte:大於等於 lt:小於 lte:小於等於
{ "query": { "bool": { "must": [ { "match": { "address": "山東" } } ], "filter": { "range": { "age": { "gte": 21 } } } } }, "_source": { "include": [ "name", "age", "address" ] }, "highlight": { "fields": { "name": {} } } }