user1001113
user1001113

Reputation: 67

Boost from query in elasticsearch using tire

I have a set of users who have backgrounds (e.g. "computer science") and skills (e.g. "php"). I'm trying to search through them while emphasizing either the backgrounds or skills (user picks either option when searching).

I've managed to get this working using curl with this JSON string (in this case I emphasize the background):

'"query" : { 
    "bool" : {
        "should" : [
            {
                "text" : {"skills" : {"query" : "php mysql html css"}}
            },
            {
                "text" : {"backgrounds" : {"query" : "computer science", "boost" : 5}}
            }
        ]
    } 
}'

Now my problem is that I can't figure out how to either use this JSON as a query in Tire, or write the equivalent in Tire DSL.

EDIT

Actually I figured it out by looking at the Tire source code.

Here is what it looks like:

results = Users.search(:load => true) do
  query do
    boolean do
      should { string "skills:#{skills_query}", {:boost => skills_boost}}
      should {string "backgrounds:#{backgrounds_query}", {:boost => backgrounds_boost}}
    end
  end
end

I set the boost to 5 on the one I want to emphasize, and 0 on the other.

Upvotes: 2

Views: 909

Answers (1)

Shawn Deprey
Shawn Deprey

Reputation: 525

There seems to be a serious lack of good documentation for how to do this properly in tire. After hours of searching I found this post. I will be blogging about this later to hopefully keep people from having to dig forever like I did. At any rate, below is what I did based on your suggestion, and, it worked like a charm by boosting the name field above other attribute matches.

page = params[:page].present? ? params[:page] : 1
tire.search(page: page, per_page: ApplicationHelper::MAX_SEARCH_LENGTH) do
  query do
    boolean do
      should { string "name:#{params[:q]}*", {:boost => 100}}
      should { string "#{params[:q]}*"}
    end
  end
end

Upvotes: 1

Related Questions