檢索概覽:
ES同MySQL一樣擁有衆多的查詢接口可以幫助用戶對指定的查詢內容進行匹配檢索查詢;如精確查詢、模糊查詢、前綴查詢、範圍查詢、正則表達式匹配查詢等。
查詢檢索子句分爲兩類:
1)查詢語句:執行全文本查詢時,基於相關度來評判其匹配結果;查詢執行過程複雜,且不會被緩存;
2)過濾語句:執行精確查詢時,基於其結果爲"yes"或"no"來進行評判;速度快,且結果可被緩存;
一、過濾語句
1)term filter:精確匹配包含指定term的文檔;
查詢語句:
[root@node2 ~]# curl -XGET -H Content-Type:application/json 'localhost:9200/student/class1/_search?pretty' -d '{
"query":{
"term":{
"age":"25"
}
}
}'
查詢結果:
{
"took" : 4, //查詢時長,毫秒;
"timed_out" : false, //是否超時;
"_shards" : { //分片情況;
"total" : 5, //總分片個數;
"successful" : 5, //成功分片的個數;
"skipped" : 0, //跳過的個數;
"failed" : 0 //失敗的個數;
},
"hits" : { //命中數據情況;
"total" : 1, //總命中數據個數;
"max_score" : 1.0, //打分情況;
"hits" : [ //命中數據集;
{
"_index" : "student", //索引名稱;
"_type" : "class1", //類型名稱;
"_id" : "3", //id情況;
"_score" : 1.0, //打分情況;
"_source" : { //源數據;
"name" : "Yangguo", //name字段與對應的值;
"age" : 25, //age字段與對應的值;
"sex" : "M" //sex字段與對應的值;
}
}
]
}
}
2)terms filter:用於多值精確匹配
查詢語句:
[root@node2 ~]# curl -XGET -H Content-Type:application/json 'localhost:9200/student/class1/_search?pretty' -d '{
"query":{
"terms":{
"age":[25,30]
}
}
}'
查詢結果:
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "student",
"_type" : "class1",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "Xiaofeng",
"age" : 30,
"sex" : "M"
}
},
{
"_index" : "student",
"_type" : "class1",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "Guojing",
"age" : 30,
"sex" : "M"
}
},
{
"_index" : "student",
"_type" : "class1",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "Yangguo",
"age" : 25,
"sex" : "M"
}
}
]
}
}
3)range filters:用於在指定的範圍內查找數值或時間
gt:大於
lt:小於
get:大於等於
let:小於等於
查詢語句:
[root@node2 ~]# curl -XGET -H Content-Type:application/json 'localhost:9200/student/class1/_search?pretty' -d'{
"query":{
"range":{
"age":{
"gte":28,
"lte":30
}
}
}
}'
查詢結果:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 4,
"max_score" : 1.0,
"hits" : [
{
"_index" : "student",
"_type" : "class1",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "Xiaofeng",
"age" : 30,
"sex" : "M"
}
},
{
"_index" : "student",
"_type" : "class1",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Huangrong",
"age" : 28,
"sex" : "F"
}
},
{
"_index" : "student",
"_type" : "class1",
"_id" : "6",
"_score" : 1.0,
"_source" : {
"name" : "Azhu",
"age" : 28,
"sex" : "F"
}
},
{
"_index" : "student",
"_type" : "class1",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "Guojing",
"age" : 30,
"sex" : "M"
}
}
]
}
}
4)exists and missing filters:字段存在與否檢索
查詢語句:
[root@node2 ~]# curl -XGET -H Content-Type:application/json 'localhost:9200/student/class1/_search?pretty' -d'{
"query":{
"exists":
{
"field":"school"
}
}
}'
查詢結果:
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "student",
"_type" : "class1",
"_id" : "11",
"_score" : 1.0,
"_source" : {
"school" : "hkz"
}
}
]
}
}
5)boolean filter:基於boolean邏輯來合併多個filter子句
must:其內部所有的子句條件必須同時匹配,即and;
查詢代碼:
[root@node2 ~]# curl -XGET -H Content-Type:application/json 'localhost:9200/student/class1/_search?pretty' -d'{
"query":{
"bool":{
"must":[
{"term":{"age":30}},
{"term":{"name":"Guojing"}}]
}
}
}'
查詢結果:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
must_not:其所有子句至少有一個子句匹配,即or
查詢語句:
[root@node2 ~]# curl -XGET -H Content-Type:application/json 'localhost:9200/student/class1/_search?pretty' -d'{
"query":{
"bool":{
"should":[
{"term":{"name":"Yangguo"}},
{"term":{"age":25}}]
}
}
}'
查詢結果:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "student",
"_type" : "class1",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "Yangguo",
"age" : 25,
"sex" : "M"
}
}
]
}
}
must_not:其所有的子句必須都不匹配,即not
查詢語句:
[root@node2 ~]# curl -XGET -H Content-Type:application/json 'localhost:9200/student/class1/_search?pretty' -d'{
"query":{
"bool":{
"must_not":[
{"term":{"name":"Yangguo"}},
{"term":{"sex":"F"}}]
}
}
}'
查詢結果:
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 11,
"max_score" : 1.0,
"hits" : [
{
"_index" : "student",
"_type" : "class1",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "Xiaofeng",
"age" : 30,
"sex" : "M"
}
},
"hits" : {
"total" : 11,
"max_score" : 1.0,
"hits" : [
{
"_index" : "student",
"_type" : "class1",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "Xiaofeng",
"age" : 30,
"sex" : "M"
}
},
6)prefix query:前綴字符串檢索匹配
查詢語句:
[root@node2 ~]# curl -XGET -H Content-Type:application/json 'localhost:9200/student/class1/_search?pretty' -d'{
"query":{
"prefix":{
"name":"Guo"
}
}
}'
7)wildcard query:通配符檢索
匹配具有匹配通配符表達式(not analyzed)的字段的文檔。
支持的通配符:
1)*:匹配任何字符序列(包括空字符序列);
2)?:匹配任何單個字符;
注意:次查詢可能會很慢,因爲它需要遍歷多個術語,爲了防止非常慢的通配符語句,通配符不能以任何一個通配符*或?開頭。
查詢語句:
[root@node2 ~]# curl -XGET -H Content-Type:application/json 'localhost:9200/student/class1/_search?pretty' -d'{
"query":{
"wildcard":{
"name":"Guo*"
}
}
}'
8)regexp query:正則表達式檢索匹配
查詢語句:
[root@node2 ~]# curl -XGET -H Content-Type:application/json 'localhost:9200/student/class1/_search?pretty' -d'{
"query":{
"regexp":{
"name":"Guo.*"
}
}
}'
9)fuzzy query:模糊查詢查找再模糊度中指定的最大編輯距離內的所有可能的匹配項,然後檢查術語字典,以找出在檢索中實際存在待檢索的關鍵詞。
[root@node2 ~]# curl -XGET -H Content-Type:application/json 'localhost:9200/student/class1/_search?pretty' -d'{
"query":{
"fuzzy":{
"name":"Guo"
}
}
}'
10)type query:類型檢索
[root@node2 ~]# curl -XGET -H Content-Type:application/json 'localhost:9200/student/class1/_search?pretty' -d'{
"query":{
"type":{
"value":"numbers"
}
}
}'
11)ids query:返回指定id的全部信息
[root@node2 ~]# curl -XGET -H Content-Type:application/json 'localhost:9200/student/class1/_search?pretty' -d'{
"query":{
"ids":{
"values":["1","2"]
}
}
}'
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "student",
"_type" : "class1",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Huangrong",
"age" : 28,
"sex" : "F"
}
},
{
"_index" : "student",
"_type" : "class1",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "Guojing",
"age" : 30,
"sex" : "M"
}
}
]
}
}
12)全文檢索字段:對全文本指定匹配到的字段內容
[root@node1 ~]# curl -XGET -H Content-Type:application/json "localhost:9200/student/class1/_search?q=Xiaofeng"
{"took":12,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"student","_type":"class1","_id":"5","_score":0.2876821,"_source":{
"name":"Xiaofeng",
"age":30,
"sex":"M"
DSL語句檢索方式:
[root@node1 ~]# curl -XGET -H Content-Type:application/json "localhost:9200/student/class1/_search?pretty" -d '{
"query":{
"multi_match":{
"query":"Xiaofeng"
}
}
}'
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "student",
"_type" : "class1",
"_id" : "5",
"_score" : 0.2876821,
"_source" : {
"name" : "Xiaofeng",
"age" : 30,
"sex" : "M"
}
}
]
}
}
13)指定字段查找匹配的字符內容
[root@node1 ~]# curl -XGET -H Content-Type:application/json "localhost:9200/student/class1/_search?q=sex:M"
{"took":6,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":3,"max_score":0.2876821,"hits":[{"_index":"student","_type":"class1","_id":"5","_score":0.2876821,"_source":{
"name":"Xiaofeng",
"age":30,
"sex":"M"
}},{"_index":"student","_type":"class1","_id":"1","_score":0.2876821,"_source":{
"name":"Guojing",
"age":20,
"sex":"M"
}},{"_index":"student","_type":"class1","_id":"3","_score":0.2876821,"_source":{
"name":"Yangguo",
"age":18,
"sex":"M"
}}]}}
DSL語句指定字段檢索:
[root@node1 ~]# curl -XGET -H Content-Type:application/json "localhost:9200/student/class1/_search?pretty" -d'{
> "query":{
> "match":{
> "sex":"M"
> }
> }
> }'
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "student",
"_type" : "class1",
"_id" : "5",
"_score" : 0.2876821,
"_source" : {
"name" : "Xiaofeng",
"age" : 30,
"sex" : "M"
}
},
{
"_index" : "student",
"_type" : "class1",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "Guojing",
"age" : 20,
"sex" : "M"
}
},
{
"_index" : "student",
"_type" : "class1",
"_id" : "3",
"_score" : 0.2876821,
"_source" : {
"name" : "Yangguo",
"age" : 18,
"sex" : "M"
}
}
]
}
}
14)在指定的多字段中檢索文檔
[root@node1 ~]# curl -XGET -H Content-Type:application/json "localhost:9200/student/class1/_search?pretty" -d'{
"query":{
"multi_match":{
> "query":"F",
> "fields":["sex","name"]
> }
> }
> }'
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.13353139,
"hits" : [
{
"_index" : "student",
"_type" : "class1",
"_id" : "2",
"_score" : 0.13353139,
"_source" : {
"name" : "Huangrong",
"age" : 18,
"sex" : "F"
}
},
{
"_index" : "student",
"_type" : "class1",
"_id" : "4",
"_score" : 0.13353139,
"_source" : {
"name" : "Xiaolongnv",
"age" : 25,
"sex" : "F"
}
},
{
"_index" : "student",
"_type" : "class1",
"_id" : "6",
"_score" : 0.13353139,
"_source" : {
"name" : "Azhu",
"age" : 24,
"sex" : "F"
}
}
]
}
}