2011-08-19 17 views
0

我正在嘗試在使用思維獅身人面像的Rails中進行字段特定搜索。ThinkingSphinx中的字段特定搜索不起作用

我有一個名爲三個表,B,C與模型A,B,C。

表的內容: 表A,B,C

mysql> select * from a; 
+----+--------+----------+ 
| id | name | city  | 
+----+--------+----------+ 
| 1 | 7-11 | Portland | 
| 2 | Costco | Bend  | 
| 3 | Costco | Astoria | 
+----+--------+----------+ 
3 rows in set (0.00 sec) 

mysql> select * from b; 
+----+--------+---------+ 
| id | name | city | 
+----+--------+---------+ 
| 1 | 7-11 | Bend | 
| 2 | Costco | Astoria | 
| 3 | Costco | Bend | 
+----+--------+---------+ 
3 rows in set (0.00 sec) 

mysql> select * from c; 
+----+--------+---------+ 
| id | name | city | 
+----+--------+---------+ 
| 1 | 7-11 | Astoria | 
| 2 | Costco | Astoria | 
| 3 | Costco | Bend | 
+----+--------+---------+ 
3 rows in set (0.00 sec) 

這是我使用的應用範圍內搜索在我的控制檯做:

目標:顯示所有7-11在波特蘭市

irb(main):021:0> @a = ThinkingSphinx.search "7-11 @city Portland", :match_mode=>:extended 
=> [] 

我知道表A有一個7- 11與波特蘭市在其中。所以讓我們嘗試:

irb(main):022:0> @a = A.search "7-11 @city Portland", :match_mode=>:extended 
=> [] 

尋找Costco的以城市爲道夫作品。

irb(main):023:0> @a = ThinkingSphinx.search "costco @city Astoria",  :match_mode=>:extended 
=> [#<B id: 2, name: "Costco", city: "Astoria">, #<A id: 3, name: "Costco", city:  "Astoria">, #<C id: 2, name: "Costco", city: "Astoria">] 

尋找Costco的以城市爲工作:

irb(main):023:0> @a = ThinkingSphinx.search "costco @city Astoria", :match_mode=>:extended 
=> [#<B id: 2, name: "Costco", city: "Astoria">, #<A id: 3, name: "Costco", city: "Astoria">, #<C id: 2, name: "Costco", city: "Astoria">] 

我不知道爲什麼領域的具體搜索與文本值工作,而不是在一個地方與所有名字數字和破折號。我已經通讀了文檔,我只是不知道我在做什麼錯。

這是什麼在起作用:

irb(main):026:0> @a = A.search "7-11" 
=> [#<A id: 1, name: "7-11", city: "Portland">] 

這並不

irb(main):027:0> @a = A.search "7-11 @city Portland", :match_mode=>:extended 
=> [] 
+0

你的define_index塊對模型A來說是什麼樣的? – pat

+0

define_index做 指標名稱 指標城市 結束 – Shan

回答

1

嗯,我想我知道問題 - 這個問題是不是在波特蘭市過濾器,但是您在擴展查詢中使用連字符這一事實。 A.search 'portland是否返回記錄?和A.search '@city portland', :match_mode => :extended

要解決這個問題,你可以使用Riddle.escape逃避查詢 - 這只是把\在特殊字符前。另外,值得注意的是,您可以使用:conditions指定以字段爲重點的搜索,並自動將匹配模式設置爲擴展。

A.search Riddle.escape('7-11'), :conditions => {:city => 'Portland'} 
+0

優秀。謝謝你拍拍。我會試一試,看看我還有什麼地方卡住了。 – Shan