检索概览:
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"
}
}
]
}
}