How to add conditions to thinking-sphinx search? - ruby-on-rails

I've recently installed thinking-sphinx on my ruby on rails app. As first sight, everything works great, I can search words and it will find them. Now, when I try to add some filters(such as, for example, provinces, categories, etc) using:
MyModel.search 'hello' :conditions => 'category_id=1' for example, it will throw me the following exception:
searchd error (status: 1): invalid or
truncated request
I've been reading some docs on the thinking-sphinx plugin, and I guess I have to do something else than this syntax.
To summarize: until the moment, I just installed the windows service, then I defined a index in one model, and then i tried to search. Again, I succeeded while searching without conditions, but failed while searching with.
Any help will be appreciated.
Thanks,
Brian

Assuming your model has category_id:
MyModel.search 'hello', :with => {:category_id => 1}
:conditions is for full text searching specific attributes, :with is for filtering search results.
More info here: http://freelancing-gods.com/posts/a_concise_guide_to_using_thinking_sphinx

Related

Using Atlas Search with Mongoid on Rails

I am trying to get full text search with Atlas to work in my Rails app. I have set up the index following this tutorial in their docs. When I test the query in a vacuum it seems to work as expected, I'm able to query my database and get the results I would expect, but it seems like the documentation around how to do this in Mongoid is lacking. I have found this documentation for running text search in Mongoid, but it explicitly calls out that it isn't Atlas Search.
Has anybody successfully implemented an Atlas Search index/query using Mongoid (or otherwise in a Rails app) and, if so, could you please point me towards the relevant docs.
Alright - after some experimentation with this I have found that the following code will work with the current version of mongoid:
TableName.collection.aggregate([{
'$search' => {
'index' => 'index_name',
'text' => {
'query' => 'some string to search',
'path' => {
'wildcard' => '*'
}
}
}
}])

Boost exact match searchkick / elastic search Rails 4

Having trouble getting exact matches to display first. I am using searchkick with elastic search on my rails 4 app.
For example, if a user searches "coke" .. "coke zero" will display first. I would like it the other way around.
If there is documentation on this can you please point me in that way? I am having an overly hard time finding a solution.
I have tried boosting the title field ('specific' in my case):
fields: ["specific^20"]
and boosting where the field matches the query exactly (although I don't know if i'm implementing this correctly):
boost_where: [:specific == :q]
Nothing seems to be working. Thank you!
Try this .....
Model.search "hi#example.com", fields: [{column_name1: :exact}, :column_name2]
Hope this will help you.
I had a similar issue. I solved it like this.
Model.search(
'search term',
fields: [
{'name^2' => :phrase},
{'name' => :word_start},
]
)

How can I match a partial string to a database's object's attribute? Regexp?

I have a database containing a list of movies. A typical entry look like this:
id: 1,
title: "Manhatten and the Murderer",
year: 1928,
synopsis: 'some text...'
rating: 67,
genre_id, etc. etc.
Now I'm trying to make a series of search tests pass and so far I have made a single test case pass where if you type the title "Manhatten and the Murderer" in a text field it will find the movie that you want. The problem is with partial matching.
Now I'd like a way to search "Manhat" and match the record "Manhatten and the Murderer". I also want it to match with any movie that has "Manhat" in it. For example, it would return maybe 2 or 3 others like title: "My life in Manhattan", title: "The Big Apple in Manhattan" etc. etc.
Below is the code that I have so far in my Movie model:
def self.search(query)
# Replace this with the appropriate ActiveRecord calls...
if query =~ where(title:)
#where(title: query)
binding.pry
end
end
My question is, how can I set this up? My problem is the "where(title:) line. One thought was to use Regexp to match the title attribute. Any help would be appreciated! Thanks.
Use a query that searches a substring in between:
name = "Manhattan"
Movie.where("title like ?", "%#{name}%")
For example:
%Manhattan will get you: Love in Manhattan
Manhattan% will get: Manhattan and Company
%Manhattan% will get you both: [Love in Manhattan, Manhattan and Company]
But, if you're searching through movies synopsis, you should use Thinking Sphinx or Elastic Search
For example, with Elastic Search, you could set the synopsis like this:
Add app/indices/movie_index.rb:
ThinkingSphinx::Index.define :movie, :with => :active_record do
# fields
indexes title, :sortable => true
indexes synopsis
end
Index your data with rake ts:index
And then run Sphynx with: rake ts:start
You can search just like this:
Movie.search :conditions => {:synopsis => "Manhattan"}
Elastic Search is a great alternative to ThinkingSphinx, there's even a RailsCast about it, so you should definitely take a look to see what really suites you best... Hope this helps!
You do not need regex to find movies that have the search string. You can use SQL query like this:
Movie.where('title LIKE ?','Batman%')
That would return all movies start with "Batman"
Movie.where('title LIKE ?','%Batman%')
That would return all movies that have Batman anywhere in it's title.
I think you figured out the '%' is a joker character in the query.
One option is to run a search server alongside your Rails application. It is certainly my go to solution. This route offers a ton of features not found within Rails itself and might be overkill, but worth consideration.
I use Sphinx and implement it using the thinking-sphinx gem.
Resources:
http://pat.github.io/thinking-sphinx/
http://sphinxsearch.com/

blacklight solr config facet fields

We have a website that we are building running ruby 2.0, rails 4.0.1, blacklight 5.1.0, Apache solr 4.2 with sunspot solr gem for ruby.
We currently have the full solr index with products. If we search * we are able to see all the products. If we try and search a category this does not work.
We are stuck at this point and are not sure how to get the facets to work.
Can anyone lead us in the right direction here as any research we have done has turned up no results.
Solrconfig.xml - http://pastebin.com/RkCS1GUT
scheme.xml - http://pastebin.com/Cffu2Q3r
These files above are modified from the original blacklight example. The material field is currently the one we are using to try and search.
Thanks
If you look at the product data, you have the category in some field. Examine your logs to see what query Blacklight is producing. Obviously it is not targeting the same field. Then move the data or adjust the Blacklight config.
To define facet fields in Blacklight, do something like this (in initializer or in a given Controller):
configure_blacklight do |config|
config.default_solr_params = {
:'q.alt' => "*:*",
:defType => 'dismax',
:facet => true,
}
config.add_facet_field 'objectType_facet', :label => 'Object Type'
config.add_facet_field 'content_type_facet', :label => 'Content Type'
end
The field names and labels will vary for your data, of course.

Rails Active Resources ignore conditions

I have been searching a solution for this issue but in no vain. Basically, I am trying to do some searches using Active Resources eg:
File.find(:all, :params => {:file_name => "blah"})
or:
File.find(:all, :conditions => {:file_name => "blah"})
File is an Active Resource object
I expect the result to be filtered but the output is the same as File.find(:all)(conditions are completely ignored). Has anyone experienced a similar problem?I am using rails 3.0.7, this code is called from a web app which is talking to another API server using AR.
Any suggestions will be much appreciated
Thanks
Dont know if i got your point. I think this should do:
File.where file_name: "blah"
I have the same type of setup, and tons of searching in my application where large results need to get filtered - however, I have been accomplishing this using the API Server side.
So calls would look like:
File.find(:all, :params => {:file_name => "blah"})
Which would be translated into some search URL by rails:
http://someip:port/someurl/files/?file_name=blah
So the API would receive this with the file_name parameter and filter the result set based on that search, and then return those filtered results to the rails server.
I hope that at least helps in some way :)

Resources