基本查询(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