Sha256: 12f3251ffc6b3c29a56b1fc69b3bd026e693541f1d86a9c17c0c1e3640951acf

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 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.
  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
    def initialize(query_string, options = {})
      @query_string = query_string
      @options = Configuration.cms_api_options.merge(options)
    end

    # Accesses Cms Api Search using #query and fetches search hits.
    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.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.210.ed204b0 lib/rails_connector/cms_api_search_request.rb