def hello_dsl(): if request.method == 'GET': return render_template('dsl_form.html') elif request.method == 'POST': data = { "query": request.form['query'], "submit_value":request.form['submit'] } query = (data["query"]) cql = dict.check_dsl_string_boolean(query) dsl = dict.construct_dynamic_dsl_boolean_query(cql) return render_template('dsl_results.html',data =dsl)
def getlts(): if (request.method == 'POST'): content = request.get_json(silent=True) txt = content["text"] chk_dsl = dict.check_dsl_string_boolean(txt) b = dict.construct_dynamic_dsl_boolean_query(chk_dsl) # a = json.loads(b) # LOG Users entries and genrated dsl from pymongo import MongoClient client = MongoClient("mongodb://localhost:27017") database = client["api"] collection = database["logs"] collection.insert_one({"nlq": txt, "check_dsl": chk_dsl, "tdsl": b}) url = 'http://35.244.98.50:9200/question/so/_search' r = requests.post('http://35.244.98.50:9200/question/so/_search', json=json.loads(b)) print(r.json()) data = r.json() # data = r.text() # noposts = (data['hits']['total']) # total hits found! # header = 'I found ' + str(noposts) + ' posts, here are few examples: \n' # d = {} # post = '' # for i in range(0,3): # title = (data['hits']['hits'][0]['_source']['title']) # Title # body_sliced = data['hits']['hits'][0]['_source']['body'] # Body # body = (body_sliced[:189]) # link = "https://www.stackoverflow.com/questions/" + str(data['hits']['hits'][0]['_source']['question_id']) # api = data['hits']['hits'][0]['_source']['api'] # api = ' '.join(str(x) for x in api) # topic = data['hits']['hits'][0]['_source']['topic'][:10] # topic = " ".join(str(x) for x in topic) # post = post + title +'\n'+ body +'\n'+ '*TOPIC*' + topic +'\n'+ '`API`' + api +'\n' return jsonify(data), 200
import requests, dict, json, html content = {"text": "access tokens and security and facebook"} print(content['text']) txt = content["text"] # GET THE TEXT FROM RIVESCRIPT # Convert the text into a TDSL a = dict.check_dsl_string_boolean(txt) # print(a) b = dict.construct_dynamic_dsl_boolean_query(a) a = json.loads(b) # now we have json body for a request to make for the search r = requests.post('http://35.244.98.50:9200/question/so/_search', json=a) # print(r.status_code,r.json()) data = r.json() noposts = (data['hits']['total']) # total hits found! title = "I found " + str(noposts) + " number of posts, here are few examples:" d = {} d['text'] = title attach = [] for i in range(0, 3): title = (data['hits']['hits'][i]['_source']['title']) # Title body_sliced = data['hits']['hits'][i]['_source']['body'] # Body #body = (html.escape(body_sliced[:200])) # sliced body and escpaed from special chars then converted to string .encode('ascii', 'xmlcharrefreplace')).decode("utf-8") body = ( body_sliced[:200] ) # sliced body and escpaed from special chars then converted to string .encode('ascii', 'xmlcharrefreplace')).decode("utf-8") p = {} # body_sliced = data[:200]