Class: Solr::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/it_tools/solr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Query

Returns a new instance of Query.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/it_tools/solr.rb', line 75

def initialize(options = {})
  @ops = {}
  @ops.merge! options unless options.nil?
  @log = Logger.new('log.txt')
  if level = @ops[:debug_level]
    @log.level = level
  else
    @log.level = Logger::DEBUG
  end
  begin
    @ops[:solr_host] = settings.solr_host
  rescue
  end
  mesg = "Must specify ':solr_host' and ':solr_port' in constructor hash"
  raise mesg unless @ops[:solr_host] and @ops[:solr_port]
end

Instance Attribute Details

#logObject

Returns the value of attribute log.



74
75
76
# File 'lib/it_tools/solr.rb', line 74

def log
  @log
end

#opsObject

Returns the value of attribute ops.



74
75
76
# File 'lib/it_tools/solr.rb', line 74

def ops
  @ops
end

Instance Method Details

#do_query(params) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/it_tools/solr.rb', line 91

def do_query(params)
  h = Net::HTTP.new(@ops[:solr_host], @ops[:solr_port])
  query = params["query"]
  category = params["category"]
  query = "category:" + category + " AND " + params["query"] unless category.nil?
  all = { 
    "q" => URI.escape(query),
    "wt" => "ruby",
    "hl" => "true",
    "hl.fl" => "*"
  }
  query = "/solr/select?"
  all.each do |key, value|
    query += key + "=" + value + "&"
  end
  hresp, data = h.get(query)
  
  if data.nil? 
    return "<p>nothing</p>"
  else
    rsp = eval(data)
    return rsp
  end
end