如何从结果集中的数字中提取Sphinx组?

时间:2011-06-09 03:08:33

标签: php mysql sphinx

我需要一些关于Sphinx MySql索引组的帮助。

我有一个产品表,其中有产品ID和相应的零售商ID。我想要做的是创建一个简单的列表,以显示零售商的名称以及它在表中的产品数量:

标准SQL:从tblproducts中选择retailerid,count(productid)作为cnt 狮身人面像:通过retailerid

从tblproducts1组中选择*

我正在使用Sphinx 2.0.1-beta

我想使用Sphinx的原因是,因为产品数量将通过复杂的匹配全文查询,产品名称和描述等进行过滤。

现在我的问题是,如何从Sphinx返回的结果中获取这些数字/ ID?因为我使用php来显示结果,我想如果有人可以向我展示一些有用的PHP代码来从结果集中提取这些数字。 Sphinx返回的结果数组让我更加困惑;我在任何地方都没有看到任何产品数量的参考!

我确信Sphinx会返回这些数字,因为它支持group by和count,但是如何从结果集中提取数字呢?

以下是我的配置文件:

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

更新

我希望如此,但我没有看到在下面的结果集中计数的引用。我必须在某处犯错:

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 ) 
   )
)

1 个答案:

答案 0 :(得分:0)

有一个魔术属性@count已添加到该查询的结果集

select * from tblproducts1 group by retailerid

结果集中的属性列表类似于

id, weight, MerchantActive, ... @groupby, @count

如果您在searchd部分中使用compat_sphinxql_magic = 0选项,则您已明确定义和别名所有需要的属性,如

select *, weight(*) as w, count(*) as c from tblproducts1 group by retailerid

结果集中的属性列表看起来像

id, MerchantActive, ... w, c