picky 2.0.0.pre1 → 2.0.0.pre2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/picky/analyzer.rb +1 -3
- data/lib/picky/index/base.rb +10 -0
- data/lib/picky/indexed/indexes.rb +1 -1
- data/lib/picky/indexing/indexes.rb +1 -1
- data/lib/picky/internals.rb +2 -0
- data/lib/picky/loader.rb +3 -0
- data/lib/picky/query.rb +2 -0
- data/lib/picky/results.rb +77 -81
- data/lib/picky/search.rb +1 -1
- data/lib/picky/statistics.rb +2 -5
- data/lib/tasks/try.rake +4 -1
- data/spec/lib/application_spec.rb +5 -4
- data/spec/lib/internals/frontend_adapters/rack_spec.rb +2 -2
- data/spec/lib/results_spec.rb +1 -1
- metadata +4 -2
    
        data/lib/picky/analyzer.rb
    CHANGED
    
    
    
        data/lib/picky/index/base.rb
    CHANGED
    
    | @@ -21,6 +21,12 @@ module Index | |
| 21 21 | 
             
                # * result_identifier: Use if you'd like a different identifier/name in the results than the name of the index.
         | 
| 22 22 | 
             
                # * after_indexing: As of this writing only used in the db source. Executes the given after_indexing as SQL after the indexing process.
         | 
| 23 23 | 
             
                #
         | 
| 24 | 
            +
                # Example:
         | 
| 25 | 
            +
                #   my_index = Index::Memory.new(:my_index, some_source) do
         | 
| 26 | 
            +
                #     define_category :bla
         | 
| 27 | 
            +
                #   end
         | 
| 28 | 
            +
                #
         | 
| 29 | 
            +
                #
         | 
| 24 30 | 
             
                def initialize name, source, options = {}
         | 
| 25 31 | 
             
                  check name, source
         | 
| 26 32 |  | 
| @@ -31,6 +37,10 @@ module Index | |
| 31 37 | 
             
                  # Centralized registry.
         | 
| 32 38 | 
             
                  #
         | 
| 33 39 | 
             
                  Indexes.register self
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                  #
         | 
| 42 | 
            +
                  #
         | 
| 43 | 
            +
                  instance_eval(&Proc.new) if block_given?
         | 
| 34 44 | 
             
                end
         | 
| 35 45 | 
             
                #
         | 
| 36 46 | 
             
                # Since this is an API, we fail hard quickly.
         | 
    
        data/lib/picky/loader.rb
    CHANGED
    
    | @@ -88,6 +88,8 @@ module Loader # :nodoc:all | |
| 88 88 | 
             
              # (Not for the user)
         | 
| 89 89 | 
             
              #
         | 
| 90 90 | 
             
              def self.load_framework_internals
         | 
| 91 | 
            +
                load_relative 'internals'
         | 
| 92 | 
            +
             | 
| 91 93 | 
             
                # Load compiled C code.
         | 
| 92 94 | 
             
                #
         | 
| 93 95 | 
             
                load_internals 'ext/maybe_compile'
         | 
| @@ -275,6 +277,7 @@ module Loader # :nodoc:all | |
| 275 277 | 
             
                # Search.
         | 
| 276 278 | 
             
                #
         | 
| 277 279 | 
             
                load_relative 'search'
         | 
| 280 | 
            +
                load_relative 'query'
         | 
| 278 281 | 
             
                #
         | 
| 279 282 | 
             
                # load_relative 'query/solr'
         | 
| 280 283 |  | 
    
        data/lib/picky/query.rb
    ADDED
    
    
    
        data/lib/picky/results.rb
    CHANGED
    
    | @@ -1,93 +1,89 @@ | |
| 1 | 
            -
             | 
| 1 | 
            +
            # This is the internal results object. Usually, to_marshal, or to_json
         | 
| 2 | 
            +
            # is called on it to get a string for the answer.
         | 
| 3 | 
            +
            #
         | 
| 4 | 
            +
            class Results
         | 
| 2 5 |  | 
| 3 | 
            -
              #  | 
| 4 | 
            -
              # is called on it to get a string for the answer.
         | 
| 6 | 
            +
              # Duration is set externally by the query.
         | 
| 5 7 | 
             
              #
         | 
| 6 | 
            -
               | 
| 8 | 
            +
              attr_writer :duration
         | 
| 9 | 
            +
              attr_reader :allocations, :offset, :amount
         | 
| 7 10 |  | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
                 | 
| 12 | 
            -
             | 
| 13 | 
            -
                 | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
                 | 
| 20 | 
            -
                 | 
| 21 | 
            -
             | 
| 22 | 
            -
                def self.from amount, offset, allocations
         | 
| 23 | 
            -
                  results = new amount, offset, allocations
         | 
