1. 描述:ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。下面介绍了利用Python API接口进行数据查询,方便其他系统的调用。
    2. 安装API
    3. 1
    4. pip install elasticsearch
    5. 建立es连接
    6. 1
    7. 2
    8. from elasticsearch import Elasticsearch
    9. es = Elasticsearch([{'host':'10.10.13.12','port':9200}])
    10.  
    11. # 如果索引不存在,则创建索引
    12. if _es.indices.exists(index='blog_index') is not True:
    13. _es.indices.create(index='blog_index', body=_index_mappings)
    14. # 将refresh设为true,使得添加的文档可以立即搜索到;
    15. # 默认为false,可能会导致下面的search没有结果
    16. _es.index(index='blog_index', doc_type='user', refresh=True, body=_doc)
    17. # 查询所有记录结果
    18. print('Search all...', flush=True)
    19. _query_all = {
    20. 'query': {
    21. 'match_all': {}
    22. }
    23. }
    24. _searched = _es.search(index='blog_index', doc_type='user', body=_query_all)
    25. print(_searched, flush=True)
    26. # 输出查询到的结果
    27. for hit in _searched['hits']['hits']:
    28. print(hit['_source'], flush=True)
    29. 数据检索功能
    30. 1
    31. es.search(index='logstash-2015.08.20', q='http_status_code:5* AND server_name:"web1"', from_='124119')
    32. 常用参数
    33. index - 索引名
    34. q - 查询指定匹配 使用Lucene查询语法
    35. from_ - 查询起始点 默认0
    36. doc_type - 文档类型
    37. size - 指定查询条数 默认10
    38. field - 指定字段 逗号分隔
    39. sort - 排序 字段:asc/desc
    40. body - 使用Query DSL
    41. scroll - 滚动查询
    42. 统计查询功能
    43. # 语法同search大致一样,但只输出统计值
    44. 1
    45. 2
    46. In[52]: es.count(index='logstash-2015.08.21', q='http_status_code:500')
    47. Out[52]:{u'_shards':{u'failed':0, u'successful':5, u'total':5}, u'count':17042}
    48.   
    49. 知识扩展
    50. 滚动demo
    51. 1
    52. 2
    53. 3
    54. 4
    55. 5
    56. 6
    57. 7
    58. 8
    59. 9
    60. 10
    61. 11
    62. 12
    63. 13
    64. 14
    65. 15
    66. 16
    67. 17
    68. 18
    69. 19
    70. 20
    71. 21
    72. 22
    73. 23
    74. 24
    75. # Initialize the scroll
    76. page = es.search(
    77. index ='yourIndex',
    78. doc_type ='yourType',
    79. scroll ='2m',
    80. search_type ='scan',
    81. size =1000,
    82. body ={
    83. # Your query's body
    84. })
    85. sid = page['_scroll_id']
    86. scroll_size = page['hits']['total']
    87. # Start scrolling
    88. while(scroll_size >0):
    89. print "Scrolling..."
    90. page = es.scroll(scroll_id = sid, scroll ='2m')
    91. # Update the scroll ID
    92. sid = page['_scroll_id']
    93. # Get the number of results that we returned in the last scroll
    94. scroll_size = len(page['hits']['hits'])
    95. print "scroll size: "+ str(scroll_size)
    96. # Do something with the obtained page
    97. 以上demo实现了一次取若干数据,数据取完之后结束,不会获取到最新更新的数据。我们滚动完之后想获取最新数据怎么办?滚动的时候会有一个统计值,如total: 5。跳出循环之后,我们可以用_from参数定位到5开始滚动之后的数据。
    98. Query DSL
    99. range过滤器查询范围
    100. gt: > 大于
    101. lt: < 小于
    102. gte: >= 大于或等于
    103. lte: <= 小于或等于
    104. 1
    105. 2
    106. 3
    107. 4
    108. 5
    109. 6
    110. "range":{
    111. "money":{
    112. "gt":20,
    113. "lt":40
    114. }
    115. }
    116.   
    117. bool组合过滤器
    118. must:所有分句都必须匹配,与 AND 相同。
    119. must_not:所有分句都必须不匹配,与 NOT 相同。
    120. should:至少有一个分句匹配,与 OR 相同。
    121. 1
    122. 2
    123. 3
    124. 4
    125. 5
    126. 6
    127. 7
    128. {
    129. "bool":{
    130.   "must":[],
    131.   "should":[],
    132.   "must_not":[],
    133. }
    134. }
    135. term过滤器
    136. term单过滤
    137. 1
    138. 2
    139. 3
    140. 4
    141. 5
    142. {
    143. "terms":{
    144.   "money":20
    145. }
    146. }
    147.   
    148. terms复数版本,允许多个匹配条件
    149. 1
    150. 2
    151. 3
    152. 4
    153. 5
    154. {
    155. "terms":{
    156.   "money": [20,30]
    157. }
    158. }
    159. 正则查询
    160. 1
    161. 2
    162. 3
    163. 4
    164. 5
    165. {
    166. "regexp": {
    167. "http_status_code": "5.*"
    168. }
    169. }
    170. match查询
    171. match 精确匹配
    172. 1
    173. 2
    174. 3
    175. 4
    176. 5
    177. {
    178. "match":{
    179.   "email":"123456@qq.com"
    180. }
    181. }
    182. multi_match 多字段搜索
    183. 1
    184. 2
    185. 3
    186. 4
    187. 5
    188. 6
    189. {
    190. "multi_match":{
    191.   "query":"11",
    192.   "fields":["Tr","Tq"]
    193. }
    194. }
    195. demo
    196. 获取最近一小时的数据
    197. 1
    198. 2
    199. 3
    200. 4
    201. 5
    202. 6
    203. 7
    204. 8
    205. 9
    206. {'query':
    207. {'filtered':
    208. {'filter':
    209. {'range':
    210. {'@timestamp':{'gt':'now-1h'}}
    211. }
    212. }
    213. }
    214. }
    215.   
    216. 条件过滤查询
    217. 1
    218. 2
    219. 3
    220. 4
    221. 5
    222. 6
    223. 7
    224. 8
    225. {
    226. "query":{
    227. "filtered":{
    228. "query":{"match":{"http_status_code":500}},
    229. "filter":{"term":{"server_name":"vip03"}}
    230. }
    231. }
    232. }
    233. Terms Facet 单字段统计
    234. 1
    235. 2
    236. 3
    237. 4
    238. 5
    239. 6
    240. 7
    241. 8
    242. 9
    243. 10
    244. {'facets':
    245. {'stat':
    246. {'terms':
    247. {'field':'http_status_code',
    248. 'order':'count',
    249. 'size':50}
    250. }
    251. },
    252. 'size':0
    253. }
    254.    
    255. 一次统计多个字段
    256. 1
    257. 2
    258. 3
    259. 4
    260. 5
    261. 6
    262. 7
    263. 8
    264. 9
    265. 10
    266. {'facets':
    267. {'cip':
    268. {'terms':
    269. {'fields':['client_ip']}},
    270. 'status_facets':{'terms':{'fields':['http_status_code'],
    271. 'order':'term',
    272. 'size':50}}},
    273. 'query':{'query_string':{'query':'*'}},
    274. 'size':0
    275. }
    276.   
    277. 多个字段一起统计
    278. 1
    279. 2
    280. 3
    281. 4
    282. 5
    283. 6
    284. 7
    285. 8
    286. 9
    287. 10
    288. 11
    289. 12
    290. {'facets':
    291. {'tag':
    292. {'terms':
    293. {'fields':['http_status_code','client_ip'],
    294. 'size':10
    295. }
    296. }
    297. },
    298. 'query':
    299. {'match_all':{}},
    300. 'size':0
    301. }
    302.   
    303. 数据组装
    304. 以下是kibana首页的demo,用来统计一段时间内的日志数量
    305. 1
    306. 2
    307. 3
    308. 4
    309. 5
    310. 6
    311. 7
    312. 8
    313. 9
    314. 10
    315. 11
    316. 12
    317. 13
    318. 14
    319. 15
    320. 16
    321. 17
    322. 18
    323. 19
    324. 20
    325. 21
    326. 22
    327. 23
    328. 24
    329. 25
    330. 26
    331. 27
    332. 28
    333. 29
    334. 30
    335. 31
    336. 32
    337. 33
    338. 34
    339. 35
    340. 36
    341. 37
    342. 38
    343. 39
    344. 40
    345. 41
    346. 42
    347. 43
    348. 44
    349. {
    350. "facets": {
    351. "0": {
    352. "date_histogram": {
    353. "field": "@timestamp",
    354. "interval": "5m"
    355. },
    356. "facet_filter": {
    357. "fquery": {
    358. "query": {
    359. "filtered": {
    360. "query": {
    361. "query_string": {
    362. "query": "*"
    363. }
    364. },
    365. "filter": {
    366. "bool": {
    367. "must": [
    368. {
    369. "range": {
    370. "@timestamp": {
    371. 'gt': 'now-1h'
    372. }
    373. }
    374. },
    375. {
    376. "exists": {
    377. "field": "http_status_code.raw"
    378. }
    379. },
    380. # --------------- -------
    381. # 此处加匹配条件
    382. ]
    383. }
    384. }
    385. }
    386. }
    387. }
    388. }
    389. }
    390. },
    391. "size": 0
    392. }
    393.   
    394. 如果想添加匹配条件,在以上代码标识部分加上过滤条件,按照以下代码格式即可
    395. 1
    396. 2
    397. 3
    398. 4
    399. 5
    400. {
    401. "query": {
    402. "query_string": {"query": "backend_name:baidu.com"}
    403. }
    404. },