Module: UtopianSolrizer

Defined in:
lib/utopian_solrizer.rb

Constant Summary collapse

@@utopian_solr_field_mapping =

define utopian solr fileds by following solr dynamic fields conventions

{
  'id'         => 'id',
  'author'     => 'author_ssi',
  'moderator'  => 'moderator_ssim',
  'permlink'   => 'permlink_ssi',
  'category'   => 'category_ssi',
  'tags'       => 'tags_ssim',
  'title'      => 'title_tsim',
  'body'       => 'body_tsim',
  'created'    => 'created_dts',
  'repository' => 'repository_ssi',
  'flagged'    => 'flagged_ssi',
  'reviewed'   => 'reviewed_ssi',
  'pending'    => 'pending_ssi',
  'last_update'=> 'last_update_dts',
  'active'     => 'active_ssi'
}
@@default_fields =

set default utopian fields to index

['author',
'moderator',
'permlink',
'category',
'tags',
'title',
'body',
'created',
'repository'
]

Class Method Summary collapse

Class Method Details

.delete(solr_options, id) ⇒ Object

delete solr document



136
137
138
139
140
141
142
# 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) ⇒ Object

check if a solr document exists



126
127
128
129
130
131
132
133
# 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) ⇒ Object

convert post fields to json format



59
60
61
62
63
64
65
66
67
68
69
# 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) ⇒ Object

return post value for a particular field_name



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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.['type']                   if field_name=='category'
  post.['tags']                   if field_name=='tags'
  post.title                                   if field_name=='title'
  post.body                                    if field_name=='body'
  post.created                                 if field_name=='created'
  post.['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) ⇒ Object

search particular posts



120
121
122
123
# 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) ⇒ Object

Add/update a post in solr



41
42
43
44
45
# 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) ⇒ Object

Add/update posts in solr



48
49
50
51
52
53
54
55
56
# 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) ⇒ Object

Add posts by criterias



91
92
93
94
95
# 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) ⇒ Object

Add posts within minutes to Solr



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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