0

I've been trying to create a search for my website using Sphinx. I query multiple indexes since the type of data is totally different in each, but I need to sort all of that data by weight and display it.

Here's the foreach loop that I use. All of the commented out code is things that I've already tried.

function addToSrchArray($res, $restype)
{
    global $total_result_count, $new_name_array;
    $resultCount = $res['total_found'];

    if ($resultCount > 0) {
        $total_result_count = $total_result_count + $resultCount;
        $srchtp = $restype;

        foreach ($res["matches"] as $key => $val) {
            $arrToAdd = array(
                'indx' => $key,
                'wgt'  => $val["weight"],
                'typ'  => $srchtp
            );

            if ($val["weight"] > $new_name_array[0]["wgt"]) {
                //add to begining
                array_unshift($new_name_array, $arrToAdd);
            } else {
                //echo count($new_name_array);
                $i = 0;

                while($i < count($new_name_array)) {
                    // echo $i.' '.$srchtp.'<br>';
                    //for($i = 0; $i < count($new_name_array); $i++) {                       
                        if($val["weight"] > $new_name_array[$i]["wgt"]) {
                            //array_splice($new_name_array, $i, 0, array('indx' => $key, 'wgt' => $val["weight"], 'typ' => $srchtp));
                            array_unshift(array_slice($new_name_array, 0, $i), array_unshift($arrToAdd, array_slice($new_name_array, $i)));

                            /*array_slice($new_name_array, 0, $i) +
                            $arrToAdd +
                            array_slice($new_name_array, $i, count($new_name_array)-1);*/
                            //$new_name_array[] = array('indx' => $key, 'wgt' => $val["weight"], 'typ' => $srchtp);
                            break;
                        } else if ($i == (count($new_name_array) - 1)) {
                            $new_name_array[] = array(
                                'indx' => $key,
                                'wgt'  => $val["weight"],
                                'typ'  => $srchtp
                            );
                            break;
                        }
                        $i++;
                }
            }
        }
        //var_dump($new_name_array);
    }
}

Each of these different methods have different success levels. There are 45 results, some of these methods show 9, some show 22, etc. The $res parameter passed to the function looks something like this

["matches"] => array(2) {
    [3]=> array(2) {
        ["weight"]=> string(1) "3"
        ["attrs"]=> array(1) {
            ["follow_count"]=> string(1) "1"
        }
    }
    [2]=> array(2) {
        ["weight"]=> string(1) "2"
        ["attrs"]=> array(1) {
            ["follow_count"]=> string(1) "1"
        }
    }
}
["total"]=> string(1) "2"
["total_found"]=> string(1) "2"
["time"]=> string(5) "0.000"

Please help. I've tried everything. Nothing shows me all of the results sorted by weight.

phaberest
  • 3,140
  • 3
  • 32
  • 40
user4559334
  • 383
  • 4
  • 13
  • I just figured out the problem. The php code was fine. Sphinx, the source for that input array was the one limiting the results. The problem is now fixed. Thanks though – user4559334 Feb 18 '16 at 01:23
  • Also you can just specify multiple indexes in the query. Sphinx will run the query against each index, and merge (and sort!) the results for you (technically a union). This is good because it makes sure to handle paging though the results correctly, something its not clear your code will do. – barryhunter Feb 18 '16 at 12:37
  • @barryhunter I would do that but all of my tables have the same index name (id) and they're all set to auto increment. So, the ids in other indexes are bound to be the same sometime. I've read that sphinx overwrites the result if the index is the same. Is there any way to overcome this limitation? – user4559334 Feb 18 '16 at 13:20
  • A number of ways to arrange for the ids to be unique in the sphinx indexes http://stackoverflow.com/questions/12857931/is-there-a-better-way-to-get-data-from-two-tables-at-once-with-sphinx-mysql/12863274#12863274 – barryhunter Feb 18 '16 at 13:47

1 Answers1

0

I'm afraid your approach is no good. Trying and imagine a better design, I believe you'd better build a new Sphinx index containing data you are interested in. You will see this is true when user queries will have consistent results without handling data any further.

Augusto
  • 2,125
  • 18
  • 27