我想用Python飞快移动进行模糊搜索,但我不想'得到它。我已经尝试在NGRAMWORDS的帮助下进行模糊搜索。
这是我的架构:
schema = Schema(id=ID(stored=True),
name=NGRAMWORDS(minsize=2, maxsize=4, stored=True, queryor=True),
street=NGRAMWORDS(minsize=2, maxsize=4, stored=True, queryor=True),
city=NGRAMWORDS(minsize=2, maxsize=4, stored=True, queryor=False))
然后如下所述填写索引:
writer.add_document(id=unicode(row["id"]), name=unicode(row["name"]), street=unicode(row["street"]), city=unicode(row["city"]))
不幸的是,在搜索时,没有从索引中检索到结果:
with self.index.searcher() as searcher:
from whoosh.query import Term, Or, FuzzyTerm
from whoosh.analysis import NgramWordAnalyzer
ngramAnalyzer = NgramWordAnalyzer(minsize=2, maxsize=4)
tokens = [token.text for token in ngramAnalyzer(unicode(name))]
fetig = list()
for t in tokens:
tt = FuzzyTerm("name", unicode(t))
fetig.append(tt)
myQuery = Or(fetig)
res = searcher.search(myQuery, limit=10)
我在搜索" Ali":
时获得零点击<Top 0 Results for Or([FuzzyTerm('name', u'al', boost=1.000000, maxdist=1, prefixlength=1), FuzzyTerm('name', u'ali', boost=1.000000, maxdist=1, prefixlength=1), FuzzyTerm('name', u'li', boost=1.000000, maxdist=1, prefixlength=1)]) runtime=0.000411987304688>
答案 0 :(得分:0)
现在解决了。问题是已存在的索引未通过
打开index = open_dir("index", schema=self.schema)
而我创造了一个新的。
此外,在查询中,使用 Term 而不是 FuzzyTerm 以获得合理的结果至关重要:
ngramAnalyzer = NgramWordAnalyzer(minsize=3, maxsize=6)
tokens = [token.text for token in ngramAnalyzer(unicode(name))]
fetig = list()
for t in tokens:
tt = Term("name", unicode(t))
fetig.append(tt)
myQuery = Or(fetig)
res = searcher.search(myQuery, limit=10)
正如您所看到的,我已将NGRAMWORDS的 minsize 增加到3而不是2。
感谢宝贵的工作,Matt Chaput。