主要內容:7種搜索方式的簡單操作
1、query string search
2、query DSL
3、query filter
4、full-text search
5、phrase search
6、highlight search
7、term query
1、query string search
名稱由來:search參數都是以http請求的query string來附帶的
參數列表
搜索示例:
搜索全部商品
GET /ecommerce/_search
{
"took" : 11,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "ecommerce",
"_type" : "_doc",
"_id" : "SH1w83ABZ60GR8nZv1rN",
"_score" : null,
"_source" : {
"name" : "zhonghua yagao",
"desc" : "caoben zhiwu",
"price" : 40,
"producer" : "zhonghua producer",
"tags" : [
"qingxin"
]
},
"sort" : [
40
]
}
]
}
}
字段說明:
took:耗費了幾毫秒
timed_out:是否超時
_shards:分片數量,搜索請求會打到所有的primary shard(或者是它的某個replica shard也可以)
hits.total:查詢結果的數量
hits.max_score:就是document對於一個search的相關度的匹配分數,越相關,就越匹配,分數也高
hits.hits:包含了匹配搜索的document的詳細數據
搜索名爲yagao的商品,並按價格進行降序排列
GET /ecommerce/_search?q=name:yagao&sort=price:desc
小結
適用於臨時的在命令行使用一些工具,比如curl,快速的發出請求,來檢索想要的信息;但是如果查詢請求很複雜,是很難去構建的,在生產環境中,幾乎很少使用query string search
2、query DSL
DSL:Domain Specified Language,特定領域的語言
http request body:請求體,可以用json的格式來構建查詢語法,比較方便,可以構建各種複雜的語法.
查詢示例:
GET /ecommerce/_search
{
"query": {
"match_all": {}
}
}
查詢名稱包含yagao的商品,同時按照價格降序排序
GET /ecommerce/_search
{
"query": {
"match": {
"name": "yagao"
}
},
"sort": [
{
"price": "desc"
}
]
}
分頁查詢商品,總共3條商品,假設每頁就顯示1條商品,現在顯示第2頁,所以就查出來第2個商品
GET /ecommerce/_search
{
"query": {
"match_all": {}
},
"from": 1,
"size": 1
}
指定查詢出來商品的名稱和價格 類似sql語句的select
GET /ecommerce/_search
{
"query": {
"match_all": {}
},
"_source": [
"name",
"price"
]
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "ecommerce",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"price" : 25,
"name" : "jiajieshi yagao"
}
},
{
"_index" : "ecommerce",
"_type" : "_doc",
"_id" : "SH1w83ABZ60GR8nZv1rN",
"_score" : 1.0,
"_source" : {
"price" : 40,
"name" : "zhonghua yagao"
}
}
]
}
}
3、query filter
對數據進行過濾
搜索商品名稱包含yagao,而且售價大於等於40元的商品
(gt:大於,lt:小於,gte:大於等於,lte:小於等於)
GET /ecommerce/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "yagao"
}
}
],
"filter": {
"range": {
"price": {
"gte": 40
}
}
}
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
返回的結果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.03390155,
"hits" : [
{
"_index" : "ecommerce",
"_type" : "_doc",
"_id" : "SH1w83ABZ60GR8nZv1rN",
"_score" : 0.03390155,
"_source" : {
"name" : "zhonghua yagao",
"desc" : "caoben zhiwu",
"price" : 40,
"producer" : "zhonghua producer",
"tags" : [
"qingxin"
]
}
}
]
}
}
4、full-text search (全文檢索)
全文檢索會將輸入的搜索串拆解開來,去倒排索引裏面去逐一匹配,只要能匹配上任意一個拆解後的單詞,就可以作爲結果返回。
返回的結果會按照匹配度進行排序
GET /ecommerce/_search
{
"query": {
"match": {
"producer": "yagao producer"
}
}
}
返回的結果
{
"took" : 36,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.395052,
"hits" : [
{
"_index" : "ecommerce",
"_type" : "_doc",
"_id" : "sBkH-3ABDLwU_RgbxhX0",
"_score" : 1.395052,
"_source" : {
"name" : "special yagao",
"desc" : "special meibai",
"price" : 50,
"producer" : "special yagao producer",
"tags" : [
"meibai"
]
}
}
]
}
}
5、phrase search(短語搜索)
跟全文檢索相對應,要求輸入的搜索串,必須在指定的字段文本中,完全包含一模一樣的,纔可以算匹配,才能作爲結果返回
GET /ecommerce/_search
{
"query": {
"match_phrase": {
"producer": "yagao producer"
}
}
}
6、highlight search(高亮搜索結果)
對匹配的producer字段進行高亮顯示
GET /ecommerce/_search
{
"query": {
"match": {
"producer": "yagao"
}
},
"highlight": {
"fields": {
"producer": {}
}
}
}
返回結果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3310189,
"hits" : [
{
"_index" : "ecommerce",
"_type" : "_doc",
"_id" : "sBkH-3ABDLwU_RgbxhX0",
"_score" : 1.3310189,
"_source" : {
"name" : "special yagao",
"desc" : "special meibai",
"price" : 50,
"producer" : "special yagao producer",
"tags" : [
"meibai"
]
},
"highlight" : {
"producer" : [
"special <em>yagao</em> producer"
]
}
}
]
}
}
7、term query( 完全匹配 )
term是代表完全匹配,即不進行分詞器分析,文檔中必須包含整個搜索的詞彙
GET /ecommerce/_search
{
"query": {
"term": {
"producer": "special"
}
}
}
(terms多值)字段有一多個值時候,用terms關鍵詞查詢,後跟數組
GET /ecommerce/_search
{
"query": {
"terms": {
"producer": [
"special",
"producer"
]
}
}
}