Sha256: 85112649413ffe0699bb71e860b41e0d06f44656145af8fff93542179dc9bd48

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

module Neo4j
  module Index

    class WrappedQuery
      include Enumerable

      def initialize(index, query)
        @index = index
        @query = query
      end

      def each
        hits.each { |n| yield n.wrapper }
      end

      def close
        @hits.close if @hits
      end

      def empty?
        hits.size == 0
      end

      def [](index)
        each_with_index {|node,i| break node if index == i}
      end

      def size
        hits.size
      end

      def hits
        @hits ||= perform_query
      end

      def desc(*fields)
        @order = fields.inject(@order || {}) { |memo, field| memo[field] = true; memo }
        self
      end

      def asc(*fields)
        @order = fields.inject(@order || {}) { |memo, field| memo[field] = false; memo }
        self
      end

      def perform_query
        if @order
          java_sort_fields = @order.keys.inject([]) do |memo, field|
            memo << org.apache.lucene.search.SortField.new(field.to_s, org.apache.lucene.search.SortField::STRING, @order[field])
          end
          sort = org.apache.lucene.search.Sort.new(*java_sort_fields)
          @query = org.neo4j.index.impl.lucene.QueryContext.new(@query).sort(sort)
        end
        @index.query(@query)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
neo4j-1.0.0.beta.17 lib/neo4j/index/wrapped_query.rb
neo4j-1.0.0.beta.16 lib/neo4j/index/wrapped_query.rb
neo4j-1.0.0.beta.15 lib/neo4j/index/wrapped_query.rb
neo4j-1.0.0.beta.14 lib/neo4j/index/wrapped_query.rb