K
K
Kuks2014-06-17 16:04:13
PHP
Kuks, 2014-06-17 16:04:13

Sphinx - how to use strict search with expressions?

Hello. There is Sphinx as a PHP module. The task is essentially simple, but I can not find a suitable solution in any way.
MySQL has a table like id | user_id | title. Sphinx indexes this and searches for user_id + title.
The title field can be of the form abc | abc:xyz i.e. either just a string or a string with a colon.
It is necessary to make a suggest-search, i.e. the entire search of the form query*.
There are two search options:
1. Find a string that starts with the query text.
2. Find the line in which the query text follows the colon.
At the moment, the search is carried out as follows:

$sphinx = Service::sphinx(); // Здесь происходит подключение клиента к серверу
$sphinx->SetMatchMode( SPH_MATCH_EXTENDED ); // Устанавливается режим совпадения
$sphinx->setFilter('user_id', [$this->id]); // Устанавливается фильтр по user_id

// Собственно сам запрос
if ($part == 'second') {
    $results = $sphinx->query(':'.$text.'*', 'index');
} else {
    $results = $sphinx->query('^'.$text.'*', 'index');
}

As a result, it sometimes works but crookedly, sometimes it doesn’t work at all. And I can not find anything useful on the topic on the Internet.
The config is something like this:
source index
{
        type                    = mysql
        sql_host                = ...
        sql_user                = ...
        sql_pass                = ...
        sql_db                  = ...
        sql_port                = 3306

        sql_query_pre           = SET NAMES utf8

        sql_query               = \
                SELECT \
                        id, user_id, title \
                FROM \
                        table

        sql_field_string        = title
        sql_attr_uint           = user_id

        sql_query_pre           = SET NAMES utf8
        sql_query_pre           = SET SESSION query_cache_type=OFF

        sql_query_info          = SELECT * FROM table WHERE id=$id
}

index index
{
        source                  = index
        path                    = /var/lib/sphinxsearch/data/index
        docinfo                 = extern
        charset_type            = utf-8
        mlock                   = 0
        index_exact_words       = 1
        min_word_len            = 1
        enable_star             = 1
        prefix_fields           = title
        min_prefix_len          = 1
}

I will be very grateful for the help and any information. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
whats, 2014-06-17
@whats

Solution one, use SphinxQL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question