Elasticsearch 基本查詢(Query查詢)
基本查詢(Query查詢)
資料準備及簡單查詢
//首先做一個數據準備,建立一個索引
PUT /lib3
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0
},
"mappings": {
"user": {
"properties": {
"name": {"type": "text"},
"address": {"type": "text"},
"age" : {"type": "integer"},
"interests": {"type": "text"},
"birthday": {"type": "date"}
}
}
}
}
//新增一些文件
PUT /lib3/user/1
{
"name": "zhaoliu",
"address": "hei long jiang sheng tie ling shi",
"age": 50,
"birthday": "1970-12-12",
"interests" : "xi huan hejiu, duanlian,lvyou"
}
PUT /lib3/user/2
{
"name": "zhaoming",
"address": "bei jing hai dian qu qing he zhen",
"age": 20,
"birthday": "1998-10-12",
"interests": "xi huan hejiu, duanlian,changge"
}
PUT /lib3/user/3
{
"name": "lisi",
"address": "bei jing hai dian qu qing he zhen" ,
"age": 23,
"birthday": "1998-10-12",
"interests": "xi huan hejiu, duanlian,changge"
}
PUT /lib3/user/4
{
"name": "wangwu",
"address": "bei jing hai dian qu qing he zhen",
"age": 26,
"birthday": "1995-10-12",
"interests": "xi huan biancheng, tingyinyue,lvyou"
}
PUT /lib3/user/5
{
"name": "zhangsan",
"address": "bei jing chao yang qu",
"age": 29,
"birthday": "1988-10-12",
"interests": "xi huan tingyinyue,changge,tiaowu"
}
//簡單查詢
GET /lib3/user/_search?q=name:lisi
//返回結果
{
"took": 9, //took:當前搜尋所用時間
"time_out": false, //time_out:是否超時
"_shards": { //shards:分片資訊
"total": 3, //總分片數
"successful": 3, //請求成功的分片數
"skipped": 0, //請求跳過的分片數
"failed": 0 //請求失敗的分片數
},
"hits": {
"total": 1, //查詢出的文件數
"max_score": 0.6931472, //相關度分數(即查出的文件和搜尋條件的匹配度,完全匹配為最大值1)
"hits": [
{
"_index": "lib3",
"_type": "user",
"_id": "3",
"_score": 0.6931472,
"_source": {
"name": "lisi",
"address": "bei jing hai dian qu qing he zhen",
"age": 23,
"birthday": "1998-10-12",
"interests": "xi huan he jiu, duan lian,chang ge"
}
}
]
}
}
//簡單查詢
//查詢興趣是changge的人,並對age進行降序排列
GET /lib3/user/_search?q=interests:changge&sort=age:desc
//返回結果
{
"took": 17,
"time_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "lib3",
"_type": "user",
"_id": "5",
"_score": null,
"_source": {
"name": "zhangsan",
"address": "bei jing chao yang qu",
"age": 29,
"birthday": "1988-10-12",
"interests": "xi huan tingyinyue,changge,tiaowu"
},
"sort": [
29
]
},
{
"_index": "lib3",
"_type": "user",
"_id": "5",
"_score": null,
"_source": {
"name": "lisi",
"address": "bei jing hai dian qu qing he zhen",
"age": 23,
"birthday": "1998-10-12",
"interests": "xi huan hejiu,duanlian,cahngge"
},
"sort": [
23
]
},
{
"_index": "lib3",
"_type": "user",
"_id": "5",
"_score": null,
"_source": {
"name": "zhaoming",
"address": "bei jing hai dian qu qing he zhen",
"age": 20,
"birthday": "1998-10-12",
"interests": "xi huan hejiu,duanlian,changge"
},
"sort": [
20
]
}
]
}
}
term查詢和terms查詢
term query回去倒排索引中尋找確切的term(即精準查詢),它並不知道分詞器的存在。這種查詢適合keyword,numeric,date。
term:查詢某個欄位裡含有某個關鍵詞的文件
GET /lib3/user/_search
{
"query": {
"term": {
"interests": "changge"
}
}
}
terms:查詢某個欄位裡含有多個關鍵字的文件
GET /lib3/user/_search
{
"query": {
"terms": {
"interests": ["hejiu", "changge"] //這裡多個條件是或(即or)的關係
}
}
}
控制查詢返回的數量
from:從哪一個文件開始
size:需要的個數
GET /lib3/user/_search
{
"from": 0,
"size": 2,
"query": {
"terms": {
"interests": ["hejiu", "changge"]
}
}
}
返回版本號
version:當設定version為true時,查詢的文件中將返回版本資訊,預設情況是false(即不設定的version時)
GET /lib3/user/_search
{
"version": true,
"query": {
"terms": {
"interests": ["hejiu", "changge"]
}
}
}
match查詢
match query知道分詞器的存在,會對filed進行分詞操作,然後在查詢
GET /lib3/user/_search
{
"query": {
"match": {
"name": "zhaoliu"
}
}
}
//分詞的體現
//匹配的條件越多,相似度的值將會越高
GET /lib3/user/_search
{
"query": {
"match": {
"name": "zhaoliu zhaoming"
}
}
}
GET /lib3/user/_search
{
"query": {
"match": {
"age": 20
}
}
}
match_all:查詢所有文件
GET /lib3/user/_search
{
"query": {
"match_all": {}
}
}
multi_match:可以指定多個欄位
GET /lib3/user/_search
{
"query": {
"multi_match": {
"query": "lvyou",
"fields": [
"interests",
"name"
]
}
}
}
match_phrase:短語匹配查詢
Elasticsearch引擎首先分析(analyze)查詢字串,從分析後的文字中構建短語查詢,這意味著必須匹配短語中的所有分詞,並且保證各個分詞的相對位置不變;
GET /lib3/user/_search
{
"query": {
"match_phrase": {
"interests": "duanlian, shuoxiangsheng"
}
}
}
指定返回的欄位
_source:可在_source中設定想返回的欄位
GET /lib3/user/_search
{
"_source": [
"address",
"name"
],
"query": {
"match": {
"interests": "changge"
}
}
}
控制載入的欄位
includes:包含某些欄位,可以使用萬用字元進行查詢
excludes:排除某些欄位,可以使用萬用字元進行查詢
GET /lib3/user/_search
{
"_source": {
"includes": ["name", "address"],
"excludes": ["age", "birthday"]
},
"query": {
"match_all": {}
}
}
//使用萬用字元查詢
GET /lib3/user/_search
{
"_source": {
"includes": ["addr*"],
"excludes": ["name", "bir*"]
},
"query": {
"match_all": {}
}
}
排序
使用sort實現排序:desc降序、asc升序
GET /lib3/user/_search
{
"sort": [
{
"age": {
"order": "asc"
}
}
],
"query": {
"match_all": {}
}
}
GET /lib3/user/_search
{
"sort": [
{
"age": {
"order": "desc"
}
}
],
"query": {
"match_all": {}
}
}
字首匹配查詢
GET /lib3/user/_search
{
"query": {
"match_phrase_prefilx": {
"name": {
"query": "zhao"
}
}
}
}
範圍查詢
range:實現範圍查詢
引數:from、to、include_lower、include_upper、boost
include_lower:是否包含範圍的左邊界,預設是true
include_upper:是否包含範圍的右邊界,預設是true
GET /lib3/user/_search
{
"query": {
"range": {
"birthday": {
"from": "1990-10-10",
"to": "2018-05-01"
}
}
}
}
GET /lib3/user/_search
{
"query": {
"range": {
"age": {
"from": 20,
"to": 25,
"include_lower": true,
"include_upper": false
}
}
}
}
wildcard查詢
允許使用萬用字元*和?來進行查詢
*代表0個或者多個字元
?代表任意一個字元
GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "zhao*"
}
}
}
GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "li?i"
}
}
}
fuzzy實現模糊查詢
value:查詢的關鍵字
boost:查詢的權值,預設值是1.0
min_similarity:設定匹配的最小相似度,預設值為0.5,對於字串,取值為0-1(包含0和1);對於數值,取值可能大於1;對於日期型別取值為1d,1m等,1d就代表1天
prefix_length:指明區分詞項的共同字首長度,預設是0
max_expansions:查詢中的詞項可以擴充套件的數目,預設可以無限大
GET /lib3/user/_search
{
"query": {
"fuzzy": {
"interests": "changge"
}
}
}
GET /lib3/user/_search
{
"query": {
"fuzzy": {
"interests": {
"value": "chagge" //此處changge寫成了chagge,但因為模糊查詢依然可以查出結果
}
}
}
}
高亮搜尋結果
GET /lib3/user/_search
{
"query": {
"match": {
"interests": "changge"
}
},
"highlight": {
"fields": {
"interests": {}
}
}
}
查詢結果分析
- took:查詢耗費的時間,單位是毫秒
- _shard:共請求了多少個shard
- total:查詢出的文件總個數
- max_score:本次查詢中,相關度分數的最大值,文件和此次查詢的匹配度越高,_score的值越大,排位越考前
- hits:預設查詢前10個文件
- time_out: 查詢超時時間,設定超時時間將返回已經查詢出來的資料
GET /lib3/user/_search?timeout=10ms
{
"_source": [
"address",
"name"
],
"query": {
"match": {
"interests": "changge"
}
}
}
{
"took": 419,
"time_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.6931472,
"hits": [
{
"_index": "lib3",
"_type": "user",
"_id": "3",
"_score": 0.6931472,
"_source": {
"address": "bei jing hai dian qu qing he zhen",
"name": "lisi"
}
},
{
"_index": "lib3",
"_type": "user",
"_id": "2",
"_score": 0.47000363,
"_source": {
"address": "bei jing hai dian qu qing he zhen",
"name": "lisi"
}
}
]
}
}
多index、多type查詢模式
//查詢所有文件,不管是哪個索引下的
GET _search
//查詢lib索引下所有文件
GET /lib/_search
//指定lib、lib3索引下的所有文件
GET /lib,lib3/_search
//*號為萬用字元,查詢結尾是3和4的索引下的所有文件
GET /*3,*4/_search
//可以指定型別type,(6.0以後一個_index下只能有一個_type,等價於GET /lib/_search)
GET /lib/user/_search
//指定多個索引,多個型別,(6.0以後一個_index下只能有一個_type,等價於GET /lib,lib3/_search)
GET /lib,lib3/user,items/_search
//_all表示叢集下的所有索引
GET /_all/_search
//查詢所有索引下,只查詢user、items型別的文件
GET /_all/user,items/_search