| 24 | 
            -
                  results.prepare!
         | 
| 25 | 
            -
                  results
         | 
| 26 | 
            -
                end
         | 
| 27 | 
            -
             | 
| 28 | 
            -
                # Returns a hash with the allocations, offset, duration and total.
         | 
| 29 | 
            -
                #
         | 
| 30 | 
            -
                def serialize
         | 
| 31 | 
            -
                  { allocations: allocations.to_result,
         | 
| 32 | 
            -
                    offset:      offset,
         | 
| 33 | 
            -
                    duration:    duration,
         | 
| 34 | 
            -
                    total:       total }
         | 
| 35 | 
            -
                end
         | 
| 36 | 
            -
                # The default format is json.
         | 
| 37 | 
            -
                #
         | 
| 38 | 
            -
                def to_response options = {}
         | 
| 39 | 
            -
                  to_json options
         | 
| 40 | 
            -
                end
         | 
| 41 | 
            -
                # Convert to json format.
         | 
| 42 | 
            -
                #
         | 
| 43 | 
            -
                def to_json options = {}
         | 
| 44 | 
            -
                  serialize.to_json options
         | 
| 45 | 
            -
                end
         | 
| 11 | 
            +
              # Takes instances of Query::Allocations as param.
         | 
| 12 | 
            +
              #
         | 
| 13 | 
            +
              def initialize amount = 0, offset = 0, allocations = Internals::Query::Allocations.new
         | 
| 14 | 
            +
                @offset      = offset
         | 
| 15 | 
            +
                @amount      = amount
         | 
| 16 | 
            +
                @allocations = allocations
         | 
| 17 | 
            +
              end
         | 
| 18 | 
            +
              # Create new results and calculate the ids.
         | 
| 19 | 
            +
              #
         | 
| 20 | 
            +
              def self.from amount, offset, allocations
         | 
| 21 | 
            +
                results = new amount, offset, allocations
         | 
| 22 | 
            +
                results.prepare!
         | 
| 23 | 
            +
                results
         | 
| 24 | 
            +
              end
         | 
| 46 25 |  | 
| 47 | 
            -
             | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 50 | 
            -
                 | 
| 51 | 
            -
             | 
| 52 | 
            -
                 | 
| 53 | 
            -
                   | 
| 54 | 
            -
             | 
| 26 | 
            +
              # Returns a hash with the allocations, offset, duration and total.
         | 
| 27 | 
            +
              #
         | 
| 28 | 
            +
              def serialize
         | 
| 29 | 
            +
                { allocations: allocations.to_result,
         | 
| 30 | 
            +
                  offset:      offset,
         | 
| 31 | 
            +
                  duration:    duration,
         | 
| 32 | 
            +
                  total:       total }
         | 
| 33 | 
            +
              end
         | 
| 34 | 
            +
              # The default format is json.
         | 
| 35 | 
            +
              #
         | 
| 36 | 
            +
              def to_response options = {}
         | 
| 37 | 
            +
                to_json options
         | 
| 38 | 
            +
              end
         | 
| 39 | 
            +
              # Convert to json format.
         | 
| 40 | 
            +
              #
         | 
| 41 | 
            +
              def to_json options = {}
         | 
| 42 | 
            +
                serialize.to_json options
         | 
| 43 | 
            +
              end
         | 
| 55 44 |  | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 58 | 
            -
             | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
                 | 
| 63 | 
            -
             | 
| 64 | 
            -
                #
         | 
| 65 | 
            -
                def total
         | 
| 66 | 
            -
                  @total || @total = allocations.total || 0
         | 
| 67 | 
            -
                end
         | 
| 45 | 
            +
              # This starts the actual processing.
         | 
| 46 | 
            +
              #
         | 
| 47 | 
            +
              # Without this, the allocations are not processed,
         | 
| 48 | 
            +
              # and no ids are calculated.
         | 
| 49 | 
            +
              #
         | 
| 50 | 
            +
              def prepare!
         | 
| 51 | 
            +
                allocations.process! amount, offset
         | 
| 52 | 
            +
              end
         | 
| 68 53 |  | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 54 | 
            +
              # Duration default is 0.
         | 
| 55 | 
            +
              #
         | 
| 56 | 
            +
              def duration
         | 
| 57 | 
            +
                @duration || 0
         | 
| 58 | 
            +
              end
         | 
| 59 | 
            +
              # The total results. Delegates to the allocations.
         | 
| 60 | 
            +
              #
         | 
| 61 | 
            +
              # Caches.
         | 
| 62 | 
            +
              #
         | 
| 63 | 
            +
              def total
         | 
| 64 | 
            +
                @total || @total = allocations.total || 0
         | 
| 65 | 
            +
              end
         | 
| 71 66 |  | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 74 | 
            -
                def ids amount = 20
         | 
