Skip to content

Commit

Permalink
To create field list support in solr sunspot
Browse files Browse the repository at this point in the history
  • Loading branch information
antonyr committed Jun 20, 2016
1 parent 964ecd0 commit 9739b78
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 34 deletions.
24 changes: 19 additions & 5 deletions README.md
Expand Up @@ -89,6 +89,7 @@ Post.search do

with :blog_id, 1
with(:published_at).less_than Time.now
field_list [:blog_id, :title]
order_by :published_at, :desc
paginate :page => 2, :per_page => 15
facet :category_ids, :author_id
Expand Down Expand Up @@ -242,6 +243,19 @@ Post.search do
end
```

#### Restrictions and Field List

```ruby
# Posts with a blog_id of 1
Post.search do
with(:blog_id, 1)
field_list [:title]
end

Post.search do
without(:category_ids, [1, 3])
field_list [:title, :author_id]
end

#### Disjunctions and Conjunctions

Expand Down Expand Up @@ -286,7 +300,7 @@ Post.search do
with(:category_ids, 3)
end
end
any do
all do
fulltext "keyword", :fields => :title
Expand Down Expand Up @@ -381,7 +395,7 @@ end

**Solr 4.7 and above**

With default Solr pagination it may turn that same records appear on different pages (e.g. if
With default Solr pagination it may turn that same records appear on different pages (e.g. if
many records have the same search score). Cursor-based pagination allows to avoid this.

Useful for any kinds of export, infinite scroll, etc.
Expand Down Expand Up @@ -778,7 +792,7 @@ end
PhotoContainer.search do
with(:caption, 'blah')
with(:photos_created).between(Date.new(2011,3,1)..Date.new(2011,4,1))
fulltext("keywords", :fields => [:name, :description])
end
Expand All @@ -787,7 +801,7 @@ end
PhotoContainer.search do
with(:caption, 'blah')
with(:photos_created).between(Date.new(2011,3,1)..Date.new(2011,4,1))
any do
fulltext("keyword1", :fields => :name)
fulltext("keyword2", :fields => :description) # will be joined from the Photo model
Expand Down Expand Up @@ -825,7 +839,7 @@ Profile.search do
fulltext("keyword1 keyword2", :fields => [:tweet_keywords]) do
minimum_match 1
end
fulltext("keyword3", :fields => [:rss_keywords])
end
end
Expand Down
58 changes: 31 additions & 27 deletions sunspot/lib/sunspot/dsl/scope.rb
@@ -1,6 +1,6 @@
module Sunspot
module DSL #:nodoc:
#
#
# This DSL presents methods for constructing restrictions and other query
# elements that are specific to fields. As well as being a superclass of
# Sunspot::DSL::StandardQuery, which presents the main query block, this
Expand All @@ -12,7 +12,12 @@ def initialize(scope, setup) #:nodoc:
@scope, @setup = scope, setup
end

#
# Build a restriction to return only fields of the type in the results.
def field_list(*args)
@query.add_field_list(Sunspot::Query::FieldList.new(args.flatten))
end

#
# Build a positive restriction. This method can take three forms: equality
# restriction, restriction by another restriction, or identity
# restriction.
Expand Down Expand Up @@ -62,7 +67,7 @@ def initialize(scope, setup) #:nodoc:
# Sunspot.search do
# with(:category_ids, [1, 5, 9])
# end
#
#
# Other restriction types:
#
# Sunspot.search(Post) do
Expand All @@ -87,7 +92,7 @@ def without(*args)
add_restriction(true, *args)
end

#
#
# Create a disjunction, scoping the results to documents that match any
# of the enclosed restrictions.
#
Expand All @@ -109,7 +114,7 @@ def any_of(&block)
disjunction
end

#
#
# Create a conjunction, scoping the results to documents that match all of
# the enclosed restrictions. When called from the top level of a search
# block, this has no effect, but can be useful for grouping a conjunction
Expand Down Expand Up @@ -138,9 +143,9 @@ def all_of(&block)
# The block API is implemented by Sunspot::DSL::FieldQuery, which is a
# superclass of the Query DSL (thus providing a subset of the API, in
# particular only methods that refer to particular fields).
#
#
# ==== Parameters
#
#
# base_name<Symbol>:: The base name for the dynamic field definition
#
# ==== Example
Expand All @@ -160,14 +165,14 @@ def dynamic(base_name, &block)
)
end

