本文使用的Elasticsearch版本为6.5.4,上文主要介绍ES的各种查询,以满足更多的需求。ES的主要功能其实就是体现在这搜索查询上,所以我就总结一下,方便以后直接拿来就用。
# 首先做一个数据准备,创建一个索引
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 query回去倒排索引中寻找确切的term(即精准查询),它并不知道分词器的存在。这种查询适合keyword,numeric,date。
term:查询某个字段里含有某个关键词的文档 terms:查询某个字段里含有多个关键字的文档
GET /lib3/user/_search
{
"query": {
"term": {
"interests": "changge"
}
}
}
GET /lib3/user/_search
{
"query": {
"terms": {
"interests": ["hejiu", "changge"] # 这里多个条件是或(即or)的关系
}
}
}
match query知道「分词器的存在」,会对filed进行分词操作,然后在查询
match_all:查询所有文档 multi_match:可以指定多个字段 match_phrase:短语匹配查询,Elasticsearch引擎首先分析(analyze)查询字符串,从分析后的文本中构建短语查询,这意味着必须匹配短语中的所有分词,并且保证各个分词的相对位置不变;
# 分词的体现# 匹配的条件越多,相似度的值将会越高
GET /lib3/user/_search
{
"query": {
"match": {
"name": "zhaoliu zhaoming"
}
}
}
GET /lib3/user/_search
{
"query": {
"match": {
"age": 20
}
}
}
GET /lib3/user/_search
{
"query": {
"multi_match": {
"query": "lvyou",
"fields": [
"interests",
"name"
]
}
}
}
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": {}
}
}
from:从哪一个文档开始 size:需要的个数
GET /lib3/user/_search
{
"from": 0,
"size": 2,
"query": {
"terms": {
"interests": ["hejiu", "changge"]
}
}
}
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
}
}
}
}
允许使用通配符 * 和 ? 来进行查询
*:代表0个或者多个字符 ?:代表任意一个字符
GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "zhao*"
}
}
}
GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "li?i"
}
}
}
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": {}
}
}
}
version:当设置version为true时,查询的文档中将返回版本信息,默认情况是false(即不设置的version时)
GET /lib3/user/_search
{
"version": true,
"query": {
"terms": {
"interests": ["hejiu", "changge"]
}
}
}
GET /lib3/user/_search
{
"query": {
"regexp" : {
"address" : "b[a-z]*u"
}
},
"highlight": {
"fields": {
"interests": {}
}
}
}
GET /lib7/items/_search
{
"query":{
"bool":{
"filter":[
{"terms":{"price":[25,40]}}
]
}
}
}
关于filter查询以及组合查询可以参考文章 https://blog.csdn.net/qq_25834767/article/details/84642051
https://www.cnblogs.com/javasl/p/11405405.html
https://blog.csdn.net/zx711166/article/details/81811548
https://www.cnblogs.com/UUUz/p/11170833.html