| 75 | 
            -
                  allocations.ids amount
         | 
| 76 | 
            -
                end
         | 
| 67 | 
            +
              # Convenience methods.
         | 
| 68 | 
            +
              #
         | 
| 77 69 |  | 
| 78 | 
            -
             | 
| 79 | 
            -
             | 
| 80 | 
            -
             | 
| 81 | 
            -
             | 
| 82 | 
            -
             | 
| 83 | 
            -
                # The first character in the blog designates what type of query it is.
         | 
| 84 | 
            -
                #
         | 
| 85 | 
            -
                # No calculated ids means: No results.
         | 
| 86 | 
            -
                #
         | 
| 87 | 
            -
                def log_type
         | 
| 88 | 
            -
                  amount.zero?? :'.' : :'>'
         | 
| 89 | 
            -
                end
         | 
| 70 | 
            +
              # Delegates to allocations.
         | 
| 71 | 
            +
              #
         | 
| 72 | 
            +
              def ids amount = 20
         | 
| 73 | 
            +
                allocations.ids amount
         | 
| 74 | 
            +
              end
         | 
| 90 75 |  | 
| 76 | 
            +
              # Human readable log.
         | 
| 77 | 
            +
              #
         | 
| 78 | 
            +
              def to_log query
         | 
| 79 | 
            +
                "#{log_type}|#{Time.now.to_s(:db)}|#{'%8f' % duration}|#{'%-50s' % query}|#{'%8d' % total}|#{'%4d' % offset}|#{'%2d' % allocations.size}|"
         | 
| 80 | 
            +
              end
         | 
| 81 | 
            +
              # The first character in the blog designates what type of query it is.
         | 
| 82 | 
            +
              #
         | 
| 83 | 
            +
              # No calculated ids means: No results.
         | 
| 84 | 
            +
              #
         | 
| 85 | 
            +
              def log_type
         | 
| 86 | 
            +
                amount.zero?? :'.' : :'>'
         | 
| 91 87 | 
             
              end
         | 
| 92 88 |  | 
| 93 89 | 
             
            end
         | 
    
        data/lib/picky/search.rb
    CHANGED
    
    | @@ -99,7 +99,7 @@ class Search | |
| 99 99 | 
             
              # Note: Internal method, use #search_with_text.
         | 
| 100 100 | 
             
              #
         | 
| 101 101 | 
             
              def execute tokens, ids, offset
         | 
| 102 | 
            -
                 | 
| 102 | 
            +
                Results.from ids, offset, sorted_allocations(tokens)
         | 
| 103 103 | 
             
              end
         | 
| 104 104 |  | 
| 105 105 | 
             
              # Delegates the tokenizing to the query tokenizer.
         | 
    
        data/lib/picky/statistics.rb
    CHANGED
    
    | @@ -1,12 +1,9 @@ | |
| 1 1 | 
             
            # encoding: utf-8
         | 
| 2 2 | 
             
            #
         | 
| 3 3 |  | 
| 4 | 
            -
            # Gathers  | 
| 5 | 
            -
            # when methods are called.
         | 
| 4 | 
            +
            # Gathers various statistics.
         | 
| 6 5 | 
             
            #
         | 
| 7 | 
            -
             | 
| 8 | 
            -
            #
         | 
| 9 | 
            -
            class Statistics
         | 
| 6 | 
            +
            class Statistics # :nodoc:all
         | 
| 10 7 |  | 
| 11 8 | 
             
              def self.instance
         | 
| 12 9 | 
             
                @statistics ||= new
         | 
    
        data/lib/tasks/try.rake
    CHANGED
    
    | @@ -6,7 +6,7 @@ namespace :try do | |
| 6 6 | 
             
              task :index, [:text, :index, :category] => :application do |_, options|
         | 
| 7 7 | 
             
                text, index, category = options.text, options.index, options.category
         | 
| 8 8 |  | 
| 9 | 
            -
                tokenizer =  | 
| 9 | 
            +
                tokenizer = category ? Indexes.find(index, category).tokenizer : Internals::Tokenizers::Index.default
         | 
| 10 10 |  | 
| 11 11 | 
             
                puts "\"#{text}\" is saved in the index as             #{tokenizer.tokenize(text.dup).to_a}"
         | 
| 12 12 | 
             
              end
         | 
| @@ -22,6 +22,9 @@ namespace :try do | |
| 22 22 | 
             
              task :both, [:text, :index, :category] => :application do |_, options|
         | 
| 23 23 | 
             
                text, index, category = options.text, options.index, options.category
         | 
| 24 24 |  | 
| 25 | 
            +
                puts
         | 
| 26 | 
            +
                fail "\x1b[31mrake try needs a text to try indexing and query preparation\x1b[m, e.g. rake 'try[yourtext]'." unless text
         | 
