简体   繁体   English

使用轮胎从Elasticsearch中的查询中提高

[英]Boost from query in elasticsearch using tire

I have a set of users who have backgrounds (eg "computer science") and skills (eg "php"). 我有一组具有背景(例如“计算机科学”)和技能(例如“ 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): 我已经设法使用带有此JSON字符串的curl来进行此工作(在这种情况下,我强调了背景):

'"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. 现在我的问题是,我无法弄清楚如何将这个JSON用作Tire中的查询,或如何在Tire DSL中编写等效内容。

EDIT 编辑

Actually I figured it out by looking at the Tire source code. 实际上,我通过查看Tire源代码了解了这一点。

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. 我将要强调的一个设置为5,将另一个设置为0。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM