Skip to content

Commit

Permalink
* fixed the next_id
Browse files Browse the repository at this point in the history
* moved to NIOFSDirectory

* allow fuzzy search to sort by score instead of id

* make full text search instead of equals complete query for non-fuzzy search
  • Loading branch information
mkristian committed Jan 28, 2010
1 parent a84bda5 commit cdc79b7
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 46 deletions.
23 changes: 23 additions & 0 deletions .autotest
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- ruby -*-

require 'autotest/restart'

# Autotest.add_hook :initialize do |at|
# at.extra_files << "../some/external/dependency.rb"
#
# at.libs << ":../some/external"
#
# at.add_exception 'vendor'
#
# at.add_mapping(/dependency.rb/) do |f, _|
# at.files_matching(/test_.*rb$/)
# end
#
# %w(TestA TestB).each do |klass|
# at.extra_class_map[klass] = "test/test_misc.rb"
# end
# end

# Autotest.add_hook :run_command do |at|
# system "rake build"
# end
10 changes: 10 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
=== 0.1.2 / 2010-01-28

* fixed the next_id

* moved to NIOFSDirectory

* allow fuzzy search to sort by score instead of id

* make full text search instead of equals complete query for non-fuzzy search

=== 0.1.1 / 2009-XX-YY

* using new gem-maven-plugin
Expand Down
1 change: 1 addition & 0 deletions lib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dm-lucene-adapter_ext.jar
10 changes: 7 additions & 3 deletions lib/dm_lucene_adapter/dm_lucene_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ def initialize(name, options = {})
# @api semipublic
def create(resources)
count = 0
reader = lucene(resources.first.model).create_reader
indexer = lucene(resources.first.model).create_indexer
resources.each do |resource|
resource.id = indexer.next_id
resource.id = reader.next_id
map = {}
resource.attributes.each { |k,v| map[k.to_s] = v.to_s}
indexer.index(map)
Expand All @@ -38,6 +39,7 @@ def create(resources)
count
ensure
indexer.close if indexer
reader.close if reader
end

