How to extract Sphinx group by numbers from result set? - php

I need some help on Sphinx MySql index group by.
I have a product table where I have product ids and corresponding retailer ids. What I want to do is to create a simple list to show the retailers' names along with how many products it has in the table:
standard SQL: select retailerid, count(productid)as cnt from tblproducts
Sphinxs: select * from tblproducts1 group by retailerid
I am using Sphinx 2.0.1-beta
The reason I want to use Sphinx is, because the number of products will be filtered by a complex match full text query, on product name and description etc.
Now my question is, how do I get these numbers/ids from the result returned by Sphinx? As I am using php to display results, I would like if somebody can show me some useful php code to extract these numbers from the result set. The result array returned by Sphinx is confusing me even more; I do not see any reference to the product count anywhere!
I am sure that Sphinx returns these numbers as it supports group by and count, but how do I extract the numbers from the result set?
Below is from my config file:
sql_query = \
SELECT ProductId, ProductName, ProductModel, ProductDesc, ProductManf, ProductHeader, \
ProductPrice, ProductPrePrice, ProductFetchDate, m.MerchantId, m.MerchantActive FROM tblproducts p \
inner join tblmerchantlist m on p.MerchantId=m.MerchantId
sql_attr_uint = MerchantActive
sql_attr_float = ProductPrice
sql_attr_float = ProductPrePrice
sql_group_column = MerchantId
sql_query_info = SELECT * FROM tblproducts WHERE ProductId=$id
UPDATE
I would expect so, but I don't see the reference to count in the below resultset. I must be making some mistakes somewhere:
Array ( [error] => [warning] => [status] => 0 [fields] =>
Array ( [0] => productname [1] => productmodel [2] => productdesc [3] => productmanf [4] => productheader [5] => productfetchdate ) [attrs] =>
Array ( [productprice] => 5 [productpreprice] => 5 [merchantid] => 1 [merchantactive] => 1 ) [matches] =>
Array ( [0] =>
Array ( [id] => 694173 [weight] => 396305 [attrs] =>
Array ( [productprice] => 1568.48999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [1] =>
Array ( [id] => 901921 [weight] => 396305 [attrs] =>
Array ( [productprice] => 1533.48999023 [productpreprice] => 1536.98999023 [merchantid] => 12 [merchantactive] => 1 ) ) [2] =>
Array ( [id] => 302573 [weight] => 249249 [attrs] =>
Array ( [productprice] => 1059.98999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [3] =>
Array ( [id] => 302579 [weight] => 249249 [attrs] =>
Array ( [productprice] => 1179.98999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [4] =>
Array ( [id] => 302592 [weight] => 249249 [attrs] =>
Array ( [productprice] => 1429.48999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [5] =>
Array ( [id] => 302595 [weight] => 249249 [attrs] =>
Array ( [productprice] => 1592.98999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [6] =>
Array ( [id] => 302597 [weight] => 249249 [attrs] =>
Array ( [productprice] => 1129.98999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [7] =>
Array ( [id] => 406798 [weight] => 249249 [attrs] =>
Array ( [productprice] => 2419.98999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [8] =>
Array ( [id] => 407480 [weight] => 249249 [attrs] =>
Array ( [productprice] => 1287.48999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [9] =>
Array ( [id] => 693715 [weight] => 249249 [attrs] =>
Array ( [productprice] => 1234.98999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) ) [total] => 29301 [total_found] => 29301 [time] => 0.137 [words] =>
Array ( [select] =>
Array ( [docs] => 390 [hits] => 462 ) [from] =>
Array ( [docs] => 4332 [hits] => 4637 ) [tblproducts1] =>
Array ( [docs] => 0 [hits] => 0 ) [where] =>
Array ( [docs] => 395 [hits] => 448 ) [match] =>
Array ( [docs] => 108 [hits] => 111 ) [cyberpowerpc] =>
Array ( [docs] => 66 [hits] => 132 ) [gamer] =>
Array ( [docs] => 307 [hits] => 715 ) [xtreme] =>
Array ( [docs] => 410 [hits] => 725 ) [1310lq] =>
Array ( [docs] => 2 [hits] => 6 ) [in] =>
Array ( [docs] => 16196 [hits] => 19786 ) [canada] =>
Array ( [docs] => 1146 [hits] => 1200 ) [group] =>
Array ( [docs] => 5716 [hits] => 5732 ) [by] =>
Array ( [docs] => 2143 [hits] => 2289 ) [merchantid] =>
Array ( [docs] => 0 [hits] => 0 )
)
)

There is a magic attribute #count that added to your result set for that query
select * from tblproducts1 group by retailerid
The attribute list in result set looks like
id, weight, MerchantActive, ... #groupby, #count
In case you use compat_sphinxql_magic=0 option in searchd section you have explicitly define and alias all needed attributes like
select *, weight(*) as w, count(*) as c from tblproducts1 group by retailerid
The attribute list in result set would look like
id, MerchantActive, ... w, c

Related

PHP sum array values with same keys

This is the original main array:
Array
(
[0] => Array
(
[subtotal] => 0.6000
[taxes] => 0.0720
[charged_amount] => 0.6720
[total_discount] => 0.0000
[provinceName] => BC
[store_key] => 1
[store_id] => 5834
[categories] => Array
(
[2] => 0.6000
[4] => 0
[3] => 0
)
)
[1] => Array
(
[subtotal] => 29.8500
[taxes] => 2.3270
[charged_amount] => 20.2370
[total_discount] => 11.9400
[provinceName] => MB
[store_key] => 9
[store_id] => 1022
[categories] => Array
(
[2] => 0
[4] => 29.8500
[3] => 0
)
)
[2] => Array
(
[subtotal] => 0.3000
[taxes] => 0.0390
[charged_amount] => 0.3390
[total_discount] => 0.0000
[provinceName] => NB
[store_key] => 8
[store_id] => 1013
[categories] => Array
(
[2] => 0.3000
[4] => 0
[3] => 0
)
)
[3] => Array
(
[subtotal] => 24.3100
[taxes] => 1.1830
[charged_amount] => 10.2830
[total_discount] => 15.2100
[provinceName] => NL
[store_key] => 4
[store_id] => 3033
[categories] => Array
(
[2] => 24.3100
[4] => 0
[3] => 0
)
)
[4] => Array
(
[subtotal] => 1116.3400
[taxes] => 127.6960
[charged_amount] => 1110.0060
[total_discount] => 134.0300
[provinceName] => ON
[store_key] => 2
[store_id] => 1139
[categories] => Array
(
[2] => 85.7300
[4] => 143.2800
[3] => 887.3300
)
)
[5] => Array
(
[subtotal] => 10.8500
[taxes] => 1.4100
[charged_amount] => 12.2600
[total_discount] => 0.0000
[provinceName] => ON
[store_key] => 5
[store_id] => 1116
[categories] => Array
(
[2] => 10.8500
[4] => 0
[3] => 0
)
)
)
I just need to add the values of the array [categories] with same keys and use it further to print the total, but not getting correct output, can someone help me out to get the desired result:
Desired result
An array with same keys but total of individual array values
Array ( [2] => 0.9000 [4] => 29.8500 [3] => 1.5 )
NOTE: Initial array is dynamic can have n number of key value pair
Thanks
The first thing that you need to do is iterate through the outer array. Then, for each row in the outer array, you and to iterate through each entry in the category element. So this means that we have two foreach loops. Inside the inner foreach, we simply set the value for the current index to be the value of the same index on a 'sum' array (if it doesn't already exist), or increment the value of that index if it already exists in the 'sum' array.
<?php
$sumArray = array();
foreach($outerArray as $row)
{
foreach($row["categories"] as $index => $value)
{
$sumArray[$index] = (isset($sumArray[$index]) ? $sumArray[$index] + $value : $value);
}
}
?>
Demo using your example array

How to get specific array values from moltin get a product

I just started using moltin and learn to developing it.
In the getting started there is code that to show product
<?php
$product = \Product::Find(['slug' => 'baju']);
?>
documentation link https://moltin.com/getting-started/php
and the result is when I print array
<?php
print_r($product);
?>
It shows like this
Array ( [status] => 1 [result] => Array ( [0] => Array ( [id] => 1207658536885027482 [order] => [created_at] => 2016-03-17 03:08:58 [updated_at] => 2016-03-17 03:08:58 [sku] => baju-1 [title] => baju [slug] => baju [sale_price] => 0 [status] => Array ( [value] => Live [data] => Array ( [key] => 1 [value] => Live ) ) [category] => Array ( [value] => Uncategorized [data] => Array ( [1134518259857490806] => Array ( [id] => 1134518259857490806 [order] => [created_at] => 2015-12-07 05:12:15 [updated_at] => 2015-12-07 05:12:15 [parent] => [slug] => uncategorized [status] => Array ( [value] => Live [data] => Array ( [key] => 1 [value] => Live ) ) [title] => Uncategorized [description] => Products that do not fit into another category ) ) ) [stock_level] => 10 [stock_status] => Array ( [value] => In Stock [data] => Array ( [key] => 1 [value] => In Stock ) ) [description] => baju [requires_shipping] => Array ( [value] => Yes [data] => Array ( [key] => 1 [value] => Yes ) ) [weight] => 0 [height] => 0 [width] => 0 [depth] => 0 [catalog_only] => Array ( [value] => No [data] => Array ( [key] => 0 [value] => No ) ) [tax_band] => Array ( [value] => Default [data] => Array ( [id] => 1134518260142703561 [title] => Default [description] => [rate] => 20 [created_at] => [updated_at] => ) ) [collection] => [brand] => [price] => Array ( [value] => £1.20 [data] => Array ( [formatted] => Array ( [with_tax] => £1.20 [without_tax] => £1.00 [tax] => £0.20 ) [rounded] => Array ( [with_tax] => 1.2 [without_tax] => 1 [tax] => 0.2 ) [raw] => Array ( [with_tax] => 1.2 [without_tax] => 1 [tax] => 0.2 ) ) ) [is_variation] => [modifiers] => Array ( ) [images] => Array ( ) ) ) [pagination] => Array ( [total] => 1 [current] => 1 [limit] => 10 [offset] => 0 [from] => 1 [to] => 1 [offsets] => Array ( [first] => [previous] => [next] => [last] => ) [links] => Array ( [first] => [previous] => [next] => [last] => ) ) ) status = 1
How to get specific array from this list?
I already try
<?php
echo $product[0]['id'];
?>
it didn't work, show error Notice: Undefined offset: 0
you have a multidimensional array under the result key
access it the following way:
$product['result'][0]['id']
Give this a try:
echo $product['result'][0]['id'];

php call sphinx, total found is 2, but matches is empty, why?

php call sphinx, total found is 2, but matches is empty, why?
php package: https://pecl.php.net/package/sphinx version:1.3.3
sphinx version:2.0.1
when I query('#author tairyao', '*'), the result is:
Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => author
[1] => title
[2] => description
)
[attrs] => Array
(
)
[matches] => Array
(
[102373] => Array
(
[weight] => 7702
[attrs] => Array
(
)
)
[102428] => Array
(
[weight] => 7702
[attrs] => Array
(
)
)
)
[total] => 2
[total_found] => 2
[time] => 0
[words] => Array
(
[tairyao] => Array
(
[docs] => 4
[hits] => 4
)
)
)
The total and total_found are all 2, why attrs is empty?

setFieldWeights doesn't work

I'm using sphinx php api to sort search results. I want to give more weight to title and less to content, I've tried setting setFieldWeights
$sp->setFieldWeights(array('title'=>10,'full_story'=>1)); but it's not working at all. not matter how i change the weight, results stays the same.
print_r with "test" as a search word and setFieldWeights(array('title'=>10,'full_story'=>1));
Array ( [error] => [warning] => [status] => 0 [fields] => Array ( [0] => title [1] => full_story [2] => category ) [attrs] => Array ( [date] => 2 ) [matches] => Array ( [0] => Array ( [id] => 1933 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [1] => Array ( [id] => 2824 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [2] => Array ( [id] => 2860 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [3] => Array ( [id] => 2865 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [4] => Array ( [id] => 5720 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [5] => Array ( [id] => 6963 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [6] => Array ( [id] => 8190 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [7] => Array ( [id] => 8591 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [8] => Array ( [id] => 10114 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [9] => Array ( [id] => 10157 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [10] => Array ( [id] => 11162 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [11] => Array ( [id] => 11659 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [12] => Array ( [id] => 13219 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [13] => Array ( [id] => 13220 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [14] => Array ( [id] => 13565 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [15] => Array ( [id] => 13576 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [16] => Array ( [id] => 14801 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [17] => Array ( [id] => 14857 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [18] => Array ( [id] => 17311 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [19] => Array ( [id] => 17511 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [20] => Array ( [id] => 18131 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [21] => Array ( [id] => 19171 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [22] => Array ( [id] => 21134 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [23] => Array ( [id] => 23007 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [24] => Array ( [id] => 23230 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [25] => Array ( [id] => 23460 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [26] => Array ( [id] => 23473 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [27] => Array ( [id] => 25609 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [28] => Array ( [id] => 28093 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [29] => Array ( [id] => 28175 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [30] => Array ( [id] => 30364 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [31] => Array ( [id] => 30506 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [32] => Array ( [id] => 31439 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [33] => Array ( [id] => 31668 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [34] => Array ( [id] => 32017 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [35] => Array ( [id] => 1432 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [36] => Array ( [id] => 5362 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [37] => Array ( [id] => 9175 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [38] => Array ( [id] => 13817 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [39] => Array ( [id] => 14132 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [40] => Array ( [id] => 14658 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [41] => Array ( [id] => 15792 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [42] => Array ( [id] => 16615 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [43] => Array ( [id] => 18177 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [44] => Array ( [id] => 18581 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [45] => Array ( [id] => 18618 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [46] => Array ( [id] => 18665 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [47] => Array ( [id] => 18718 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [48] => Array ( [id] => 19096 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [49] => Array ( [id] => 19128 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [50] => Array ( [id] => 19464 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [51] => Array ( [id] => 20014 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [52] => Array ( [id] => 20144 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [53] => Array ( [id] => 20306 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [54] => Array ( [id] => 22604 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [55] => Array ( [id] => 23870 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [56] => Array ( [id] => 23994 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [57] => Array ( [id] => 24507 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [58] => Array ( [id] => 24617 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [59] => Array ( [id] => 25043 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [60] => Array ( [id] => 25350 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [61] => Array ( [id] => 26063 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [62] => Array ( [id] => 26614 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [63] => Array ( [id] => 26819 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [64] => Array ( [id] => 28292 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [65] => Array ( [id] => 29155 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [66] => Array ( [id] => 30854 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [67] => Array ( [id] => 31566 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [68] => Array ( [id] => 31704 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [69] => Array ( [id] => 32092 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) ) [total] => 70 [total_found] => 70 [time] => 0,017 [words] => Array ( [*test*] => Array ( [docs] => 70 [hits] => 70 ) ) )
print_r with "test" as a search word and setFieldWeights(array('title'=>10000,'full_story'=>1));
Array ( [error] => [warning] => [status] => 0 [fields] => Array ( [0] => title [1] => full_story [2] => category [3] => date ) [attrs] => Array ( ) [matches] => Array ( [0] => Array ( [id] => 1933 [weight] => 10000 [attrs] => Array ( ) ) [1] => Array ( [id] => 2824 [weight] => 10000 [attrs] => Array ( ) ) [2] => Array ( [id] => 2860 [weight] => 10000 [attrs] => Array ( ) ) [3] => Array ( [id] => 2865 [weight] => 10000 [attrs] => Array ( ) ) [4] => Array ( [id] => 5720 [weight] => 10000 [attrs] => Array ( ) ) [5] => Array ( [id] => 6963 [weight] => 10000 [attrs] => Array ( ) ) [6] => Array ( [id] => 8190 [weight] => 10000 [attrs] => Array ( ) ) [7] => Array ( [id] => 8591 [weight] => 10000 [attrs] => Array ( ) ) [8] => Array ( [id] => 10114 [weight] => 10000 [attrs] => Array ( ) ) [9] => Array ( [id] => 10157 [weight] => 10000 [attrs] => Array ( ) ) [10] => Array ( [id] => 11162 [weight] => 10000 [attrs] => Array ( ) ) [11] => Array ( [id] => 11659 [weight] => 10000 [attrs] => Array ( ) ) [12] => Array ( [id] => 13219 [weight] => 10000 [attrs] => Array ( ) ) [13] => Array ( [id] => 13220 [weight] => 10000 [attrs] => Array ( ) ) [14] => Array ( [id] => 13565 [weight] => 10000 [attrs] => Array ( ) ) [15] => Array ( [id] => 13576 [weight] => 10000 [attrs] => Array ( ) ) [16] => Array ( [id] => 14801 [weight] => 10000 [attrs] => Array ( ) ) [17] => Array ( [id] => 14857 [weight] => 10000 [attrs] => Array ( ) ) [18] => Array ( [id] => 17311 [weight] => 10000 [attrs] => Array ( ) ) [19] => Array ( [id] => 17511 [weight] => 10000 [attrs] => Array ( ) ) [20] => Array ( [id] => 18131 [weight] => 10000 [attrs] => Array ( ) ) [21] => Array ( [id] => 19171 [weight] => 10000 [attrs] => Array ( ) ) [22] => Array ( [id] => 21134 [weight] => 10000 [attrs] => Array ( ) ) [23] => Array ( [id] => 23007 [weight] => 10000 [attrs] => Array ( ) ) [24] => Array ( [id] => 23230 [weight] => 10000 [attrs] => Array ( ) ) [25] => Array ( [id] => 23460 [weight] => 10000 [attrs] => Array ( ) ) [26] => Array ( [id] => 23473 [weight] => 10000 [attrs] => Array ( ) ) [27] => Array ( [id] => 25609 [weight] => 10000 [attrs] => Array ( ) ) [28] => Array ( [id] => 28093 [weight] => 10000 [attrs] => Array ( ) ) [29] => Array ( [id] => 28175 [weight] => 10000 [attrs] => Array ( ) ) [30] => Array ( [id] => 30364 [weight] => 10000 [attrs] => Array ( ) ) [31] => Array ( [id] => 30506 [weight] => 10000 [attrs] => Array ( ) ) [32] => Array ( [id] => 31439 [weight] => 10000 [attrs] => Array ( ) ) [33] => Array ( [id] => 31668 [weight] => 10000 [attrs] => Array ( ) ) [34] => Array ( [id] => 32017 [weight] => 10000 [attrs] => Array ( ) ) [35] => Array ( [id] => 1432 [weight] => 1 [attrs] => Array ( ) ) [36] => Array ( [id] => 5362 [weight] => 1 [attrs] => Array ( ) ) [37] => Array ( [id] => 9175 [weight] => 1 [attrs] => Array ( ) ) [38] => Array ( [id] => 13817 [weight] => 1 [attrs] => Array ( ) ) [39] => Array ( [id] => 14132 [weight] => 1 [attrs] => Array ( ) ) [40] => Array ( [id] => 14658 [weight] => 1 [attrs] => Array ( ) ) [41] => Array ( [id] => 15792 [weight] => 1 [attrs] => Array ( ) ) [42] => Array ( [id] => 16615 [weight] => 1 [attrs] => Array ( ) ) [43] => Array ( [id] => 18177 [weight] => 1 [attrs] => Array ( ) ) [44] => Array ( [id] => 18581 [weight] => 1 [attrs] => Array ( ) ) [45] => Array ( [id] => 18618 [weight] => 1 [attrs] => Array ( ) ) [46] => Array ( [id] => 18665 [weight] => 1 [attrs] => Array ( ) ) [47] => Array ( [id] => 18718 [weight] => 1 [attrs] => Array ( ) ) [48] => Array ( [id] => 19096 [weight] => 1 [attrs] => Array ( ) ) [49] => Array ( [id] => 19128 [weight] => 1 [attrs] => Array ( ) ) [50] => Array ( [id] => 19464 [weight] => 1 [attrs] => Array ( ) ) [51] => Array ( [id] => 20014 [weight] => 1 [attrs] => Array ( ) ) [52] => Array ( [id] => 20144 [weight] => 1 [attrs] => Array ( ) ) [53] => Array ( [id] => 20306 [weight] => 1 [attrs] => Array ( ) ) [54] => Array ( [id] => 22604 [weight] => 1 [attrs] => Array ( ) ) [55] => Array ( [id] => 23870 [weight] => 1 [attrs] => Array ( ) ) [56] => Array ( [id] => 23994 [weight] => 1 [attrs] => Array ( ) ) [57] => Array ( [id] => 24507 [weight] => 1 [attrs] => Array ( ) ) [58] => Array ( [id] => 24617 [weight] => 1 [attrs] => Array ( ) ) [59] => Array ( [id] => 25043 [weight] => 1 [attrs] => Array ( ) ) [60] => Array ( [id] => 25350 [weight] => 1 [attrs] => Array ( ) ) [61] => Array ( [id] => 26063 [weight] => 1 [attrs] => Array ( ) ) [62] => Array ( [id] => 26614 [weight] => 1 [attrs] => Array ( ) ) [63] => Array ( [id] => 26819 [weight] => 1 [attrs] => Array ( ) ) [64] => Array ( [id] => 28292 [weight] => 1 [attrs] => Array ( ) ) [65] => Array ( [id] => 29155 [weight] => 1 [attrs] => Array ( ) ) [66] => Array ( [id] => 30854 [weight] => 1 [attrs] => Array ( ) ) [67] => Array ( [id] => 31566 [weight] => 1 [attrs] => Array ( ) ) [68] => Array ( [id] => 31704 [weight] => 1 [attrs] => Array ( ) ) [69] => Array ( [id] => 32092 [weight] => 1 [attrs] => Array ( ) ) ) [total] => 70 [total_found] => 70 [time] => 0,000 [words] => Array ( [*test*] => Array ( [docs] => 70 [hits] => 70 ) ) )
print_r with "test" as a search word without setFieldWeights
Array ( [error] => [warning] => [status] => 0 [fields] => Array ( [0] => title [1] => full_story [2] => category [3] => date ) [attrs] => Array ( ) [matches] => Array ( [0] => Array ( [id] => 1432 [weight] => 1 [attrs] => Array ( ) ) [1] => Array ( [id] => 1933 [weight] => 1 [attrs] => Array ( ) ) [2] => Array ( [id] => 2824 [weight] => 1 [attrs] => Array ( ) ) [3] => Array ( [id] => 2860 [weight] => 1 [attrs] => Array ( ) ) [4] => Array ( [id] => 2865 [weight] => 1 [attrs] => Array ( ) ) [5] => Array ( [id] => 5362 [weight] => 1 [attrs] => Array ( ) ) [6] => Array ( [id] => 5720 [weight] => 1 [attrs] => Array ( ) ) [7] => Array ( [id] => 6963 [weight] => 1 [attrs] => Array ( ) ) [8] => Array ( [id] => 8190 [weight] => 1 [attrs] => Array ( ) ) [9] => Array ( [id] => 8591 [weight] => 1 [attrs] => Array ( ) ) [10] => Array ( [id] => 9175 [weight] => 1 [attrs] => Array ( ) ) [11] => Array ( [id] => 10114 [weight] => 1 [attrs] => Array ( ) ) [12] => Array ( [id] => 10157 [weight] => 1 [attrs] => Array ( ) ) [13] => Array ( [id] => 11162 [weight] => 1 [attrs] => Array ( ) ) [14] => Array ( [id] => 11659 [weight] => 1 [attrs] => Array ( ) ) [15] => Array ( [id] => 13219 [weight] => 1 [attrs] => Array ( ) ) [16] => Array ( [id] => 13220 [weight] => 1 [attrs] => Array ( ) ) [17] => Array ( [id] => 13565 [weight] => 1 [attrs] => Array ( ) ) [18] => Array ( [id] => 13576 [weight] => 1 [attrs] => Array ( ) ) [19] => Array ( [id] => 13817 [weight] => 1 [attrs] => Array ( ) ) [20] => Array ( [id] => 14132 [weight] => 1 [attrs] => Array ( ) ) [21] => Array ( [id] => 14658 [weight] => 1 [attrs] => Array ( ) ) [22] => Array ( [id] => 14801 [weight] => 1 [attrs] => Array ( ) ) [23] => Array ( [id] => 14857 [weight] => 1 [attrs] => Array ( ) ) [24] => Array ( [id] => 15792 [weight] => 1 [attrs] => Array ( ) ) [25] => Array ( [id] => 16615 [weight] => 1 [attrs] => Array ( ) ) [26] => Array ( [id] => 17311 [weight] => 1 [attrs] => Array ( ) ) [27] => Array ( [id] => 17511 [weight] => 1 [attrs] => Array ( ) ) [28] => Array ( [id] => 18131 [weight] => 1 [attrs] => Array ( ) ) [29] => Array ( [id] => 18177 [weight] => 1 [attrs] => Array ( ) ) [30] => Array ( [id] => 18581 [weight] => 1 [attrs] => Array ( ) ) [31] => Array ( [id] => 18618 [weight] => 1 [attrs] => Array ( ) ) [32] => Array ( [id] => 18665 [weight] => 1 [attrs] => Array ( ) ) [33] => Array ( [id] => 18718 [weight] => 1 [attrs] => Array ( ) ) [34] => Array ( [id] => 19096 [weight] => 1 [attrs] => Array ( ) ) [35] => Array ( [id] => 19128 [weight] => 1 [attrs] => Array ( ) ) [36] => Array ( [id] => 19171 [weight] => 1 [attrs] => Array ( ) ) [37] => Array ( [id] => 19464 [weight] => 1 [attrs] => Array ( ) ) [38] => Array ( [id] => 20014 [weight] => 1 [attrs] => Array ( ) ) [39] => Array ( [id] => 20144 [weight] => 1 [attrs] => Array ( ) ) [40] => Array ( [id] => 20306 [weight] => 1 [attrs] => Array ( ) ) [41] => Array ( [id] => 21134 [weight] => 1 [attrs] => Array ( ) ) [42] => Array ( [id] => 22604 [weight] => 1 [attrs] => Array ( ) ) [43] => Array ( [id] => 23007 [weight] => 1 [attrs] => Array ( ) ) [44] => Array ( [id] => 23230 [weight] => 1 [attrs] => Array ( ) ) [45] => Array ( [id] => 23460 [weight] => 1 [attrs] => Array ( ) ) [46] => Array ( [id] => 23473 [weight] => 1 [attrs] => Array ( ) ) [47] => Array ( [id] => 23870 [weight] => 1 [attrs] => Array ( ) ) [48] => Array ( [id] => 23994 [weight] => 1 [attrs] => Array ( ) ) [49] => Array ( [id] => 24507 [weight] => 1 [attrs] => Array ( ) ) [50] => Array ( [id] => 24617 [weight] => 1 [attrs] => Array ( ) ) [51] => Array ( [id] => 25043 [weight] => 1 [attrs] => Array ( ) ) [52] => Array ( [id] => 25350 [weight] => 1 [attrs] => Array ( ) ) [53] => Array ( [id] => 25609 [weight] => 1 [attrs] => Array ( ) ) [54] => Array ( [id] => 26063 [weight] => 1 [attrs] => Array ( ) ) [55] => Array ( [id] => 26614 [weight] => 1 [attrs] => Array ( ) ) [56] => Array ( [id] => 26819 [weight] => 1 [attrs] => Array ( ) ) [57] => Array ( [id] => 28093 [weight] => 1 [attrs] => Array ( ) ) [58] => Array ( [id] => 28175 [weight] => 1 [attrs] => Array ( ) ) [59] => Array ( [id] => 28292 [weight] => 1 [attrs] => Array ( ) ) [60] => Array ( [id] => 29155 [weight] => 1 [attrs] => Array ( ) ) [61] => Array ( [id] => 30364 [weight] => 1 [attrs] => Array ( ) ) [62] => Array ( [id] => 30506 [weight] => 1 [attrs] => Array ( ) ) [63] => Array ( [id] => 30854 [weight] => 1 [attrs] => Array ( ) ) [64] => Array ( [id] => 31439 [weight] => 1 [attrs] => Array ( ) ) [65] => Array ( [id] => 31566 [weight] => 1 [attrs] => Array ( ) ) [66] => Array ( [id] => 31668 [weight] => 1 [attrs] => Array ( ) ) [67] => Array ( [id] => 31704 [weight] => 1 [attrs] => Array ( ) ) [68] => Array ( [id] => 32017 [weight] => 1 [attrs] => Array ( ) ) [69] => Array ( [id] => 32092 [weight] => 1 [attrs] => Array ( ) ) ) [total] => 70 [total_found] => 70 [time] => 0,000 [words] => Array ( [*test*] => Array ( [docs] => 70 [hits] => 70 ) ) )
sphinx php api
// Include the sphinx API class
require(ENGINE_DIR . "/modules/sphinxapi.php");
$sp = new SphinxClient();
$sp->setFieldWeights(array('title'=>10,'full_story'=>1));
$sp->SetMatchMode(SPH_MATCH_all);
$sp->SetArrayResult(true);
$sp->SetLimits(0,$count_result = ($config['search_number'] * 5));
$query = explode(" ",$story);
$query = implode(array_map(function($item) { return ' *' . $item . '* '; }, $query));
$results = $sp->Query($query, $config['sphinx_index']);
print_r($results);
$count_result = $results['total_found'];
$result_count = $count_result;
than i'm performing a select in on ids
$sql_request = "SELECT * from post WHERE id IN {$rIds} LIMIT $search_start,{$config['search_number']}";
sphinx conf
type = mysql
sql_host = localhost
sql_user = user
sql_pass = pass
sql_db = user
sql_port = 3306 # optional, default is 3306
sql_query_pre = REPLACE INTO sph_counter SELECT 1, MAX(id) FROM post
sql_query_pre = SET NAMES utf8
sql_query_pre = SET CHARACTER SET utf8
sql_query_pre = SET SESSION query_cache_type=OFF
sql_query = SELECT id, title, full_story, category, DATE FROM post WHERE approve='1' AND id <=( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 )
#sql_attr_timestamp = DATE
sql_ranged_throttle = 0
index
index online_test
{
source = online_test_posts
path = /var/lib/sphinx/online_test
docinfo = extern
charset_type = utf-8
morphology = stem_enru
min_word_len = 1
min_prefix_len = 0
min_infix_len = 1
mlock = 0
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
enable_star = 1
rt_mem_limit = 512M
}
my indication is a post with a title "Test Your Brain" with an id '2865' it's should be at least second. instead i'm getting post with a title "Coming & Going" with full_story content test, id post - 1432
by the way, if I'm running search from command line the results are much more accurate.
search test
index 'online_test': query 'test ': returned 7 matches of 7 total in 0.008 sec
displaying matches:
1. document=2865, weight=1684
2. document=5362, weight=1684
3. document=8190, weight=1684
4. document=23460, weight=1684
5. document=25609, weight=1684
6. document=31704, weight=1684
7. document=32092, weight=1684
Well you've proved yourself, that setFieldWeights is working :)
When no weights: [4] => Array ( [id] => 2865 [weight] => 1 [attrs] => Array ( )
When 10: [4] => Array ( [id] => 5720 [weight] => 10 [attrs] => Array ( [date] => 2013 )
When 10000: [4] => Array ( [id] => 5720 [weight] => 10000 [attrs] => Array ( )
And when have weights set, you have documents with different weights:
[34] => Array ( [id] => 32017 [weight] => 10 [attrs] => Array ( [date] => 2013 )
[35] => Array ( [id] => 1432 [weight] => 1 [attrs] => Array ( [date] => 2012 )
... so some results (persuambly with a match on the title) have a higher wieght, and promoted above the description matches.
The fact that some tests have the date atttribute, and some dont, is just because you run the tests at different times? (so not a real problem, just an artifact of your testing?)
What I wonder, if you realise, you are just doing:
$sql_request = "SELECT * from post WHERE id IN {$rIds} LIMIT $search_start,{$config['search_number']}";
which will just return the results in arbitary order (what ever mysql deems fit).
Maybe you are looking at the results of the mysql query directly, and not realising you need to sort the results yourself, so they match the order returned from sphinx.
And you have odd LIMIT. The SQL query shouldnt have a limit, because paging is done in sphinx NOT in mysql.
Suggest
$sql_request = "SELECT * from post WHERE id IN {$rIds} ORDER BY FIELD(id,{$rIds})";
So that mysql will return you the results sorted correctly.
From: http://sphinxsearch.com/info/faq/

Group by on a multidimensional array

I'm afraid that it is simply not possible what I'm trying to do, but I hope you can help me to find a nice way to solve this problem.
I've got the following PHP-array:
Array (
[0] => Array ( [article] => 10.499-1 [operation] => KN_KABEL [date] => 31-05-2013 [hours] => 0 [quantity] => 1 )
[1] => Array ( [article] => 10.499-1 [operation] => LAS_LABEL [date] => 31-05-2013 [hours] => 0 [quantity] => 1 )
[2] => Array ( [article] => 10.499-1 [operation] => ASS_HARNES [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[3] => Array ( [article] => 10.499-1 [operation] => CONTROLE [date] => 07-06-2013 [hours] => 0 [quantity] => 1 )
[4] => Array ( [article] => 24.030 [operation] => LAS_LABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[5] => Array ( [article] => 24.030 [operation] => ZAGEN-RAIL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[6] => Array ( [article] => 24.030 [operation] => KN_KABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[7] => Array ( [article] => 24.030 [operation] => ASS_RAIL [date] => 05-06-2013 [hours] => 0 [quantity] => 1 )
[8] => Array ( [article] => 791 070-6/GS/P [operation] => GS_UNIT [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[9] => Array ( [article] => 791 070-6/GS/P [operation] => PR_UNIT [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[10] => Array ( [article] => 791 070-6/GS/P [operation] => LAS_LABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[11] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => MAGAZIJN [date] => 10-06-2013 [hours] => 0 [quantity] => 1 )
[12] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => PR_UNIT [date] => 11-06-2013 [hours] => 0 [quantity] => 1 )
[13] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => LAB_PLAKKE [date] => 11-06-2013 [hours] => 0 [quantity] => 1 )
)
What I'm trying to do is to count for each date (key "datum") the sum of hours (in this example they are all 0 but I still want to do this because this will change in the future). What would be very practicle is to do a query like sql like SELECT SUM(hours) GROUP BY date but this is no SQL unfortunately.
Is there a way to group (and order) my array by a specific key (in this case "date") or, if not, is there an other way to get the result what I want?
EDIT
I recently added a key "department" which should be grouped by to. Thereby I do not only want to count the sum of "hours", but "quantity" too
Just create simply foreach for this.
$arr = array();
$sums = array();
foreach($arr as $k=>$v)
{
if(!isset($sums[$v['date']][$v['department']]['hours'])) $sums[$v['date']][$v['department']]['hours'] = 0;
if(!isset($sums[$v['date']][$v['department']]['quantity'])) $sums[$v['date']][$v['department']]['quantity'] = 0;
$sums[$v['date']][$v['department']]['hours'] += $v['hours'];
$sums[$v['date']][$v['department']]['quantity'] += $v['quantity'];
}
print_r($sums);
it will create array $sum where keys are your dates. If value doesn't exists it will add the value of hours to 0, else if it exists it will add to existing value.
edit
Fitted to OP needs.

Categories