module UtopianSolrizer

Public Class Methods

delete(solr_options, id) click to toggle source

delete solr document

# File lib/utopian_solrizer.rb, line 136
def delete(solr_options, id)
  if exist(solr_options, id)
    rsolr = RSolr.connect solr_options
    rsolr.delete_by_id(id)
    rsolr.commit
  end
end
exist(solr_options, id) click to toggle source

check if a solr document exists

# File lib/utopian_solrizer.rb, line 126
def exist(solr_options, id)
  params = { :q => 'id:'+id.to_s }
  r = query(solr_options, params)
  if r["response"]["numFound"].to_i > 0
    return true
  end
  false
end
post_to_json(post, solr_fields=nil) click to toggle source

convert post fields to json format

# File lib/utopian_solrizer.rb, line 59
def post_to_json(post, solr_fields=nil)
  if solr_fields.nil? or solr_fields.length==0
    solr_fields = @@default_fields
  end
  solr_json = {}

  solr_fields.each do |f|
    solr_json[@@utopian_solr_field_mapping[f]] = post_value(post, f)
  end
  solr_json
end
post_value(post, field_name) click to toggle source

return post value for a particular field_name

# File lib/utopian_solrizer.rb, line 72
def post_value(post, field_name)
  post.id                                      if field_name=='id'
  post.author                                  if field_name=='author'
  post.moderator                               if field_name=='moderator'
  post.permlink                                if field_name=='permlink'
  post.json_metadata['type']                   if field_name=='category'
  post.json_metadata['tags']                   if field_name=='tags'
  post.title                                   if field_name=='title'
  post.body                                    if field_name=='body'
  post.created                                 if field_name=='created'
  post.json_metadata['repository']['html_url'] if field_name=='repository'
  post.flagged                                 if field_name=='flagged'
  post.reviewed                                if field_name=='reviewed'
  post.pending                                 if field_name=='pending'
  post.last_update                             if field_name=='last_update'
  post.active                                  if field_name=='active'
end
query(solr_options, params) click to toggle source

search particular posts

# File lib/utopian_solrizer.rb, line 120
def query(solr_options, params)
  rsolr = RSolr.connect solr_options
  response = rsolr.select :params => params
end
solrize_post(post, solr_options, solr_fields=nil) click to toggle source

Add/update a post in solr

# File lib/utopian_solrizer.rb, line 41
def solrize_post(post, solr_options, solr_fields=nil)
  rsolr = RSolr.connect solr_options
  rsolr.add post_to_json(post, solr_fields=nil)
  rsolr.commit
end
solrize_posts(posts, solr_options, solr_fields=nil) click to toggle source

Add/update posts in solr

# File lib/utopian_solrizer.rb, line 48
def solrize_posts(posts, solr_options, solr_fields=nil)
  posts_json = []
  posts.each do |post|
    posts_json.append post_to_json(post, solr_fields=nil)
  end
  rsolr = RSolr.connect solr_options
  rsolr.add posts_json
  rsolr.commit
end
solrize_posts_by_criterias(criterias, solr_options, solr_fields) click to toggle source

Add posts by criterias

# File lib/utopian_solrizer.rb, line 91
def solrize_posts_by_criterias(criterias, solr_options, solr_fields)
  posts = UtopianRuby::UtopianRubyAPI.get_posts_obj(criterias)
  solrize_posts(posts, solr_options, solr_fields=nil)
  posts.length
end
solrize_posts_within_minutes(criterias, solr_options, solr_fields, minutes) click to toggle source

Add posts within minutes to Solr

# File lib/utopian_solrizer.rb, line 98
def solrize_posts_within_minutes(criterias, solr_options, solr_fields, minutes)
  total_updated = 0
  limit = 100
  skip  = 0
  reached_end = false
  posts = Set.new
  unless reached_end==true
    criterias = {"limit":limit,"skip":skip}
    UtopianRuby::UtopianRubyAPI.get_posts_obj(criterias).each do |post|
      if (Time.parse(Time.now.to_s)-Time.parse(post.created)) <= minutes*60
        posts << post
      else
        reached_end=true
        break
      end
    end
  end
  solrize_posts(posts, solr_options, solr_fields=nil)
  posts.length
end