Getting Sphinx to filter search results

I've installed Sphinx with the purpose of using it for fulltext search on a MySQL table ( InnoDB ).

Table columns consist of an id (numeric), name and description (these both to be fulltext indexed; actually there's some more, but for the sake of simplifying the question...), and lang (language identifier).

Indexing goes through without problems and I am getting correct results for simple searches.

However I run into troubles as soon as I try to query name and description for a specified word while filtering the results by language ( lang ).

I'd basically like to achieve the equivalent of (say the searched phrase was 'yerba') :

SELECT * FROM products
WHERE MATCH('yerba') AGAINST(name, description)
AND 'lang' == 'eng'

Not excessively ambitious, yet I must be missing something, because I keep on getting incorrect results. I am using Sphinx's PHP API:

$s = new SphinxClient();
$db = new DB();
$s->setServer('192.168.2.57', 9312);
$s->setMatchMode(SPH_MATCH_EXTENDED2);
$result = $s->query("@* yerba @lang eng");

print (count($result['matches']));
var_dump($s->GetLastError(),$s->GetLastWarning());
print '<pre>';
print_r($result);
print '</pre>';

20 matches are returned:

20
string '' (length=0) // no error
string '' (length=0) // no warning
Array
(
    [error] => 
    [warning] => 
    [status] => 0
    [fields] => Array
        (
            [0] => lang
            [1] => name
            [2] => description
            // snip...
        )

    [attrs] => Array
        (
            [lang] => 7
        )

    [matches] => Array
        (
            [568] => Array
                (
                    [weight] => 6640
                    [attrs] => Array
                        (
                            [lang] => spa
                        )

                )

            [234] => Array
                (
                    [weight] => 6630
                    [attrs] => Array
                        (
                            [lang] => pol
                        )

                )

            // snip

    [total] => 44
    [total_found] => 44
    [time] => 0.000
    [words] => Array
        (
            [yerba] => Array
                (
                    [docs] => 44
                    [hits] => 238
                )

            [pol] => Array
                (
                    [docs] => 715
                    [hits] => 772
                )

        )

)

It's quite easy to notice that in the very first record it found, lang is spa , not eng .

Why??

The configuration file ( sphinx.conf ) looks as below - source:

source src1
{
    [...]   
    sql_query       = 
        SELECT id, lang, name, description // snip! 
        FROM my_table
    [...]
    sql_field_string    = lang
    [...]
}

Index:

index test1
{
    source          = src1
    [...]
    min_word_len        = 1
    [...]
}

All other options, apart from db access data, which works fine as I said (for non-filtered queries), are left as default.

I've been trying to crack this nut for a while now. I tried declaring lang as an sql_attr_string , using different match modes etc. alas, all in vain.

Do I have indexing set up wrong? Where have I messed up here?


To make things easier, I suggest to change language from string field to integer attribute.

So, you query will look like:

$s->SetFilter('lang', array(1)); //1 - eng
$s->Query('yerba');

You have this...

$result = $s->query("@* yerba @lang eng");

then...

[words] => Array
       [yerba] => Array
       [pol] => Array

Are you sure they are for the same query? "pol" is not in your query

Otherwise check wordforms/exceptions, maybe you are transforming your text in some way...?

链接地址: http://www.djcxy.com/p/75264.html

上一篇: 全文搜索

下一篇: 让狮身人面像过滤搜索结果