Boost exact match searchkick / elastic search Rails 4 - ruby-on-rails

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},
]
)

Related

MongoID Like query on embedded model

I have User model embeds_one :profile and Profile model has name. I need to run LIKE query on profile name. I tried below as suggested here
User.where("profile.name" => "/.*Senthil.*/")
But above solution not working. I tried lot of stock overflow answers , but no luck. Any help will be highly appreciated.
Screenshot : I am very sure , there is matching record.
This finds all people with the name senthil (first OR last).
User.where("profile.name" => /.*senthil.*/i )
Here i used to make the query case insensitive.
I think you mean to remove the quotes, otherwise the engine will try to match the string exactly, instead of as regex
edit: The correct regexp would be
User.where("profile.name" => /.*Senthil.*/)

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/

Difficult search in ruby on rails

How can I search first in model (i know how to do this). And then search in this array for more concretence? As you see:
#articles = Article.find(:all, :conditions => { :ART_ID => #search.map(&:ARL_ART_ID)})
#a = #articles.find_all{|item| item.ART_ARTICLE_NR == search.upcase }
First i search in model, but thanks to my db) it have many wrong results, so i must to clarify my array. But there how to search like sql:
like % %
Now it search very strong: if i search AC451, it's good, but if AC45 or C451 it's nothing fetches. How to say him so that before and after
search
could be everything?
Like this, maybe?
item.ART_ARTICLE_NR.include?(search.upcase)
You are asking for trouble by not following rails naming conventions an using upper case column names. That said, the rails3 way to do it is probably:
#articles = Article.where(:ART_ID => #search.map(&:ARL_ART_ID)).where('ART_ARTICLE_NR LIKE', "%#{search.upcase}%")
Without knowing what #search is, it's hard to be sure. But you should read up on the active record guide on the rails 3 query format.
It's maybe not straigtforward answer, but have you considered using ransack gem?

How do i go about searching in Ruby on Rails?

I would like to implement a simple search. Let's say the user enters 'york', then I would like to find all records that has a matching substring like 'new york' or 'yorkshire'.
So far I have figured out I will have to use the find method, but I can't figure out how to match for substrings.
city = params[:q]
User.find(:all, :conditions=>["city like :text", {:text=>"%#{city}%"} ] )
You might want to take a look at the Thinking Sphinx plugin for doing such full text search . Solr is also another option .

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