benjaminkrause-sunspot 0.9.7
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +107 -0
- data/LICENSE +18 -0
- data/README.rdoc +159 -0
- data/Rakefile +9 -0
- data/TODO +11 -0
- data/VERSION.yml +4 -0
- data/bin/sunspot-configure-solr +46 -0
- data/bin/sunspot-solr +86 -0
- data/lib/light_config.rb +40 -0
- data/lib/sunspot/adapters.rb +265 -0
- data/lib/sunspot/composite_setup.rb +184 -0
- data/lib/sunspot/configuration.rb +49 -0
- data/lib/sunspot/data_extractor.rb +50 -0
- data/lib/sunspot/dsl/field_query.rb +77 -0
- data/lib/sunspot/dsl/fields.rb +95 -0
- data/lib/sunspot/dsl/fulltext.rb +106 -0
- data/lib/sunspot/dsl/query.rb +107 -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 +4 -0
- data/lib/sunspot/facet.rb +24 -0
- data/lib/sunspot/facet_data.rb +152 -0
- data/lib/sunspot/facet_row.rb +12 -0
- data/lib/sunspot/field.rb +148 -0
- data/lib/sunspot/field_factory.rb +141 -0
- data/lib/sunspot/indexer.rb +129 -0
- data/lib/sunspot/instantiated_facet.rb +45 -0
- data/lib/sunspot/instantiated_facet_row.rb +27 -0
- data/lib/sunspot/query/base_query.rb +55 -0
- data/lib/sunspot/query/boost_query.rb +20 -0
- data/lib/sunspot/query/connective.rb +148 -0
- data/lib/sunspot/query/dynamic_query.rb +61 -0
- data/lib/sunspot/query/field_facet.rb +129 -0
- data/lib/sunspot/query/field_query.rb +69 -0
- data/lib/sunspot/query/fulltext_base_query.rb +86 -0
- data/lib/sunspot/query/highlighting.rb +36 -0
- data/lib/sunspot/query/local.rb +24 -0
- data/lib/sunspot/query/pagination.rb +39 -0
- data/lib/sunspot/query/query_facet.rb +78 -0
- data/lib/sunspot/query/query_facet_row.rb +19 -0
- data/lib/sunspot/query/query_field_facet.rb +20 -0
- data/lib/sunspot/query/restriction.rb +272 -0
- data/lib/sunspot/query/scope.rb +185 -0
- data/lib/sunspot/query/sort.rb +105 -0
- data/lib/sunspot/query/sort_composite.rb +33 -0
- data/lib/sunspot/query/text_field_boost.rb +15 -0
- data/lib/sunspot/query.rb +108 -0
- data/lib/sunspot/schema.rb +147 -0
- data/lib/sunspot/search/highlight.rb +38 -0
- data/lib/sunspot/search/hit.rb +113 -0
- data/lib/sunspot/search.rb +240 -0
- data/lib/sunspot/session.rb +206 -0
- data/lib/sunspot/setup.rb +312 -0
- data/lib/sunspot/text_field_setup.rb +29 -0
- data/lib/sunspot/type.rb +200 -0
- data/lib/sunspot/util.rb +190 -0
- data/lib/sunspot.rb +459 -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 +64 -0
- data/solr/solr/conf/solrconfig.xml +726 -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/indexer/attributes_spec.rb +100 -0
- data/spec/api/indexer/batch_spec.rb +46 -0
- data/spec/api/indexer/dynamic_fields_spec.rb +33 -0
- data/spec/api/indexer/fixed_fields_spec.rb +57 -0
- data/spec/api/indexer/fulltext_spec.rb +43 -0
- data/spec/api/indexer/removal_spec.rb +46 -0
- data/spec/api/indexer/spec_helper.rb +1 -0
- data/spec/api/indexer_spec.rb +4 -0
- data/spec/api/query/connectives_spec.rb +161 -0
- data/spec/api/query/dsl_spec.rb +12 -0
- data/spec/api/query/dynamic_fields_spec.rb +148 -0
- data/spec/api/query/faceting_spec.rb +272 -0
- data/spec/api/query/fulltext_spec.rb +152 -0
- data/spec/api/query/highlighting_spec.rb +82 -0
- data/spec/api/query/local_spec.rb +37 -0
- data/spec/api/query/ordering_pagination_spec.rb +95 -0
- data/spec/api/query/scope_spec.rb +253 -0
- data/spec/api/query/spec_helper.rb +1 -0
- data/spec/api/query/text_field_scoping_spec.rb +30 -0
- data/spec/api/query/types_spec.rb +20 -0
- data/spec/api/search/dynamic_fields_spec.rb +27 -0
- data/spec/api/search/faceting_spec.rb +206 -0
- data/spec/api/search/highlighting_spec.rb +65 -0
- data/spec/api/search/hits_spec.rb +62 -0
- data/spec/api/search/results_spec.rb +52 -0
- data/spec/api/search/search_spec.rb +11 -0
- data/spec/api/search/spec_helper.rb +1 -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/helpers/indexer_helper.rb +29 -0
- data/spec/helpers/query_helper.rb +13 -0
- data/spec/helpers/search_helper.rb +78 -0
- data/spec/integration/dynamic_fields_spec.rb +55 -0
- data/spec/integration/faceting_spec.rb +169 -0
- data/spec/integration/highlighting_spec.rb +22 -0
- data/spec/integration/keyword_search_spec.rb +148 -0
- data/spec/integration/local_search_spec.rb +47 -0
- data/spec/integration/scoped_search_spec.rb +303 -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 +75 -0
- data/spec/mocks/super_class.rb +2 -0
- data/spec/mocks/user.rb +8 -0
- data/spec/spec_helper.rb +60 -0
- data/tasks/gemspec.rake +35 -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.erb +36 -0
- metadata +312 -0
@@ -0,0 +1,20 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
#
|
4
|
+
# Representation of a BoostQuery, which allows the searcher to specify a
|
5
|
+
# scope for which matching documents should have an extra boost. This is
|
6
|
+
# essentially a conjunction, with an extra instance variable containing
|
7
|
+
# the boost that should be applied.
|
8
|
+
#
|
9
|
+
class BoostQuery < Connective::Conjunction #:nodoc:
|
10
|
+
def initialize(boost, setup)
|
11
|
+
super(setup)
|
12
|
+
@boost = boost
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_boolean_phrase
|
16
|
+
"#{super}^#{@boost}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
module Connective #:nodoc:all
|
4
|
+
#
|
5
|
+
# Base class for connectives (conjunctions and disjunctions).
|
6
|
+
#
|
7
|
+
class Abstract < Scope
|
8
|
+
def initialize(setup, negated = false) #:nodoc:
|
9
|
+
super(setup)
|
10
|
+
@negated = negated
|
11
|
+
end
|
12
|
+
|
13
|
+
#
|
14
|
+
# Connective as solr params.
|
15
|
+
#
|
16
|
+
def to_params #:nodoc:
|
17
|
+
if boolean_phrase = to_boolean_phrase
|
18
|
+
{ :fq => to_boolean_phrase }
|
19
|
+
else
|
20
|
+
{}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
#
|
25
|
+
# Express the connective as a Lucene boolean phrase.
|
26
|
+
#
|
27
|
+
def to_boolean_phrase #:nodoc:
|
28
|
+
unless @components.empty?
|
29
|
+
phrase =
|
30
|
+
if @components.length == 1
|
31
|
+
@components.first.to_boolean_phrase
|
32
|
+
else
|
33
|
+
component_phrases = @components.map do |component|
|
34
|
+
component.to_boolean_phrase
|
35
|
+
end
|
36
|
+
"(#{component_phrases.join(" #{connector} ")})"
|
37
|
+
end
|
38
|
+
if negated?
|
39
|
+
"-#{phrase}"
|
40
|
+
else
|
41
|
+
phrase
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# Connectives can be negated during the process of denormalization that
|
48
|
+
# is performed when a disjunction contains a negated component. This
|
49
|
+
# method conforms to the duck type for all boolean query components.
|
50
|
+
#
|
51
|
+
def negated?
|
52
|
+
@negated
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
# Returns a new connective that's a negated version of this one.
|
57
|
+
#
|
58
|
+
def negate
|
59
|
+
negated = self.class.new(@setup, !negated?)
|
60
|
+
for component in @components
|
61
|
+
negated.add_component(component)
|
62
|
+
end
|
63
|
+
negated
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
# Disjunctions combine their components with an OR operator.
|
69
|
+
#
|
70
|
+
class Disjunction < Abstract
|
71
|
+
class <<self
|
72
|
+
def inverse
|
73
|
+
Conjunction
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
#
|
78
|
+
# Express this disjunction as a Lucene boolean phrase
|
79
|
+
#
|
80
|
+
def to_boolean_phrase
|
81
|
+
if @components.any? { |component| component.negated? }
|
82
|
+
denormalize.to_boolean_phrase
|
83
|
+
else
|
84
|
+
super
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
#
|
89
|
+
# Add a conjunction to the disjunction. This overrides the method in
|
90
|
+
# the Scope class since scopes are implicitly conjunctive and thus
|
91
|
+
# can return themselves as a conjunction. Inside a disjunction, however,
|
92
|
+
# a conjunction must explicitly be created.
|
93
|
+
#
|
94
|
+
def add_conjunction
|
95
|
+
@components << conjunction = Conjunction.new(@setup)
|
96
|
+
conjunction
|
97
|
+
end
|
98
|
+
|
99
|
+
#
|
100
|
+
# No-op - this is already a disjunction
|
101
|
+
#
|
102
|
+
def add_disjunction
|
103
|
+
self
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
|
108
|
+
def connector
|
109
|
+
'OR'
|
110
|
+
end
|
111
|
+
|
112
|
+
#
|
113
|
+
# If a disjunction contains negated components, it must be
|
114
|
+
# "denormalized", because the Lucene parser interprets any negated
|
115
|
+
# boolean phrase using AND semantics (this isn't a bug, it's just a
|
116
|
+
# subtlety of how Lucene parses queries). So, per DeMorgan's law we
|
117
|
+
# create a negated conjunction and add to it all of our components,
|
118
|
+
# negated themselves, which creates a query whose Lucene semantics are
|
119
|
+
# in line with our intentions.
|
120
|
+
#
|
121
|
+
def denormalize
|
122
|
+
denormalized = self.class.inverse.new(@setup, !negated?)
|
123
|
+
for component in @components
|
124
|
+
denormalized.add_component(component.negate)
|
125
|
+
end
|
126
|
+
denormalized
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
#
|
131
|
+
# Conjunctions combine their components with an AND operator.
|
132
|
+
#
|
133
|
+
class Conjunction < Abstract
|
134
|
+
class <<self
|
135
|
+
def inverse
|
136
|
+
Disjunction
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
private
|
141
|
+
|
142
|
+
def connector
|
143
|
+
'AND'
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
@@ -0,0 +1,61 @@
|
|
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 #:nodoc:
|
16
|
+
def initialize(dynamic_field_factory, query) #:nodoc:
|
17
|
+
super(dynamic_field_factory)
|
18
|
+
@query = query
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# This has the same effect as calling Query#exclude_instance; it is
|
23
|
+
# included for interface completeness.
|
24
|
+
#
|
25
|
+
def exclude_instance(instance)
|
26
|
+
@query.exclude_instance(instance)
|
27
|
+
end
|
28
|
+
|
29
|
+
#
|
30
|
+
# This has the same effect as calling Query#exclude_instance; it is
|
31
|
+
# included for interface completeness.
|
32
|
+
#
|
33
|
+
def dynamic_query(field_name)
|
34
|
+
@query.dynamic_query(field_name)
|
35
|
+
end
|
36
|
+
|
37
|
+
#
|
38
|
+
# Add a Sort to the query
|
39
|
+
#
|
40
|
+
def add_sort(sort) #:nodoc:
|
41
|
+
@query.add_sort(sort)
|
42
|
+
end
|
43
|
+
|
44
|
+
#
|
45
|
+
# Add a component to the query
|
46
|
+
#
|
47
|
+
def add_component(component) #:nodoc:
|
48
|
+
@query.add_component(component)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
#
|
54
|
+
# So query facets can be added to the query from within dynamic queries
|
55
|
+
#
|
56
|
+
def query_facets
|
57
|
+
@query.query_facets
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,129 @@
|
|
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
|
+
elsif options.has_key?(:only)
|
32
|
+
QueryFieldFacet.new(field, options.delete(:only))
|
33
|
+
else
|
34
|
+
FieldFacet.new(field, options)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def initialize(field, options)
|
40
|
+
@field, @options = field, options
|
41
|
+
end
|
42
|
+
|
43
|
+
# ==== Returns
|
44
|
+
#
|
45
|
+
# Hash:: solr-ruby params for this field facet
|
46
|
+
#
|
47
|
+
def to_params
|
48
|
+
params = { :"facet.field" => [@field.indexed_name], :facet => 'true' }
|
49
|
+
params[param_key(:sort)] =
|
50
|
+
case @options[:sort]
|
51
|
+
when :count then 'true'
|
52
|
+
when :index then 'false'
|
53
|
+
when nil
|
54
|
+
else raise(ArgumentError, 'Allowed facet sort options are :count and :index')
|
55
|
+
end
|
56
|
+
params[param_key(:limit)] = @options[:limit]
|
57
|
+
params[param_key(:mincount)] =
|
58
|
+
if @options[:minimum_count] then @options[:minimum_count]
|
59
|
+
elsif @options[:zeros] then 0
|
60
|
+
else 1
|
61
|
+
end
|
62
|
+
params
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
#
|
68
|
+
# Given a facet parameter name, return the appropriate Solr parameter for
|
69
|
+
# this facet.
|
70
|
+
#
|
71
|
+
# ==== Returns
|
72
|
+
#
|
73
|
+
# Symbol:: Solr query parameter key
|
74
|
+
#
|
75
|
+
def param_key(name)
|
76
|
+
:"f.#{@field.indexed_name}.facet.#{name}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
class DateFieldFacet < FieldFacet #:nodoc:
|
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
|
+
)
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
#
|
96
|
+
# Start time for facet range
|
97
|
+
#
|
98
|
+
# ==== Returns
|
99
|
+
#
|
100
|
+
# Time:: Start time
|
101
|
+
#
|
102
|
+
def start_time
|
103
|
+
@options[:time_range].first
|
104
|
+
end
|
105
|
+
|
106
|
+
#
|
107
|
+
# End time for facet range
|
108
|
+
#
|
109
|
+
# ==== Returns
|
110
|
+
#
|
111
|
+
# Time:: End time
|
112
|
+
#
|
113
|
+
def end_time
|
114
|
+
@options[:time_range].last
|
115
|
+
end
|
116
|
+
|
117
|
+
#
|
118
|
+
# Time interval that each facet row should cover. Default is 1 day.
|
119
|
+
#
|
120
|
+
# ===== Returns
|
121
|
+
#
|
122
|
+
# Integer:: Time interval in seconds
|
123
|
+
#
|
124
|
+
def interval
|
125
|
+
@options[:time_interval] || 86400
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,69 @@
|
|
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 #:nodoc:
|
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
|
+
options ||= {}
|
22
|
+
facet =
|
23
|
+
if only = options.delete(:only)
|
24
|
+
query_facets[field_name.to_sym] = QueryFieldFacet.new(@setup.field(field_name), only, options)
|
25
|
+
else
|
26
|
+
FieldFacet.build(build_field(field_name), options)
|
27
|
+
end
|
28
|
+
add_component(facet)
|
29
|
+
end
|
30
|
+
|
31
|
+
#
|
32
|
+
# Add a query facet.
|
33
|
+
#
|
34
|
+
# ==== Parameters
|
35
|
+
#
|
36
|
+
# name<Symbol>::
|
37
|
+
# The name associated with the query facet. This is not passed to Solr,
|
38
|
+
# but allows the user to retrieve the facet result by passing the name
|
39
|
+
# to the Search#facet method.
|
40
|
+
#
|
41
|
+
# ==== Returns
|
42
|
+
#
|
43
|
+
# QueryFacet:: The query facet object
|
44
|
+
#
|
45
|
+
def add_query_facet(name, options)
|
46
|
+
add_component(facet = QueryFacet.new(name, options, @setup))
|
47
|
+
query_facets[name.to_sym] = facet
|
48
|
+
end
|
49
|
+
|
50
|
+
#
|
51
|
+
# Set result ordering.
|
52
|
+
#
|
53
|
+
# ==== Parameters
|
54
|
+
#
|
55
|
+
# field_name<Symbol>:: Name of the field on which to order
|
56
|
+
# direction<Symbol>:: :asc or :desc (default :asc)
|
57
|
+
#
|
58
|
+
def order_by(field_name, direction = nil)
|
59
|
+
sort =
|
60
|
+
if special = Sort.special(field_name)
|
61
|
+
special.new(direction)
|
62
|
+
else
|
63
|
+
Sort::FieldSort.new(build_field(field_name), direction)
|
64
|
+
end
|
65
|
+
add_sort(sort)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
class FulltextBaseQuery < BaseQuery #:nodoc:
|
4
|
+
def initialize(keywords, options, types, setup)
|
5
|
+
super(types, setup)
|
6
|
+
@keywords = keywords
|
7
|
+
if highlight_options = options.delete(:highlight)
|
8
|
+
if highlight_options == true
|
9
|
+
set_highlight
|
10
|
+
else
|
11
|
+
set_highlight(highlight_options)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
if fulltext_fields = options.delete(:fields)
|
15
|
+
Array(fulltext_fields).each do |field|
|
16
|
+
add_fulltext_field(field)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_params
|
22
|
+
params = { :q => @keywords }
|
23
|
+
params[:fl] = '* score'
|
24
|
+
params[:fq] = types_phrase
|
25
|
+
params[:qf] = query_fields
|
26
|
+
params[:defType] = 'dismax'
|
27
|
+
if @phrase_fields
|
28
|
+
params[:pf] = @phrase_fields.map { |field| field.to_boosted_field }.join(' ')
|
29
|
+
end
|
30
|
+
if @boost_query
|
31
|
+
params[:bq] = @boost_query.to_boolean_phrase
|
32
|
+
end
|
33
|
+
if @highlight
|
34
|
+
Sunspot::Util.deep_merge!(params, @highlight.to_params)
|
35
|
+
end
|
36
|
+
params
|
37
|
+
end
|
38
|
+
|
39
|
+
def add_fulltext_field(field_name, boost = nil)
|
40
|
+
@fulltext_fields ||= []
|
41
|
+
@fulltext_fields.concat(
|
42
|
+
@setup.text_fields(field_name).map do |field|
|
43
|
+
TextFieldBoost.new(field, boost)
|
44
|
+
end
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
48
|
+
def add_phrase_field(field_name, boost = nil)
|
49
|
+
@phrase_fields ||= []
|
50
|
+
@phrase_fields.concat(
|
51
|
+
@setup.text_fields(field_name).map do |field|
|
52
|
+
TextFieldBoost.new(field, boost)
|
53
|
+
end
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
def create_boost_query(factor)
|
58
|
+
@boost_query ||= BoostQuery.new(factor, @setup)
|
59
|
+
end
|
60
|
+
|
61
|
+
def set_highlight(options = {})
|
62
|
+
@highlight = Highlighting.new(options)
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
#
|
68
|
+
# Returns the names of text fields that should be queried in a keyword
|
69
|
+
# search. If specific fields are requested, use those; otherwise use the
|
70
|
+
# union of all fields configured for the types under search.
|
71
|
+
#
|
72
|
+
def query_fields
|
73
|
+
@query_fields ||=
|
74
|
+
begin
|
75
|
+
fulltext_fields =
|
76
|
+
@fulltext_fields || @setup.all_text_fields.map do |field|
|
77
|
+
TextFieldBoost.new(field)
|
78
|
+
end
|
79
|
+
fulltext_fields.map do |fulltext_field|
|
80
|
+
fulltext_field.to_boosted_field
|
81
|
+
end.join(' ')
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
#
|
4
|
+
# A query component that builds parameters for requesting highlights
|
5
|
+
#
|
6
|
+
class Highlighting #:nodoc:
|
7
|
+
def initialize(options)
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_params
|
12
|
+
params = {
|
13
|
+
:hl => 'on',
|
14
|
+
:"hl.simple.pre" => '@@@hl@@@',
|
15
|
+
:"hl.simple.post" => '@@@endhl@@@'
|
16
|
+
}
|
17
|
+
if max_snippets = @options[:max_snippets]
|
18
|
+
params[:"hl.snippets"] = max_snippets
|
19
|
+
end
|
20
|
+
if fragment_size = @options[:fragment_size]
|
21
|
+
params[:"hl.fragsize"] = fragment_size
|
22
|
+
end
|
23
|
+
if @options[:merge_continuous_fragments]
|
24
|
+
params[:"hl.mergeContinuous"] = 'true'
|
25
|
+
end
|
26
|
+
if @options[:phrase_highlighter]
|
27
|
+
params[:"hl.usePhraseHighlighter"] = 'true'
|
28
|
+
if @options[:require_field_match]
|
29
|
+
params[:"hl.requireFieldMatch"] = 'true'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
params
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
#
|
4
|
+
# This query component generates parameters for LocalSolr geo-radial
|
5
|
+
# searches. The LocalSolr API is fairly rigid, so the Local component
|
6
|
+
# doesn't have any options - it just takes coordinates and a radius, and
|
7
|
+
# generates the appropriate parameters.
|
8
|
+
#
|
9
|
+
class Local #:nodoc:
|
10
|
+
def initialize(coordinates, radius)
|
11
|
+
@coordinates, @radius = Util::Coordinates.new(coordinates), radius
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_params
|
15
|
+
{
|
16
|
+
:qt => 'geo',
|
17
|
+
:lat => @coordinates.lat,
|
18
|
+
:long => @coordinates.lng,
|
19
|
+
:radius => @radius
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
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).to_i
|
22
|
+
end
|
23
|
+
|
24
|
+
def per_page=(per_page)
|
25
|
+
@per_page = (per_page || @configuration.pagination.default_per_page).to_i
|
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
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Query
|
3
|
+
#
|
4
|
+
# QueryFacets encapsulate requests for Sunspot's query faceting capability.
|
5
|
+
# They are created by the FieldQuery#add_query_facet method.
|
6
|
+
#
|
7
|
+
#--
|
8
|
+
#
|
9
|
+
# The actual concept of a QueryFacet is somewhat artificial - it provides a
|
10
|
+
# grouping for the facet at the Sunspot level, which provides a nicer and
|
11
|
+
# more consistent API in Sunspot; Solr does not provide any grouping for
|
12
|
+
# query facet rows, instead returning each requested row individually, keyed
|
13
|
+
# by the boolean phrase used in the facet query.
|
14
|
+
#
|
15
|
+
class QueryFacet #:nodoc:
|
16
|
+
attr_reader :name #:nodoc:
|
17
|
+
attr_reader :field #:nodoc:
|
18
|
+
attr_reader :options #:nodoc:
|
19
|
+
|
20
|
+
def initialize(name, options, setup = nil) #:nodoc:
|
21
|
+
@name, @options, @setup = name, options, setup
|
22
|
+
@components = []
|
23
|
+
end
|
24
|
+
|
25
|
+
#
|
26
|
+
# Add a QueryFacetRow to this facet. The label argument becomes the value
|
27
|
+
# of the Sunspot::QueryFacetRow object corresponding to this query facet
|
28
|
+
# row.
|
29
|
+
#
|
30
|
+
# ==== Parameters
|
31
|
+
#
|
32
|
+
# label<Object>::
|
33
|
+
# An object that will become the value of the result row. Use whatever
|
34
|
+
# type is most intuitive.
|
35
|
+
#
|
36
|
+
# ==== Returns
|
37
|
+
#
|
38
|
+
# QueryFacetRow:: QueryFacetRow object containing scope for this row
|
39
|
+
#
|
40
|
+
def add_row(label)
|
41
|
+
@components << row = QueryFacetRow.new(label, @setup)
|
42
|
+
row
|
43
|
+
end
|
44
|
+
|
45
|
+
#
|
46
|
+
# Express this query facet as Solr parameters
|
47
|
+
#
|
48
|
+
# ==== Returns
|
49
|
+
#
|
50
|
+
# Hash:: Solr params hash
|
51
|
+
#
|
52
|
+
def to_params #:nodoc:
|
53
|
+
components = @components.map { |component| component.to_boolean_phrase }
|
54
|
+
components.compact!
|
55
|
+
if components.empty?
|
56
|
+
{}
|
57
|
+
else
|
58
|
+
components = components.first if components.length == 1
|
59
|
+
{
|
60
|
+
:facet => 'true',
|
61
|
+
:"facet.query" => components
|
62
|
+
}
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# Get query facet rows (used when constructing results)
|
68
|
+
#
|
69
|
+
# ==== Returns
|
70
|
+
#
|
71
|
+
# Array:: Array of QueryFacetRow objects.
|
72
|
+
#
|
73
|
+
def rows #:nodoc:
|
74
|
+
@components
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|