def parse_collection(collection): works = [] docs = json.loads(collection) for doc in docs: corpus, nn = doc.split(",") c = Corpus(corpus) for n in nn.strip("[]").split(", "): w = c.work_by_docix(int(n)) works.append(w) return Collection(works=works)
def update(corpus, docix, path): """Reindex a document by index number. """ docix = int(docix) with click_spinner.spinner(): c = Corpus(corpus) c.update(docix=docix, path=Path(path)) if c.work_by_docix(docix).searchable: click.echo(f"[+] updated document {docix} in '{corpus}'") else: click.echo(f"[-] failed")
def history(): db.connect() history = [] for h in Search.select(): history.append(h) db.close() kwargs = request.args id = kwargs.get("id") db.connect() s = Search.get_by_id(id) db.close() works = [] work_ids = json.loads(s.collection) for work_id in work_ids: corpus, docix = work_id.split(",") c = Corpus(corpus) w = c.work_by_docix(int(docix)) works.append(w) results = [ r.html for r in SearchResult.select().where(SearchResult.search == s) ] response = { "version": __version__, "collection": s.collection, "works": works, "corpora": _corpora, "query": s.query, "history": history, "results": results, "count": len(s.results), "id": id } return render_template("index.html", **response)
def search(): if request.method == "POST": form = request.form else: form = request.args collection = form.get("collection") query = form.get("query") works = [] ids = json.loads(collection) for id in ids: corpus, n = id.split(",") c = Corpus(corpus) w = c.work_by_docix(int(n)) works.append(w) results = search_request(works, query) if results: count_matches, count_documents, _ = results.count db.connect() try: s = (Search.get(query=query, collection=collection), ) except Search.DoesNotExist: names = [f"{work.author}, {work.title}" for work in works] s = Search.create( query=query, collection=collection, prettified=f"{'; '.join(names)}", count_matches=count_matches, count_documents=count_documents, minscore=results.minscore, top=results.top, start_dt=results.start_dt, end_dt=results.end_dt, maxchars=results.maxchars, surround=results.surround, ) s.save() finally: for href in results.highlights: r = SearchResult.get_or_create(search=s, html=next(as_html([ href, ]))) if r[1]: r[0].save() db.close() db.connect() history = [] for h in Search.select(): history.append(h) db.close() response = { "version": __version__, "collection": collection, "works": works, "corpora": _corpora, "query": query, "history": history, "results": as_html(results.highlights) if results else [], "count": results.count if results else (0, 0), "id": history[-1].id } return render_template("index.html", **response)