| 27 | 
            +
             | 
| 25 28 | 
             
                Rake::Task[:"try:index"].invoke text, index, category
         | 
| 26 29 | 
             
                Rake::Task[:"try:query"].invoke text
         | 
| 27 30 | 
             
              end
         | 
| @@ -47,10 +47,11 @@ describe Application do | |
| 47 47 | 
             
                      books_index.define_category :isbn,
         | 
| 48 48 | 
             
                                                  partial: Partial::None.new # Partially searching on an ISBN makes not much sense.
         | 
| 49 49 |  | 
| 50 | 
            -
                      geo_index = Index::Memory.new :geo, Sources::CSV.new(:location, :north, :east, file: 'data/ch.csv', col_sep: ',')
         | 
| 51 | 
            -
             | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 50 | 
            +
                      geo_index = Index::Memory.new :geo, Sources::CSV.new(:location, :north, :east, file: 'data/ch.csv', col_sep: ',') do
         | 
| 51 | 
            +
                        category        :location
         | 
| 52 | 
            +
                        ranged_category :north1, 1, precision: 3, from: :north
         | 
| 53 | 
            +
                        ranged_category :east1,  1, precision: 3, from: :east
         | 
| 54 | 
            +
                      end
         | 
| 54 55 |  | 
| 55 56 | 
             
                      rack_adapter.stub! :exclaim # Stopping it from exclaiming.
         | 
| 56 57 |  | 
| @@ -102,7 +102,7 @@ describe Internals::FrontendAdapters::Rack do | |
| 102 102 | 
             
                  env = rack_defaults_for '/searches/some_route?query=some_query'
         | 
| 103 103 |  | 
| 104 104 | 
             
                  search = stub :search
         | 
| 105 | 
            -
                  search.should_receive(:search_with_text).once.with(anything, 20, 0).and_return( | 
| 105 | 
            +
                  search.should_receive(:search_with_text).once.with(anything, 20, 0).and_return(Results.new)
         | 
| 106 106 | 
             
                  Search.stub! :new => search
         | 
| 107 107 |  | 
| 108 108 | 
             
                  @rack_adapter.route '/searches/some_route' => Search.new(:some_index, :some_other_index)
         | 
| @@ -114,7 +114,7 @@ describe Internals::FrontendAdapters::Rack do | |
| 114 114 | 
             
                  env = rack_defaults_for '/searches/some_route?query=some_query&type=some_type'
         | 
| 115 115 |  | 
| 116 116 | 
             
                  search = stub :search
         | 
| 117 | 
            -
                  search.should_receive(:search_with_text).once.with(anything, 20, 0).and_return( | 
| 117 | 
            +
                  search.should_receive(:search_with_text).once.with(anything, 20, 0).and_return(Results.new)
         | 
| 118 118 | 
             
                  Search.stub! :new => search
         | 
| 119 119 |  | 
| 120 120 | 
             
                  @rack_adapter.route '/searches/some_route' => Search.new(:some_index, :some_other_index), :query => { :type => :some_type }
         | 
    
        data/spec/lib/results_spec.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -2,7 +2,7 @@ | |
| 2 2 | 
             
            name: picky
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 4 | 
             
              prerelease: 6
         | 
| 5 | 
            -
              version: 2.0.0. | 
| 5 | 
            +
              version: 2.0.0.pre2
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors: 
         | 
| 8 8 | 
             
            - Florian Hanke
         | 
| @@ -10,7 +10,7 @@ autorequire: | |
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 12 |  | 
| 13 | 
            -
            date: 2011-03- | 
| 13 | 
            +
            date: 2011-03-21 00:00:00 +01:00
         | 
| 14 14 | 
             
            default_executable: picky
         | 
| 15 15 | 
             
            dependencies: 
         | 
| 16 16 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| @@ -127,9 +127,11 @@ files: | |
| 127 127 | 
             
            - lib/picky/internals/tokenizers/base.rb
         | 
| 128 128 | 
             
            - lib/picky/internals/tokenizers/index.rb
         | 
| 129 129 | 
             
            - lib/picky/internals/tokenizers/query.rb
         | 
| 130 | 
            +
            - lib/picky/internals.rb
         | 
| 130 131 | 
             
            - lib/picky/loader.rb
         | 
| 131 132 | 
             
            - lib/picky/loggers/search.rb
         | 
| 132 133 | 
             
            - lib/picky/query/solr.rb
         | 
| 134 | 
            +
            - lib/picky/query.rb
         | 
| 133 135 | 
             
            - lib/picky/rack/harakiri.rb
         | 
| 134 136 | 
             
            - lib/picky/results.rb
         | 
| 135 137 | 
             
            - lib/picky/search.rb
         |