我编写了以下类来获取与给定查询相对应的solr的结果
class Recommendation
class GetResults
attr_accessor :query, :filter, :sort, :facet, :highlight, :filter_fields, :sort_order, :highlight_fields, :facet_query, :facet_fields, :sort_field
def initialize(query, filter=false, sort=false, facet=false, highlight=false, filter_fields=nil, sort_order=nil, sort_field=nil, highlight_fields=nil, facet_query=nil, facet_fields=nil)
@query = query
@filter = filter||false
@sort = sort||false
@facet = facet||false
@highlight = highlight||false
@filter_fields = filter_fields||nil
@sort_order = sort_order||nil
@sort_field = sort_field||nil
@highlight_fields = highlight_fields||nil
@facet_query = facet_query||nil
@facet_fields = facet_fields||nil
end
def obtain_results
h=Net::HTTP.new('localhost',8983)
@query_path = '/solr/voylla/select/?indent=on&q=%s&wt=ruby' % query
if @filter==true
#puts "hello filter"
@query_path = @query_path.to_s + "&fl=%s" % @filter_fields
puts @query_path
end
if @sort==true
#puts "hello sort"
@query_path = @query_path.to_s + "&sort=%s "+"%s" % @sort_field,@sort_order
puts @query_path
end
#hsrep, data = h.get(@query_path)
#rsp = eval(data)
#puts rsp
end
end
end
results=Recommendation::GetResults.new(query='ring', filter=true, sort=true, filter_fields='allText,Price', sort_order='asc', sort_field='Price')
results.obtain_results
问题是@query_path
未按要求更新。
输出是:
hello filter
/solr/voylla/select/?indent=on&q=ring&wt=ruby&fl=Price
hello sort
/solr/voylla/select/?indent=on&q=ring&wt=ruby&fl=Price&sort=%s
所需的输出是:
hello filter
/solr/voylla/select/?indent=on&q=ring&wt=ruby&fl=allText,Price
hello sort
/solr/voylla/select/?indent=on&q=ring&wt=ruby&fl=allText,Price&sort=Price asc
答案 0 :(得分:4)
您在方法定义中使用positional arguments,即您必须按照定义它们的顺序传递参数:
调用时,必须按照确切的顺序提供参数。换句话说,参数是位置的。
# def initialize(query, filter=false, sort=false, facet=false, highlight=false, filter_fields=nil, sort_order=nil, sort_field=nil, highlight_fields=nil, facet_query=nil, facet_fields=nil)
# ^ ^ ^ ^ ^ ^
# | | | | | |
GetResults.new( 'ring', true, true, 'allText,Price', 'asc', 'Price')
方法调用中的变量赋值(例如filter=true
)不引用参数。他们只是设置了一些局部变量:
def test(foo=1, bar=2)
[foo, bar]
end
bar = 3
test(bar=4, baz=5) # this overwrites "bar" and defines a new variable "baz"
#=> [4, 5] # note the order, "bar" doesn't refer to the method's "bar" argument
bar
#=> 4
baz
#=> 5
您可以使用keyword arguments(Ruby 2.0):
当使用关键字参数调用方法时,参数可以按任何顺序出现。
def initialize(query, filter: false, sort: false, facet: false, highlight: false, filter_fields: nil, sort_order: nil, sort_field: nil, highlight_fields: nil, facet_query: nil, facet_fields: nil)
# ...
end
或选项哈希:
def initialize(query, options={})
@query = query
@filter = options.fetch(:filter, false)
@sort = options.fetch(:sort, false)
@facet = options.fetch(:facet, false)
@highlight = options.fetch(:highlight, false)
@filter_fields = options.fetch(:filter_fields, nil)
@sort_order = options.fetch(:sort_order, nil)
@sort_field = options.fetch(:sort_field, nil)
@highlight_fields = options.fetch(:highlight_fields, nil)
@facet_query = options.fetch(:facet_query, nil)
@facet_fields = options.fetch(:facet_fields, nil)
end
答案 1 :(得分:2)
不完全是一个答案,但是为什么你会以这种乏味的方式构建这个字符串,当你能做到:
# build a hash with your query params
query_params = {
foo: 'bar',
baz: 'baz',
buzz: nil
}
query = query_params
.reject{|k,v| v.nil? } # reject params with no value
.map{|(k,v)| "#{k}=#{v}" } # interpolate to "key=value"
.join('&') # => "foo=bar&baz=baz"
@query_path = "#{@query_path}?#{query}"
答案 2 :(得分:1)
问题在于初始化GetResults对象的方式。
此代码(注释facet=false, highlight=false
):
results=Recommendation::GetResults.new(query='ring', filter=true, sort=true,
facet=false, highlight=false, filter_fields='allText,Price',
sort_order='asc', sort_field='Price')
results.obtain_results
将为您提供所需的输出:
hello filter
#=> /solr/voylla/select/?indent=on&q=ring&wt=ruby&fl=allText,Price
hello sort
#=> /solr/voylla/select/?indent=on&q=ring&wt=ruby&fl=allText,Price&sort=Price asc