#
#
# Apply scope-type restrictions on fulltext fields. In certain situations,
# it may be desirable to place logical restrictions on text fields.
# Remember that text fields are tokenized; your mileage may very.
#
# The block works exactly like a normal scope, except that the field names
# refer to text fields instead of attribute fields.
#
#
# === Example
#
# Sunspot.search(Post) do
Expand All @@ -189,26 +194,25 @@ def text_fields(&block)

def add_restriction(negated, *args)
case args.first
when String, Symbol
raise ArgumentError if args.length > 2
field = @setup.field(args[0].to_sym)
if args.length > 1
value = args[1]
@scope.add_shorthand_restriction(negated, field, value)
else # NONE
DSL::Restriction.new(field, @scope, negated)
when String, Symbol
raise ArgumentError if args.length > 2
field = @setup.field(args[0].to_sym)
if args.length > 1
value = args[1]
@scope.add_shorthand_restriction(negated, field, value)
else # NONE
DSL::Restriction.new(field, @scope, negated)
end
else # args are instances
@scope.add_restriction(
negated,
IdField.instance,
Sunspot::Query::Restriction::AnyOf,
args.flatten.map { |instance|
Sunspot::Adapters::InstanceAdapter.adapt(instance).index_id }
)
end
else # args are instances
@scope.add_restriction(
negated,
IdField.instance,
Sunspot::Query::Restriction::AnyOf,
args.flatten.map { |instance|
Sunspot::Adapters::InstanceAdapter.adapt(instance).index_id }
)
end
end

end
end
end
3 changes: 2 additions & 1 deletion sunspot/lib/sunspot/query.rb
@@ -1,4 +1,5 @@
%w(filter abstract_field_facet connective boost_query date_field_facet range_facet abstract_fulltext dismax join
%w(filter abstract_field_facet connective boost_query date_field_facet
range_facet abstract_fulltext dismax join field_list
field_facet highlighting pagination restriction common_query spellcheck
standard_query more_like_this more_like_this_query geo geofilt bbox query_facet
scope sort sort_composite text_field_boost function_query field_stats
Expand Down
5 changes: 5 additions & 0 deletions sunspot/lib/sunspot/query/common_query.rb
Expand Up @@ -23,6 +23,11 @@ def add_sort(sort)
@sort << sort
end

def add_field_list(field_list)
@components << field_list
field_list
end

def add_group(group)
@components << group
group
Expand Down
15 changes: 15 additions & 0 deletions sunspot/lib/sunspot/query/field_list.rb
@@ -0,0 +1,15 @@
module Sunspot
module Query #:nodoc:

# This DSL represents only fields that would come out of the results of the search type.
class FieldList
def initialize(list)
@list = list
end

def to_params
{ :fl => @list }
end
end
end
end
12 changes: 11 additions & 1 deletion sunspot/spec/api/query/dsl_spec.rb
Expand Up @@ -4,9 +4,20 @@
it 'should allow building search using block argument rather than instance_eval' do
@blog_id = 1
session.search Post do |query|
query.field_list [:blog_id, :title]
query.with(:blog_id, @blog_id)
end
connection.should have_last_search_including(:fq, 'blog_id_i:1')
connection.should have_last_search_with(fl: [:blog_id, :title])
end

it 'should allow field_list specified as arguments' do
@blog_id = 1
session.search Post do |query|
query.field_list :blog_id, :title
query.with(:blog_id, @blog_id)
end
connection.should have_last_search_with(fl: [:blog_id, :title])
end

it 'should accept a block in the #new_search method' do
Expand All @@ -15,4 +26,3 @@
connection.should have_last_search_including(:fq, 'blog_id_i:1')
end
end

0 comments on commit 9739b78

Please sign in to comment.