elasticsearch 的基本查询语法使用(上)
1.设置查询时间
(1)默认是没有设置超时时间的,设置之后会执行timeout的机制
(2)timeout机制:如果一条查询需要查询5秒,但是设置了timeout=1s,那么不管查询到了多少条数据,都会在时间到达1s以后停止
(3)用法: get /索引名/_search?timeout=1s/ms/m 或者
GET /zzl/_search
{ "timeout": "1s",
"query": {
"bool": {
"must": [
{ "match": {
"name": "xiaomi"
}},
{
"match": {
"tage": "fashao"}
}
]
}
}
}
2.Query DSL:
前提:你需要一个索引表
// 1 注意这个 1 他是对应的索引,自己想要添加多条的时候直接改就行
PUT /zzl/_doc/1?pretty
{
"name" : "iphone",
"desc" : "buzhidao",
"price" : 9999,
"tags": [ "sha dong xi","phone"],
"current": "zzz",
"zzl": 15
}
(1)匹配所有:(以下均以zzl索引示例)
GET /zzl/_search
{
#_source不显示元数据,也就是索引下的数据,自行测试的时候可以删掉这个
#_source可以设置你想要的对应的字段示例 "_source": ["name","tags"]
"_source": false,
"query":{"match_all": {} }
}
查询结果:(东西太长,只有6粘贴展示我当前zzl索引对应的字段)
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0
},
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0
},
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0
},
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0
},
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.0
},
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "6",
"_score" : 1.0,
"_source" : {
"name" : "vivo nfc phone",
"desc" : "buzhidao",
"price" : 9999,
"tags" : [
"sha dong xi"
],
"current" : "zzz",
"zzl" : 15
}
}
]
}
}
(2)match: 全文检索,会分词 (分词下次会介绍)
GET /zzl/_search
{
"_source": ["name"], # 只显示对应的name 篇幅有限
"query": {
"match": {
# 如果你现在就想直接起飞,并且没有在创建索引的时候对字段设置分词器
# 那么对应的你可能会踩到一个坑,像中国式的拼音,es默认的分词器不给你分词,需要用空格分开
"name": "xiaomi"
}
}
}
查询结果:
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.1704489,
"hits" : [
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.1704489,
"_source" : {
"name" : " xiaomi phone"
}
},
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0054247,
"_source" : {
"name" : "xiaomi nfc phone"
}
}
]
}
(3)multi_match:根据多个字段查询一个关键词
GET /zzl/_search
{
"_source": ["name","tags"],
"query": {
"multi_match": {
"query": "iphone",
"fields": ["name","tags"]
}
}
}
查询结果
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.5114288,
"hits" : [
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "8",
"_score" : 1.5114288,
"_source" : {
"name" : "Air iphone",
"tags" : [
"sha dong xi",
"iphone",
"Air iphone"
]
}
},
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "7",
"_score" : 1.241217,
"_source" : {
"name" : "iphone",
"tags" : [
"sha dong xi",
"iphone"
]
}
},
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "10",
"_score" : 1.1505529,
"_source" : {
"name" : "iphone",
"tags" : [
"sha dong xi",
"phone"
]
}
},
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.77807164,
"_source" : {
"name" : "iphone nfc phone",
"tags" : [
"pingguo",
"wending"
]
}
}
]
}
(4)sort :排序
(注意sort属于聚合查询,一般不对text类型的字段进行查询,聚合查询不在这次笔记中)
GET /zzl/_search
{
"_source": ["name","tags","price"],
"query": {
"multi_match": {
"query": "iphone",
"fields": ["name","tags"]
}
},
"sort": [
{
#我设置的字段里面只有price目前是long 类型
"price": {
"order": "desc"
}
}
]
}
查询结果:
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "7",
"_score" : null,
"_source" : {
"price" : 9999,
"name" : "iphone",
"tags" : [
"sha dong xi",
"iphone"
]
},
"sort" : [
9999
]
},
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "8",
"_score" : null,
"_source" : {
"price" : 9999,
"name" : "Air iphone",
"tags" : [
"sha dong xi",
"iphone",
"Air iphone"
]
},
"sort" : [
9999
]
},
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "10",
"_score" : null,
"_source" : {
"price" : 9999,
"name" : "iphone",
"tags" : [
"sha dong xi",
"phone"
]
},
"sort" : [
9999
]
},
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"_source" : {
"price" : 6999,
"name" : "iphone nfc phone",
"tags" : [
"pingguo",
"wending"
]
},
"sort" : [
6999
]
}
]
}
(5)分页
GET /product/_search
{
"query":{
"match_all": {}
},
"sort": [
{
"price": "asc"
}
],
"from": 0,
"size": 2
}
查询结果
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "7",
"_score" : null,
"_source" : {
"price" : 9999,
"name" : "iphone",
"tags" : [
"sha dong xi",
"iphone"
]
},
"sort" : [
9999
]
},
{
"_index" : "zzl",
"_type" : "_doc",
"_id" : "8",
"_score" : null,
"_source" : {
"price" : 9999,
"name" : "Air iphone",
"tags" : [
"sha dong xi",
"iphone",
"Air iphone"
]
},
"sort" : [
9999
]
}
]
}
剩下的下次再介绍
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
二维码