如何将嵌套的哈希参数传递给searchkick

时间:2018-09-28 07:26:47

标签: ruby elasticsearch searchkick

我正在使用现有的Rails应用程序,他们目前正在使用elasticsearch gem。现在,我尝试将其切换为searchkick。我已经在应用程序上配置了gem。 目前,如果我这样做:

Model.search(keyword),从控制台输出。

但是目前,他们将查询字符串作为嵌套散列传递给实际应用程序,如下所示:

{:query=>{:bool=>{:must=>[{:multi_match=>{:query=>"apple", :fields=>["title^2", "content", "description"]}}], :should=>[], :filter=>[{:term=>{:data_proxy_id=>"4d629f00-c8b1-aacc-97b5-e21e4c0bbe3f"}}, {:term=>{:article_type=>"Article"}}, {:term=>{:status=>"published"}}]}}, :sort=>{"_score"=>{"order"=>"desc"}}, :aggs=>{:group_by_section=>{:terms=>{:field=>"section.name.raw", :size=>0}}, :group_by_category=>{:terms=>{:field=>"category.name.raw", :size=>0}}, :group_by_author=>{:terms=>{:field=>"authors.fullname.raw", :size=>0}}, :year_based=>{:date_histogram=>{:field=>"publish_date", :interval=>"1y", :format=>"yyyy", :min_doc_count=>1, :time_zone=>"+00:00"}}, :month_year_based=>{:date_histogram=>{:field=>"publish_date", :interval=>"month", :format=>"MM-yyy", :min_doc_count=>1, :time_zone=>"+00:00"}}}} 如果我将此哈希作为参数传递给Model.search(query_hash)。我收到以下错误。

ArgumentError: unknown keywords: query, sort

1 个答案:

答案 0 :(得分:1)

尝试 Model.search body: {:query=>{:bool=>{:must=>[{:multi_match=>{:query=>"apple", :fields=>["title^2", "content", "description"]}}], :should=>[], :filter=>[{:term=>{:data_proxy_id=>"4d629f00-c8b1-aacc-97b5-e21e4c0bbe3f"}}, {:term=>{:article_type=>"Article"}}, {:term=>{:status=>"published"}}]}}, :sort=>{"_score"=>{"order"=>"desc"}}, :aggs=>{:group_by_section=>{:terms=>{:field=>"section.name.raw", :size=>0}}, :group_by_category=>{:terms=>{:field=>"category.name.raw", :size=>0}}, :group_by_author=>{:terms=>{:field=>"authors.fullname.raw", :size=>0}}, :year_based=>{:date_histogram=>{:field=>"publish_date", :interval=>"1y", :format=>"yyyy", :min_doc_count=>1, :time_zone=>"+00:00"}}, :month_year_based=>{:date_histogram=>{:field=>"publish_date", :interval=>"month", :format=>"MM-yyy", :min_doc_count=>1, :time_zone=>"+00:00"}}}}