HappyDeveloper
HappyDeveloper

Reputation: 12805

How to count results in sphinx?

I have to deal with queries that have lots of results, but I only show them in sets of 20-30 rows.

Then I use the SetLimits() method from the php API.

But I need to know what's the total number of results, to calculate the number of pages (or sets of results)

The only way I can do this right now is pulling all the results by setting the limit to 10000000 and see what is in the 'total' key of the array returned by sphinx, but this isn't good because I only need the count() number, I don't wan't sphinx to create a huge array with all the id's.

Performing a select..count() query in mysql won't work, because the indexed data in sphinx is always different.

Any ideas?

Upvotes: 1

Views: 11633

Answers (3)

Moosh
Moosh

Reputation: 396

SELECT VARIABLE_NAME, VARIABLE_VALUE
FROM information_schema.GLOBAL_STATUS WHERE
VARIABLE_NAME LIKE 'SPHINX_TOTAL_FOUND';

for more info

SELECT VARIABLE_NAME, VARIABLE_VALUE
FROM information_schema.GLOBAL_STATUS WHERE
VARIABLE_NAME LIKE 'SPHINX_%';

Upvotes: 1

Vladislav Rastrusny
Vladislav Rastrusny

Reputation: 29985

Isn't SphinxClient:query returning data about how many records matched your request?

"total" is the number of entries returned by this request (affected by SetLimit) and total_found is the total number of results matching query (not affected by SetLimit) as I understand.

Upvotes: 4

Wh1T3h4Ck5
Wh1T3h4Ck5

Reputation: 8509

According to manual: SphinxClient::setLimits,

This should do the trick

  $cl->SetLimits(0,0);

I'm not Sphinx developer, so this is just a blind guess... It should avoid memory overflow with large number of results.

Let me know does it work so I can remove answer if this is not correct.

I've also found that SELECT..COUNT() doesn't work in Sphinx query, so you're right about that.

Also, according to Sphinx documentation, you can retrive number of results using SHOW META query.

SHOW META

SHOW META shows additional meta-information about the latest query such as query time and keyword statistics:

mysql> SELECT * FROM test1 WHERE MATCH('test|one|two');
+------+--------+----------+------------+
| id   | weight | group_id | date_added |
+------+--------+----------+------------+
|    1 |   3563 |      456 | 1231721236 |
|    2 |   2563 |      123 | 1231721236 |
|    4 |   1480 |        2 | 1231721236 |
+------+--------+----------+------------+
3 rows in set (0.01 sec)

mysql> SHOW META;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| total         | 3     |
| total_found   | 3     |
| time          | 0.005 |
| keyword[0]    | test  |
| docs[0]       | 3     |
| hits[0]       | 5     |
| keyword[1]    | one   |
| docs[1]       | 1     |
| hits[1]       | 2     |
| keyword[2]    | two   |
| docs[2]       | 1     |
| hits[2]       | 2     |
+---------------+-------+
12 rows in set (0.00 sec)

References:

Upvotes: 4

Related Questions