Skip to main content
 首页 » 编程设计

python之用数字搜索之python之whoosh

2024年10月24日11qq号

我已经用这样的模式为我的所有文档编制了索引:

ID = ID(stored=True) 
Body = TEXT(analyzer=StemmingAnalyzer(), stored=False,field_boost=4.0) 
Name = TEXT(stored=True, field_boost=5.0) 
Brand= TEXT(StemmingAnalyzer(),stored=False, field_boost=4.0) 
... 

我的搜索模块如下所示:

qp = MultifieldParser(["Name", "Body", "Brand",  
"Familia","Superpadre","Tags","ID"], schema=ix.schema) 

但是当我搜索iphone 6时,它是这样查询的:

<Top 20 Results for Or([Term('Name', u'iphone'), Term('Body', 
 u'iphon'), Term('Brand', u'iphon'), Term('Familia', u'iphon'),  
Term('Superpadre', u'iphon'), And([Term('Tags', u'iphone'),   
Term('Tags', u'6')]), Term('ID', u'iphon')]) runtime=0.0327291488647> 

只搜索TAGS中的数字6,不搜索名称、品牌等

你能帮我在其他领域也搜索一下吗?

提前谢谢大家。

请您参考如下方法:

所有单字词在 Whoosh 中默认被视为停用词并被忽略。这意味着所有字母和数字都将被忽略。

stop words are words which are filtered out before or after processing of natural language data (text). (ref)

您可以检查 StopFilter默认情况下将 minsize = 2 添加到预定义集。

class whoosh.analysis.StopFilter( 
        stoplist=frozenset(['and', 'is', 'it', 'an', 'as', 'at', 'have', 'in', 'yet', 'if', 'from', 'for', 'when', 'by', 'to', 'you', 'be', 'we', 'that', 'may', 'not', 'with', 'tbd', 'a', 'on', 'your', 'this', 'of', 'us', 'will', 'can', 'the', 'or', 'are']), 
        minsize=2, 
        maxsize=None, 
        renumber=True, 
        lang=None 
        ) 

因此,您可以通过重新定义架构并删除 StopFilter 或将其与 minsize = 1 一起使用来解决此问题:

from whoosh.analysis import StemmingAnalyzer 
schema = Schema(content=TEXT(analyzer=StemmingAnalyzer(stoplist=None))) 

或

schema = Schema(content=TEXT(analyzer=StemmingAnalyzer(minsize=1)))