diff --git a/frappe/modules/full_text_search.py b/frappe/modules/full_text_search.py new file mode 100644 index 0000000000..de0ee375b5 --- /dev/null +++ b/frappe/modules/full_text_search.py @@ -0,0 +1,38 @@ +# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors +# MIT License. See license.txt + +from __future__ import unicode_literals +import frappe +import os +from whoosh.index import create_in, open_dir +from whoosh.fields import TEXT, ID, Schema + + +def build_index(index_name, documents): + schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT) + + index_dir = os.path.join(frappe.utils.get_bench_path(), "indexes", index_name) + frappe.create_folder(index_dir) + + ix = create_in(index_dir, schema) + writer = ix.writer() + + for document in documents: + writer.add_document( + title=document.title, path=document.path, content=document.content + ) + + writer.commit() + + +def search(index_name, text): + from whoosh.qparser import QueryParser + + index_dir = os.path.join(frappe.utils.get_bench_path(), "indexes", index_name) + ix = open_dir(index_dir) + + with ix.searcher() as searcher: + query = QueryParser("content", ix.schema).parse(text) + results = searcher.search(query) + + return results diff --git a/requirements.txt b/requirements.txt index 431f216afa..5d46f799be 100644 --- a/requirements.txt +++ b/requirements.txt @@ -66,3 +66,4 @@ watchdog==0.8.0 Werkzeug==0.16.1 xlrd==1.2.0 zxcvbn-python==4.4.24 +Whoosh==2.7.4