I am trying to do field specific searching in Rails using Thinking Sphinx.
I have three tables named a,b,c with Models A,B,C.
Contents of tables: Table 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)
This is what I do in my console using Application Wide Searching:
Goal: Display all 7-11's in the city of Portland
irb(main):021:0> @a = ThinkingSphinx.search "7-11 @city开发者_运维知识库 Portland", :match_mode=>:extended
=> []
I know that table A has a 7-11 with the city of Portland in it. So lets try that:
irb(main):022:0> @a = A.search "7-11 @city Portland", :match_mode=>:extended
=> []
Finding Costco with city as Astoria works.
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">]
Finding Costco with city as Bend works:
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">]
I don't know why field specific searching works with text values as opposed to when a place has a name with all numbers and dashes. I have read through the documentation, and I just don't know what I am doing wrong.
This is what works:
irb(main):026:0> @a = A.search "7-11"
=> [#<A id: 1, name: "7-11", city: "Portland">]
And this doesn't
irb(main):027:0> @a = A.search "7-11 @city Portland", :match_mode=>:extended
=> []
Ah, I think I know the problem - the issue is not the city filter on Portland, but the fact that you're using a hyphen in an extended query. Does A.search 'portland
return the record? And A.search '@city portland', :match_mode => :extended
?
To get around this, you can use Riddle.escape to escape queries - which just puts \
in front of special characters. Also, it's worth noting that you can use :conditions
to specify field-focused searches, and that automatically sets the match mode to extended.
A.search Riddle.escape('7-11'), :conditions => {:city => 'Portland'}
精彩评论