繁体   English   中英

使用Whoosh搜索和索引带连字符的单词

[英]Searching and indexing hyphenated words with 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接受连字符作为单词的一部分。 您可以通过使用正则表达式覆盖StandardAnalyzerRegexTokenizer表达式来执行此操作,该表达式接受连字符作为令牌的有效部分。

    from whoosh import fields, analysis

    myanalyzer = analysis.StandardAnalyzer(expression=r'[\w-]+(\.?\w+)*')
    schema = fields.Schema(myfield=fields.TEXT(analyzer=myanalyzer))

现在寻找dual-scale thermometer相当于dual-scale AND thermometer ,将找到dual-scale digital thermometer但不是"dual purpose bathroom scale with thermometer"

但是,您将无法独立搜索带连字符的单词。 如果您的文档包含high-quality components ,如果您搜索quality ,则无法与之匹配; 只有high-quality ,因为这已成为一个标志。 由于这种副作用,除非你的内容在使用连字符严格限制为真正的原子连字词,否则我会建议使用短语方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM