sunspot_solr 2.0.0.pre.120720 → 2.0.0.pre.120924
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +11 -0
- data/.travis.yml +35 -0
- data/README.md +863 -0
- data/Rakefile +37 -0
- data/ci/travis.sh +67 -0
- data/sunspot/.gitignore +13 -0
- data/sunspot/Gemfile +5 -0
- data/sunspot/History.txt +258 -0
- data/sunspot/LICENSE +18 -0
- data/sunspot/Rakefile +13 -0
- data/sunspot/TODO +13 -0
- data/sunspot/lib/light_config.rb +40 -0
- data/sunspot/lib/sunspot.rb +579 -0
- data/sunspot/lib/sunspot/adapters.rb +349 -0
- data/sunspot/lib/sunspot/batcher.rb +62 -0
- data/sunspot/lib/sunspot/class_set.rb +23 -0
- data/sunspot/lib/sunspot/composite_setup.rb +202 -0
- data/sunspot/lib/sunspot/configuration.rb +53 -0
- data/sunspot/lib/sunspot/data_extractor.rb +50 -0
- data/sunspot/lib/sunspot/dsl.rb +5 -0
- data/sunspot/lib/sunspot/dsl/adjustable.rb +47 -0
- data/sunspot/lib/sunspot/dsl/field_group.rb +57 -0
- data/sunspot/lib/sunspot/dsl/field_query.rb +345 -0
- data/sunspot/lib/sunspot/dsl/fields.rb +103 -0
- data/sunspot/lib/sunspot/dsl/fulltext.rb +243 -0
- data/sunspot/lib/sunspot/dsl/function.rb +27 -0
- data/sunspot/lib/sunspot/dsl/functional.rb +44 -0
- data/sunspot/lib/sunspot/dsl/more_like_this_query.rb +56 -0
- data/sunspot/lib/sunspot/dsl/paginatable.rb +32 -0
- data/sunspot/lib/sunspot/dsl/query_facet.rb +36 -0
- data/sunspot/lib/sunspot/dsl/restriction.rb +25 -0
- data/sunspot/lib/sunspot/dsl/restriction_with_near.rb +160 -0
- data/sunspot/lib/sunspot/dsl/scope.rb +214 -0
- data/sunspot/lib/sunspot/dsl/search.rb +30 -0
- data/sunspot/lib/sunspot/dsl/standard_query.rb +122 -0
- data/sunspot/lib/sunspot/field.rb +193 -0
- data/sunspot/lib/sunspot/field_factory.rb +129 -0
- data/sunspot/lib/sunspot/indexer.rb +136 -0
- data/sunspot/lib/sunspot/query.rb +11 -0
- data/sunspot/lib/sunspot/query/abstract_field_facet.rb +55 -0
- data/sunspot/lib/sunspot/query/bbox.rb +15 -0
- data/sunspot/lib/sunspot/query/boost_query.rb +24 -0
- data/sunspot/lib/sunspot/query/common_query.rb +96 -0
- data/sunspot/lib/sunspot/query/composite_fulltext.rb +36 -0
- data/sunspot/lib/sunspot/query/connective.rb +206 -0
- data/sunspot/lib/sunspot/query/date_field_facet.rb +14 -0
- data/sunspot/lib/sunspot/query/dismax.rb +132 -0
- data/sunspot/lib/sunspot/query/field_facet.rb +41 -0
- data/sunspot/lib/sunspot/query/field_group.rb +37 -0
- data/sunspot/lib/sunspot/query/filter.rb +38 -0
- data/sunspot/lib/sunspot/query/function_query.rb +52 -0
- data/sunspot/lib/sunspot/query/geo.rb +53 -0
- data/sunspot/lib/sunspot/query/geofilt.rb +16 -0
- data/sunspot/lib/sunspot/query/highlighting.rb +62 -0
- data/sunspot/lib/sunspot/query/more_like_this.rb +61 -0
- data/sunspot/lib/sunspot/query/more_like_this_query.rb +12 -0
- data/sunspot/lib/sunspot/query/pagination.rb +42 -0
- data/sunspot/lib/sunspot/query/query_facet.rb +53 -0
- data/sunspot/lib/sunspot/query/range_facet.rb +15 -0
- data/sunspot/lib/sunspot/query/restriction.rb +308 -0
- data/sunspot/lib/sunspot/query/scope.rb +9 -0
- data/sunspot/lib/sunspot/query/sort.rb +109 -0
- data/sunspot/lib/sunspot/query/sort_composite.rb +34 -0
- data/sunspot/lib/sunspot/query/standard_query.rb +16 -0
- data/sunspot/lib/sunspot/query/text_field_boost.rb +17 -0
- data/sunspot/lib/sunspot/schema.rb +151 -0
- data/sunspot/lib/sunspot/search.rb +9 -0
- data/sunspot/lib/sunspot/search/abstract_search.rb +286 -0
- data/sunspot/lib/sunspot/search/date_facet.rb +35 -0
- data/sunspot/lib/sunspot/search/facet_row.rb +27 -0
- data/sunspot/lib/sunspot/search/field_facet.rb +88 -0
- data/sunspot/lib/sunspot/search/field_group.rb +70 -0
- data/sunspot/lib/sunspot/search/group.rb +54 -0
- data/sunspot/lib/sunspot/search/highlight.rb +38 -0
- data/sunspot/lib/sunspot/search/hit.rb +150 -0
- data/sunspot/lib/sunspot/search/hit_enumerable.rb +68 -0
- data/sunspot/lib/sunspot/search/more_like_this_search.rb +31 -0
- data/sunspot/lib/sunspot/search/paginated_collection.rb +57 -0
- data/sunspot/lib/sunspot/search/query_facet.rb +67 -0
- data/sunspot/lib/sunspot/search/range_facet.rb +37 -0
- data/sunspot/lib/sunspot/search/standard_search.rb +21 -0
- data/sunspot/lib/sunspot/session.rb +262 -0
- data/sunspot/lib/sunspot/session_proxy.rb +95 -0
- data/sunspot/lib/sunspot/session_proxy/abstract_session_proxy.rb +29 -0
- data/sunspot/lib/sunspot/session_proxy/class_sharding_session_proxy.rb +66 -0
- data/sunspot/lib/sunspot/session_proxy/id_sharding_session_proxy.rb +89 -0
- data/sunspot/lib/sunspot/session_proxy/master_slave_session_proxy.rb +43 -0
- data/sunspot/lib/sunspot/session_proxy/retry_5xx_session_proxy.rb +67 -0
- data/sunspot/lib/sunspot/session_proxy/sharding_session_proxy.rb +222 -0
- data/sunspot/lib/sunspot/session_proxy/silent_fail_session_proxy.rb +42 -0
- data/sunspot/lib/sunspot/session_proxy/thread_local_session_proxy.rb +37 -0
- data/sunspot/lib/sunspot/setup.rb +350 -0
- data/sunspot/lib/sunspot/text_field_setup.rb +29 -0
- data/sunspot/lib/sunspot/type.rb +393 -0
- data/sunspot/lib/sunspot/util.rb +252 -0
- data/sunspot/lib/sunspot/version.rb +3 -0
- data/sunspot/script/console +10 -0
- data/sunspot/spec/api/adapters_spec.rb +68 -0
- data/sunspot/spec/api/batcher_spec.rb +112 -0
- data/sunspot/spec/api/binding_spec.rb +50 -0
- data/sunspot/spec/api/class_set_spec.rb +24 -0
- data/sunspot/spec/api/hit_enumerable_spec.rb +47 -0
- data/sunspot/spec/api/indexer/attributes_spec.rb +149 -0
- data/sunspot/spec/api/indexer/batch_spec.rb +72 -0
- data/sunspot/spec/api/indexer/dynamic_fields_spec.rb +42 -0
- data/sunspot/spec/api/indexer/fixed_fields_spec.rb +57 -0
- data/sunspot/spec/api/indexer/fulltext_spec.rb +43 -0
- data/sunspot/spec/api/indexer/removal_spec.rb +53 -0
- data/sunspot/spec/api/indexer/spec_helper.rb +1 -0
- data/sunspot/spec/api/indexer_spec.rb +14 -0
- data/sunspot/spec/api/query/advanced_manipulation_examples.rb +35 -0
- data/sunspot/spec/api/query/connectives_examples.rb +201 -0
- data/sunspot/spec/api/query/dsl_spec.rb +18 -0
- data/sunspot/spec/api/query/dynamic_fields_examples.rb +165 -0
- data/sunspot/spec/api/query/faceting_examples.rb +497 -0
- data/sunspot/spec/api/query/fulltext_examples.rb +313 -0
- data/sunspot/spec/api/query/function_spec.rb +79 -0
- data/sunspot/spec/api/query/geo_examples.rb +68 -0
- data/sunspot/spec/api/query/group_spec.rb +32 -0
- data/sunspot/spec/api/query/highlighting_examples.rb +245 -0
- data/sunspot/spec/api/query/more_like_this_spec.rb +140 -0
- data/sunspot/spec/api/query/ordering_pagination_examples.rb +116 -0
- data/sunspot/spec/api/query/scope_examples.rb +275 -0
- data/sunspot/spec/api/query/spatial_examples.rb +27 -0
- data/sunspot/spec/api/query/spec_helper.rb +1 -0
- data/sunspot/spec/api/query/standard_spec.rb +29 -0
- data/sunspot/spec/api/query/text_field_scoping_examples.rb +30 -0
- data/sunspot/spec/api/query/types_spec.rb +20 -0
- data/sunspot/spec/api/search/dynamic_fields_spec.rb +33 -0
- data/sunspot/spec/api/search/faceting_spec.rb +360 -0
- data/sunspot/spec/api/search/highlighting_spec.rb +69 -0
- data/sunspot/spec/api/search/hits_spec.rb +147 -0
- data/sunspot/spec/api/search/paginated_collection_spec.rb +36 -0
- data/sunspot/spec/api/search/results_spec.rb +72 -0
- data/sunspot/spec/api/search/search_spec.rb +23 -0
- data/sunspot/spec/api/search/spec_helper.rb +1 -0
- data/sunspot/spec/api/session_proxy/class_sharding_session_proxy_spec.rb +85 -0
- data/sunspot/spec/api/session_proxy/id_sharding_session_proxy_spec.rb +30 -0
- data/sunspot/spec/api/session_proxy/master_slave_session_proxy_spec.rb +41 -0
- data/sunspot/spec/api/session_proxy/retry_5xx_session_proxy_spec.rb +78 -0
- data/sunspot/spec/api/session_proxy/sharding_session_proxy_spec.rb +77 -0
- data/sunspot/spec/api/session_proxy/silent_fail_session_proxy_spec.rb +24 -0
- data/sunspot/spec/api/session_proxy/spec_helper.rb +9 -0
- data/sunspot/spec/api/session_proxy/thread_local_session_proxy_spec.rb +39 -0
- data/sunspot/spec/api/session_spec.rb +232 -0
- data/sunspot/spec/api/spec_helper.rb +3 -0
- data/sunspot/spec/api/sunspot_spec.rb +29 -0
- data/sunspot/spec/ext.rb +11 -0
- data/sunspot/spec/helpers/indexer_helper.rb +17 -0
- data/sunspot/spec/helpers/integration_helper.rb +8 -0
- data/sunspot/spec/helpers/mock_session_helper.rb +13 -0
- data/sunspot/spec/helpers/query_helper.rb +26 -0
- data/sunspot/spec/helpers/search_helper.rb +68 -0
- data/sunspot/spec/integration/dynamic_fields_spec.rb +57 -0
- data/sunspot/spec/integration/faceting_spec.rb +330 -0
- data/sunspot/spec/integration/field_grouping_spec.rb +100 -0
- data/sunspot/spec/integration/geospatial_spec.rb +96 -0
- data/sunspot/spec/integration/highlighting_spec.rb +44 -0
- data/sunspot/spec/integration/indexing_spec.rb +55 -0
- data/sunspot/spec/integration/keyword_search_spec.rb +317 -0
- data/sunspot/spec/integration/local_search_spec.rb +64 -0
- data/sunspot/spec/integration/more_like_this_spec.rb +43 -0
- data/sunspot/spec/integration/scoped_search_spec.rb +386 -0
- data/sunspot/spec/integration/stored_fields_spec.rb +12 -0
- data/sunspot/spec/integration/test_pagination.rb +43 -0
- data/sunspot/spec/integration/unicode_spec.rb +15 -0
- data/sunspot/spec/mocks/adapters.rb +33 -0
- data/sunspot/spec/mocks/blog.rb +3 -0
- data/sunspot/spec/mocks/comment.rb +21 -0
- data/sunspot/spec/mocks/connection.rb +126 -0
- data/sunspot/spec/mocks/mock_adapter.rb +30 -0
- data/sunspot/spec/mocks/mock_class_sharding_session_proxy.rb +24 -0
- data/sunspot/spec/mocks/mock_record.rb +52 -0
- data/sunspot/spec/mocks/mock_sharding_session_proxy.rb +15 -0
- data/sunspot/spec/mocks/photo.rb +11 -0
- data/sunspot/spec/mocks/post.rb +86 -0
- data/sunspot/spec/mocks/super_class.rb +2 -0
- data/sunspot/spec/mocks/user.rb +13 -0
- data/sunspot/spec/spec_helper.rb +40 -0
- data/sunspot/sunspot.gemspec +37 -0
- data/sunspot/tasks/rdoc.rake +27 -0
- data/sunspot/tasks/schema.rake +19 -0
- data/sunspot/tasks/todo.rake +4 -0
- data/sunspot_rails/.gitignore +7 -0
- data/sunspot_rails/.rspec +1 -0
- data/sunspot_rails/History.txt +74 -0
- data/sunspot_rails/LICENSE +18 -0
- data/sunspot_rails/MIT-LICENSE +20 -0
- data/sunspot_rails/README.rdoc +265 -0
- data/sunspot_rails/Rakefile +17 -0
- data/sunspot_rails/TODO +8 -0
- data/sunspot_rails/dev_tasks/rdoc.rake +24 -0
- data/sunspot_rails/dev_tasks/release.rake +4 -0
- data/sunspot_rails/dev_tasks/spec.rake +107 -0
- data/sunspot_rails/dev_tasks/todo.rake +4 -0
- data/sunspot_rails/gemfiles/rails-2.3.14 +12 -0
- data/sunspot_rails/gemfiles/rails-3.0.15 +12 -0
- data/sunspot_rails/gemfiles/rails-3.1.6 +12 -0
- data/sunspot_rails/gemfiles/rails-3.2.6 +12 -0
- data/sunspot_rails/generators/sunspot/sunspot_generator.rb +9 -0
- data/sunspot_rails/generators/sunspot/templates/sunspot.yml +20 -0
- data/sunspot_rails/install.rb +1 -0
- data/sunspot_rails/lib/generators/sunspot_rails.rb +9 -0
- data/sunspot_rails/lib/generators/sunspot_rails/install/install_generator.rb +13 -0
- data/sunspot_rails/lib/generators/sunspot_rails/install/templates/config/sunspot.yml +19 -0
- data/sunspot_rails/lib/sunspot/rails.rb +69 -0
- data/sunspot_rails/lib/sunspot/rails/adapters.rb +88 -0
- data/sunspot_rails/lib/sunspot/rails/configuration.rb +352 -0
- data/sunspot_rails/lib/sunspot/rails/init.rb +5 -0
- data/sunspot_rails/lib/sunspot/rails/log_subscriber.rb +45 -0
- data/sunspot_rails/lib/sunspot/rails/railtie.rb +41 -0
- data/sunspot_rails/lib/sunspot/rails/railties/controller_runtime.rb +36 -0
- data/sunspot_rails/lib/sunspot/rails/request_lifecycle.rb +36 -0
- data/sunspot_rails/lib/sunspot/rails/searchable.rb +495 -0
- data/sunspot_rails/lib/sunspot/rails/server.rb +106 -0
- data/sunspot_rails/lib/sunspot/rails/solr_instrumentation.rb +19 -0
- data/sunspot_rails/lib/sunspot/rails/solr_logging.rb +62 -0
- data/sunspot_rails/lib/sunspot/rails/spec_helper.rb +26 -0
- data/sunspot_rails/lib/sunspot/rails/stub_session_proxy.rb +146 -0
- data/sunspot_rails/lib/sunspot/rails/tasks.rb +94 -0
- data/sunspot_rails/lib/sunspot_rails.rb +12 -0
- data/sunspot_rails/spec/configuration_spec.rb +209 -0
- data/sunspot_rails/spec/model_lifecycle_spec.rb +63 -0
- data/sunspot_rails/spec/model_spec.rb +601 -0
- data/sunspot_rails/spec/rails_template/app/controllers/application_controller.rb +10 -0
- data/sunspot_rails/spec/rails_template/app/controllers/posts_controller.rb +6 -0
- data/sunspot_rails/spec/rails_template/app/models/author.rb +10 -0
- data/sunspot_rails/spec/rails_template/app/models/blog.rb +14 -0
- data/sunspot_rails/spec/rails_template/app/models/location.rb +3 -0
- data/sunspot_rails/spec/rails_template/app/models/photo_post.rb +2 -0
- data/sunspot_rails/spec/rails_template/app/models/post.rb +13 -0
- data/sunspot_rails/spec/rails_template/app/models/post_with_auto.rb +12 -0
- data/sunspot_rails/spec/rails_template/app/models/post_with_default_scope.rb +13 -0
- data/sunspot_rails/spec/rails_template/config/boot.rb +127 -0
- data/sunspot_rails/spec/rails_template/config/preinitializer.rb +22 -0
- data/sunspot_rails/spec/rails_template/config/routes.rb +9 -0
- data/sunspot_rails/spec/rails_template/config/sunspot.yml +24 -0
- data/sunspot_rails/spec/rails_template/db/schema.rb +27 -0
- data/sunspot_rails/spec/request_lifecycle_spec.rb +61 -0
- data/sunspot_rails/spec/schema.rb +27 -0
- data/sunspot_rails/spec/searchable_spec.rb +12 -0
- data/sunspot_rails/spec/server_spec.rb +33 -0
- data/sunspot_rails/spec/session_spec.rb +57 -0
- data/sunspot_rails/spec/shared_examples/indexed_after_save.rb +8 -0
- data/sunspot_rails/spec/shared_examples/not_indexed_after_save.rb +8 -0
- data/sunspot_rails/spec/spec_helper.rb +48 -0
- data/sunspot_rails/spec/stub_session_proxy_spec.rb +122 -0
- data/sunspot_rails/sunspot_rails.gemspec +43 -0
- data/{Gemfile → sunspot_solr/Gemfile} +0 -0
- data/{README.rdoc → sunspot_solr/README.rdoc} +0 -0
- data/{bin → sunspot_solr/bin}/sunspot-installer +0 -0
- data/{bin → sunspot_solr/bin}/sunspot-solr +0 -0
- data/{lib → sunspot_solr/lib}/sunspot/solr/installer.rb +0 -0
- data/{lib → sunspot_solr/lib}/sunspot/solr/installer/config_installer.rb +0 -0
- data/{lib → sunspot_solr/lib}/sunspot/solr/installer/task_helper.rb +0 -0
- data/{lib → sunspot_solr/lib}/sunspot/solr/java.rb +0 -0
- data/{lib → sunspot_solr/lib}/sunspot/solr/railtie.rb +0 -0
- data/{lib → sunspot_solr/lib}/sunspot/solr/server.rb +1 -2
- data/{lib → sunspot_solr/lib}/sunspot/solr/tasks.rb +0 -0
- data/{lib → sunspot_solr/lib}/sunspot_solr.rb +0 -0
- data/{solr → sunspot_solr/solr}/README.txt +0 -0
- data/{solr → sunspot_solr/solr}/etc/jetty.xml +0 -0
- data/{solr → sunspot_solr/solr}/etc/webdefault.xml +0 -0
- data/{solr → sunspot_solr/solr}/lib/jetty-6.1.26-patched-JETTY-1340.jar +0 -0
- data/{solr → sunspot_solr/solr}/lib/jetty-util-6.1.26-patched-JETTY-1340.jar +0 -0
- data/{solr → sunspot_solr/solr}/lib/jsp-2.1/ant-1.6.5.jar +0 -0
- data/{solr → sunspot_solr/solr}/lib/jsp-2.1/core-3.1.1.jar +0 -0
- data/{solr → sunspot_solr/solr}/lib/jsp-2.1/jsp-2.1.jar +0 -0
- data/{solr → sunspot_solr/solr}/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
- data/{solr → sunspot_solr/solr}/lib/servlet-api-2.5-20081211.jar +0 -0
- data/{solr → sunspot_solr/solr}/solr/.gitignore +0 -0
- data/{solr → sunspot_solr/solr}/solr/README.txt +0 -0
- data/{solr → sunspot_solr/solr}/solr/conf/admin-extra.html +0 -0
- data/{solr → sunspot_solr/solr}/solr/conf/elevate.xml +0 -0
- data/{solr → sunspot_solr/solr}/solr/conf/mapping-ISOLatin1Accent.txt +0 -0
- data/{solr → sunspot_solr/solr}/solr/conf/protwords.txt +0 -0
- data/{solr → sunspot_solr/solr}/solr/conf/schema.xml +0 -0
- data/{solr → sunspot_solr/solr}/solr/conf/scripts.conf +0 -0
- data/{solr → sunspot_solr/solr}/solr/conf/solrconfig.xml +0 -0
- data/{solr → sunspot_solr/solr}/solr/conf/spellings.txt +0 -0
- data/{solr → sunspot_solr/solr}/solr/conf/stopwords.txt +0 -0
- data/{solr → sunspot_solr/solr}/solr/conf/synonyms.txt +0 -0
- data/{solr → sunspot_solr/solr}/solr/conf/xslt/example.xsl +0 -0
- data/{solr → sunspot_solr/solr}/solr/conf/xslt/example_atom.xsl +0 -0
- data/{solr → sunspot_solr/solr}/solr/conf/xslt/example_rss.xsl +0 -0
- data/{solr → sunspot_solr/solr}/solr/conf/xslt/luke.xsl +0 -0
- data/{solr → sunspot_solr/solr}/start.jar +0 -0
- data/{solr → sunspot_solr/solr}/webapps/solr.war +0 -0
- data/{spec → sunspot_solr/spec}/server_spec.rb +0 -0
- data/{spec → sunspot_solr/spec}/spec_helper.rb +0 -0
- data/{sunspot_solr.gemspec → sunspot_solr/sunspot_solr.gemspec} +0 -0
- data/tools/gem_tasks.rb +69 -0
- metadata +340 -110
@@ -0,0 +1,109 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
#
|
4
|
+
# The classes in this module implement query components that build sort
|
5
|
+
# parameters for Solr. As well as regular sort on fields, there are several
|
6
|
+
# "special" sorts that allow ordering for metrics calculated during the
|
7
|
+
# search.
|
8
|
+
#
|
9
|
+
module Sort #:nodoc: all
|
10
|
+
DIRECTIONS = {
|
11
|
+
:asc => 'asc',
|
12
|
+
:ascending => 'asc',
|
13
|
+
:desc => 'desc',
|
14
|
+
:descending => 'desc'
|
15
|
+
}
|
16
|
+
|
17
|
+
class <<self
|
18
|
+
#
|
19
|
+
# Certain field names are "special", referring to specific non-field
|
20
|
+
# sorts, which are generally by other metrics associated with hits.
|
21
|
+
#
|
22
|
+
# XXX I'm not entirely convinced it's a good idea to prevent anyone from
|
23
|
+
# ever sorting by a field named 'score', etc.
|
24
|
+
#
|
25
|
+
def special(name)
|
26
|
+
special_class_name = "#{Util.camel_case(name.to_s)}Sort"
|
27
|
+
if const_defined?(special_class_name) && special_class_name != 'FieldSort'
|
28
|
+
const_get(special_class_name)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# Base class for sorts. All subclasses should implement the #to_param
|
35
|
+
# method, which is a string that is then concatenated with other sort
|
36
|
+
# strings by the SortComposite to form the sort parameter.
|
37
|
+
#
|
38
|
+
class Abstract
|
39
|
+
def initialize(direction)
|
40
|
+
@direction = (direction || :asc).to_sym
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
#
|
46
|
+
# Translate fairly forgiving direction argument into solr direction
|
47
|
+
#
|
48
|
+
def direction_for_solr
|
49
|
+
DIRECTIONS[@direction] ||
|
50
|
+
raise(
|
51
|
+
ArgumentError,
|
52
|
+
"Unknown sort direction #{@direction}. Acceptable input is: #{DIRECTIONS.keys.map { |input| input.inspect } * ', '}"
|
53
|
+
)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
#
|
58
|
+
# A FieldSort is the usual kind of sort, by the value of a particular
|
59
|
+
# field, ascending or descending
|
60
|
+
#
|
61
|
+
class FieldSort < Abstract
|
62
|
+
def initialize(field, direction = nil)
|
63
|
+
if field.multiple?
|
64
|
+
raise(ArgumentError, "#{field.name} cannot be used for ordering because it is a multiple-value field")
|
65
|
+
end
|
66
|
+
@field, @direction = field, (direction || :asc).to_sym
|
67
|
+
end
|
68
|
+
|
69
|
+
def to_param
|
70
|
+
"#{@field.indexed_name.to_sym} #{direction_for_solr}"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
#
|
75
|
+
# A RandomSort uses Solr's random field functionality to sort results
|
76
|
+
# (usually) randomly.
|
77
|
+
#
|
78
|
+
class RandomSort < Abstract
|
79
|
+
def to_param
|
80
|
+
"random_#{rand(1<<16)} #{direction_for_solr}"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
#
|
85
|
+
# A ScoreSort sorts by keyword relevance score. This is only useful when
|
86
|
+
# performing fulltext search.
|
87
|
+
#
|
88
|
+
class ScoreSort < Abstract
|
89
|
+
def to_param
|
90
|
+
"score #{direction_for_solr}"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
#
|
95
|
+
# A GeodistSort sorts by distance from a given point.
|
96
|
+
#
|
97
|
+
class GeodistSort < FieldSort
|
98
|
+
def initialize(field, lat, lon, direction)
|
99
|
+
@lat, @lon = lat, lon
|
100
|
+
super(field, direction)
|
101
|
+
end
|
102
|
+
|
103
|
+
def to_param
|
104
|
+
"geodist(#{@field.indexed_name.to_sym},#{@lat},#{@lon}) #{direction_for_solr}"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
#
|
4
|
+
# The SortComposite class encapsulates an ordered collection of Sort
|
5
|
+
# objects. It's necessary to keep this as a separate class as Solr takes
|
6
|
+
# the sort as a single parameter, so adding sorts as regular components
|
7
|
+
# would not merge correctly in the #to_params method.
|
8
|
+
#
|
9
|
+
class SortComposite #:nodoc:
|
10
|
+
def initialize
|
11
|
+
@sorts = []
|
12
|
+
end
|
13
|
+
|
14
|
+
#
|
15
|
+
# Add a sort to the composite
|
16
|
+
#
|
17
|
+
def <<(sort)
|
18
|
+
@sorts << sort
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# Combine the sorts into a single param by joining them
|
23
|
+
#
|
24
|
+
def to_params(prefix = "")
|
25
|
+
unless @sorts.empty?
|
26
|
+
key = "#{prefix}sort".to_sym
|
27
|
+
{ key => @sorts.map { |sort| sort.to_param } * ', ' }
|
28
|
+
else
|
29
|
+
{}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
class StandardQuery < CommonQuery
|
4
|
+
attr_accessor :scope, :fulltext
|
5
|
+
|
6
|
+
def initialize(types)
|
7
|
+
super
|
8
|
+
@components << @fulltext = CompositeFulltext.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_fulltext(keywords)
|
12
|
+
@fulltext.add(keywords)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
class TextFieldBoost #:nodoc:
|
4
|
+
attr_reader :boost
|
5
|
+
|
6
|
+
def initialize(field, boost = nil)
|
7
|
+
@field, @boost = field, boost
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_boosted_field
|
11
|
+
boosted_field = @field.indexed_name
|
12
|
+
boosted_field.concat("^#{@boost}") if @boost
|
13
|
+
boosted_field
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
3
|
+
module Sunspot
|
4
|
+
#
|
5
|
+
# Object that encapsulates schema information for building a Solr schema.xml
|
6
|
+
# file. This class is used by the schema:compile task as well as the
|
7
|
+
# sunspot-configure-solr executable.
|
8
|
+
#
|
9
|
+
class Schema #:nodoc:all
|
10
|
+
FieldType = Struct.new(:name, :class_name, :suffix)
|
11
|
+
FieldVariant = Struct.new(:attribute, :suffix)
|
12
|
+
|
13
|
+
DEFAULT_TOKENIZER = 'solr.StandardTokenizerFactory'
|
14
|
+
DEFAULT_FILTERS = %w(solr.StandardFilterFactory solr.LowerCaseFilterFactory)
|
15
|
+
|
16
|
+
FIELD_TYPES = [
|
17
|
+
FieldType.new('boolean', 'Bool', 'b'),
|
18
|
+
FieldType.new('sfloat', 'SortableFloat', 'f'),
|
19
|
+
FieldType.new('date', 'Date', 'd'),
|
20
|
+
FieldType.new('sint', 'SortableInt', 'i'),
|
21
|
+
FieldType.new('string', 'Str', 's'),
|
22
|
+
FieldType.new('sdouble', 'SortableDouble', 'e'),
|
23
|
+
FieldType.new('slong', 'SortableLong', 'l'),
|
24
|
+
FieldType.new('tint', 'TrieInteger', 'it'),
|
25
|
+
FieldType.new('tfloat', 'TrieFloat', 'ft'),
|
26
|
+
FieldType.new('tdate', 'TrieInt', 'dt')
|
27
|
+
|
28
|
+
]
|
29
|
+
|
30
|
+
FIELD_VARIANTS = [
|
31
|
+
FieldVariant.new('multiValued', 'm'),
|
32
|
+
FieldVariant.new('stored', 's')
|
33
|
+
]
|
34
|
+
|
35
|
+
attr_reader :tokenizer, :filters
|
36
|
+
|
37
|
+
def initialize
|
38
|
+
@tokenizer = DEFAULT_TOKENIZER
|
39
|
+
@filters = DEFAULT_FILTERS.dup
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# Attribute field types defined in the schema
|
44
|
+
#
|
45
|
+
def types
|
46
|
+
FIELD_TYPES
|
47
|
+
end
|
48
|
+
|
49
|
+
#
|
50
|
+
# DynamicField instances representing all the available types and variants
|
51
|
+
#
|
52
|
+
def dynamic_fields
|
53
|
+
fields = []
|
54
|
+
variant_combinations.each do |field_variants|
|
55
|
+
FIELD_TYPES.each do |type|
|
56
|
+
fields << DynamicField.new(type, field_variants)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
fields
|
60
|
+
end
|
61
|
+
|
62
|
+
#
|
63
|
+
# Which tokenizer to use for text fields
|
64
|
+
#
|
65
|
+
def tokenizer=(tokenizer)
|
66
|
+
@tokenizer =
|
67
|
+
if tokenizer =~ /\./
|
68
|
+
tokenizer
|
69
|
+
else
|
70
|
+
"solr.#{tokenizer}TokenizerFactory"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
#
|
75
|
+
# Add a filter for text field tokenization
|
76
|
+
#
|
77
|
+
def add_filter(filter)
|
78
|
+
@filters <<
|
79
|
+
if filter =~ /\./
|
80
|
+
filter
|
81
|
+
else
|
82
|
+
"solr.#{filter}FilterFactory"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
#
|
87
|
+
# Return an XML representation of this schema using the ERB template
|
88
|
+
#
|
89
|
+
def to_xml
|
90
|
+
template = File.join(File.dirname(__FILE__), '..', '..', 'templates', 'schema.xml.erb')
|
91
|
+
ERB.new(File.read(template), nil, '-').result(binding)
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
#
|
97
|
+
# All of the possible combinations of variants
|
98
|
+
#
|
99
|
+
def variant_combinations
|
100
|
+
combinations = []
|
101
|
+
0.upto(2 ** FIELD_VARIANTS.length - 1) do |b|
|
102
|
+
combinations << combination = []
|
103
|
+
FIELD_VARIANTS.each_with_index do |variant, i|
|
104
|
+
combination << variant if b & 1<<i > 0
|
105
|
+
end
|
106
|
+
end
|
107
|
+
combinations
|
108
|
+
end
|
109
|
+
|
110
|
+
#
|
111
|
+
# Represents a dynamic field (in the Solr schema sense, not the Sunspot
|
112
|
+
# sense).
|
113
|
+
#
|
114
|
+
class DynamicField
|
115
|
+
def initialize(type, field_variants)
|
116
|
+
@type, @field_variants = type, field_variants
|
117
|
+
end
|
118
|
+
|
119
|
+
#
|
120
|
+
# Name of the field in the schema
|
121
|
+
#
|
122
|
+
def name
|
123
|
+
variant_suffixes = @field_variants.map { |variant| variant.suffix }.join
|
124
|
+
"*_#{@type.suffix}#{variant_suffixes}"
|
125
|
+
end
|
126
|
+
|
127
|
+
#
|
128
|
+
# Name of the type as defined in the schema
|
129
|
+
#
|
130
|
+
def type
|
131
|
+
@type.name
|
132
|
+
end
|
133
|
+
|
134
|
+
#
|
135
|
+
# Implement magic methods to ask if a field is of a particular variant.
|
136
|
+
# Returns "true" if the field is of that variant and "false" otherwise.
|
137
|
+
#
|
138
|
+
def method_missing(name, *args, &block)
|
139
|
+
if name.to_s =~ /\?$/ && args.empty?
|
140
|
+
if @field_variants.any? { |variant| "#{variant.attribute}?" == name.to_s }
|
141
|
+
'true'
|
142
|
+
else
|
143
|
+
'false'
|
144
|
+
end
|
145
|
+
else
|
146
|
+
super(name.to_sym, *args, &block)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
%w(abstract_search standard_search more_like_this_search query_facet field_facet
|
2
|
+
date_facet range_facet facet_row hit highlight field_group group hit_enumerable).each do |file|
|
3
|
+
require File.join(File.dirname(__FILE__), 'search', file)
|
4
|
+
end
|
5
|
+
|
6
|
+
module Sunspot
|
7
|
+
module Search
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,286 @@
|
|
1
|
+
require 'sunspot/search/paginated_collection'
|
2
|
+
require 'sunspot/search/hit_enumerable'
|
3
|
+
|
4
|
+
module Sunspot
|
5
|
+
module Search #:nodoc:
|
6
|
+
|
7
|
+
#
|
8
|
+
# This class encapsulates the results of a Solr search. It provides access
|
9
|
+
# to search results, total result count, facets, and pagination information.
|
10
|
+
# Instances of Search are returned by the Sunspot.search and
|
11
|
+
# Sunspot.new_search methods.
|
12
|
+
#
|
13
|
+
class AbstractSearch
|
14
|
+
#
|
15
|
+
# Retrieve all facet objects defined for this search, in order they were
|
16
|
+
# defined. To retrieve an individual facet by name, use #facet()
|
17
|
+
#
|
18
|
+
attr_reader :facets, :groups
|
19
|
+
attr_reader :query #:nodoc:
|
20
|
+
attr_accessor :request_handler
|
21
|
+
|
22
|
+
include HitEnumerable
|
23
|
+
|
24
|
+
def initialize(connection, setup, query, configuration) #:nodoc:
|
25
|
+
@connection, @setup, @query = connection, setup, query
|
26
|
+
@query.paginate(1, configuration.pagination.default_per_page)
|
27
|
+
|
28
|
+
@facets = []
|
29
|
+
@facets_by_name = {}
|
30
|
+
|
31
|
+
@groups_by_name = {}
|
32
|
+
@groups = []
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# Execute the search on the Solr instance and store the results. If you
|
37
|
+
# use Sunspot#search() to construct your searches, there is no need to call
|
38
|
+
# this method as it has already been called. If you use
|
39
|
+
# Sunspot#new_search(), you will need to call this method after building the
|
40
|
+
# query.
|
41
|
+
#
|
42
|
+
def execute
|
43
|
+
reset
|
44
|
+
params = @query.to_params
|
45
|
+
@solr_result = @connection.post "#{request_handler}", :data => params
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
def execute! #:nodoc: deprecated
|
50
|
+
execute
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
# Get the collection of results as instantiated objects. If WillPaginate is
|
55
|
+
# available, the results will be a WillPaginate::Collection instance; if
|
56
|
+
# not, it will be a vanilla Array.
|
57
|
+
#
|
58
|
+
# If not all of the results referenced by the Solr hits actually exist in
|
59
|
+
# the data store, Sunspot will only return the results that do exist.
|
60
|
+
#
|
61
|
+
# ==== Returns
|
62
|
+
#
|
63
|
+
# WillPaginate::Collection or Array:: Instantiated result objects
|
64
|
+
#
|
65
|
+
def results
|
66
|
+
@results ||= paginate_collection(verified_hits.map { |hit| hit.instance })
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
# Access raw Solr result information. Returns a collection of Hit objects
|
71
|
+
# that contain the class name, primary key, keyword relevance score (if
|
72
|
+
# applicable), and any stored fields.
|
73
|
+
#
|
74
|
+
# ==== Options (options)
|
75
|
+
#
|
76
|
+
# :verify::
|
77
|
+
# Only return hits that reference objects that actually exist in the data
|
78
|
+
# store. This causes results to be eager-loaded from the data store,
|
79
|
+
# unlike the normal behavior of this method, which only loads the
|
80
|
+
# referenced results when Hit#result is first called.
|
81
|
+
#
|
82
|
+
# ==== Returns
|
83
|
+
#
|
84
|
+
# Array:: Ordered collection of Hit objects
|
85
|
+
#
|
86
|
+
def hits(options = {})
|
87
|
+
if options[:verify]
|
88
|
+
super
|
89
|
+
else
|
90
|
+
@hits ||= paginate_collection(super)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
alias_method :raw_results, :hits
|
94
|
+
|
95
|
+
#
|
96
|
+
# The total number of documents matching the query parameters
|
97
|
+
#
|
98
|
+
# ==== Returns
|
99
|
+
#
|
100
|
+
# Integer:: Total matching documents
|
101
|
+
#
|
102
|
+
def total
|
103
|
+
@total ||= solr_response['numFound'] || 0
|
104
|
+
end
|
105
|
+
|
106
|
+
#
|
107
|
+
# The time elapsed to generate the Solr response
|
108
|
+
#
|
109
|
+
# ==== Returns
|
110
|
+
#
|
111
|
+
# Integer:: Query runtime in milliseconds
|
112
|
+
#
|
113
|
+
def query_time
|
114
|
+
@query_time ||= solr_response_header['QTime']
|
115
|
+
end
|
116
|
+
|
117
|
+
#
|
118
|
+
# Get the facet object for the given name. `name` can either be the name
|
119
|
+
# given to a query facet, or the field name of a field facet. Returns a
|
120
|
+
# Sunspot::Facet object.
|
121
|
+
#
|
122
|
+
# ==== Parameters
|
123
|
+
#
|
124
|
+
# name<Symbol>::
|
125
|
+
# Name of the field to return the facet for, or the name given to the
|
126
|
+
# query facet when the search was constructed.
|
127
|
+
# dynamic_name<Symbol>::
|
128
|
+
# If faceting on a dynamic field, this is the dynamic portion of the field
|
129
|
+
# name.
|
130
|
+
#
|
131
|
+
# ==== Example:
|
132
|
+
#
|
133
|
+
# search = Sunspot.search(Post) do
|
134
|
+
# facet :category_ids
|
135
|
+
# dynamic :custom do
|
136
|
+
# facet :cuisine
|
137
|
+
# end
|
138
|
+
# facet :age do
|
139
|
+
# row 'Less than a month' do
|
140
|
+
# with(:published_at).greater_than(1.month.ago)
|
141
|
+
# end
|
142
|
+
# row 'Less than a year' do
|
143
|
+
# with(:published_at, 1.year.ago..1.month.ago)
|
144
|
+
# end
|
145
|
+
# row 'More than a year' do
|
146
|
+
# with(:published_at).less_than(1.year.ago)
|
147
|
+
# end
|
148
|
+
# end
|
149
|
+
# end
|
150
|
+
# search.facet(:category_ids)
|
151
|
+
# #=> Facet for :category_ids field
|
152
|
+
# search.facet(:custom, :cuisine)
|
153
|
+
# #=> Facet for the dynamic field :cuisine in the :custom field definition
|
154
|
+
# search.facet(:age)
|
155
|
+
# #=> Facet for the query facet named :age
|
156
|
+
#
|
157
|
+
def facet(name, dynamic_name = nil)
|
158
|
+
if name
|
159
|
+
if dynamic_name
|
160
|
+
@facets_by_name[:"#{name}:#{dynamic_name}"]
|
161
|
+
else
|
162
|
+
@facets_by_name[name.to_sym]
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def group(name)
|
168
|
+
if name
|
169
|
+
@groups_by_name[name.to_sym]
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
#
|
174
|
+
# Deprecated in favor of optional second argument to #facet
|
175
|
+
#
|
176
|
+
def dynamic_facet(base_name, dynamic_name) #:nodoc:
|
177
|
+
facet(base_name, dynamic_name)
|
178
|
+
end
|
179
|
+
|
180
|
+
def facet_response #:nodoc:
|
181
|
+
@solr_result['facet_counts']
|
182
|
+
end
|
183
|
+
|
184
|
+
def group_response #:nodoc:
|
185
|
+
@solr_result['grouped']
|
186
|
+
end
|
187
|
+
|
188
|
+
#
|
189
|
+
# Build this search using a DSL block. This method can be called more than
|
190
|
+
# once on an unexecuted search (e.g., Sunspot.new_search) in order to build
|
191
|
+
# a search incrementally.
|
192
|
+
#
|
193
|
+
# === Example
|
194
|
+
#
|
195
|
+
# search = Sunspot.new_search(Post)
|
196
|
+
# search.build do
|
197
|
+
# with(:published_at).less_than Time.now
|
198
|
+
# end
|
199
|
+
# search.execute
|
200
|
+
#
|
201
|
+
def build(&block)
|
202
|
+
Util.instance_eval_or_call(dsl, &block)
|
203
|
+
self
|
204
|
+
end
|
205
|
+
|
206
|
+
|
207
|
+
def inspect #:nodoc:
|
208
|
+
"<Sunspot::Search:#{query.to_params.inspect}>"
|
209
|
+
end
|
210
|
+
|
211
|
+
def add_field_group(field) #:nodoc:
|
212
|
+
add_group(field.name, FieldGroup.new(field, self))
|
213
|
+
end
|
214
|
+
|
215
|
+
def add_field_facet(field, options = {}) #:nodoc:
|
216
|
+
name = (options[:name] || field.name)
|
217
|
+
add_facet(name, FieldFacet.new(field, self, options))
|
218
|
+
end
|
219
|
+
|
220
|
+
def add_query_facet(name, options) #:nodoc:
|
221
|
+
add_facet(name, QueryFacet.new(name, self, options))
|
222
|
+
end
|
223
|
+
|
224
|
+
def add_date_facet(field, options) #:nodoc:
|
225
|
+
name = (options[:name] || field.name)
|
226
|
+
add_facet(name, DateFacet.new(field, self, options))
|
227
|
+
end
|
228
|
+
|
229
|
+
def add_range_facet(field, options) #:nodoc:
|
230
|
+
name = (options[:name] || field.name)
|
231
|
+
add_facet(name, RangeFacet.new(field, self, options))
|
232
|
+
end
|
233
|
+
|
234
|
+
def highlights_for(doc) #:nodoc:
|
235
|
+
if @solr_result['highlighting']
|
236
|
+
@solr_result['highlighting'][doc['id']]
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
private
|
241
|
+
|
242
|
+
def dsl
|
243
|
+
raise NotImplementedError
|
244
|
+
end
|
245
|
+
|
246
|
+
def execute_request(params)
|
247
|
+
raise NotImplementedError
|
248
|
+
end
|
249
|
+
|
250
|
+
def solr_response
|
251
|
+
@solr_response ||= @solr_result['response'] || {}
|
252
|
+
end
|
253
|
+
|
254
|
+
def solr_response_header
|
255
|
+
@solr_response_header ||= @solr_result['responseHeader'] || {}
|
256
|
+
end
|
257
|
+
|
258
|
+
def solr_docs
|
259
|
+
solr_response['docs']
|
260
|
+
end
|
261
|
+
|
262
|
+
def verified_hits
|
263
|
+
@verified_hits ||= paginate_collection(super)
|
264
|
+
end
|
265
|
+
|
266
|
+
def paginate_collection(collection)
|
267
|
+
PaginatedCollection.new(collection, @query.page, @query.per_page, total)
|
268
|
+
end
|
269
|
+
|
270
|
+
def add_facet(name, facet)
|
271
|
+
@facets << facet
|
272
|
+
@facets_by_name[name.to_sym] = facet
|
273
|
+
end
|
274
|
+
|
275
|
+
def add_group(name, group)
|
276
|
+
@groups << group
|
277
|
+
@groups_by_name[name.to_sym] = group
|
278
|
+
end
|
279
|
+
|
280
|
+
# Clear out all the cached ivars so the search can be called again.
|
281
|
+
def reset
|
282
|
+
@results = @hits = @verified_hits = @total = @solr_response = @doc_ids = nil
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|