Module: UtopianSolrizer

Defined in:
lib/utopian_solrizer.rb

Constant Summary collapse

@@fields =

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'
}

Class Method Summary collapse

Class Method Details

.delete(solr_options, id) ⇒ Object

delete solr document



91
92
93
94
95
96
97
# File 'lib/utopian_solrizer.rb', line 91

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



81
82
83
84
85
86
87
88
# File 'lib/utopian_solrizer.rb', line 81

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

.query(solr_options, params) ⇒ Object

search particular posts



75
76
77
78
# File 'lib/utopian_solrizer.rb', line 75

def query(solr_options, params)
  rsolr = RSolr.connect solr_options
  response = rsolr.select :params => params
end

.solrize_post(post, solr_options, overwrite = true) ⇒ Object

Add/update a post in solr



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/utopian_solrizer.rb', line 25

def solrize_post(post, solr_options, overwrite=true)
  rsolr = RSolr.connect solr_options
  if (overwrite==true) or (not exist(solr_options, post.id))
    rsolr.add(
      @@fields['id']         => post.id,
      @@fields['author']     => post.author,
      @@fields['moderator']  => post.moderator,
      @@fields['permlink']   => post.permlink,
      @@fields['category']   => post.['type'],
      @@fields['tags']       => post.['tags'],
      @@fields['title']      => post.title,
      @@fields['body']       => post.body,
      @@fields['created']    => post.created,
      @@fields['repository'] => post.['repository']['html_url']
    )
    rsolr.commit
  end
end

.solrize_posts_by_criterias(criterias, solr_options) ⇒ Object

Add posts by criterias



45
46
47
48
49
# File 'lib/utopian_solrizer.rb', line 45

def solrize_posts_by_criterias(criterias, solr_options)
  UtopianRuby::UtopianRubyAPI.get_posts_obj(criterias).each do |post|
    solrize_post(post, solr_options)
  end
end

.solrize_posts_within_minutes(criterias, solr_options, minutes) ⇒ Object

Add posts within minutes to Solr



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/utopian_solrizer.rb', line 52

def solrize_posts_within_minutes(criterias, solr_options, minutes)
  total_updated = 0
  limit = 100
  skip  = 0
  reached_end = false
  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
        #puts 'Solrizing post: ' + post.permlink
        total_updated = total_updated + 1
        solrize_post(post, solr_options)
      else
        reached_end=true
        break
      end
    end
    skip = skip + limit
  end
  total_updated
end