Permalink
Browse files

Merge branch 'master' into works-br

* master: (36 commits)
  Added test for facet_filter
  Added support for boosting query
  [#472] Added support for loading partial fields
  [#269] Added the `min_score` DSL method
  [#269] Added the `min_score` DSL method
  [TEST] Refactored equality checks in expectations fot the `Index` unit tests
  [#256] Added support for the `parent` URL parameter in `Index#store`
  [REFACTOR] Cleaned up the `Index#store` method
  [#446] Changed, that arbitrary objects passed in the `:as` mapping option are indexed
  Improved support for Kaminari pagination
  Added proper `Results::Collection#slice` implementation
  Added Rake task to generate ENV['COUNT'] records into the template for the Rails example application
  Added `attr_accessible` to Article model in the template for example Rails application
  [FIX] Simplified `method_missing` in `Item`
  [FIX] Implemented `respond_to?` for `Item`
  [FIX] Added, that a warning is displayed when the index cannot be created on class load
  Changed, that "edge" version of Tire is used in the Rails application template
  Exit with an error in the Rake import task when the index as not created in elasticsearch
  Added the ability to pass arguments to the `all` query
  [FIX] [#453] Changed Hash syntax to be Ruby 1.8 compatible in Tire::Model::Persistence::Finders
  ...
  • Loading branch information...
2 parents 2545e0f + 1667737 commit fdd8caffaba6bd1e0a47b293a3e0b2acdf91c359 @NOX73 committed Oct 23, 2012
@@ -88,6 +88,13 @@ def ids(values, type)
@value = { :ids => { :values => values, :type => type } }
end
+ def boosting(options={}, &block)
+ @boosting ||= BoostingQuery.new(options)
+ block.arity < 1 ? @boosting.instance_eval(&block) : block.call(@boosting) if block_given?
+ @value[:boosting] = @boosting.to_hash
+ @value
+ end
+
def to_hash
@value
end
@@ -186,5 +193,27 @@ def to_json(options={})
end
end
+ class BoostingQuery
+ def initialize(options={}, &block)
+ @options = options
+ @value = {}
+ block.arity < 1 ? self.instance_eval(&block) : block.call(self) if block_given?
+ end
+
+ def positive(&block)
+ (@value[:positive] ||= []) << Query.new(&block).to_hash
+ @value
+ end
+
+ def negative(&block)
+ (@value[:negative] ||= []) << Query.new(&block).to_hash
+ @value
+ end
+
+ def to_hash
+ @value.update(@options)
+ end
+ end
+
end
end
@@ -0,0 +1,32 @@
+require 'test_helper'
+
+module Tire
+
+ class BoostingQueriesIntegrationTest < Test::Unit::TestCase
+ include Test::Integration
+
+ context "Boosting queries" do
+
+ should "allow to set multiple queries per condition" do
+ s = Tire.search('articles-test') do
+ query do
+ boosting(:negative_boost => 0.2) do
+ positive do
+ string "Two One"
+ end
+ negative do
+ term :tags, 'python'
+ end
+ end
+ end
+ end
+
+ assert_equal 'One', s.results[0].title
+ assert_equal 'Two', s.results[1].title
+ end
+
+ end
+
+ end
+
+end
@@ -248,6 +248,17 @@ class FacetsIntegrationTest < Test::Unit::TestCase
facets = s.results.facets["filtered"]
assert_equal 2, facets["count"], facets.inspect
end
+ should "return a filtered facet" do
+ s = Tire.search('articles-test', :search_type => 'count') do
+ facet 'filtered' do
+ terms :published_on
+ facet_filter :term, :words => 250
+ end
+ end
+
+ facets = s.results.facets["filtered"]
+ assert_equal 2, facets["total"], facets.inspect
+ end
end
end
@@ -312,5 +312,54 @@ class QueryTest < Test::Unit::TestCase
end
+ context "BoostingQuery" do
+
+ should "not raise an error when no block is given" do
+ assert_nothing_raised { Query.new.boosting }
+ end
+
+ should "encode options" do
+ query = Query.new.boosting(:negative_boost => 0.2) do
+ positive { string 'foo' }
+ end
+
+ assert_equal 0.2, query[:boosting][:negative_boost]
+ end
+
+ should "wrap positive query" do
+ assert_equal( { :boosting => {:positive => [{ :query_string => { :query => 'foo' } }] }},
+ Query.new.boosting { positive { string 'foo' } } )
+ end
+
+ should "wrap negative query" do
+ assert_equal( { :boosting => {:negative => [{ :query_string => { :query => 'foo' } }] }},
+ Query.new.boosting { negative { string 'foo' } } )
+ end
+
+ should "wrap multiple queries for the same condition" do
+ query = Query.new.boosting do
+ positive { string 'foo' }
+ positive { term('bar', 'baz') }
+ end
+
+ assert_equal( 2, query[:boosting][:positive].size, query[:boosting][:positive].inspect )
+ assert_equal( { :query_string => {:query => 'foo'} }, query[:boosting][:positive].first )
+ assert_equal( { :term => { "bar" => { :term => "baz" } } }, query[:boosting][:positive].last )
+ end
+
+ should "allow passing variables from outer scope" do
+ @q1 = 'foo'
+ @q2 = 'bar'
+ query = Query.new.boosting do |boosting|
+ boosting.positive { |query| query.string @q1 }
+ boosting.negative { |query| query.string @q2 }
+ end
+
+ assert_equal( { :query_string => {:query => 'foo'} }, query[:boosting][:positive].first )
+ assert_equal( { :query_string => {:query => 'bar'} }, query[:boosting][:negative].last )
+ end
+
+ end
+
end
end
@@ -490,6 +490,31 @@ def foo; 'bar'; end
end
+ context "boosting queries" do
+
+ should "wrap other queries" do
+ s = Search::Search.new('index') do
+ query do
+ boosting do
+ positive { string 'foo' }
+ positive { term('bar', 'baz') }
+ negative { term('bar', 'moo') }
+ end
+ end
+ end
+
+ hash = MultiJson.decode(s.to_json)
+ query = hash['query']['boosting']
+
+ assert_equal 2, query['positive'].size
+ assert_equal 1, query['negative'].size
+
+ assert_equal( { 'query_string' => { 'query' => 'foo' } }, query['positive'].first)
+ assert_equal( { 'term' => { 'bar' => {'term' => 'moo' } } }, query['negative'].first)
+ end
+
+ end
+
end
context "script field" do

0 comments on commit fdd8caf

Please sign in to comment.