solr-ruby 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,7 +28,7 @@ class Solr::Connection
28
28
  # conn = Solr::Connection.new('http://example.com:8080/solr',
29
29
  # :autocommit => :on)
30
30
 
31
- def initialize(url, opts={})
31
+ def initialize(url="http://localhost:8983/solr", opts={})
32
32
  @url = URI.parse(url)
33
33
  unless @url.kind_of? URI::HTTP
34
34
  raise "invalid http url: #{url}"
@@ -13,7 +13,7 @@
13
13
  require 'solr'
14
14
 
15
15
  class Solr::Importer::SolrSource
16
- def initialize(solr_url, query, filter_queries, options={})
16
+ def initialize(solr_url, query, filter_queries=nil, options={})
17
17
  @connection = Solr::Connection.new(solr_url)
18
18
  @query = query
19
19
  @filter_queries = filter_queries
@@ -12,6 +12,7 @@
12
12
 
13
13
  module Solr; module Request; end; end
14
14
  require 'solr/request/add_document'
15
+ require 'solr/request/modify_document'
15
16
  require 'solr/request/base'
16
17
  require 'solr/request/commit'
17
18
  require 'solr/request/delete'
@@ -23,7 +23,7 @@ class Solr::Request::AddDocument < Solr::Request::Update
23
23
  #
24
24
  # as a short cut you can pass in a Hash instead:
25
25
  #
26
- # request = Solr::Request.new :creator => 'Jorge Luis Borges'
26
+ # request = Solr::Request::AddDocument.new :creator => 'Jorge Luis Borges'
27
27
  #
28
28
  # or an array, to add multiple documents at the same time:
29
29
  #
@@ -30,7 +30,7 @@ class Solr::Request::Base
30
30
  # respond to this type of request
31
31
 
32
32
  def handler
33
- raise "unkown request type: #{self.class}"
33
+ raise "unknown request type: #{self.class}"
34
34
  end
35
35
 
36
36
  end
@@ -0,0 +1,51 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ require 'solr/xml'
14
+ require 'solr/request/base'
15
+ require 'solr/document'
16
+ require 'solr/request/update'
17
+
18
+ class Solr::Request::ModifyDocument < Solr::Request::Update
19
+
20
+ # Example: ModifyDocument.new(:id => 10, :overwrite => {:field_name => "new value"})
21
+ def initialize(update_data)
22
+ modes = []
23
+ @doc = {}
24
+ [:overwrite, :append, :distinct, :increment, :delete].each do |mode|
25
+ field_data = update_data[mode]
26
+ if field_data
27
+ field_data.each do |field_name, field_value|
28
+ modes << "#{field_name}:#{mode.to_s.upcase}"
29
+ @doc[field_name] = field_value if field_value # if value is nil, omit so it can be removed
30
+ end
31
+ update_data.delete mode
32
+ end
33
+ end
34
+ @mode = modes.join(",")
35
+
36
+ # only one key should be left over, the id
37
+ @doc[update_data.keys[0].to_s] = update_data.values[0]
38
+ end
39
+
40
+ # returns the request as a string suitable for posting
41
+ def to_s
42
+ e = Solr::XML::Element.new 'add'
43
+ e.add_element(Solr::Document.new(@doc).to_xml)
44
+ return e.to_s
45
+ end
46
+
47
+ def handler
48
+ "update?mode=#{@mode}"
49
+ end
50
+
51
+ end
@@ -13,7 +13,7 @@
13
13
  class Solr::Request::Standard < Solr::Request::Select
14
14
 
15
15
  VALID_PARAMS = [:query, :sort, :default_field, :operator, :start, :rows,
16
- :filter_queries, :field_list, :debug_query, :explain_other, :facets, :highlighting]
16
+ :filter_queries, :field_list, :debug_query, :explain_other, :facets, :highlighting, :mlt]
17
17
 
18
18
  def initialize(params)
19
19
  super('standard')
@@ -98,8 +98,21 @@ class Solr::Request::Standard < Solr::Request::Select
98
98
  hash["hl.requireFieldMatch"] = @params[:highlighting][:require_field_match]
99
99
  hash["hl.simple.pre"] = @params[:highlighting][:prefix]
100
100
  hash["hl.simple.post"] = @params[:highlighting][:suffix]
101
+ hash["hl.fragsize"] = @params[:highlighting][:fragment_size]
101
102
  end
102
103
 
104
+ if @params[:mlt]
105
+ hash[:mlt] = true
106
+ hash["mlt.count"] = @params[:mlt][:count]
107
+ hash["mlt.fl"] = @params[:mlt][:field_list].join(',')
108
+ hash["mlt.mintf"] = @params[:mlt][:min_term_freq]
109
+ hash["mlt.mindf"] = @params[:mlt][:min_doc_freq]
110
+ hash["mlt.minwl"] = @params[:mlt][:min_word_length]
111
+ hash["mlt.maxwl"] = @params[:mlt][:max_word_length]
112
+ hash["mlt.maxqt"] = @params[:mlt][:max_query_terms]
113
+ hash["mlt.maxntp"] = @params[:mlt][:max_tokens_parsed]
114
+ hash["mlt.boost"] = @params[:mlt][:boost]
115
+ end
103
116
 
104
117
  hash.merge(super.to_hash)
105
118
  end
@@ -16,6 +16,7 @@ require 'solr/response/xml'
16
16
  require 'solr/response/ruby'
17
17
  require 'solr/response/ping'
18
18
  require 'solr/response/add_document'
19
+ require 'solr/response/modify_document'
19
20
  require 'solr/response/standard'
20
21
  require 'solr/response/dismax'
21
22
  require 'solr/response/commit'
