Refactor to use postgresql end to end
This commit is contained in:
parent
8605ee6b2c
commit
20d198e559
5 changed files with 144 additions and 72 deletions
|
|
@ -1,30 +1,30 @@
|
|||
#!/bin/bash
|
||||
#!/usr/bin/python3
|
||||
from sqlalchemy import create_engine
|
||||
from config import DATABASE_URI
|
||||
from models import Base, Tokens
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from flask import Flask
|
||||
from flask import Request
|
||||
import json
|
||||
from urllib.parse import unquote
|
||||
|
||||
app = Flask(__name__)
|
||||
## Todo - Boolean search (AND/OR/NOT/"")
|
||||
engine = create_engine(DATABASE_URI)
|
||||
Base.metadata.create_all(engine)
|
||||
Session = sessionmaker(bind=engine)
|
||||
# Todo - Boolean search (AND/OR/NOT/"")
|
||||
|
||||
|
||||
@app.route("/search/<query>")
|
||||
def search(query):
|
||||
with open('data/index.json', 'r') as index_json:
|
||||
index = json.load(index_json)
|
||||
query = unquote(query)
|
||||
query_split = query.split()
|
||||
result = []
|
||||
for q in query_split:
|
||||
q = q.lower()
|
||||
if q in index:
|
||||
for item in index[q]:
|
||||
matching_results = list(filter(lambda entry: entry['url'] == item["url"], result))
|
||||
if len(matching_results) == 0:
|
||||
result.append(item)
|
||||
else:
|
||||
matching_results[0]["count"] += item["count"]
|
||||
return result
|
||||
|
||||
def handle_and():
|
||||
pass
|
||||
session = Session()
|
||||
result = []
|
||||
query_words = unquote(query).split()
|
||||
for word in query_words:
|
||||
word = word.lower()
|
||||
matching_token = session.query(Tokens).filter_by(token=word).first()
|
||||
if session is None:
|
||||
continue
|
||||
for document_token in matching_token.document_tokens:
|
||||
|
||||
result.append(document_token.document.url)
|
||||
return result
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue