2007-01-15 01:41:18 +00:00
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
require 'test/unit'
|
|
|
|
require 'solr'
|
|
|
|
|
|
|
|
class StandardRequestTest < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def test_basic_query
|
|
|
|
request = Solr::Request::Standard.new(:query => 'query')
|
|
|
|
assert_equal :ruby, request.response_format
|
|
|
|
assert_equal 'select', request.handler
|
|
|
|
assert_equal 'query', request.to_hash[:q]
|
|
|
|
assert_match /q=query/, request.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bad_params
|
|
|
|
assert_raise(RuntimeError) do
|
|
|
|
Solr::Request::Standard.new(:foo => "invalid")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise(RuntimeError) do
|
|
|
|
Solr::Request::Standard.new(:query => "valid", :foo => "invalid")
|
|
|
|
end
|
2007-01-21 20:42:14 +00:00
|
|
|
|
|
|
|
assert_raise(RuntimeError) do
|
|
|
|
Solr::Request::Standard.new(:query => "valid", :operator => :bogus)
|
|
|
|
end
|
2007-01-15 01:41:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_common_params
|
|
|
|
request = Solr::Request::Standard.new(:query => 'query', :start => 10, :rows => 50,
|
2007-01-21 20:42:14 +00:00
|
|
|
:filter_queries => ['fq1', 'fq2'], :field_list => ['id','title','score'], :operator => :and)
|
2007-01-15 01:41:18 +00:00
|
|
|
assert_equal 10, request.to_hash[:start]
|
|
|
|
assert_equal 50, request.to_hash[:rows]
|
|
|
|
assert_equal ['fq1','fq2'], request.to_hash[:fq]
|
|
|
|
assert_equal "id,title,score", request.to_hash[:fl]
|
2007-01-24 15:28:17 +00:00
|
|
|
assert_equal "AND", request.to_hash["q.op"]
|
2007-01-15 01:41:18 +00:00
|
|
|
end
|
2007-01-16 21:28:44 +00:00
|
|
|
|
2007-01-17 07:57:48 +00:00
|
|
|
def test_missing_params
|
|
|
|
request = Solr::Request::Standard.new(:query => 'query', :debug_query => false, :facets => {:fields =>[:category_facet]})
|
2007-01-16 21:28:44 +00:00
|
|
|
assert_nil request.to_hash[:rows]
|
|
|
|
assert_no_match /rows/, request.to_s
|
2007-01-17 07:57:48 +00:00
|
|
|
assert_no_match /facet\.sort/, request.to_s
|
2007-01-16 21:28:44 +00:00
|
|
|
assert_match /debugQuery/, request.to_s
|
|
|
|
end
|
2007-01-15 01:41:18 +00:00
|
|
|
|
2007-05-14 09:36:05 +00:00
|
|
|
def test_only_facet_query
|
|
|
|
request = Solr::Request::Standard.new(:query => 'query',
|
|
|
|
:facets => {
|
|
|
|
:queries => ["q1", "q2"],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
hash = request.to_hash
|
|
|
|
assert_equal ["q1", "q2"], hash["facet.query"]
|
|
|
|
end
|
|
|
|
|
2007-01-16 21:28:44 +00:00
|
|
|
def test_facet_params_all
|
2007-01-15 01:41:18 +00:00
|
|
|
request = Solr::Request::Standard.new(:query => 'query',
|
|
|
|
:facets => {
|
2007-01-24 02:59:17 +00:00
|
|
|
:fields => [:genre,
|
|
|
|
# field that overrides the global facet parameters
|
2008-03-01 11:17:28 +00:00
|
|
|
{:year => {:limit => 50, :mincount => 0, :missing => false, :sort => :term, :prefix=>"199", :offset => 7}}],
|
2007-01-15 01:41:18 +00:00
|
|
|
:queries => ["q1", "q2"],
|
2007-01-24 02:59:17 +00:00
|
|
|
:prefix => "cat",
|
2008-03-01 11:17:28 +00:00
|
|
|
:offset => 3, :limit => 5, :zeros => true, :mincount => 20, :sort => :count # global facet parameters
|
2007-01-15 01:41:18 +00:00
|
|
|
}
|
|
|
|
)
|
2007-01-24 02:59:17 +00:00
|
|
|
|
|
|
|
hash = request.to_hash
|
|
|
|
assert_equal true, hash[:facet]
|
2007-01-24 15:28:17 +00:00
|
|
|
assert_equal [:genre, :year], hash["facet.field"]
|
|
|
|
assert_equal ["q1", "q2"], hash["facet.query"]
|
|
|
|
assert_equal 5, hash["facet.limit"]
|
|
|
|
assert_equal 20, hash["facet.mincount"]
|
|
|
|
assert_equal true, hash["facet.sort"]
|
|
|
|
assert_equal "cat", hash["facet.prefix"]
|
|
|
|
assert_equal 50, hash["f.year.facet.limit"]
|
|
|
|
assert_equal 0, hash["f.year.facet.mincount"]
|
|
|
|
assert_equal false, hash["f.year.facet.sort"]
|
|
|
|
assert_equal "199", hash["f.year.facet.prefix"]
|
2008-03-01 11:17:28 +00:00
|
|
|
assert_equal 3, hash["facet.offset"]
|
|
|
|
assert_equal 7, hash["f.year.facet.offset"]
|
2007-01-15 01:41:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_basic_sort
|
|
|
|
request = Solr::Request::Standard.new(:query => 'query', :sort => [{:title => :descending}])
|
|
|
|
assert_equal 'query;title desc', request.to_hash[:q]
|
|
|
|
end
|
2007-02-09 00:39:50 +00:00
|
|
|
|
|
|
|
def test_highlighting
|
|
|
|
request = Solr::Request::Standard.new(:query => 'query',
|
|
|
|
:highlighting => {
|
|
|
|
:field_list => ['title', 'author'],
|
2008-05-17 03:31:18 +00:00
|
|
|
:alternate_fields => {'title'=>'title', 'author'=>'author'},
|
|
|
|
:max_alternate_field_length => {'title'=>30, 'author'=>20},
|
2007-02-09 00:39:50 +00:00
|
|
|
:max_snippets => 3,
|
|
|
|
:require_field_match => true,
|
|
|
|
:prefix => "<blink>",
|
2007-08-22 01:05:55 +00:00
|
|
|
:suffix => "</blink>",
|
|
|
|
:fragment_size => 300
|
2007-02-09 00:39:50 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
hash = request.to_hash
|
|
|
|
assert_equal true, hash[:hl]
|
|
|
|
assert_equal "title,author", hash["hl.fl"]
|
2008-05-17 03:31:18 +00:00
|
|
|
assert_equal "title", hash["f.title.hl.alternateField"]
|
|
|
|
assert_equal "author", hash["f.author.hl.alternateField"]
|
|
|
|
assert_equal 30, hash["f.title.hl.maxAlternateFieldLength"]
|
|
|
|
assert_equal 20, hash["f.author.hl.maxAlternateFieldLength"]
|
2007-02-09 00:39:50 +00:00
|
|
|
assert_equal true, hash["hl.requireFieldMatch"]
|
|
|
|
assert_equal "<blink>", hash["hl.simple.pre"]
|
|
|
|
assert_equal "</blink>", hash["hl.simple.post"]
|
2007-08-22 01:05:55 +00:00
|
|
|
assert_equal 300, hash["hl.fragsize"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mlt
|
|
|
|
request = Solr::Request::Standard.new(:query => 'query',
|
|
|
|
:mlt => {
|
|
|
|
:count => 5, :field_list => ['field1', 'field2'],
|
|
|
|
:min_term_freq => 3, :min_doc_freq => 10,
|
|
|
|
:min_word_length => 4, :max_word_length => 17,
|
|
|
|
:max_query_terms => 20, :max_tokens_parsed => 100,
|
|
|
|
:boost => true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
hash = request.to_hash
|
|
|
|
assert_equal true, hash[:mlt]
|
|
|
|
assert_equal 5, hash["mlt.count"]
|
2007-08-23 12:17:29 +00:00
|
|
|
assert_equal 'field1,field2', hash["mlt.fl"]
|
2007-08-22 01:05:55 +00:00
|
|
|
assert_equal 3, hash["mlt.mintf"]
|
|
|
|
assert_equal 10, hash["mlt.mindf"]
|
|
|
|
assert_equal 4, hash["mlt.minwl"]
|
|
|
|
assert_equal 17, hash["mlt.maxwl"]
|
|
|
|
assert_equal 20, hash["mlt.maxqt"]
|
|
|
|
assert_equal 100, hash["mlt.maxntp"]
|
|
|
|
assert_equal true, hash["mlt.boost"]
|
2007-02-09 00:39:50 +00:00
|
|
|
end
|
2007-01-16 22:02:13 +00:00
|
|
|
|
2007-01-15 01:41:18 +00:00
|
|
|
end
|