kuahyeow-sunspot 0.9.7
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +83 -0
- data/LICENSE +18 -0
- data/README.rdoc +154 -0
- data/Rakefile +9 -0
- data/TODO +9 -0
- data/VERSION.yml +4 -0
- data/bin/sunspot-configure-solr +46 -0
- data/bin/sunspot-solr +62 -0
- data/lib/light_config.rb +40 -0
- data/lib/sunspot/adapters.rb +265 -0
- data/lib/sunspot/composite_setup.rb +186 -0
- data/lib/sunspot/configuration.rb +38 -0
- data/lib/sunspot/data_extractor.rb +47 -0
- data/lib/sunspot/date_facet.rb +36 -0
- data/lib/sunspot/date_facet_row.rb +17 -0
- data/lib/sunspot/dsl/field_query.rb +72 -0
- data/lib/sunspot/dsl/fields.rb +86 -0
- data/lib/sunspot/dsl/query.rb +59 -0
- data/lib/sunspot/dsl/query_facet.rb +31 -0
- data/lib/sunspot/dsl/restriction.rb +25 -0
- data/lib/sunspot/dsl/scope.rb +193 -0
- data/lib/sunspot/dsl/search.rb +30 -0
- data/lib/sunspot/dsl.rb +3 -0
- data/lib/sunspot/facet.rb +51 -0
- data/lib/sunspot/facet_row.rb +34 -0
- data/lib/sunspot/field.rb +157 -0
- data/lib/sunspot/field_factory.rb +126 -0
- data/lib/sunspot/indexer.rb +123 -0
- data/lib/sunspot/instantiated_facet.rb +38 -0
- data/lib/sunspot/instantiated_facet_row.rb +12 -0
- data/lib/sunspot/query/base_query.rb +94 -0
- data/lib/sunspot/query/connective.rb +126 -0
- data/lib/sunspot/query/dynamic_query.rb +69 -0
- data/lib/sunspot/query/field_facet.rb +149 -0
- data/lib/sunspot/query/field_query.rb +57 -0
- data/lib/sunspot/query/pagination.rb +39 -0
- data/lib/sunspot/query/query_facet.rb +72 -0
- data/lib/sunspot/query/query_facet_row.rb +19 -0
- data/lib/sunspot/query/restriction.rb +233 -0
- data/lib/sunspot/query/scope.rb +165 -0
- data/lib/sunspot/query/sort.rb +36 -0
- data/lib/sunspot/query/sort_composite.rb +33 -0
- data/lib/sunspot/query.rb +190 -0
- data/lib/sunspot/query_facet.rb +33 -0
- data/lib/sunspot/query_facet_row.rb +21 -0
- data/lib/sunspot/schema.rb +165 -0
- data/lib/sunspot/search/hit.rb +66 -0
- data/lib/sunspot/search.rb +220 -0
- data/lib/sunspot/session.rb +201 -0
- data/lib/sunspot/setup.rb +271 -0
- data/lib/sunspot/type.rb +200 -0
- data/lib/sunspot/util.rb +164 -0
- data/lib/sunspot.rb +470 -0
- data/solr/etc/jetty.xml +212 -0
- data/solr/etc/webdefault.xml +379 -0
- data/solr/lib/jetty-6.1.3.jar +0 -0
- data/solr/lib/jetty-util-6.1.3.jar +0 -0
- data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
- data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
- data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
- data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
- data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
- data/solr/solr/conf/elevate.xml +36 -0
- data/solr/solr/conf/protwords.txt +21 -0
- data/solr/solr/conf/schema.xml +50 -0
- data/solr/solr/conf/solrconfig.xml +696 -0
- data/solr/solr/conf/stopwords.txt +57 -0
- data/solr/solr/conf/synonyms.txt +31 -0
- data/solr/start.jar +0 -0
- data/solr/webapps/solr.war +0 -0
- data/spec/api/adapters_spec.rb +33 -0
- data/spec/api/build_search_spec.rb +1018 -0
- data/spec/api/indexer_spec.rb +311 -0
- data/spec/api/query_spec.rb +153 -0
- data/spec/api/search_retrieval_spec.rb +335 -0
- data/spec/api/session_spec.rb +157 -0
- data/spec/api/spec_helper.rb +1 -0
- data/spec/api/sunspot_spec.rb +18 -0
- data/spec/integration/dynamic_fields_spec.rb +55 -0
- data/spec/integration/faceting_spec.rb +169 -0
- data/spec/integration/keyword_search_spec.rb +83 -0
- data/spec/integration/scoped_search_spec.rb +289 -0
- data/spec/integration/spec_helper.rb +1 -0
- data/spec/integration/stored_fields_spec.rb +10 -0
- data/spec/integration/test_pagination.rb +32 -0
- data/spec/mocks/adapters.rb +32 -0
- data/spec/mocks/blog.rb +3 -0
- data/spec/mocks/comment.rb +19 -0
- data/spec/mocks/connection.rb +84 -0
- data/spec/mocks/mock_adapter.rb +30 -0
- data/spec/mocks/mock_record.rb +48 -0
- data/spec/mocks/photo.rb +8 -0
- data/spec/mocks/post.rb +73 -0
- data/spec/mocks/user.rb +8 -0
- data/spec/spec_helper.rb +47 -0
- data/tasks/gemspec.rake +24 -0
- data/tasks/rcov.rake +28 -0
- data/tasks/rdoc.rake +22 -0
- data/tasks/schema.rake +19 -0
- data/tasks/spec.rake +24 -0
- data/tasks/todo.rake +4 -0
- data/templates/schema.xml.haml +24 -0
- metadata +247 -0
@@ -0,0 +1,123 @@
|
|
1
|
+
module Sunspot
|
2
|
+
#
|
3
|
+
# This class presents a service for adding, updating, and removing data
|
4
|
+
# from the Solr index. An Indexer instance is associated with a particular
|
5
|
+
# setup, and thus is capable of indexing instances of a certain class (and its
|
6
|
+
# subclasses).
|
7
|
+
#
|
8
|
+
class Indexer #:nodoc:
|
9
|
+
include RSolr::Char
|
10
|
+
|
11
|
+
def initialize(connection)
|
12
|
+
@connection = connection
|
13
|
+
end
|
14
|
+
|
15
|
+
#
|
16
|
+
# Construct a representation of the model for indexing and send it to the
|
17
|
+
# connection for indexing
|
18
|
+
#
|
19
|
+
# ==== Parameters
|
20
|
+
#
|
21
|
+
# model<Object>:: the model to index
|
22
|
+
#
|
23
|
+
def add(model)
|
24
|
+
documents = Array(model).map { |m| prepare(m) }
|
25
|
+
if @batch.nil?
|
26
|
+
add_documents(documents)
|
27
|
+
else
|
28
|
+
@batch.concat(documents)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# Remove the given model from the Solr index
|
34
|
+
#
|
35
|
+
def remove(model)
|
36
|
+
@connection.delete_by_id(Adapters::InstanceAdapter.adapt(model).index_id)
|
37
|
+
end
|
38
|
+
|
39
|
+
def remove_by_id(class_name, id)
|
40
|
+
@connection.delete_by_id(
|
41
|
+
Adapters::InstanceAdapter.index_id_for(class_name, id)
|
42
|
+
)
|
43
|
+
end
|
44
|
+
|
45
|
+
#
|
46
|
+
# Delete all documents of the class indexed by this indexer from Solr.
|
47
|
+
#
|
48
|
+
def remove_all(clazz)
|
49
|
+
@connection.delete_by_query("type:#{escape(clazz.name)}")
|
50
|
+
end
|
51
|
+
|
52
|
+
def start_batch
|
53
|
+
@batch = []
|
54
|
+
end
|
55
|
+
|
56
|
+
def flush_batch
|
57
|
+
add_documents(@batch)
|
58
|
+
@batch = nil
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
#
|
64
|
+
# Convert documents into hash of indexed properties
|
65
|
+
#
|
66
|
+
def prepare(model)
|
67
|
+
document = document_for(model)
|
68
|
+
setup = setup_for(model)
|
69
|
+
if boost = setup.document_boost_for(model)
|
70
|
+
document.attrs[:boost] = boost
|
71
|
+
end
|
72
|
+
for field_factory in setup.all_field_factories
|
73
|
+
field_factory.populate_document(document, model)
|
74
|
+
end
|
75
|
+
document
|
76
|
+
end
|
77
|
+
|
78
|
+
def add_documents(documents)
|
79
|
+
@connection.add(documents)
|
80
|
+
end
|
81
|
+
|
82
|
+
#
|
83
|
+
# All indexed documents index and store the +id+ and +type+ fields.
|
84
|
+
# This method constructs the document hash containing those key-value
|
85
|
+
# pairs.
|
86
|
+
#
|
87
|
+
def document_for(model)
|
88
|
+
RSolr::Message::Document.new(
|
89
|
+
:id => Adapters::InstanceAdapter.adapt(model).index_id,
|
90
|
+
:type => Util.superclasses_for(model.class).map { |clazz| clazz.name }
|
91
|
+
)
|
92
|
+
end
|
93
|
+
|
94
|
+
#
|
95
|
+
# Get the Setup object for the given object's class.
|
96
|
+
#
|
97
|
+
# ==== Parameters
|
98
|
+
#
|
99
|
+
# object<Object>:: The object whose setup is to be retrieved
|
100
|
+
#
|
101
|
+
# ==== Returns
|
102
|
+
#
|
103
|
+
# Sunspot::Setup:: The setup for the object's class
|
104
|
+
#
|
105
|
+
def setup_for(object)
|
106
|
+
Setup.for(object.class) || raise(NoSetupError, "Sunspot is not configured for #{object.class.inspect}")
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
class <<self
|
111
|
+
#
|
112
|
+
# Delete all documents from the Solr index
|
113
|
+
#
|
114
|
+
# ==== Parameters
|
115
|
+
#
|
116
|
+
# connection<Solr::Connection>::
|
117
|
+
# connection to which to send the delete request
|
118
|
+
def remove_all(connection)
|
119
|
+
connection.delete_by_query("type:[* TO *]")
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Sunspot
|
2
|
+
#
|
3
|
+
# InstantiatedFacet instances allow access to a model instance based on a
|
4
|
+
# primary key stored in facet rows' values. The rows are hydrated lazily, but
|
5
|
+
# all rows are hydrated the first time #instance is called on any of the rows.
|
6
|
+
#
|
7
|
+
# The #rows method returns InstantiatedFacetRow objects.
|
8
|
+
#
|
9
|
+
class InstantiatedFacet < Facet
|
10
|
+
#
|
11
|
+
# Hydrate all rows for the facet. For data accessors that can efficiently
|
12
|
+
# batch load, this is more efficient than individually lazy-loading
|
13
|
+
# instances for each row, but allows us to still stay lazy and not do work
|
14
|
+
# in the persistent store if the instances are not needed.
|
15
|
+
#
|
16
|
+
def populate_instances! #:nodoc:
|
17
|
+
ids = rows.map { |row| row.value }
|
18
|
+
reference_class = Sunspot::Util.full_const_get(@field.reference.to_s)
|
19
|
+
accessor = Adapters::DataAccessor.create(reference_class)
|
20
|
+
instance_map = accessor.load_all(ids).inject({}) do |map, instance|
|
21
|
+
map[Adapters::InstanceAdapter.adapt(instance).id] = instance
|
22
|
+
map
|
23
|
+
end
|
24
|
+
for row in rows
|
25
|
+
row.instance = instance_map[row.value]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
#
|
32
|
+
# Override the Facet#new_row method to return an InstantiateFacetRow
|
33
|
+
#
|
34
|
+
def new_row(pair)
|
35
|
+
InstantiatedFacetRow.new(pair, self)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
#
|
4
|
+
# Encapsulates information common to all queries - in particular, keywords
|
5
|
+
# and types.
|
6
|
+
#
|
7
|
+
class BaseQuery #:nodoc:
|
8
|
+
include RSolr::Char
|
9
|
+
|
10
|
+
attr_writer :keywords
|
11
|
+
|
12
|
+
def initialize(types, setup)
|
13
|
+
@types, @setup = types, setup
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
# Generate params for the base query. If keywords are specified, build
|
18
|
+
# params for a dismax query, request all stored fields plus the score,
|
19
|
+
# and put the types in a filter query. If keywords are not specified,
|
20
|
+
# put the types query in the q parameter.
|
21
|
+
#
|
22
|
+
def to_params
|
23
|
+
params = {}
|
24
|
+
if @keywords
|
25
|
+
params[:q] = @keywords
|
26
|
+
if @keywords.match(/\*\Z/)
|
27
|
+
params['q.alt'] = @keywords
|
28
|
+
params.delete(:q)
|
29
|
+
end
|
30
|
+
params[:fl] = '* score'
|
31
|
+
params[:fq] = types_phrase
|
32
|
+
params[:qf] = text_field_names.join(' ')
|
33
|
+
params[:defType] = 'dismax'
|
34
|
+
else
|
35
|
+
params[:q] = types_phrase
|
36
|
+
end
|
37
|
+
params
|
38
|
+
end
|
39
|
+
|
40
|
+
#
|
41
|
+
# Set keyword options
|
42
|
+
#
|
43
|
+
def keyword_options=(options)
|
44
|
+
if options
|
45
|
+
@text_field_names = options.delete(:fields)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
#
|
52
|
+
# Boolean phrase that restricts results to objects of the type(s) under
|
53
|
+
# query. If this is an open query (no types specified) then it sends a
|
54
|
+
# no-op phrase because Solr requires that the :q parameter not be empty.
|
55
|
+
#
|
56
|
+
# ==== Returns
|
57
|
+
#
|
58
|
+
# String:: Boolean phrase for type restriction
|
59
|
+
#
|
60
|
+
def types_phrase
|
61
|
+
if escaped_types.length == 1 then "type:#{escaped_types.first}"
|
62
|
+
else "type:(#{escaped_types * ' OR '})"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# Wraps each type in quotes to escape names of the form Namespace::Class
|
68
|
+
#
|
69
|
+
def escaped_types
|
70
|
+
@escaped_types ||=
|
71
|
+
@types.map { |type| escape(type.name)}
|
72
|
+
end
|
73
|
+
|
74
|
+
#
|
75
|
+
# Returns the names of text fields that should be queried in a keyword
|
76
|
+
# search. If specific fields are requested, use those; otherwise use the
|
77
|
+
# union of all fields configured for the types under search.
|
78
|
+
#
|
79
|
+
def text_field_names
|
80
|
+
text_fields =
|
81
|
+
if @text_field_names
|
82
|
+
Array(@text_field_names).map do |field_name|
|
83
|
+
@setup.text_field(field_name.to_sym)
|
84
|
+
end
|
85
|
+
else
|
86
|
+
@setup.text_fields
|
87
|
+
end
|
88
|
+
text_fields.map do |text_field|
|
89
|
+
text_field.indexed_name
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
module Connective #:nodoc:
|
4
|
+
#
|
5
|
+
# Base class for connectives (conjunctions and disjunctions).
|
6
|
+
#
|
7
|
+
class Abstract < Scope
|
8
|
+
def initialize(setup, negated = false) #:nodoc:
|
9
|
+
@setup, @negated = setup, negated
|
10
|
+
@components = []
|
11
|
+
end
|
12
|
+
|
13
|
+
#
|
14
|
+
# Connective as solr params.
|
15
|
+
#
|
16
|
+
def to_params #:nodoc:
|
17
|
+
{ :fq => to_boolean_phrase }
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# Express the connective as a Lucene boolean phrase.
|
22
|
+
#
|
23
|
+
def to_boolean_phrase #:nodoc:
|
24
|
+
phrase = if @components.length == 1
|
25
|
+
@components.first.to_boolean_phrase
|
26
|
+
else
|
27
|
+
component_phrases = @components.map do |component|
|
28
|
+
component.to_boolean_phrase
|
29
|
+
end
|
30
|
+
"(#{component_phrases.join(" #{connector} ")})"
|
31
|
+
end
|
32
|
+
if negated?
|
33
|
+
"-#{phrase}"
|
34
|
+
else
|
35
|
+
phrase
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# Add a component to the connective. All components must implement the
|
41
|
+
# #to_boolean_phrase method.
|
42
|
+
#
|
43
|
+
def add_component(component) #:nodoc:
|
44
|
+
@components << component
|
45
|
+
end
|
46
|
+
|
47
|
+
def negated?
|
48
|
+
@negated
|
49
|
+
end
|
50
|
+
|
51
|
+
def negate
|
52
|
+
negated = self.class.new(@setup, !negated?)
|
53
|
+
for component in @components
|
54
|
+
negated.add_component(component)
|
55
|
+
end
|
56
|
+
negated
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
#
|
61
|
+
# Disjunctions combine their components with an OR operator.
|
62
|
+
#
|
63
|
+
class Disjunction < Abstract
|
64
|
+
class <<self
|
65
|
+
def inverse
|
66
|
+
Conjunction
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def to_boolean_phrase
|
71
|
+
if @components.any? { |component| component.negated? }
|
72
|
+
denormalize.to_boolean_phrase
|
73
|
+
else
|
74
|
+
super
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
#
|
79
|
+
# Add a conjunction to the disjunction. This overrides the method in
|
80
|
+
# the Scope class since scopes are implicitly conjunctive and thus
|
81
|
+
# can return themselves as a conjunction. Inside a disjunction, however,
|
82
|
+
# a conjunction must explicitly be created.
|
83
|
+
#
|
84
|
+
def add_conjunction
|
85
|
+
@components << conjunction = Conjunction.new(setup)
|
86
|
+
conjunction
|
87
|
+
end
|
88
|
+
|
89
|
+
def add_disjunction
|
90
|
+
self
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
def connector
|
96
|
+
'OR'
|
97
|
+
end
|
98
|
+
|
99
|
+
def denormalize
|
100
|
+
denormalized = self.class.inverse.new(@setup, !negated?)
|
101
|
+
for component in @components
|
102
|
+
denormalized.add_component(component.negate)
|
103
|
+
end
|
104
|
+
denormalized
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
#
|
109
|
+
# Conjunctions combine their components with an AND operator.
|
110
|
+
#
|
111
|
+
class Conjunction < Abstract
|
112
|
+
class <<self
|
113
|
+
def inverse
|
114
|
+
Disjunction
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
119
|
+
|
120
|
+
def connector
|
121
|
+
'AND'
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
#
|
4
|
+
# A dynamic query is a proxy object that implements the API of the FieldQuery
|
5
|
+
# class, but wraps a dynamic field factory and thus applies the query
|
6
|
+
# components using dynamic field instances.
|
7
|
+
#--
|
8
|
+
# Dynamic queries do not hold their own state, but rather proxy to the query
|
9
|
+
# that generated them, adding components directly to the owning query's
|
10
|
+
# internal state.
|
11
|
+
#++
|
12
|
+
# DynamicQuery instances are publicly generated by the Query#dynamic_query
|
13
|
+
# factory method.
|
14
|
+
#
|
15
|
+
class DynamicQuery < FieldQuery
|
16
|
+
def initialize(dynamic_field_factory, query) #:nodoc:
|
17
|
+
@dynamic_field_factory, @query = dynamic_field_factory, query
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# This has the same effect as calling Query#exclude_instance; it is
|
22
|
+
# included for interface completeness.
|
23
|
+
#
|
24
|
+
def exclude_instance(instance)
|
25
|
+
@query.exclude_instance(instance)
|
26
|
+
end
|
27
|
+
|
28
|
+
#
|
29
|
+
# This has the same effect as calling Query#exclude_instance; it is
|
30
|
+
# included for interface completeness.
|
31
|
+
#
|
32
|
+
def dynamic_query(field_name)
|
33
|
+
@query.dynamic_query(field_name)
|
34
|
+
end
|
35
|
+
|
36
|
+
#
|
37
|
+
# Add a Sort to the query
|
38
|
+
#
|
39
|
+
def add_sort(sort) #:nodoc:
|
40
|
+
@query.add_sort(sort)
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# Add a component to the query
|
45
|
+
#
|
46
|
+
def add_component(component) #:nodoc:
|
47
|
+
@query.add_component(component)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
#
|
53
|
+
# DynamicFieldFactory implements the part of the Setup interface that we
|
54
|
+
# need, so methods in DynamicQuery's superclasses can rely on it without
|
55
|
+
# knowing what it is.
|
56
|
+
#
|
57
|
+
def setup
|
58
|
+
@dynamic_field_factory
|
59
|
+
end
|
60
|
+
|
61
|
+
#
|
62
|
+
# So query facets can be added to the query from within dynamic queries
|
63
|
+
#
|
64
|
+
def query_facets
|
65
|
+
@query.query_facets
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module Sunspot
|
4
|
+
module Query
|
5
|
+
#
|
6
|
+
# Encapsulates a query component representing a field facet. Users create
|
7
|
+
# instances using DSL::Query#facet
|
8
|
+
#
|
9
|
+
class FieldFacet #:nodoc:
|
10
|
+
class <<self
|
11
|
+
protected :new
|
12
|
+
|
13
|
+
#
|
14
|
+
# Return the appropriate FieldFacet instance for the field and options.
|
15
|
+
# If a :time_range option is specified, and the field type is TimeType,
|
16
|
+
# build a DateFieldFacet. Otherwise, build a normal FieldFacet.
|
17
|
+
#
|
18
|
+
# ==== Returns
|
19
|
+
#
|
20
|
+
# FieldFacet:: FieldFacet instance of appropriate class.
|
21
|
+
#
|
22
|
+
def build(field, options)
|
23
|
+
if options.has_key?(:time_range)
|
24
|
+
unless field.type == Type::TimeType
|
25
|
+
raise(
|
26
|
+
ArgumentError,
|
27
|
+
":time_range key can only be specified for time fields"
|
28
|
+
)
|
29
|
+
end
|
30
|
+
DateFieldFacet.new(field, options)
|
31
|
+
else
|
32
|
+
FieldFacet.new(field, options)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize(field, options)
|
38
|
+
@field, @options = field, options
|
39
|
+
end
|
40
|
+
|
41
|
+
# ==== Returns
|
42
|
+
#
|
43
|
+
# Hash:: solr-ruby params for this field facet
|
44
|
+
#
|
45
|
+
def to_params
|
46
|
+
params = { :"facet.field" => [@field.indexed_name], :facet => 'true' }
|
47
|
+
params[param_key(:sort)] =
|
48
|
+
case @options[:sort]
|
49
|
+
when :count then 'true'
|
50
|
+
when :index then 'false'
|
51
|
+
when nil
|
52
|
+
else raise(ArgumentError, 'Allowed facet sort options are :count and :index')
|
53
|
+
end
|
54
|
+
params[param_key(:limit)] = @options[:limit]
|
55
|
+
params[param_key(:mincount)] =
|
56
|
+
if @options[:minimum_count] then @options[:minimum_count]
|
57
|
+
elsif @options[:zeros] then 0
|
58
|
+
else 1
|
59
|
+
end
|
60
|
+
params
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
#
|
66
|
+
# Given a facet parameter name, return the appropriate Solr parameter for
|
67
|
+
# this facet.
|
68
|
+
#
|
69
|
+
# ==== Returns
|
70
|
+
#
|
71
|
+
# Symbol:: Solr query parameter key
|
72
|
+
#
|
73
|
+
def param_key(name)
|
74
|
+
:"f.#{@field.indexed_name}.facet.#{name}"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
class DateFieldFacet < FieldFacet #:nodoc:
|
79
|
+
ALLOWED_OTHER = Set.new(%w(before after between none all))
|
80
|
+
|
81
|
+
#
|
82
|
+
# Convert the facet to date params.
|
83
|
+
#
|
84
|
+
def to_params
|
85
|
+
super.merge(
|
86
|
+
:"facet.date" => [@field.indexed_name],
|
87
|
+
param_key('date.start') => start_time.utc.xmlschema,
|
88
|
+
param_key('date.end') => end_time.utc.xmlschema,
|
89
|
+
param_key('date.gap') => "+#{interval}SECONDS",
|
90
|
+
param_key('date.other') => others
|
91
|
+
)
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
#
|
97
|
+
# Start time for facet range
|
98
|
+
#
|
99
|
+
# ==== Returns
|
100
|
+
#
|
101
|
+
# Time:: Start time
|
102
|
+
#
|
103
|
+
def start_time
|
104
|
+
@options[:time_range].first
|
105
|
+
end
|
106
|
+
|
107
|
+
#
|
108
|
+
# End time for facet range
|
109
|
+
#
|
110
|
+
# ==== Returns
|
111
|
+
#
|
112
|
+
# Time:: End time
|
113
|
+
#
|
114
|
+
def end_time
|
115
|
+
@options[:time_range].last
|
116
|
+
end
|
117
|
+
|
118
|
+
#
|
119
|
+
# Time interval that each facet row should cover. Default is 1 day.
|
120
|
+
#
|
121
|
+
# ===== Returns
|
122
|
+
#
|
123
|
+
# Integer:: Time interval in seconds
|
124
|
+
#
|
125
|
+
def interval
|
126
|
+
@options[:time_interval] || 86400
|
127
|
+
end
|
128
|
+
|
129
|
+
#
|
130
|
+
# Other time ranges to create facet rows for. Allowed values are defined
|
131
|
+
# in ALLOWED_OTHER constant.
|
132
|
+
#
|
133
|
+
def others
|
134
|
+
if others = @options[:time_other]
|
135
|
+
Array(others).map do |other|
|
136
|
+
other = other.to_s
|
137
|
+
unless ALLOWED_OTHER.include?(other)
|
138
|
+
raise(
|
139
|
+
ArgumentError,
|
140
|
+
"#{other.inspect} is not a valid argument for :time_other"
|
141
|
+
)
|
142
|
+
end
|
143
|
+
other
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
#
|
4
|
+
# This class acts as a base class for query components that encapsulate
|
5
|
+
# operations on fields. It is subclassed by the Query::Query class and the
|
6
|
+
# Query::DynamicQuery class.
|
7
|
+
#
|
8
|
+
class FieldQuery < Scope
|
9
|
+
#
|
10
|
+
# Add a field facet. See Sunspot::Facet for more information.
|
11
|
+
#
|
12
|
+
# ==== Parameters
|
13
|
+
#
|
14
|
+
# field_name<Symbol>:: Name of the field on which to get a facet
|
15
|
+
#
|
16
|
+
# ==== Returns
|
17
|
+
#
|
18
|
+
# FieldFacet:: The field facet object
|
19
|
+
#
|
20
|
+
def add_field_facet(field_name, options = nil)
|
21
|
+
add_component(FieldFacet.build(build_field(field_name), options || {}))
|
22
|
+
end
|
23
|
+
|
24
|
+
#
|
25
|
+
# Add a query facet.
|
26
|
+
#
|
27
|
+
# ==== Parameters
|
28
|
+
#
|
29
|
+
# name<Symbol>::
|
30
|
+
# The name associated with the query facet. This is not passed to Solr,
|
31
|
+
# but allows the user to retrieve the facet result by passing the name
|
32
|
+
# to the Search#facet method.
|
33
|
+
#
|
34
|
+
# ==== Returns
|
35
|
+
#
|
36
|
+
# QueryFacet:: The query facet object
|
37
|
+
#
|
38
|
+
def add_query_facet(name)
|
39
|
+
add_component(facet = QueryFacet.new(name, setup))
|
40
|
+
query_facets[name.to_sym] = facet
|
41
|
+
facet
|
42
|
+
end
|
43
|
+
|
44
|
+
#
|
45
|
+
# Set result ordering.
|
46
|
+
#
|
47
|
+
# ==== Parameters
|
48
|
+
#
|
49
|
+
# field_name<Symbol>:: Name of the field on which to order
|
50
|
+
# direction<Symbol>:: :asc or :desc (default :asc)
|
51
|
+
#
|
52
|
+
def order_by(field_name, direction = nil)
|
53
|
+
add_sort(Sort.new(build_field(field_name), direction))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
#
|
4
|
+
# A query component that holds information about pagination. Unlike other
|
5
|
+
# query components, this one is mutable, because the query itself holds a
|
6
|
+
# reference to it and updates it if pagination is changed.
|
7
|
+
#
|
8
|
+
class Pagination #:nodoc:
|
9
|
+
attr_reader :page, :per_page
|
10
|
+
|
11
|
+
def initialize(configuration, page = nil, per_page = nil)
|
12
|
+
@configuration = configuration
|
13
|
+
self.page, self.per_page = page, per_page
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_params
|
17
|
+
{ :start => start, :rows => rows }
|
18
|
+
end
|
19
|
+
|
20
|
+
def page=(page)
|
21
|
+
@page = page || 1
|
22
|
+
end
|
23
|
+
|
24
|
+
def per_page=(per_page)
|
25
|
+
@per_page = per_page || @configuration.pagination.default_per_page
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def start
|
31
|
+
(@page - 1) * @per_page
|
32
|
+
end
|
33
|
+
|
34
|
+
def rows
|
35
|
+
@per_page
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|