¿Cómo devolver solo el primer registro con thinking_sphinx?

In a typical Rails scenario where I only want the first and only one record returned, I would say:

SomeModel.find(:first, :conditions => {:some_field => 'some_value'})

How would I ask this using thinking_sphinx? I'm supposing or hoping it would be something like:

SomeModel.search :conditions => {:limit => 'first', :some_field => some_value} 

However, I'm fairly certain this won't work as it is just a wishful guess. Any clues?

preguntado el 08 de noviembre de 11 a las 10:11

2 Respuestas

Try the following if you need to search on any field:

SomeModel.search "search term", :star => true, :page => 1, :per_page => 1

But if you need some order like 'name' column:

SomeModel.search "some_value", :order => "name DESC", :star => true, :page => 1, :per_page => 1

If you need to search on specific field

SomeModel.search :conditions => {:some_field => 'some_value'}, :order => "name DESC", :star => true, :page => 1, :per_page => 1

respondido 09 nov., 11:15

Hm, I'm afraid it's wrong way to compare searching by exact matching of looked up phrase (the first example) with full-text searching provided by Sphinx (the second one). While using the first query you are able to retrieve the only one first record with equal value to the searched one. And if you use Sphinx you pass a ranking function that defines what matching best suits for you (eg. if you give more keywords, then you can state whether you want to match all the keywords in each document, or only one keyword per document). If you want to read more about it, you can follow a link: http://sphinxsearch.com/blog/2010/08/17/how-sphinx-relevance-ranking-works/.

However it's possible to get the first matching value using thinking-sphinx, example:

Article.search('sphinx relevance', :match_mode => :all).first

respondido 09 nov., 11:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.