我正在使用Whoosh索引和搜索大量文档,我需要搜索的许多内容都是连字符.飞快移动似乎将连字符视为某种特殊的角色,但对于我的生活,我无法弄清楚它的行为.
任何人都可以建议在索引和搜索时Whoosh如何对待连字符?
Whoosh 只是将所有标点符号视为空格。假设使用默认AND搜索,该查询dual-scale thermometer相当于dual AND scale AND thermometer. 这将找到一个包含 的文档dual-scale digital thermometer,但它也会找到dual purpose bathroom scale with thermometer.
避免这种情况的一种解决方案是将查询中的连字符单词转换为短语:"dual-scale" thermometer,相当于"dual scale" AND thermometer。
您还可以强制 Whoosh 接受连字符作为单词的一部分。为此,您可以使用接受连字符作为标记的有效部分的正则表达式覆盖RegexTokenizer中的表达式。StandardAnalyzer
from whoosh import fields, analysis
myanalyzer = analysis.StandardAnalyzer(expression=r'[\w-]+(\.?\w+)*')
schema = fields.Schema(myfield=fields.TEXT(analyzer=myanalyzer))
Run Code Online (Sandbox Code Playgroud)
现在搜索dual-scale thermometer相当于dual-scale AND thermometerand will find dual-scale digital thermometerbut not "dual purpose bathroom scale with thermometer"。
但是,您将无法独立搜索连字符的单词。如果您的文档包含high-quality components,则搜索时不会匹配到它quality;只是high-quality,因为这现在已经成为一种令牌了。由于这种副作用,除非您的内容严格限制使用连字符来真正原子连字符的单词,否则我会推荐短语方法。