0

我编写了以下类来从 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 
4

3 回答 3

4

您在方法定义中使用位置参数,即您必须按照定义它们的顺序传递参数:

调用时,参数必须以准确的顺序提供。换句话说,参数是位置的。

# 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

您可以使用关键字参数(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
于 2013-09-20T14:43:54.010 回答
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}"
于 2013-09-20T14:43:05.843 回答
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 
于 2013-09-20T14:31:03.960 回答