2015-12-15 46 views
0

我有以下手冊斯芬克斯查詢(經由MySQL客戶端),即產生正確的結果,我想通過從導軌思考斯芬克斯調用它。對於我的生活,我正在思考如何在思維獅身人面像中做出「獨特」的查詢工作。通過,思考斯芬克斯組與重複計數

mysql> select merchant_name, count (distinct part_number) from product_core group by merchant_name; 

+-----------------------+-----------------------------------------+ 
| merchant_name   | count (distinct part_number)   | 
+-----------------------+-----------------------------------------+ 
|   1962041491 |          1 | 
|   3208850848 |          1 | 
|   1043652526 |         48754 | 
|    770188128 |          1 | 
|    374573991 |         34113 | 
+-----------------------+-----------------------------------------+ 

請注意:這個mySQL查詢是agaist獅身人面像,而不是mySQL。我使用mySQL客戶端連接到Sphinx,如:mysql -h 127.0.0.1 -P 9306。這適用於調試/開發。我的實際分貝是Postgres。

鑑於此,並添加更多的上下文,我試圖結合思考斯芬克斯group_bycount('Distinct' ...)

所以,這個查詢的工作:

Product.search group_by: :merchant_name 

...而且,這個查詢的工作:

Product.count ('DISTINCT part_number') 

...但是,這個組合查詢拋出一個錯誤:

Product.search group_by: :merchant_name, count ('DISTINCT part_number') 

SyntaxError: (irb):90: syntax error, unexpected (arg, expecting keyword_do or '{' or '(' 
...merchant_name, count ('DISTINCT part_num... 

merchant_name和part_number都被定義爲屬性。

環境:

Sphinx 2.2.10-id64-release (2c212e0) 
thinking-sphinx 3.1.4 
rails 4.2.4 
postgres (PostgreSQL) 9.3.4 

我也曾嘗試使用構面,但無濟於事:

Product.search group_by: :merchant_name, facets: :part_number 
Product.facets :part_number, group_by: :merchant_name 

有關其他信息,並看看這可以通過思考獅身人面像調用來完成,這裏是一個基本的例子。我有一個產品表(和相關索引),列出了商家和他們的產品(我同意,它可以進行標準化,但是它來自數據饋送,Sphinx可以按原樣處理):

+-----------------+-------------------+ 
| merchant  | product   | 
+-----------------+-------------------+ 
| Best Buy  | Android phone | 
| Best Buy  | Android phone | 
| Best Buy  | Android phone | 
| Best Buy  | iPhone   | 
| Amazon  | Android phone | 
| Amazon  | iPhone   | 
| Amazon  | iPhone   | 
| Amazon  | iPhone   | 
| Amazon  | Onkyo Receiver | 
+-----------------+-------------------+ 

的思維獅身人面像,我想:一組)的行由商人,和b)爲每個組創建一個「獨特」的產品數量。

上面的例子,應該給予以下結果:

+-----------------+------------------------+ 
| merchant  | count(DISTINCT product | 
+-----------------+------------------------+ 
| Best Buy  | 2     | 
| Amazon  | 3     | 
+-----------------+------------------------+ 
+0

什麼是與查詢的問題? merchant_name是否返回整數?或者,除此之外,總值是否不準確?你使用的是什麼版本的獅身人面像? – pat

+0

Pat,我的原始查詢通過MySQL客戶端(它證明我的索引和屬性起作用)完美地對抗Sphinx,我只是無法將查詢的'區域'部分寫入ThinkingSphinx調用。 –

回答

1

你不會是能夠運行通過一個模型的搜索調用此查詢,因爲這是設置爲始終返回一個模型的實例,而你想要的是原始結果。下面的代碼應該做的伎倆:

ThinkingSphinx::Connection.take do |connection| 
    result = connection.execute <<-SQL 
    SELECT merchant_name, COUNT(distinct part_number) 
    FROM product_core 
    GROUP BY merchant_name 
    SQL 
    result.to_a 
end 

或者說,我認爲這會工作經歷一個正常的search電話:

Product.search(
    select:  "merchant_name, COUNT(distinct part_number) AS count", 
    group_by: :merchant_name, 
    middleware: ThinkingSphinx::Middlewares::RAW_ONLY 
) 
+0

嗨帕特,感謝您的及時回覆!我正在編寫自己的連接對象,但更願意使用Thinking Sphinx已經創建和管理的連接。 –

+0

現在我已經有了這個工作,我發現您必須爲我的屬性創建自己的CRC值,這些屬性已經轉換爲bigint。我可以將sphinx_internal_id映射回我的id,但只需要再問一次,是否有辦法使用Thinking Sphinx方法創建相同的group_by merchant_name/count unique part_number結果? 我缺少一些基本的w/TS嗎? –

+0

馬丁,我不是很追隨:以上述查詢不是你所追求的是什麼?或者更好的是,你想達到什麼目標? – pat