简体   繁体   English

Laravel 4中的MySQL / PHP布尔全文检索问题

[英]MySQL/PHP Boolean Fulltext Search Issue in Laravel 4

I have the following code that searches my models in Laravel 4 for a search phrase. 我有以下代码,可以在Laravel 4中的模型中搜索搜索词组。 It uses 'IN BOOLEAN MODE' and MATCH() and AGAINST(). 它使用'IN BOOLEAN MODE'和MATCH()和AGAINST()。

public function scopeSearch($query, $q) 
{
    $fields = Static::getFields();
    $fields = implode(', ', $fields);

    $query->whereRaw("MATCH(" . $fields . ") AGAINST('" . $q . "' IN BOOLEAN MODE)");
}

public static function getFields()
{
    $field_names = array();
    $disallowed = array('id', 'created_at', 'updated_at', 'deleted_at');

    $columns = DB::select('SHOW COLUMNS FROM accounts');
    foreach ($columns as $c) {
        $field = $c->Field;
        if ( ! in_array($field, $disallowed)) {
            $field_names[$field] = $field;
        }
    }

    return $field_names;
}

I'd like help modifying the code above to allow a user to search the fields using partial words and phrases. 我想要帮助修改上面的代码,以允许用户使用部分单词和短语来搜索字段。 For example, if a user types purple, I'd like the search to also find any records with email addresses containing the word purple, so info@purplegriffon.com. 例如,如果用户键入purple,我希望搜索还查找包含电子邮件地址包含单词purple的任何记录,因此为info@purplegriffon.com。 So, essentially partial matches. 因此,基本上是部分匹配。

I'd also like to be able to find everything containing griffon in the field for the typed phrase john griffon, even if john does not exist. 我还希望能够在字段中为键入的短语john griffon找到所有包含griffon的内容,即使john不存在也是如此。

Can anyone help me out with this? 谁能帮我这个忙吗? Cheers. 干杯。

OK, I've got it working as best I can with FULLTEXT search and using wildcard operators to search for partials. 好的,通过FULLTEXT搜索并使用通配符运算符搜索部分,可以使其发挥最大的作用。 This may not be the best solution but it works. 这可能不是最佳解决方案,但它可以工作。

public function scopeSearch($query, $q) 
{
    $fields = Static::getFields();
    $fields = implode(', ', $fields);
    $terms = explode(' ', $q);

    if (count($terms) > 1) {
        $query->whereRaw("MATCH(" . $fields . ") AGAINST ('" . $q . "' IN BOOLEAN MODE)");
    } else {
        foreach ($terms as $term) {
            $query->whereRaw("MATCH(" . $fields . ") AGAINST ('*" . $term . "*' IN BOOLEAN MODE)");
        } 
    }
}

public static function getFields()
{
    $field_names = array();
    $disallowed = array('id', 'country_id', 'created_at', 'updated_at', 'deleted_at');

    $columns = DB::select('SHOW COLUMNS FROM venues');
    foreach ($columns as $c) {
        $field = $c->Field;
        if ( ! in_array($field, $disallowed)) {
            $field_names[$field] = $field;
        }
    }

    return $field_names;
}

If anyone can either simplify or improve this, then I would love to see it. 如果有人可以简化或改进它,那么我很乐意看到它。 Cheers. 干杯。

i got solution by : 我得到了解决方案:

$terms = mysql_real_escape_string($terms);

$contact_results = Contact::where_account_user_id(Auth::user()->account_user_id)

->raw_where("match (`first`, `last`) against ('{$terms}*' IN BOOLEAN MODE)")

->where_deleted(0)

->paginate(20);
phpconsole($contact_results->results);

return $contact_results;

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

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