@@ -0,0 +1,17 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Response::ModifyDocument < Solr::Response::Xml
14
+ def initialize(xml)
15
+ super
16
+ end
17
+ end
@@ -36,6 +36,7 @@ class Solr::Response::Standard < Solr::Response::Ruby
36
36
  @response['maxScore']
37
37
  end
38
38
 
39
+ # TODO: consider the use of json.nl parameter
39
40
  def field_facets(field)
40
41
  facets = []
41
42
  values = @data['facet_counts']['facet_fields'][field]
@@ -25,4 +25,8 @@ class Solr::Util
25
25
  Hash[*a]
26
26
  end
27
27
 
28
+ def self.query_parser_escape(string)
29
+ # backslash prefix everything that isn't a word character
30
+ string.gsub(/(\W)/,'\\\\\1')
31
+ end
28
32
  end
@@ -0,0 +1,25 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ require 'test/unit'
14
+ require 'solr'
15
+
16
+ class ModifyDocumentTest < Test::Unit::TestCase
17
+
18
+ def test_update_formatting
19
+ request = Solr::Request::ModifyDocument.new(:id => 10, :overwrite => {:name => ['val1', 'val2'], :copyfield => nil})
20
+ assert_equal :xml, request.response_format
21
+ assert_equal 'update?mode=copyfield:OVERWRITE,name:OVERWRITE', request.handler
22
+
23
+ assert_match(/<add>[\s]*<doc>[\s]*<field name=["']id['"]>10<\/field>[\s]*<field name=['"]name['"]>val1<\/field>[\s]*<field name=['"]name['"]>val2<\/field>[\s]*<\/doc>[\s]*<\/add>/, request.to_s)
24
+ end
25
+ end
@@ -104,7 +104,8 @@ class StandardRequestTest < Test::Unit::TestCase
104
104
  :max_snippets => 3,
105
105
  :require_field_match => true,
106
106
  :prefix => "<blink>",
107
- :suffix => "</blink>"
107
+ :suffix => "</blink>",
108
+ :fragment_size => 300
108
109
  }
109
110
  )
110
111
 
@@ -114,6 +115,31 @@ class StandardRequestTest < Test::Unit::TestCase
114
115
  assert_equal true, hash["hl.requireFieldMatch"]
115
116
  assert_equal "<blink>", hash["hl.simple.pre"]
116
117
  assert_equal "</blink>", hash["hl.simple.post"]
118
+ assert_equal 300, hash["hl.fragsize"]
119
+ end
120
+
121
+ def test_mlt
122
+ request = Solr::Request::Standard.new(:query => 'query',
123
+ :mlt => {
124
+ :count => 5, :field_list => ['field1', 'field2'],
125
+ :min_term_freq => 3, :min_doc_freq => 10,
126
+ :min_word_length => 4, :max_word_length => 17,
127
+ :max_query_terms => 20, :max_tokens_parsed => 100,
128
+ :boost => true
129
+ }
130
+ )
131
+
132
+ hash = request.to_hash
133
+ assert_equal true, hash[:mlt]
134
+ assert_equal 5, hash["mlt.count"]
135
+ assert_equal 'field1,field2', hash["mlt.fl"]
136
+ assert_equal 3, hash["mlt.mintf"]
137
+ assert_equal 10, hash["mlt.mindf"]
138
+ assert_equal 4, hash["mlt.minwl"]
139
+ assert_equal 17, hash["mlt.maxwl"]
140
+ assert_equal 20, hash["mlt.maxqt"]
141
+ assert_equal 100, hash["mlt.maxntp"]
142
+ assert_equal true, hash["mlt.boost"]
117
143
  end
118
144
 
119
145
  end
@@ -14,9 +14,11 @@ require 'solr'
14
14
  require 'test/unit'
15
15
 
16
16
  class UtilTest < Test::Unit::TestCase
17
-
18
17
  def test_paired_array_to_hash
19
18
  assert_equal({:key1 => :value1, :key2 => :value2}, Solr::Util.paired_array_to_hash([:key1, :value1, :key2, :value2]))
20
19
  end
21
20
 
21
+ def test_query_parser_escape
22
+ assert_equal %q(http\:\/\/lucene\.apache\.org\/solr), Solr::Util.query_parser_escape("http://lucene.apache.org/solr")
23
+ end
22
24
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: solr-ruby
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.4
7
- date: 2007-08-16 00:00:00 -04:00
6
+ version: 0.0.5
7
+ date: 2007-08-27 00:00:00 -04:00
8
8
  summary: Ruby library for working with Apache Solr
9
9
  require_paths:
10
10
  - lib
@@ -57,6 +57,7 @@ files:
57
57
  - lib/solr/request/delete.rb
58
58
  - lib/solr/request/dismax.rb
59
59
  - lib/solr/request/index_info.rb
60
+ - lib/solr/request/modify_document.rb
60
61
  - lib/solr/request/optimize.rb
61
62
  - lib/solr/request/ping.rb
62
63
  - lib/solr/request/select.rb
@@ -68,6 +69,7 @@ files:
68
69
  - lib/solr/response/delete.rb
69
70
  - lib/solr/response/dismax.rb
70
71
  - lib/solr/response/index_info.rb
72
+ - lib/solr/response/modify_document.rb
71
73
  - lib/solr/response/optimize.rb
72
74
  - lib/solr/response/ping.rb
73
75
  - lib/solr/response/ruby.rb
@@ -86,6 +88,7 @@ files:
86
88
  - test/unit/hpricot_mapper_test.rb
87
89
  - test/unit/hpricot_test_file.xml
88
90
  - test/unit/indexer_test.rb
91
+ - test/unit/modify_document_test.rb
89
92
  - test/unit/ping_test.rb
90
93
  - test/unit/request_test.rb
91
94
  - test/unit/response_test.rb