Sunspot search by id - solr

Is there any way to search entity by id with subspot
like so:
Content.search do
with(:ids, [1,2,3])
end

Unfortunately the only way to achieve this is indexing the id field:
class YourModel < ActiveRecord::Base
searchable do
integer :id
# ...
end
end

Related

Spring Data Solr phrase search and Sorting

I am looking for help on Spring data solr phrase search and ordering.
When I search for "Spring Data", it should return the data that has "Spring Data" in it.
> Ex:- Should return --- Spring Data is good
> Should return --- 123Spring Data123 is good
> Should not return --- Spring and Data
Also, the result should be in the sorted order based on the number of fields matched.
When I get the search input from the user, I do search as below.
public interface SolrSecurityRepository extends
SolrCrudRepository<SolrSecurity, String> {
#Query(value = "subject:*?0* OR content:*?0*")
List<SolrSecurity> find(String searchStr,
Pageable pagebale);
}
Not sure how to achieve exact phrase match ....
Also, Is there anyway to enable the debug to see the query being generated by Spring data solr ...
Thanks,
Baskar.S
Spring Data Solr relies on the behavior of the Standard Query Parser. By default no action is performed to enforce sorting in any way, which means documents are sorted by score. An exact phrase search would mean you'd have to quote the terms string. You can have a look at the score assigned by adding a readonly score property to the entity and alter the annotated Query to read the given list of fields.
class SomeDocument {
#Id String id;
#Indexed(readonly=true) Float score;
// ...
}
#Query(value = "subject:*?0* OR content:*?0*", fields={"*","score"})
List<SomeDocument> find(String searchStr, Pageable pagebale);
Or use simply use #Score (currently only available for 1.4.RC1) within the document.
If you want to print out the query before execution please enable logging for SolrTemplate by adding <logger name="org.springframework.data.solr.core.SolrTemplate" level="debug" /> to logback.xml.

How do I do Mongoid 1:N:M with a field

I'm trying to describe a 1:N M:1 relationship with the linking table containing an extra fields of info. Both table1 and table3 both have a lot of fields.
The existing table looks like this:
Table1 1:N Table2 M:1 Table3
somethings extra info otherthings
id table1_id id
table3_id
The table2.extra info is messing things up for me. How do I describe this in mongoid?
class Model1
include Mongoid::Document
field :somethings, :type => String
has_many_and_belongs_to :inbetween
end
class ModelInbetween
include Mongoid::Document
field :extra_info, :type => String
???
end
class Model2
include Mongoid::Document
field :otherthings, :type => String
has_many_and_belongs_to :inbetween
end
In Mongoid, has_many and belongs_to are used to express a 1:N relationship between two collections. has_many_and_belongs_to is only used for N:N relationships. See here for the official documentation. As far as I can tell, your tables should look like this:
class Model1
include Mongoid::Document
field :somethings, :type => String
has_many :modelInbetweens
end
class ModelInbetween
include Mongoid::Document
field :extra_info, :type => String
belongs_to :model1
belongs_to :model2
end
class Model2
include Mongoid::Document
field :otherthings, :type => String
has_many :modelinbetweens
end
Also, make sure to pay attention to whether the plural/singular form of the model should be used in defining the relationships.

Searching String Fields Using Sunspot and Rails

My team is attempting to implement Solr and Sunspot-Rails as a search provider for our application. Our story requires that certain string fields be searched in addition to the text fields. I have seen some people aggregate these fields into a consolidated indexing field (with a type of Text) with ActiveRecord callbacks. Is this my only hope or is there a wildcard argument that I am missing?
you can define your fields that will be indexed like this
class Post < ActiveRecord::Base
searchable do
text :title, :body
text :comments do
comments.map { |comment| comment.body }
end
string :sort_title do
title.downcase.gsub(/^(an?|the)/, '')
end
end
end
then you search them you can specify the fields you want to search like this
Post.search do
fulltext 'pizza' do
fields(:body, :title)
end
end
if you didn't specify fields it'll apply the search to all the text fields indexed
Post.search do
fulltext 'best pizza'
end

limiting fields returned from a mongoid polymorphic relation

I have a polymorphic relation in mongoid like the following one:
class Company
include Mongoid::Document
field :name, :type => String
has_many :posts, as: :postable
end
class Person
include Mongoid::Document
field :name, :type => String
has_many :posts, as: :postable
end
class Post
include Mongoid::Document
belongs_to :postable, polymorphic: true
end
I would like to sometimes (and not most of the time) load only some fields of the postable. In a non polymorphic relation (say only a Person has posts) I can do:
Person.only(:name).find(some_post.postable_id)
But is this possible in a polymorphic relation?
I can see a few ways of doing this but I am not sure which is the best. Complex relations are not a strength MongoDB and other NoSQLs and queries can get expensive if you start scanning multiple documents for your answers.
Using where:
Person.where(name: "name").posts.where(postable_id: "id")
Finding the parent from the post:
Post.where(id: "id").person.only(:name)
(note that postable_id and id are not the same value!)
However, be careful. If all you are doing is listing posts you are probably better off including the person's name in the post and writing a callback to update the person's name if they change their name. Alternatively, if you are linking to comments from a person why not include the comment IDs (or enough data to make the link) in the Person model so you can get all comments with one document call. Similarly if you are getting a count, use a callback to make a post count in the Person model so you aren't mapping hundreds of Posts every time.
I hope this helps, if not provide me with some more information and I'll update this.

How to search embedded mongoid documents with sunspot SOLR?

Does anyone know how to index and search embedded documents with sunpot_mongoid?
The question has been asked in the sunspot_mongoid issues, but has no solution, so far.
Just tried it. It's a hack, but it works for searching embedded documents, and returning the parent document holding it. Is that what you want? If so, do this then. Define method that returns the embedded fields you want as an array, and then index that array.
Assuming you have class Company, with embedded departments
searchable do
# Your regular index
# ...
text :company_departments
end
def company_departments
departments.map(&:name).join(" ")
end
reindex and try to search.
You can also include a block that returns the text you want index right in the searchable block. For example:
searchable do
text :innerdoc do
innerdocs.map { |i| i.title + ' ' + i.description }
end
end
That takes the title and description from an embedded array of "innerdocs" and adds it to the index for the main document.
The sunspot docs have the best info on the syntax for the "searchable" block:
http://outoftime.github.com/sunspot/docs/

Resources