[英]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接受連字符作為單詞的一部分。 您可以通過使用正則表達式覆蓋StandardAnalyzer
的RegexTokenizer
表達式來執行此操作,該表達式接受連字符作為令牌的有效部分。
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.