# @param [Query] query
Expand Down Expand Up @@ -116,15 +118,17 @@ def make_query(lquery, ops, operator)
ops.each do |comp|
case comp.slug
when :like
comp.value.split(/\s/).each do |value|
comp.value.split(/\s+/).each do |value|
lquery += "#{comp.subject.name.to_s}:#{value}"
unless comp.value =~ /%|_|\?|\*/
lquery += "~"
end
lquery += " #{operator} "
end
when :eql
lquery += "#{comp.subject.name.to_s}:\"#{comp.value}\" #{operator} "
comp.value.to_s.split(/\s+/).each do |value|
lquery += "#{comp.subject.name.to_s}:\"#{value}\" #{operator} "
end
when :not
if lquery.size == 0
lquery = "NOT "
Expand Down
34 changes: 13 additions & 21 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<groupId>rubygems</groupId>
<artifactId>dm-lucene-adapter</artifactId>
<packaging>java-gem</packaging>
<version>0.2.0-SNAPSHOT</version>
<name>dm adapter for lucene</name>
<description>datamapper adapter for search index lucene</description>
<version>0.1.2</version>
<name>datamapper adapter for lucene</name>
<description>datamapper adapter for search index lucene from apache</description>
<url>http://github.com/mkristian/dm-lucene-adapter</url>
<licenses>
<license>
Expand Down Expand Up @@ -58,7 +58,7 @@
<plugin>
<groupId>de.saumya.mojo</groupId>
<artifactId>jruby-maven-plugin</artifactId>
<version>${jruby.maven.plugins}</version>
<version>${jruby.plugins.version}</version>
<executions>
<execution>
<id>spec</id>
Expand All @@ -71,7 +71,7 @@
<plugin>
<groupId>de.saumya.mojo</groupId>
<artifactId>gem-maven-plugin</artifactId>
<version>0.7.0</version>
<version>${jruby.plugins.version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
Expand All @@ -84,7 +84,7 @@
<plugin>
<groupId>de.saumya.mojo</groupId>
<artifactId>gem-maven-plugin</artifactId>
<version>${jruby.maven.plugins}</version>
<version>${jruby.plugins.version}</version>
<extensions>true</extensions>
<executions>
<execution>
Expand Down Expand Up @@ -148,23 +148,15 @@

<profile>
<id>localgems</id>
<build>
<plugins>
<plugin>
<groupId>de.saumya.mojo</groupId>
<artifactId>gem-maven-plugin</artifactId>
<version>${jruby.maven.plugins}</version>
<extensions>true</extensions>
<configuration>
<gemHome>${project.build.directory}/rubygems</gemHome>
<gemPath>${project.build.directory}/rubygems</gemPath>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<!-- this allows to configure it for rails-maven-plugin as well jruby-maven-plugin and gem-maven-plugin -->
<jruby.gem.home>${project.build.directory}/rubygems</jruby.gem.home>
<jruby.gem.path>${project.build.directory}/rubygems</jruby.gem.path>
<jruby.fork>true</jruby.fork>
</properties>
</profile>
</profiles>
<properties>
<jruby.maven.plugins>0.8.0-SNAPSHOT</jruby.maven.plugins>
<jruby.plugins.version>0.8.0</jruby.plugins.version>
</properties>
</project>
27 changes: 27 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
SNAPSHOT=$1
VERSION=$2
NEXT=$3
if [ "x" = "x"$2 ] ; then
echo "usage: $0 SNAPHOT_VERSION VERSION NEXT_VERSION"
exit 1
fi

mvn clean install
mvn3 -P maven3 clean install
jruby -S rake clean compile spec

find -name "pom.xml" | xargs sed -i s/${SNAPSHOT}-SNAPSHOT/${VERSION}/
find -name "pom.xml" | xargs git add
git ci -m "release of version ${VERSION}" || exit
git tag v${VERSION}

mvn clean deploy

find -name "pom.xml" | xargs sed -i s/${VERSION}/${NEXT}-SNAPSHOT/

find -name "pom.xml" | xargs git add
git ci -m "next snapshot version ${NEXT}"

mvn install

git push --tags origin master
6 changes: 4 additions & 2 deletions spec/dm_lucene_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
end
b = Book.all
size.should == b.size + 2
book = Book.create(:author => "kristian", :title => "me and the corner")
id.should < book.id
book = Book.create(:author => "kristian 2", :title => "me and the corner 2")
id.should == book.id
b = Book.all
size.should == b.size + 1
end

it 'should read a single' do
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/de/saumya/lucene/LuceneIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ public class LuceneIndexer {
this.reader = reader;
}

public int nextId() throws IOException {
// TODO use fieldselector
final int max = this.reader.maxDoc();
return max == 0 ? 1 : Integer.parseInt(this.reader.document(max - 1)
.getField("id")
.stringValue()) + 1;
}

public void index(final Map<String, String> resource)
throws CorruptIndexException, IOException {
final Document document = new Document();
Expand Down
59 changes: 50 additions & 9 deletions src/main/java/de/saumya/lucene/LuceneReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.document.FieldSelectorResult;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.StaleReaderException;
import org.apache.lucene.index.Term;
Expand Down Expand Up @@ -58,22 +60,58 @@ public Collection<Map<String, String>> readAll(final int offset,
final int limit) throws StaleReaderException,
CorruptIndexException, LockObtainFailedException, IOException {
final Query query = new WildcardQuery(new Term("id", "*"));
return readAll(offset, limit, query);
return readAll(offset, limit, query, false);
}

private Collection<Map<String, String>> readAll(final int offset,
int limit, final Query query) throws IOException,
CorruptIndexException {
final TopDocs docs = this.searcher.search(query,
public int nextId() throws IOException {
final TopDocs docs = this.searcher.search(new WildcardQuery(new Term("id",
"*")),
null,
1000000,
1,
new Sort(new SortField("id",
SortField.INT)));
SortField.INT,
true)));

final Document doc = this.searcher.doc(docs.scoreDocs[0].doc,
new FieldSelector() {

private static final long serialVersionUID = 1L;

@Override
public FieldSelectorResult accept(
final String fieldName) {
return "id".equals(fieldName)
? FieldSelectorResult.LOAD
: FieldSelectorResult.NO_LOAD;
}
});

return Integer.parseInt(doc.getField("id").stringValue()) + 1;
}

private Collection<Map<String, String>> readAll(final int offset,
int limit, final Query query, final boolean fuzzy)
throws IOException, CorruptIndexException {
int size = limit + offset;
size = size < 1 ? 100000 : size;
final TopDocs docs;

if (fuzzy) {
docs = this.searcher.search(query, size);
}
else {
docs = this.searcher.search(query,
null,
size,
new Sort(new SortField("id",
SortField.INT)));
}
final List<Map<String, String>> result = new ArrayList<Map<String, String>>();
int index = 0;
for (final ScoreDoc sdoc : docs.scoreDocs) {
if (index >= offset) {
final Map<String, String> map = new HashMap<String, String>();
map.put("score", "" + sdoc.score);
final Document doc = this.searcher.doc(sdoc.doc);
for (final Object o : doc.getFields()) {
final Field f = (Field) o;
Expand Down Expand Up @@ -102,10 +140,13 @@ public Collection<Map<String, String>> readAll(final int offset,
final BooleanQuery query2 = new BooleanQuery();
query2.add(query3, Occur.MUST);
query2.add(parser.parse(query.substring(4)), Occur.MUST_NOT);
return readAll(offset, limit, query2);
return readAll(offset, limit, query2, query.contains("~"));
}
else {
return readAll(offset, limit, parser.parse(query));
return readAll(offset,
limit,
parser.parse(query),
query.contains("~"));
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/de/saumya/lucene/LuceneService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.NIOFSDirectory;
import org.apache.lucene.util.Version;

public class LuceneService {
Expand All @@ -20,7 +21,7 @@ public LuceneService(final File indexDir) {
}

public LuceneIndexer createIndexer() throws IOException {
return new LuceneIndexer(new IndexWriter(FSDirectory.open(this.indexDir),
return new LuceneIndexer(new IndexWriter(NIOFSDirectory.open(this.indexDir),
new StandardAnalyzer(Version.LUCENE_CURRENT),
!this.indexDir.exists(),
IndexWriter.MaxFieldLength.LIMITED),
Expand All @@ -29,13 +30,13 @@ public LuceneIndexer createIndexer() throws IOException {

public LuceneReader createReader() throws CorruptIndexException,
IOException {
return new LuceneReader(new IndexSearcher(FSDirectory.open(this.indexDir),
return new LuceneReader(new IndexSearcher(NIOFSDirectory.open(this.indexDir),
true));
}

public LuceneDeleter createDeleter() throws CorruptIndexException,
IOException {
return new LuceneDeleter(IndexReader.open(FSDirectory.open(this.indexDir),
return new LuceneDeleter(IndexReader.open(NIOFSDirectory.open(this.indexDir),
false));
}
}

0 comments on commit cdc79b7

Please sign in to comment.