Using Atlas Search with Mongoid on Rails - ruby-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' => '*'
}
}
}
}])

Related

Searching in polygons in sphinx using Thinking Sphinx

I have setup thinking sphinx for real-time indexing and It works very well and search with geodist as well.But now I want to search the records within polygon.
Sphinx documentation explains it very well at Geo-distance searching
now I want to use this functionality using thinking sphinx.
Thinking sphinx does explain about geodist search Here
But It does not state how to search within a polygon.
Can any one help me here to do that?
Thinking Sphinx doesn't have anything inbuilt to provide a neat interface for the polygon searching, but it's certainly possible to use the functionality.
You'll want to generate the SphinxQL SELECT clause that you'd like, filter on it accordingly, and/or you can access your custom attributes using the AttributesPane. The following code is hopefully clear:
search = Model.search("foo",
:select => "*, CONTAINS(GEOPOLY2D(...), ...) AS inside",
:with => {:inside => true}
); ""
search.context[:panes] << ThinkingSphinx::Panes::AttributesPane
search.collect { |instance| instance.sphinx_attributes["inside"] }
The ; "" at the end of the first statement is only needed when running this in IRB or a Rails console - you want to avoid calling search in a way that evaluates the results until you've added the pane in. Search results are normally lazily loaded, but IRB calls inspect to print statement results which removes the lazy advantage.
Panes are discussed in a blog post I wrote, and the source code for the AttributesPane class is very simple. You may also want to write a custom middleware class that uses your own options and translates them into the polygon functions - the Geographer class (which is what translates :geo into GEODIST) is a good reference for that.

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.

mongodb with rails, find by id in array

I can fetch an element by BSON id from Mongodb with
db.my_collection.find({_id: ObjectId("567bc95ab62c732243123450")})
And it works. But how can I get an array of ids? something like
db.my_collection.find({_id: [ObjectId("567bc95ab62c732243123450"])})
I tried different ways, as suggested on mongodb's website, but the interactive shell complained for syntax.
EDIT:
Found a problem:
it should be
db.my_collections.find({_id: { $in : [ObjectId("567bc95ab62c732243123450")]}})
And in Rails:
MyCollection.find({'_id' => { "$in" => collection_ids}})

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 :)

How to add conditions to thinking-sphinx search?

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

Resources