简体   繁体   English

轮胎宝石的搜索方法有哪些参数?

[英]What are the parameters for the search method of the Tire gem?

I need to run a search using Tire with my query specifically defined as a parameter, but I'm unsure how to proceed. 我需要使用Tire来运行搜索,并将我的查询专门定义为参数,但是我不确定如何继续。

search = {
  query: {
    function_score: {
      query: { match_all: {} },
      # filters is an array previously built
      functions: filters,
      score_mode: "total"
    }
  }
}

Program.tire.search(load: true, size: 50, search)

I'm receiving the following error: /Users/app/models/program_match.rb:122: syntax error, unexpected ')', expecting tASSOC which makes me believe I'm simply missing a key word before I call search . 我收到以下错误: /Users/app/models/program_match.rb:122: syntax error, unexpected ')', expecting tASSOC : /Users/app/models/program_match.rb:122: syntax error, unexpected ')', expecting tASSOC ,这使我相信我只是在调用search之前错过了一个关键词。

Any help would be greatly appreciated! 任何帮助将不胜感激!

You probably just need to do: 您可能只需要做:

 Program.tire.search({load: true, size: 50}.merge(search)) 

EDIT 编辑

Actually, looking at the source for search ( https://github.com/karmi/retire/blob/master/lib/tire/model/search.rb ), it looks like you need to do: 实际上,查看search源( https://github.com/karmi/retire/blob/master/lib/tire/model/search.rb ),看起来您需要这样做:

Program.tire.search(search, {load: true, size: 50})

search expects two params (query, options) or one param (for options) and a block (for the query). search需要两个参数(查询,选项)或一个参数(用于选项)和一个块(用于查询)。 Ruby gets confused because you have started a hash ( load: true ... ) and then just put a new hash (your search hash), which it sees as a hash key (with no value). Ruby感到困惑,因为您已经启动了一个哈希( load: true ... ),然后仅放置了一个新哈希(您的search哈希),它被视为哈希键(无值)。

Also, if you are just starting out with Tire, I would suggest checking out elasticsearch-rails , which, according to the author, is replacing Tire. 另外,如果您刚开始使用Tire,我建议您检查一下elasticsearch-rails ,据作者说,它正在替代Tire。

I recently converted a Tire project to elasticsearch-rails, and have found that it can do everything Tire does, although it doesn't provide the query DSL (it seems like you're not using that anyway, so no loss). 最近,我将Tire项目转换为elasticsearch-rails,发现它可以完成Tire所能做的一切,尽管它不提供查询DSL(似乎您无论如何都没有使用它,因此不会造成任何损失)。

EDIT 2 编辑2

You can do a simple match_all query like: 您可以执行一个简单的match_all查询,例如:

Program.tire.search(load: true, size: 50) { query { all } }

You can get something similar by doing: 您可以通过执行以下操作来获得类似的结果:

Program.tire.search('*', load: true, size: 50)

As I noted in a comment below, a query as the first param for search will always be wrapped in a query_string query. 正如我在下面的评论中指出的那样,作为search的第一个参数的search将始终包装在query_string查询中。

Probably the best way to do what you asked initially is to do: 做您最初要求的最好的方法可能是:

Tire.search(Video.tire.index_name, query: {
  function_score: {
    query: { match_all: {} },
    functions: filters,
    score_mode: "total"
  }
}).results

I just tested a similar function_score query on a local project and confirmed that it produces the expected query. 我刚刚在本地项目上测试了类似的function_score查询,并确认它产生了预期的查询。

EDIT 3 编辑3

I've never used the load option before, but it looks like you can do: 我以前从未使用过load选项,但看起来您可以做到:

Tire.search(Video.tire.index_name, payload: {
  query: {
    function_score: {
      query: { match_all: {} },
      functions: filters,
      score_mode: "total"
    }
  }
}, load: true).results

Note that you have to wrap the query as the value for payload . 请注意,您必须将查询包装为payload的值。

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

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