# 首先做一个数据准备,创建一个索引
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。
GET /lib3/user/_search
{
"query": {
"term": {
"interests": "changge"
}
}
}
GET /lib3/user/_search
{
"query": {
"terms": {
"interests": ["hejiu", "changge"] # 这里多个条件是或(即or)的关系
}
}
}
GET /lib3/user/_search
{
"from": 0,
"size": 2,
"query": {
"terms": {
"interests": ["hejiu", "changge"]
}
}
}
GET /lib3/user/_search
{
"version": true,
"query": {
"terms": {
"interests": ["hejiu", "changge"]
}
}
}
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"
}
}
}
GET /lib3/user/_search
{
"_source": [
"address",
"name"
],
"query": {
"match": {
"interests": "changge"
}
}
}
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实现排序:
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
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
}
}
}
}
允许使用通配符 * 和 ? 来进行查询
GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "zhao*"
}
}
}
GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "li?i"
}
}
}
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": {}
}
}
}
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"
}
}
]
}
}
# 查询所有文档,不管是哪个索引下的
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