Eduard Luca
Eduard Luca

Reputation: 6612

Searching through multiple fields in SphinxSearch (PHP)

I have the following code:

$this->api = new App_Other_SphinxSearch();
$this->api->SetServer($host, $port);
$this->api->SetConnectTimeout(1);
$this->api->SetArrayResult(true);
$results = $this->api->Query("@(title,content) test", 'members');

echo "<pre>";print_r($results);die;

According to their documentation, a syntax like @(field_1,field_2) query should return docs which match the string query in either field_1 or field_2.

The PHP SDK returns something entirely different:

Array
(
    [error] => 
    [warning] => 
    [status] => 0
    [fields] => Array
        (
            [0] => title
            [1] => content
        )

    [attrs] => Array
        (
            [created] => 2
            [content] => 7
        )

    [total] => 0
    [total_found] => 0
    [time] => 0.000
    [words] => Array
        (
            [title] => Array
                (
                    [docs] => 10
                    [hits] => 34
                )

            [content] => Array
                (
                    [docs] => 34
                    [hits] => 139
                )

            [test] => Array
                (
                    [docs] => 26
                    [hits] => 34
                )

        )

)

There is no matches key in the array, however it got some hits. I don't really understand why this is happening, especially because if I try the same query from the command line, everything works correctly.

Any help?

Edit: Querying like this works though: @* test. Not really what I want though, cause that searches in all fields.

Upvotes: 0

Views: 2040

Answers (1)

barryhunter
barryhunter

Reputation: 21091

[total_found] => 0 says there where no matches.

The words array, just tells you now many documents and how many times that word appears in ANY (and all) fields. (WITHOUT regard to your specific query)

Sphinx caches those word stats, which help it make quick sanity checks on queries. When the query runs it can end up not matching any documents (because only then does it apply the field level filters), even though the individual words are found.


That explains your misinpretation of the results, but not why getting the results you get.

You are entering a Extended Mode query (as evidenced by the link to the sphinx documentation) , but the sphinx API defaults to ALL query mode. Notice also that title and content are in the words array, so are being taken as plain keywords not as syntax.

So you need to include a

$this->api->SetMatchMode(SPH_MATCH_EXTENDED);

btw, @* test, works because the @* are simply ignored in ALL query mode.

Upvotes: 1

Related Questions