2024-07-15 14:23:14 +00:00
|
|
|
import xapian
|
|
|
|
|
2024-07-18 15:21:22 +00:00
|
|
|
# database = xapian.WritableDatabase("db", xapian.DB_CREATE_OR_OPEN)
|
2024-07-15 14:23:14 +00:00
|
|
|
|
2024-07-18 15:21:22 +00:00
|
|
|
# Read Only
|
|
|
|
# database = xapian.Database("db")
|
|
|
|
|
|
|
|
# indexer = xapian.TermGenerator()
|
|
|
|
# stemmer = xapian.Stem("english")
|
|
|
|
# indexer.set_stemmer(stemmer)
|
2024-07-15 14:23:14 +00:00
|
|
|
|
|
|
|
|
2024-10-04 15:32:53 +00:00
|
|
|
def search(institution, qs, offset=0, limit=10):
|
2024-07-18 15:21:22 +00:00
|
|
|
database = xapian.Database("db")
|
2024-07-15 14:23:14 +00:00
|
|
|
|
|
|
|
qp = xapian.QueryParser()
|
|
|
|
qp.set_database(database)
|
2024-07-18 15:21:22 +00:00
|
|
|
qp.set_stemmer(xapian.Stem("english"))
|
2024-07-15 14:23:14 +00:00
|
|
|
qp.set_stemming_strategy(xapian.QueryParser.STEM_SOME)
|
2024-07-18 15:21:22 +00:00
|
|
|
qp.add_prefix("uuid", "uuid")
|
|
|
|
# qp.add_prefix("snapshot", "snapshot")
|
|
|
|
query = qp.parse_query(qs)
|
2024-10-04 15:32:53 +00:00
|
|
|
institution_term = "U{}".format(institution.id)
|
|
|
|
final_query = xapian.Query(
|
|
|
|
xapian.Query.OP_AND, query, xapian.Query(institution_term)
|
|
|
|
)
|
2024-07-15 14:23:14 +00:00
|
|
|
enquire = xapian.Enquire(database)
|
2024-10-04 15:32:53 +00:00
|
|
|
enquire.set_query(final_query)
|
2024-07-15 14:23:14 +00:00
|
|
|
matches = enquire.get_mset(offset, limit)
|
|
|
|
return matches
|
|
|
|
|
2024-07-18 15:21:22 +00:00
|
|
|
|
2024-10-04 15:32:53 +00:00
|
|
|
def index(institution, uuid, snap):
|
2024-07-18 15:21:22 +00:00
|
|
|
uuid = 'uuid:"{}"'.format(uuid)
|
|
|
|
try:
|
2024-10-04 15:32:53 +00:00
|
|
|
matches = search(institution, uuid, limit=1)
|
2024-07-18 15:21:22 +00:00
|
|
|
if matches.size() > 0:
|
|
|
|
return
|
2024-09-03 14:41:27 +00:00
|
|
|
except (xapian.DatabaseNotFoundError, xapian.DatabaseOpeningError):
|
2024-07-18 15:21:22 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
database = xapian.WritableDatabase("db", xapian.DB_CREATE_OR_OPEN)
|
|
|
|
indexer = xapian.TermGenerator()
|
|
|
|
stemmer = xapian.Stem("english")
|
|
|
|
indexer.set_stemmer(stemmer)
|
|
|
|
|
|
|
|
doc = xapian.Document()
|
|
|
|
doc.set_data(snap)
|
|
|
|
|
|
|
|
indexer.set_document(doc)
|
|
|
|
indexer.index_text(snap)
|
|
|
|
indexer.index_text(uuid, 10, "uuid")
|
|
|
|
# indexer.index_text(snap, 1, "snapshot")
|
2024-10-04 15:32:53 +00:00
|
|
|
institution_term = "U{}".format(institution.id)
|
|
|
|
doc.add_term(institution_term)
|
2024-07-18 15:21:22 +00:00
|
|
|
|
|
|
|
database.add_document(doc)
|