Sha256: 0173247ebedb3b001a0f0ca05b835de766375b76d470bdfff8001747cde84306

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

module RailsConnector

  # This class provides a basic implementation for accessing the search using the cms api.
  # It can be activated by making it the superclass of SearchRequest.
  # It should be customized by subclassing.
  # @api public
  class CmsApiSearchRequest

    # Takes +query_string+ and +options+ for accessing Cms Api Search.
    #
    # +options+ is a hash and may include:
    #
    # <tt>:limit</tt>:: The maximum number of hits
    # <tt>:offset</tt>:: The search offset
    # @api public
    def initialize(query_string, options = {})
      @query_string = query_string
      @options = options
    end

    # Accesses Cms Api Search using #query and fetches search hits.
    # @api public
    def fetch_hits
      hits = CmsRestApi.get(resource_path, {
        :query => query,
        :offset => @options[:offset],
        :size => @options[:limit],
      })

      result = SES::SearchResult.new(hits['total'])
      hits['results'].each do |hit|
        hard_coded_score = 1
        result << SES::Hit.new(hit['id'], hard_coded_score, hit)
      end
      result
    end

    private

    def resource_path
      "revisions/#{Workspace.current.revision_id}/objs/search"
    end

    def query
      now = Time.now

      q = [
        {
          "field" => "_obj_class",
          "operator" => "equal",
          "value" => "Image",
          "negate" => true
        },
        {
          "field" => "_valid_from",
          "operator" => "less_than",
          "value" => now.to_iso
        },
        {
          "field" => "_valid_until",
          "operator" => "less_than",
          "value" => now.to_iso,
          "negate" => true
        }
      ]

      @query_string.strip.split(/[\s]+/).each do |word|
        q << {
          "field" => "*",
          "operator" => "prefix_search",
          "value" => word,
        }
      end

      q
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
infopark_cloud_connector-6.8.0.406.131718077 lib/rails_connector/cms_api_search_request.rb