active_lucene 0.5.2 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rake/gempackagetask'
5
5
  require 'spec/rake/spectask'
6
6
 
7
7
  GEM = "active_lucene"
8
- GEM_VERSION = "0.5.2"
8
+ GEM_VERSION = "0.6"
9
9
  SUMMARY = "ActiveRecord/ActiveModel's like interface for Lucene"
10
10
  AUTHOR = "Diego Carrion"
11
11
  EMAIL = "dc.rec1@gmail.com"
@@ -44,4 +44,4 @@ task :make_spec do
44
44
  File.open("#{GEM}.gemspec", "w") do |file|
45
45
  file.puts spec.to_ruby
46
46
  end
47
- end
47
+ end
@@ -1,36 +1,20 @@
1
- require 'java'
2
- require 'rubygems'
3
1
  require 'active_support'
4
-
5
- Dir[File.expand_path(File.dirname(__FILE__) + "/*.jar")].each { |path| require path.split('/').last.gsub('.jar', '') }
6
- import org.apache.lucene.document.Field
7
- import org.apache.lucene.store.FSDirectory
8
- import org.apache.lucene.index.IndexWriter
9
- import org.apache.lucene.analysis.standard.StandardAnalyzer
10
- import org.apache.lucene.queryParser.standard.StandardQueryParser
11
-
12
- import org.apache.lucene.search.IndexSearcher
13
- import org.apache.lucene.search.BooleanClause
14
- import org.apache.lucene.search.BooleanQuery
15
- import org.apache.lucene.search.MatchAllDocsQuery
16
- import org.apache.lucene.search.WildcardQuery
17
-
18
- import org.apache.lucene.search.highlight.QueryScorer
19
- import org.apache.lucene.search.highlight.Highlighter
20
-
21
- import org.apache.lucene.util.Version
2
+ require 'java_classes'
22
3
 
23
4
  if defined? RAILS_ROOT
24
5
  APP_ROOT = RAILS_ROOT
25
6
  APP_ENV = RAILS_ENV
26
- else
27
- APP_ROOT ||= '.'
28
- APP_ENV ||= 'default'
7
+ elsif not defined? APP_ROOT
8
+ APP_ROOT = '.'
9
+ APP_ENV = 'default'
29
10
  end
30
11
 
31
- %w(analyzer document index query searcher term writer).each { |name| require "active_lucene/#{name}" }
12
+ %w(analyzer document index index/reader index/writer index/searcher dictionary query search_result suggest term).each do |name|
13
+ require "active_lucene/#{name}"
14
+ end
32
15
 
33
16
  module ActiveLucene
34
17
  ID = 'id'
35
18
  ALL = '_all'
36
- end
19
+ TYPE = '_type'
20
+ end
@@ -0,0 +1,9 @@
1
+ module ActiveLucene
2
+ module Dictionary
3
+ PATH = "#{Index::PATH}/dictionary"
4
+
5
+ def directory
6
+ FSDirectory.open java.io.File.new(PATH)
7
+ end
8
+ end
9
+ end
@@ -19,11 +19,12 @@ module ActiveLucene
19
19
  end
20
20
  document.add Field.new ID, @id, Field::Store::YES, Field::Index::NOT_ANALYZED
21
21
  document.add Field.new ALL, _all.join(' '), Field::Store::NO, Field::Index::ANALYZED
22
- Writer.write document
22
+ document.add Field.new TYPE, self.class.to_s, Field::Store::YES, Field::Index::NOT_ANALYZED
23
+ Index::Writer.write document
23
24
  end
24
25
 
25
26
  def destroy
26
- Writer.delete @id
27
+ Index::Writer.delete @id
27
28
  end
28
29
 
29
30
  def update_attributes(new_attributes)
@@ -35,31 +36,36 @@ module ActiveLucene
35
36
  def to_param
36
37
  @id
37
38
  end
39
+
40
+ def self.all(opts = {})
41
+ find :all, opts
42
+ end
38
43
 
39
- def self.create!(attributes)
44
+ def self.create!(attributes = {})
40
45
  returning new(attributes) do |model|
41
46
  model.save
42
47
  end
43
48
  end
44
49
 
45
- def self.find(param)
50
+ def self.find(param, opts = {})
46
51
  if param.instance_of? Symbol
47
- search :all
52
+ search :all, opts
48
53
  else
49
54
  search(:id => param).first
50
55
  end
51
56
  end
52
57
 
53
- def self.search(param)
54
- search = Searcher.search(param)
55
- results = search.attributes.map { |attributes| new attributes }
56
- results.instance_eval "def total_pages; #{search.total_pages}; end;"
57
- results
58
+ def self.search(param, opts = {})
59
+ Index::Searcher.search(param, opts)
58
60
  end
59
61
 
60
62
  def self.columns
61
63
  []
62
64
  end
65
+
66
+ def self.paginate(opts)
67
+ all opts
68
+ end
63
69
 
64
70
  private
65
71
 
@@ -2,8 +2,8 @@ module ActiveLucene
2
2
  module Index
3
3
  PATH = "#{APP_ROOT}/db/lucene/#{APP_ENV}"
4
4
 
5
- def directory
5
+ def self.directory
6
6
  FSDirectory.open java.io.File.new(PATH)
7
7
  end
8
8
  end
9
- end
9
+ end
@@ -0,0 +1,9 @@
1
+ module ActiveLucene
2
+ module Index
3
+ class Reader < IndexReader
4
+ def self.open
5
+ super Index.directory
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,34 @@
1
+ module ActiveLucene
2
+ module Index
3
+ class Searcher < IndexSearcher
4
+ def self.search(param, opts = {})
5
+ new.search param, opts
6
+ end
7
+
8
+ def initialize
9
+ super Index.directory, true
10
+ end
11
+
12
+ def search(param, opts)
13
+ page = (opts[:page] || 1).to_i
14
+ query = Query.for(param)
15
+ highlighter = Highlighter.new(QueryScorer.new(query))
16
+ top_docs = super(query, nil, page * Document::PER_PAGE)
17
+ score_docs = top_docs.scoreDocs
18
+ returning SearchResult.new(param) do |search_result|
19
+ score_docs[(page - 1) * Document::PER_PAGE ... score_docs.size].each do |score_doc|
20
+ attributes = {}
21
+ doc(score_doc.doc).fields.each do |field|
22
+ attributes.store field.name, field.string_value
23
+ highlight = highlighter.get_best_fragment(Analyzer.new, ALL, field.string_value)
24
+ attributes[:highlight] = highlight if highlight
25
+ end
26
+ search_result.add_document attributes
27
+ end
28
+ search_result.current_page = page
29
+ search_result.total_pages = (top_docs.totalHits / Document::PER_PAGE.to_f).ceil
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ module ActiveLucene
2
+ module Index
3
+ class Writer < IndexWriter
4
+ def initialize
5
+ overwrite = Dir[PATH + '/*'].size < 1
6
+ super Index.directory, Analyzer.new, overwrite, IndexWriter::MaxFieldLength::UNLIMITED
7
+ yield self
8
+ close
9
+ end
10
+
11
+ def self.write(document)
12
+ new do |index|
13
+ index.add_document document
14
+ end
15
+ end
16
+
17
+ def self.delete(id)
18
+ new do |index|
19
+ index.delete_documents ActiveLucene::Term.for(ID, id)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,32 @@
1
+ module ActiveLucene
2
+ class SearchResult < Array
3
+ include Dictionary
4
+
5
+ attr_reader :query
6
+ attr_accessor :total_pages, :current_page
7
+
8
+ def initialize(query)
9
+ @query = query
10
+ end
11
+
12
+ def add_document(attributes)
13
+ self << eval(attributes.delete(TYPE)).new(attributes)
14
+ end
15
+
16
+ def suggest
17
+ spell_checker = SpellChecker.new directory
18
+ spell_checker.index_dictionary LuceneDictionary.new(Index::Reader.open, ALL)
19
+ query.split(' ').map do |word|
20
+ spell_checker.suggest_similar(word, 1).first || word
21
+ end.join(' ')
22
+ end
23
+
24
+ def previous_page
25
+ @current_page - 1
26
+ end
27
+
28
+ def next_page
29
+ @current_page + 1
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveLucene
2
+ class Suggest
3
+ end
4
+ end
@@ -0,0 +1,28 @@
1
+ Dir[File.expand_path(File.dirname(__FILE__) + "/*.jar")].each do |path|
2
+ require path.split('/').last.gsub('.jar', '')
3
+ end
4
+
5
+ import org.apache.lucene.document.Field
6
+
7
+ import org.apache.lucene.store.FSDirectory
8
+
9
+ import org.apache.lucene.index.IndexReader
10
+ import org.apache.lucene.index.IndexWriter
11
+
12
+ import org.apache.lucene.analysis.standard.StandardAnalyzer
13
+
14
+ import org.apache.lucene.queryParser.standard.StandardQueryParser
15
+
16
+ import org.apache.lucene.search.IndexSearcher
17
+ import org.apache.lucene.search.BooleanClause
18
+ import org.apache.lucene.search.BooleanQuery
19
+ import org.apache.lucene.search.MatchAllDocsQuery
20
+ import org.apache.lucene.search.WildcardQuery
21
+
22
+ import org.apache.lucene.search.spell.LuceneDictionary;
23
+ import org.apache.lucene.search.spell.SpellChecker;
24
+
25
+ import org.apache.lucene.search.highlight.QueryScorer
26
+ import org.apache.lucene.search.highlight.Highlighter
27
+
28
+ import org.apache.lucene.util.Version
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_lucene
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: "0.6"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diego Carrion
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-26 00:00:00 -02:00
12
+ date: 2010-02-07 00:00:00 -02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -22,20 +22,26 @@ extensions: []
22
22
  extra_rdoc_files: []
23
23
 
24
24
  files:
25
- - lib/active_lucene.rb
26
- - lib/lucene-highlighter-3.0.0.jar
27
- - lib/lucene-queryparser-3.0.0.jar
28
- - lib/lucene-core-3.0.0.jar
29
- - lib/lucene-memory-3.0.0.jar
30
25
  - lib/active_lucene/analyzer.rb
31
- - lib/active_lucene/writer.rb
26
+ - lib/active_lucene/dictionary.rb
32
27
  - lib/active_lucene/document.rb
28
+ - lib/active_lucene/index/reader.rb
29
+ - lib/active_lucene/index/searcher.rb
30
+ - lib/active_lucene/index/writer.rb
33
31
  - lib/active_lucene/index.rb
34
- - lib/active_lucene/searcher.rb
35
32
  - lib/active_lucene/query.rb
33
+ - lib/active_lucene/search_result.rb
34
+ - lib/active_lucene/suggest.rb
36
35
  - lib/active_lucene/term.rb
37
- - README.textile
36
+ - lib/active_lucene.rb
37
+ - lib/java_classes.rb
38
+ - lib/lucene-core-3.0.0.jar
39
+ - lib/lucene-highlighter-3.0.0.jar
40
+ - lib/lucene-memory-3.0.0.jar
41
+ - lib/lucene-queryparser-3.0.0.jar
42
+ - lib/lucene-spellchecker-3.0.0.jar
38
43
  - Rakefile
44
+ - README.textile
39
45
  has_rdoc: true
40
46
  homepage: http://www.diegocarrion.com
41
47
  licenses: []
@@ -1,33 +0,0 @@
1
- module ActiveLucene
2
- class Searcher < IndexSearcher
3
- include Index
4
-
5
- attr_reader :attributes, :total_pages
6
-
7
- def initialize
8
- super directory, true
9
- end
10
-
11
- def self.search(param)
12
- returning new do |searcher|
13
- searcher.search param
14
- end
15
- end
16
-
17
- def search(param)
18
- query = Query.for(param)
19
- top_docs = super(query, nil, Document::PER_PAGE)
20
- @attributes = top_docs.scoreDocs.map do |score_doc|
21
- attributes = {}
22
- doc(score_doc.doc).fields.each do |field|
23
- attributes.store field.name, field.string_value
24
- highlight = Highlighter.new(QueryScorer.new(query)).get_best_fragment(Analyzer.new, ALL, field.string_value)
25
- attributes[:highlight] = highlight if highlight
26
- end
27
- attributes
28
- end
29
- @total_pages = (top_docs.totalHits / Document::PER_PAGE.to_f).ceil
30
- end
31
-
32
- end
33
- end
@@ -1,24 +0,0 @@
1
- module ActiveLucene
2
- class Writer < IndexWriter
3
- include Index
4
-
5
- def initialize
6
- overwrite = Dir[PATH + '/*'].size < 1
7
- super directory, Analyzer.new, overwrite, IndexWriter::MaxFieldLength::UNLIMITED
8
- yield self
9
- close
10
- end
11
-
12
- def self.write(document)
13
- new do |index|
14
- index.add_document document
15
- end
16
- end
17
-
18
- def self.delete(id)
19
- new do |index|
20
- index.delete_documents ActiveLucene::Term.for(ID, id)
21
- end
22
- end
23
- end
24
- end