elasticsearch中文文档精解三_基础_轻量查找

这篇文字讲下es最基本的查找分页等方法,这里要明白es为提供方便查找提供了2套查找方案,一是使用get请求带参数的形式轻量查找,一是把查找语句放入请求体中实现更多高级一点的特性

空查询

1
2
curl -X GET "localhost:9200/_search"

返回

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{
"took": 5, #耗时单位微妙
"timed_out": false,
"_shards": { #分片情况
"total": 13,
"successful": 13,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 20105,
"max_score": 1,
"hits": [ #默认返回10条
{
"_index": "bt",
"_type": "text",
"_id": "4930340",
"_score": 1,
"_source": {
"name": "Volver (2006) BDRip-Avc UkrEsp.mkv",
"keywords": "Volver,(2006),BDRip-Avc",
"url": "981D242E410C5B380FC7EC5B67FC3125ECDC268F"
}
},
{
"_index": "bt",
"_type": "text",
"_id": "4930342",
"_score": 1,
"_source": {
"name": "Song of the Sea 2014 1080p BRRip x264 AAC-KiNGDOM",
"keywords": "1080p,BRRip,AAC-KiNGDOM",
"url": "813AB10C05E374DDDE5986710D6BA6F35CAD1E8C"
}
},
{
"_index": "bt",
"_type": "text",
"_id": "4930343",
"_score": 1,
"_source": {
"name": "Sing - Quem Canta Seus Males Espanta 2017 (720p) DUBLADO",
"keywords": "Canta,Males,Espanta,(720p),DUBLADO",
"url": "14C5E021537A92CC026A9CD49D237691E0C1B2E3"
}
},
{
"_index": "bt",
"_type": "text",
"_id": "4930344",
"_score": 1,
"_source": {
"name": "Idealnyi_muzhchyna.2015.BDRip.720p_Sborka_grab777.mkv",
"keywords": "",
"url": "E11FFF8B0E53E9F041FA0F7D59BC0D0E2B056FCD"
}
},
{
"_index": "bt",
"_type": "text",
"_id": "4930350",
"_score": 1,
"_source": {
"name": "Raziskovalka.Dora.V.Čudežni.Deželi.2014.ENG.DVDRip.x264",
"keywords": "",
"url": "422141BBB8048A3844D8D5510C7F25E08CE65A6D"
}
},
{
"_index": "bt",
"_type": "text",
"_id": "4930356",
"_score": 1,
"_source": {
"name": "Ранго",
"keywords": "Ранго",
"url": "949B4F8C8C4771BF96850403ED6B903FF22E86BC"
}
},
{
"_index": "bt",
"_type": "text",
"_id": "4930357",
"_score": 1,
"_source": {
"name": "A.trois.on.y.va_2015.WEB-DLRip.avi",
"keywords": "",
"url": "022E86258986B31A50DF710F33F2FCA6CA4B3DAB"
}
},
{
"_index": "bt",
"_type": "text",
"_id": "4930362",
"_score": 1,
"_source": {
"name": "Suzumiya_Haruhi_no_Shoushitsu_1080p_Hi10p_Ukr_UAMAX.mkv",
"keywords": "",
"url": "B41C345EDD0845EBEEB0B22FACAFEF6EBECEF13D"
}
},
{
"_index": "bt",
"_type": "text",
"_id": "4930363",
"_score": 1,
"_source": {
"name": "X-Men - Days of Future Past [BDRip-720p] (www.kinokopilka.tv)",
"keywords": "X-Men,Future,[BDRip-720p]",
"url": "C031DA00FE0C3A77C66CAE6BDC88DBDED843984D"
}
},
{
"_index": "bt",
"_type": "text",
"_id": "4930364",
"_score": 1,
"_source": {
"name": "Hatufim.S02.720p.WEBRip.AAC2.0.H.264-HRiP",
"keywords": "",
"url": "327C175612A471708D76C0BC609ADC049F2FB40A"
}
}
]
}
}

可选参数

一 . timenout

GET /_search?timeout=18ms

注:timenout并不会在过期后终止查询,查询在内部还在进行,只是es会返回收集到当前时间节点的数据

二 . 查询区间

参数 介绍
/_search 在所有的索引中搜索所有的类型
/gb/_search 在 gb 索引中搜索所有的类型
/gb,us/_search 在 gb 和 us 索引中搜索所有的文档
/g*,u*/_search 在任何以 g 或者 u 开头的索引中搜索所有的类型
/gb/user/_search 在 gb 索引中搜索 user 类型
/gb,us/user,tweet/_search 在 gb 和 us 索引中搜索 user 和 tweet 类型
/_all/user,tweet/_search 在所有的索引中搜索 user 和 tweet 类型

分页

1
curl -X GET "localhost:9200/_search?size=5"
GET /_search ?size=5 &from=10
请求方式 搜索参数 每页条数 第几页

深度分页

一个比较有意思的概念,假如我们有5个主分片。需要查询前10条数据详情,ES内部会在5个分片上分别查询前10个然后对比提取出前10个。也就是5*10-10的浪费率。任何web查询搜索引擎都要超过1000个性能损耗极大。

轻量搜索

有两种查询方法,一种是轻量级通过get参数的形式查询,一种是放在结构体中的查询。

查询bt索引的type等于text,name包含es的数据

1
2
curl -X GET "localhost:9200/bt/text/_search?q=name:es

查询 name包含test并且keywords包含Hoepli的数据

1
2
3
4
表达式为 name:test +keywords:Hoepli
UrlEncode 为 name%3atest+%2bkeywords%3aHoepli

curl -X GET "localhost:9200/bt/text/_search?q=name%3atest+%2bkeywords%3aHoepli

不指定字段查找

es 内部会把所有字段拼接成一段话提供查找

1
2
curl -X GET "localhost:9200/_search?q=mary"

加深下难度

  1. name 字段中包含 mary 或者 john
  2. date 值大于 2014-09-10
  3. _all 字段包含 aggregations 或者 geo

get请求都需要url编码

1
2
+name:(mary john) +date:>2014-09-10 +(aggregations geo)

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

请我喝杯咖啡吧~