-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
73 lines (56 loc) · 2.17 KB
/
models.py
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
import xapian
import hashlib
import os
import sys
class UploadModel:
def get_file(self, myfile):
return
def upload_file(self, myfile, dbpath="db"):
contents = myfile.file.read()
contents.decode('utf-8', 'ignore')
filename = hashlib.md5(contents).hexdigest() + ".txt"
filepath = "lib/" + filename[:2] + "/" + filename[2:4] + "/"
if not os.path.exists(os.path.dirname(filepath + filename)):
os.makedirs(os.path.dirname(filepath))
with open(filepath + filename, "w") as fileout:
fileout.write(contents)
# Indexing new file
db = xapian.WritableDatabase(dbpath, xapian.DB_CREATE_OR_OPEN)
indexer = xapian.TermGenerator()
stemmer = xapian.Stem("english")
indexer.set_stemmer(stemmer)
doc = xapian.Document()
doc.set_data(contents)
doc.add_value(0, filepath + filename)
indexer.set_document(doc)
indexer.index_text(contents)
# Use the identifier to prevent duplicating documents
idterm = u"Q" + filename
doc.add_boolean_term(idterm)
db.replace_document(idterm, doc)
return filename
class SearchModel:
matches = None
def add_query_to_file(self, query_string):
with open("searchlog.txt", "ab") as myfile:
myfile.write(query_string + '\n')
def search_in_database(self, query_string, dbpath="db"):
try:
# Open the database for searching.
db = xapian.Database(dbpath)
# Start an enquire session.
enquire = xapian.Enquire(db)
# Parse the query string to produce a Xapian::Query object.
qp = xapian.QueryParser()
stemmer = xapian.Stem("english")
qp.set_stemmer(stemmer)
qp.set_database(db)
qp.set_stemming_strategy(xapian.QueryParser.STEM_SOME)
query = qp.parse_query(query_string)
# TODO add logs
# Find the top 10 results for the query.
enquire.set_query(query)
self.matches = enquire.get_mset(0, 10)
except xapian.DatabaseModifiedError:
db.reopen()
raise