Fixed Xapian search: issue #19 #34

Open
rskthomas wants to merge 3 commits from issue_19 into main
1 changed files with 20 additions and 1 deletions
Showing only changes of commit b7d7b9041d - Show all commits

View File

@ -1,4 +1,5 @@
import xapian import xapian
from datetime import datetime
# database = xapian.WritableDatabase("db", xapian.DB_CREATE_OR_OPEN) # database = xapian.WritableDatabase("db", xapian.DB_CREATE_OR_OPEN)
@ -28,11 +29,17 @@ def search(institution, qs, offset=0, limit=10):
) )
enquire = xapian.Enquire(database) enquire = xapian.Enquire(database)
enquire.set_query(final_query) enquire.set_query(final_query)
enquire.set_sort_by_value_then_relevance(0, True)
#colapse key is device_id
enquire.set_collapse_key(1)
matches = enquire.get_mset(offset, limit) matches = enquire.get_mset(offset, limit)
return matches return matches
def index(institution, uuid, snap): def index(institution, device_id, uuid, timestamp, snap):
uuid = 'uuid:"{}"'.format(uuid) uuid = 'uuid:"{}"'.format(uuid)
matches = search(institution, uuid, limit=1) matches = search(institution, uuid, limit=1)
if matches and matches.size() > 0: if matches and matches.size() > 0:
@ -50,6 +57,18 @@ def index(institution, uuid, snap):
indexer.index_text(snap) indexer.index_text(snap)
indexer.index_text(uuid, 10, "uuid") indexer.index_text(uuid, 10, "uuid")
# indexer.index_text(snap, 1, "snapshot") # indexer.index_text(snap, 1, "snapshot")
# store device_id, uuid and timestamp
doc.add_value(1, device_id)
doc.add_value(2, str(uuid))
try:
timestamp_dt = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')
timestamp_unix = timestamp_dt.timestamp()
doc.add_value(0, xapian.sortable_serialise(timestamp_unix))
except ValueError as e:
print(f"Error parsing timestamp: {e}")
institution_term = "U{}".format(institution.id) institution_term = "U{}".format(institution.id)
doc.add_term(institution_term) doc.add_term(institution_term)