I told Sphinx to index some strings in their CRC32 forms as attributes, like so:
sql_query = SELECT [...], CRC32(LOWER(color)) AS color, [...], FROM table
sql_attr_uint = color
I’m trying to get some faceted search going in PHP, where users can click on a link with one of the above colors and Sphinx would get another search request going with narrowed down results, something like:
Previous page:
<h3>Narrow down results:</h3>
<p><a href="search.php?query=something&color=1678454">Red (11)</a></p>
<p><a href="search.php?query=something&color=4133605867">Yellow (5)</a></p>
<?php
if (isset($_GET[$name]))
{
$sphinx->SetFilter('color', intval($_GET['color'])); // <-- Uh-oh
}
[...]
$sphinx->Query($query, 'table');
?>
Things goes wrong here because MySQL’s CRC32() returns an unsigned 32-bit integer, which PHP very helpfully does not support and caps at 2^31-1. The above link for the color Yellow exposes my problem.
StackOverflow, what may be an acceptable workaround for this? I suppose doing some math on the SQL query side is possible, but I’m wary of making the worrying chance of collision even worse.
Thanks in advance.
Quoting from PHP’s crc32() manual page:
In other words, PHP cannot handle large integers but in this precise case you can emulate unsigned integers to double precision. You can get the real value when casting to string.
This should be enough since you don’t need to do mathematical operations, such as addition or multiplication.
Update: I guess I’ve just ignored the vital part of your question. Rather than this:
… you can validate or filter with a regular expression:
… and handle the CRC value as string.