Class: ActiveFedora::SolrService

Inherits:
Object
  • Object
show all
Extended by:
Deprecation
Defined in:
lib/active_fedora/solr_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, args) ⇒ SolrService

Returns a new instance of SolrService.



10
11
12
13
14
15
# File 'lib/active_fedora/solr_service.rb', line 10

def initialize(host, args)
  host = 'http://localhost:8080/solr' unless host
  args = {read_timeout: 120, open_timeout: 120}.merge(args.dup)
  args.merge!(url: host)
  @conn = RSolr.connect args
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



8
9
10
# File 'lib/active_fedora/solr_service.rb', line 8

def conn
  @conn
end

Class Method Details

.add(doc, params = {}) ⇒ Object

Parameters:

  • doc (Hash)

    the document to index

  • params (Hash) (defaults to: {})

    :commit => commits immediately :softCommit => commit to memory, but don’t flush to disk



129
130
131
# File 'lib/active_fedora/solr_service.rb', line 129

def add(doc, params = {})
  SolrService.instance.conn.add(doc, params: params)
end

.class_from_solr_document(hit, opts = {}) ⇒ Object

Returns the best singular class for the solr object



59
60
61
62
# File 'lib/active_fedora/solr_service.rb', line 59

def class_from_solr_document(hit, opts = {})
  Deprecation.warn SolrService, "SolrService.class_from_solr_document is deprecated. Use QueryResultBuilder.class_from_solr_document instead. This will be removed in active-fedora 10.0"
  QueryResultBuilder.class_from_solr_document(hit, opts)
end

.classes_from_solr_document(hit, opts = {}) ⇒ Object

Returns all possible classes for the solr object



53
54
55
56
# File 'lib/active_fedora/solr_service.rb', line 53

def classes_from_solr_document(hit, opts = {})
  Deprecation.warn SolrService, "SolrService.classes_from_solr_document is deprecated. Use QueryResultBuilder.classes_from_solr_document instead. This will be removed in active-fedora 10.0"
  QueryResultBuilder.classes_from_solr_document(hit, opts)
end

.commitObject



133
134
135
# File 'lib/active_fedora/solr_service.rb', line 133

def commit
  SolrService.instance.conn.commit
end

.construct_query_for_ids(id_array) ⇒ Object

Construct a solr query for a list of ids This is used to get a solr response based on the list of ids in an object’s RELS-EXT relationhsips If the id_array is empty, defaults to a query of “id:NEVER_USE_THIS_ID”, which will return an empty solr response

Parameters:

  • id_array (Array)

    the ids that you want included in the query



68
69
70
71
# File 'lib/active_fedora/solr_service.rb', line 68

def construct_query_for_ids(id_array)
  Deprecation.warn SolrService, "SolrService.construct_query_for_ids is deprecated. Use SolrQueryBuilder.construct_query_for_ids instead. This will be removed in active-fedora 10.0"
  SolrQueryBuilder.construct_query_for_ids(id_array)
end

.construct_query_for_pids(id_array) ⇒ Object



73
74
75
76
# File 'lib/active_fedora/solr_service.rb', line 73

def construct_query_for_pids(id_array)
  Deprecation.warn SolrService, "construct_query_for_pids is deprecated and will be removed in active-fedora 10.0"
  SolrQueryBuilder.construct_query_for_ids(id_array)
end

.construct_query_for_rel(field_pairs, join_with = 'AND') ⇒ Object

Create a query with a clause for each key, value

Examples:

construct_query_for_rel [[:has_model, "info:fedora/afmodel:ComplexCollection"], [:has_model, "info:fedora/afmodel:ActiveFedora_Base"]], 'OR'
# => _query_:"{!raw f=has_model_ssim}info:fedora/afmodel:ComplexCollection" OR _query_:"{!raw f=has_model_ssim}info:fedora/afmodel:ActiveFedora_Base"

construct_query_for_rel [[Book.reflect_on_association(:library), "foo/bar/baz"]]

Parameters:

  • field_pairs (Hash, Array<Array<String>>)

    key is the predicate, value is the target_uri

  • join_with (String) (defaults to: 'AND')

    (‘AND’) the value we’re joining the clauses with



99
100
101
102
# File 'lib/active_fedora/solr_service.rb', line 99

def construct_query_for_rel(field_pairs, join_with = 'AND')
  Deprecation.warn SolrService, "SolrService.construct_query_for_rel is deprecated. Use SolrQueryBuilder.construct_query_for_rel instead. This will be removed in active-fedora 10.0"
  SolrQueryBuilder.construct_query_for_rel(field_pairs, join_with)
end

.count(query, args = {}) ⇒ Integer

Get the count of records that match the query

Parameters:

  • query (String)

    a solr query

  • args (Hash) (defaults to: {})

    arguments to pass through to ‘args’ param of SolrService.query (note that :rows will be overwritten to 0)

Returns:

  • (Integer)

    number of records matching



120
121
122
123
# File 'lib/active_fedora/solr_service.rb', line 120

def count(query, args={})
  args = args.merge(:raw=>true, :rows=>0)
  SolrService.query(query, args)['response']['numFound'].to_i
end

.delete(id) ⇒ Object



112
113
114
# File 'lib/active_fedora/solr_service.rb', line 112

def delete(id)
  SolrService.instance.conn.delete_by_id(id, params: {'softCommit' => true})
end

.instanceObject

Raises:



26
27
28
29
30
31
32
33
34
35
# File 'lib/active_fedora/solr_service.rb', line 26

def instance
# Register Solr

  unless Thread.current[:solr_service]
    register(ActiveFedora.solr_config[:url])
  end

  raise SolrNotInitialized unless Thread.current[:solr_service]
  Thread.current[:solr_service]
end

.lazy_reify_solr_results(solr_results, opts = {}) ⇒ Object



37
38
39
40
# File 'lib/active_fedora/solr_service.rb', line 37

def lazy_reify_solr_results(solr_results, opts = {})
  Deprecation.warn SolrService, "SolrService.lazy_reify_solr_results is deprecated. Use QueryResultBuilder.lazy_reify_solr_results instead. This will be removed in active-fedora 10.0"
  QueryResultBuilder.lazy_reify_solr_results(solr_results, opts)
end

.query(query, args = {}) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/active_fedora/solr_service.rb', line 104

def query(query, args={})
  raw = args.delete(:raw)
  args = args.merge(:q=>query, :qt=>'standard')
  result = SolrService.instance.conn.get('select', :params=>args)
  return result if raw
  result['response']['docs']
end

.raw_query(key, value) ⇒ Object

Create a raw query clause suitable for sending to solr as an fq element

Parameters:

  • key (String)
  • value (String)


81
82
83
84
# File 'lib/active_fedora/solr_service.rb', line 81

def raw_query(key, value)
  Deprecation.warn SolrService, "SolrService.raw_query is deprecated. Use SolrQueryBuilder.raw_query instead. This will be removed in active-fedora 10.0"
  SolrQueryBuilder.raw_query(key, value)
end

.register(host = nil, args = {}) ⇒ Object



18
19
20
# File 'lib/active_fedora/solr_service.rb', line 18

def register(host=nil, args={})
  Thread.current[:solr_service] = new(host, args)
end

.reify_solr_result(hit, opts = {}) ⇒ Object



47
48
49
50
# File 'lib/active_fedora/solr_service.rb', line 47

def reify_solr_result(hit, opts = {})
  Deprecation.warn SolrService, "SolrService.reify_solr_result is deprecated. Use QueryResultBuilder.reify_solr_result instead. This will be removed in active-fedora 10.0"
  QueryResultBuilder.reify_solr_result(hit, opts)
end

.reify_solr_results(solr_results, opts = {}) ⇒ Object



42
43
44
45
# File 'lib/active_fedora/solr_service.rb', line 42

def reify_solr_results(solr_results, opts = {})
  Deprecation.warn SolrService, "SolrService.reify_solr_results is deprecated. Use QueryResultBuilder.reify_solr_results instead. This will be removed in active-fedora 10.0"
  QueryResultBuilder.reify_solr_results(solr_results, opts)
end

.reset!Object



22
23
24
# File 'lib/active_fedora/solr_service.rb', line 22

def reset!
  Thread.current[:solr_service] = nil
end

.solr_name(*args) ⇒ Object



86
87
88
89
# File 'lib/active_fedora/solr_service.rb', line 86

def solr_name(*args)
  Deprecation.warn SolrService, "SolrService.solr_name is deprecated. Use SolrQueryBuilder.solr_name instead. This will be removed in active-fedora 10.0"
  SolrQueryBuilder.solr_name(*args)
end