簡體   English   中英

默認情況下是否有一種方法要求全文搜索中的所有單詞?

[英]Is there a way by default to require ALL words in full text searching?

我正在嘗試找到一種方法來實現,因此當用戶進行搜索時,默認情況下所有單詞都是必需的。

在開始時這似乎很容易,只需將單詞分解並在每個單詞的開頭添加一個+號即可; 但是,當您開始嘗試實現其他運算符時,它會變得很復雜。

這是我到目前為止所擁有的..

function prepareKeywords($str) {

    // Remove any + signs since we add them ourselves
    // Also remove any operators we don't allow
    // We don't allow some operators as they would have no use in the search since we don't order our results by relevance
    $str = str_replace(array('+','~','<','>'), '', $str);

    // Remove anything more than once space
    $str = preg_replace('/\s{2,}/', ' ', $str);

    // Now break up words into parts
    $str = str_getcsv($str, ' ', '"');

    // Now prepend a + sign to the front of each word

    foreach ($ks as $key => $word) {

        // First we check to make sure the start of the word doesn't already contain an operator before we add the + sign to the start

        if (in_array($word{0}, array('-','<','>','~'))) {

        } else {
            $ks[$key] = '+' . $word;   
        }

    }

    // Now put word back to string
    //$ks = implode(' ', $ks);    

}

如您所見,它目前僅適用於加引號的字符串,但隨后我開始考慮也不要拆分() ,然后如果它包含嵌套的雙引號,反之亦然...。很毛。

因此,我試圖找出是否有一種方法可以做我想做的事情而不會弄亂字符串,並且僅使默認情況下需要的所有單詞成為可能,除非用戶使用-專門否定了它們。

當然可以在模式中使用preg_match()和\\ b作為單詞邊界嗎?

然后,您可以將搜索字詞拆分為類似

preg_match_all('/\b(.*)\b/', $matches);

我可能會想錯了,因為它來晚了,但它可能會給您帶來一些幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM