【发布时间】:2020-01-30 06:08:32
【问题描述】:
我有一个搜索几个数据库的网络应用程序,保存的一些数据是大写的,一些是大小写混合的,但是在搜索关键字时,我希望它忽略大小写,只显示结果匹配单词。例如,我想搜索“document_reference”而不必编写正确的保存方式,即“Document_Reference”
我被告知要在我的索引中添加不区分大小写的功能,但我不确定该做什么或在那里添加, 我试过这个(在 whoosh 文档中找到)
class CaseSensitivizer(analysis.Filter):
def __call__(self, tokens):
for t in tokens:
yield t
if t.mode == "index":
low = t.text.lower()
if low != t.text:
t.text = low
yield t
这就是我的索引和查询解析器的样子
def open_index(indexDirectory):
# open index and return a idex object
ix = index.open_dir(indexDirectory)
return ix
def search_index(srch, ix):
# Search the index and print results
# ix = open_index(indexDirectory)
results = ''
lst = []
qp = MultifieldParser(['Text', 'colname',
'tblname', 'Length', 'DataType', 'tag_name'],
schema=ix.schema, group=qparser.OrGroup)
# qp = QueryParser('Text', schema=ix.schema)
q = qp.parse(srch)
with ix.searcher() as s:
results = s.search(q, limit=None)
for r in results:
print('\n', r)
lst.append(r.fields())
if(DEBUG):
print('Search Results:\n', lst)
print('\nFinished in search.py')
return lst
目前它只会给出与我在搜索栏中输入的内容完全匹配的结果,所以如果我输入“文档”但源实际上存储为“文档”,我不会得到任何结果
【问题讨论】:
标签: python flask indexing case-insensitive whoosh