Skip to content

Messing with context.output_hash directly! #4

@jrochkind

Description

@jrochkind

You can, but probably should not, mess around with this directly, doing stuff like changing the values or even adding new keys (and hence new field names). If doing stuff with side-effects is unavoidable, it's there for your use, but it exists outside any sanity checking, so you're on your own.

I actually had intended that this was an expected use case -- it was in fact my original motivation for yielding the Context object at all!

Expected use cases that require messing with the context.output_hash directly include:

Outputting to more than one field at once

Check out this macro:

def multi_title_boosts(options = {})
    t1 = options[:title_most_boosted] or raise ArgumentError.new("need title_most_boosted")
    t2 = options[:title_less_boosted] or raise...

    lambda do |record, acc, context|
        big_title, little_title = i_dunno_get_em_both(record)

        # put em BOTH in in title_most_boosted
        context.output_hash[t1].concat = [big_title, little_title]
        # but just one in less boosted
        context.output_hash[t2] << little_title
     end
end

# in config file:

each_record multi_title_boosts(:title_most_boosted => "title1_s", :title_less_boosted => "title2_s")

Taking the already computed output of one field, and basing another field on it.

Like Solr copy field, but for things you can't do with a Solr analyzer. I have some of my own experiments related to 'browse' search that use this.

# macro
def browsable_field_normalize(opts)
   source_field = opts[:source_field] or raise ArgumentError.new

   lambda do |record, acc, context|
       source = context.output_hash[source_field]
       acc.concat normalize_in_ways_we_cant_do_in_solr(source)
   end
end

# config file:
to_field("author_facet") do #......
#...
to_field("destination_field") browsable_field_normalize(:source_field => "author_facet")

So

This was actualy an intentional use case I mean to support and encourage, for these sorts of purposes.

But are there dangers I'm missing? What do you see as the dangers?

I actually have done much of this stuff yet, it's true. It's likely there are some enhancements to be made to make it more convenient, that will become apparent only once someone starts doing it, I'm fine with fleshing it out then. For instance, maybe context.output_hash ought to be a hash that 'auto-vivifies' to an array, so you can just do context.output_hash[:something] << 'something' without worrying if it already existed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions