Skip to content

Commit

Permalink
Merge branch 'default_create_query'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Donnell committed Nov 30, 2009
2 parents d36592f + 46e2726 commit f0c8a72
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
15 changes: 9 additions & 6 deletions README
Expand Up @@ -5,19 +5,22 @@ Moonstone is a JRuby wrapper around Java's Lucene search engine toolkit.
== Goals

* Engines: An engine abstraction that brings all common index and search operations under a single roof.
* Lucene Transparency: a transparent JRuby wrapper around Lucene
* Rubify Lucene: higher level classes that simplify use of Lucene constructs in Ruby
* Lucene Transparency: A transparent JRuby wrapper around Lucene which allows clear and direct access to the underlying lucene classes.
* Rubify Lucene: Higher level classes that simplify and Rubify the use of Lucene.

== Engines

Using moonstone is simple.

engine = Moonstone::Engine.new
# note: you can't use symbols for the keys
engine.index([{'title' => 'my blog post', 'content' => "blah blah blah"}])
q = Lucene::Search::TermQuery.new("title", "blog")
documents = engine.search(q)
documents.each {|d| puts d['name']}
engine.index([{'title' => 'first post', 'content' => "blah blah blah"}])
# this sets the field that is searched by default
engine.default_query_parser('title')
documents = engine.search('first')
documents.each {|d| puts d['title']}
# to search on a different field
engine.search("content:blah")

A typical engine will override two things; the doc_from method and the analyzer.

Expand Down
11 changes: 6 additions & 5 deletions lib/moonstone/engine.rb
Expand Up @@ -150,8 +150,10 @@ def close
@reader = nil
end

#def create_query(query_string)
#end
def create_query(query_string)
raise "no default query parser" unless @default_query_parser
@default_query_parser.parse(query_string)
end

# Returns an instance of the Analyzer class defined within
# this class's namespace.
Expand Down Expand Up @@ -191,9 +193,8 @@ def reader
reader.close
end

def parser(field, analyzer = nil)
@parser ||= {}
@parser[field.to_sym] ||= Lucene::QueryParser::Parser.new(field, analyzer || self.analyzer)
def default_query_parser(field, analyzer = nil)
@default_query_parser ||= Lucene::QueryParser::Parser.new(field, analyzer || self.analyzer)
end

def inspect_mode?
Expand Down
7 changes: 6 additions & 1 deletion test/moonstone/engine_test.rb
Expand Up @@ -46,7 +46,12 @@ def create_query(string)
engine.index(@some_docs)
engine.reader do |r|
r.terms.for_field('name').sort.should == %w{ burger king depeche mode }.sort
end
end

engine.default_query_parser("name")
results = engine.search("burger")
results[0]["name"].should == "Burger King"
results.length.should == 1
end

it "initializes with an options hash" do
Expand Down

0 comments on commit f0c8a72

Please sign in to comment.