-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/flask_example_v2.2'
- Loading branch information
Showing
10 changed files
with
181 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ def create_app(flask_config_name=None): | |
|
||
from app.model.userModel import User | ||
|
||
me = User('admin', '[email protected]',None, None) | ||
me = User('admin', '[email protected]', None, None) | ||
daoPool.sqlDAO.session.add(me) | ||
daoPool.sqlDAO.session.commit() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# -*- coding: utf-8 -*- | ||
import json, re | ||
import os | ||
|
||
from flask_restplus import reqparse, Resource, Api, fields, Namespace, abort | ||
from flask import request, session, make_response, redirect, Response | ||
from elasticsearch_dsl import Q, A, Search | ||
|
||
from app.util.regToolBox import RegToolBox | ||
from app.util.exceptionHandler import ExcepitonHandler | ||
from app.dao import daoPool | ||
|
||
|
||
api = Namespace('es', description='Es operation') | ||
esDAO = daoPool.esDAO | ||
|
||
regBox = RegToolBox() | ||
excpHandler = ExcepitonHandler() | ||
|
||
|
||
## Request parsing | ||
parser = reqparse.RequestParser() | ||
parser.add_argument('from', type=int, required=True, help='Offset from the first result') | ||
parser.add_argument('size', type=int, required=True, help='Amount of cve item to be returned') | ||
|
||
parserAgg = reqparse.RequestParser() | ||
parserAgg.add_argument('size', type=int, required=True, help='Number of item to be returned') | ||
|
||
@api.route('/search') | ||
class GoogleLogin(Resource): | ||
"""google resource""" | ||
def __init__(self, Resource): | ||
self.api = api | ||
esDAO.setIndexAndType("cve", "detail") | ||
|
||
|
||
@api.expect(parser) | ||
@api.doc(description='ESDAO Search example') | ||
def get(self): | ||
""" Return es result """ | ||
args = parser.parse_args() | ||
|
||
size = args['size'] | ||
fromOffset = args['from'] | ||
|
||
q = Q() | ||
sortQ = {} | ||
|
||
result = {} | ||
resultList = [] | ||
resp = esDAO.search(q, sortQ, fromOffset, size) | ||
|
||
for hit in resp.to_dict()['hits']['hits']: | ||
resultList.append(hit) | ||
|
||
result['result'] = resultList | ||
|
||
return result | ||
|
||
@api.route('/aggs') | ||
class EsAggs(Resource): | ||
""" """ | ||
def __init__(self, Resource): | ||
self.api = api | ||
esDAO.setIndexAndType("cve", "detail") | ||
|
||
|
||
@api.expect(parserAgg) | ||
@api.doc(description='ESDAO Aggregation example' \ | ||
'Get top `size` recent item') | ||
def get(self): | ||
""" get top recent result """ | ||
args = parserAgg.parse_args() | ||
|
||
topSize = args['size'] | ||
|
||
q = Q() | ||
s = Search().query(q) | ||
sortDate = [{"original_release_date": {"order": "desc"}}] | ||
a_date = A('top_hits', sort=sortDate, size=topSize) | ||
s.aggs.metric('_topDate', a_date) | ||
|
||
result = {} | ||
resultList = [] | ||
resp = esDAO.aggs(s).to_dict() | ||
for hit in resp['_topDate']['hits']['hits']: | ||
resultList.append(hit) | ||
|
||
result['_topDate'] = resultList | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import sys | ||
import datetime | ||
import logging | ||
from elasticsearch import Elasticsearch | ||
from flask_restplus import reqparse, Resource | ||
from elasticsearch_dsl import Search, Q, A | ||
|
||
|
||
reload(sys) | ||
sys.setdefaultencoding('utf-8') | ||
logger = logging.getLogger('ESDAO') | ||
class ElasticsearchDAO(): | ||
client = None | ||
indexES = None | ||
typeES = None | ||
esearch = None | ||
def __init__(self, host, port): | ||
url = "%s:%s" % (host, port) | ||
try: | ||
self.client = Elasticsearch([url], send_get_body_as="POST") | ||
except: | ||
logger.error('elasticsearch cannot connect') | ||
|
||
def setIndexAndType(self, index, type): | ||
self.indexES = index | ||
self.typeES = type | ||
self.esearch = Search(using=self.client, index=self.indexES, doc_type=self.typeES) | ||
|
||
|
||
def saveJson(self, json): | ||
"""put json to ES """ | ||
res = self.client.index(index=self.indexES, doc_type=self.typeES, body=json) | ||
return res | ||
|
||
|
||
def aggs(self, searchQuery): | ||
s = self.esearch.query() | ||
s.aggs = searchQuery.aggs | ||
s.query = searchQuery.query | ||
s = s[0:0] | ||
response = s.execute() | ||
|
||
return response.aggregations | ||
|
||
def search(self, q, sortQ, fromOffset, size): | ||
s = self.esearch.query(q) \ | ||
.sort({"_score":{"order":"desc"}}, sortQ) | ||
s = s[fromOffset:fromOffset+size] | ||
response = s.execute() | ||
|
||
return response | ||
|
||
|