Merge branch 'text-search-whoosh-2020-05-19' into page-builder-with-text-search-whoosh-2020-05-24

This commit is contained in:
Faris Ansari 2020-05-24 08:44:51 +05:30
commit 23b00f1ccf
2 changed files with 39 additions and 0 deletions

View file

@ -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

View file

@ -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