Skip to content

Instantly share code, notes, and snippets.

@rob-mcgrail
Created July 29, 2012 22:30
Show Gist options
  • Select an option

  • Save rob-mcgrail/3202249 to your computer and use it in GitHub Desktop.

Select an option

Save rob-mcgrail/3202249 to your computer and use it in GitHub Desktop.
Search + delete for Solr
require 'rubygems'
require 'rest_client'
require 'nokogiri'
ROOT = 'http:/SOLRHOST:8983'
# Colorful strings
module Colourize
colour_codes = {
:black => 30, :white => 37, :red => 31, :green => 32,
:yellow => 33, :blue => 34, :magenta => 35, :cyan => 36
}
colour_codes.each do |colour, colour_code|
send :define_method, colour do
ansi = "\e[#{colour_code}m"; reset = "\e[0m"
colourized_string = ansi + self + reset
colourized_string
end
end
end
class String
include Colourize
end
# World's tiniest, toughest Solr client
class Solr
require 'open-uri'
require 'ostruct'
require 'rubygems'
require 'nokogiri'
def initialize
@xpaths = {
:pid => '//str[@name="id"]',
:title => '//arr[@name="title_t"]/str[1]',
:url => '//arr[@name="url"]/*',
}
end
# Search, returns array of ostructs
def search
query = "#{ROOT}/solr/select?q=#{ARGV[0]}&rows=50000&qt=tki"
begin
raw_results = Nokogiri::XML(open(URI.encode(query)))
rescue SystemCallError, EOFError
retry
end
results = []
raw_results.xpath('//doc').each do |doc|
doc = Nokogiri::XML.parse(doc.to_s) # confused... inefficient...
item = OpenStruct.new
# Populate struct objects with name/matches from @xpaths
@xpaths.each do |k,v|
item.send("#{k}=", doc.xpath(v).text)
end
results << item
end
results
end
# Update solr
def update(record, uri="#{ROOT}/solr/update/?commit=true")
begin
response = RestClient.post uri, record, :content_type => 'text/xml; charset=utf-8'
puts response.blue
rescue
warn "SolrHelpers.update failed to update #{uri}".red
end
end
# Delete record from solr
def delete(id)
puts "Deleting #{id}...".red
delete_reference = "<delete><id>#{id}</id></delete>"
update(delete_reference)
end
end
# Find records
solr = Solr.new
results = solr.search
# Print results
results.each do |x|
puts "#{x.pid.red} - #{x.url.green}"
end
# Offer to remove them from Solr
puts; puts "Delete these records from #{ROOT}?"
puts "Y/N"
decision = $stdin.gets.chomp
# Delete from solr
if decision == 'Y'
results.each do |x|
solr.delete(x.pid)
end
else
# Or just exit
puts 'Exiting...'.yellow
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment