Accessing Solr fields in the catalog_controller and performing separate Solr queries

133 views
Skip to first unread message

Lakeisha Robinson

unread,
Apr 11, 2013, 12:59:58 PM4/11/13
to blacklight-...@googlegroups.com
Hello Everyone! I have 2 questions that I would like to ask the community. We currently have our Blacklight instance up and running with our values indexed into Solr and displaying. What I'm trying to do is access our Solr index values from the catalog_controller. So for example, if one of our indexed values is 'language_term_t' (which is being displayed in the config.add_show_field) and I wanted to perform an operation like:
 
           if langauge_term_t == 'English'
 
Is there a way to do this from the catalog_controller? I tried to use config.search_fields.values.select { 'langauge_term_t'}) =='English' but that didn't work.
 
My second question is how to perform a customized Solr query? What I did was follow the example for the 'default_document_solr_params' hash. I created a empty hash in configuration.rb called
:solr_child_field => {}. I then constructed two methods in the solr_helper.rb file like so:
 
 def solr_doc_child_params(id=nil)
    id ||= params[:id]
    p = blacklight_config.solr_child_field.merge({
      :id => id # this assumes the document request handler will map the 'id' param to the unique key field
    })
    p[:qt] ||= 'document'
    p
  end
 
 def get_solr_response_for_child_id(id=nil, extra_controller_params={})
    solr_params = solr_doc_child_params(id).merge(extra_controller_params)
    solr_response = find((blacklight_config.document_solr_request_handler || blacklight_config.qt), solr_params)
    raise Blacklight::Exceptions::InvalidSolrID.new if solr_response.docs.empty?
    document = SolrDocument.new(solr_response.docs.first, solr_response)
    [solr_response, document]
  end
 
With 'solr_doc_child_params' method mirroring the 'solr_doc_params' method and the 'get_solr_response_for_child_id' method mirroring the 'get_solr_response_for_doc_id' method. I then called the method in the 'show' method of the catalog.rb file right below the 'get_solr_response_for_doc_id' method like so:
 
     @response, @document = get_solr_response_for_doc_id   
     @response, @document = get_solr_response_for_child_id
 
Then in the catalog_controller I populate the hash by using:
 
        config.solr_child_field = {
              :fl => 'id,display_label_s,zindex_i'
       }
 
However, nothing happens. But if I use the default_document_solr_params hash, the values appear. I know this is a very specific question, but can someone please point out what I'm doing wrong or what I'm missing? Or maybe this isn't the right path to go for creating a customized Solr query and it could be done simpler? Thanks,
 
-Lakeisha Robinson-
Yale University
 

Lakeisha Robinson

unread,
Apr 17, 2013, 10:52:09 AM4/17/13
to blacklight-...@googlegroups.com
Hello Everyone, I actually found out how to write a Solr query in Blacklight. Just in case someone is looking for help on this topic, I am posting my results:
 
1. First, I added the code below to the catalog_controller.rb file. This allowed for the usage of the params[:id] hash. I copied the solr_search_params methof from the solr_helper.rb file :

         

    include Blacklight::Catalog 

    include Blacklight::SolrHelper

  

    def solr_search_params(user_params = params || {})
      solr_parameters = {}
      solr_search_params_logic.each do |method_name|
        send(method_name, solr_parameters, user_params)
      end

      return solr_parameters
    end

 

  2. I then added a new method to the solr_helper.rb file:
 
  def get_children_from_parent_pid(pid)
        query = "is_member_of_s:\"info\:fedora\/" + pid + "\""
        @solr_response = find(blacklight_config.qt,{:fq => query,:fl => "id", :sort =>"zindex_i asc"});
        if !(@solr_response.response.empty?)
                json_response = @solr_response.response
                @numFound = json_response['numFound']
                if @numFound > 0
                        @docs = json_response['docs']
                end
        end
  end

 Where is_member_of_s: is a solr indexed field, and numFound and docs are fields within the results.
 
3. I then added the below code the catalog.rb file:
 
        @docs1 = Array.new
        pid = params[:id]
        pid = pid.to_s
        @docs = get_children_from_parent_pid(pid)
        if @docs != nil
                @docs.each do |i|
                        @h1 = Hash[*i.flatten]
                        @h1.each do |key,value|
                                @docs1.push value
                        end
                end
        end

Had to turn each element of @docs into a hash (based on structure of the results given from the get_children_from_parent_pid method).
 
4. Then I was able to access @docs1 in the view like so:
 
  <% if @docs1 != nil then @docs1.each do |value|    %>
     <h6> <%= image_tag "http://libserver7.yale.edu:8983/fedora/objects/" + value.to_s  + "/datastreams/jpg/content" , :height => '256', :width => '256' %> </h6>
  <% end end %>
5. Restared the server. Each time I modified a file in the /lib/blacklight directory the changes wouldn't take effect unless I restarted the server (found this out the hard way).
 
Please note, I am new to Blacklight, Ruby and Ruby on Rails so this may not be the best solution. But this is the way I implemented it and it works. I still do not know how (or if possible) to access a solr indexed field from catalog_controller.rb. If someone knows, please share. Thanks!
 
-Lakeisha Robinson-
Yale University
 
 
 

Justin Coyne

unread,
Apr 18, 2013, 1:11:47 PM4/18/13
to Blacklight Development
Lakeisha,

 Have you looked at hydra-head? I think that would bridge the gap that you have between Blacklight and Fedora.

You can declare your object like this:

class Image < ActiveFedora::Base
  belongs_to :book
  has_file_datastream 'jpg'
end

class Book < ActiveFedora::Base
   has_many :images
end

And then look up related objects:

book = Book.find(pid)

book.image_ids
# => ['yale:1', 'yale:2', 'yale:3']


If you create a DownloadController which includes Hydra::Controller::DownloadBehavior (https://github.com/projecthydra/hydra-head/blob/master/hydra-core/lib/hydra/controller/download_behavior.rb)  to proxy the image so you don't have to expose your Fedora repository over the web.


You can direct any questions you have to the hydra...@googlegroups.com list

Regards,
Justin








 
-Lakeisha Robinson-
Yale University
 
 
 

--
You received this message because you are subscribed to the Google Groups "Blacklight Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blacklight-develo...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Aakash

unread,
May 1, 2013, 11:16:20 AM5/1/13
to blacklight-...@googlegroups.com
Hi Lakeisha,
I am trying to do the same thing, not able to display my indexed fields. Can you please let me know where and what in solrconfig files you made changes. I have the solrconfig file from  solr installation.

Thanks,

Lakeisha Robinson

unread,
May 9, 2013, 4:20:28 PM5/9/13
to blacklight-...@googlegroups.com
Hi Aakash, we are using our catalog_controller.rb file to show the indexed fields. So for instance, if you have the code config.add_show_field 'some_thing_s', :label => 'Something:' in the catalog_controller.rb file, the 'some_thing_s' would be the solr indexed field. This will be displayed on the page once you click an object from the search results page. As far as the solr configuration, which solr config files are you referring to?
Reply all
Reply to author
Forward
0 new messages