Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* 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
  • Loading branch information...
commit cdc79b7542af43db696b4f7e36ba4088f82d6766 1 parent a84bda5
@mkristian authored
View
23 .autotest
@@ -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
View
10 History.txt
@@ -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
View
1  lib/.gitignore
@@ -0,0 +1 @@
+dm-lucene-adapter_ext.jar
View
10 lib/dm_lucene_adapter/dm_lucene_adapter.rb
@@ -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)
@@ -38,6 +39,7 @@ def create(resources)
count
ensure
indexer.close if indexer
+ reader.close if reader
end
# @param [Query] query
@@ -116,7 +118,7 @@ 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 += "~"
@@ -124,7 +126,9 @@ def make_query(lquery, ops, operator)
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 "
View
34 pom.xml
@@ -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>
@@ -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>
@@ -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>
@@ -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>
@@ -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>
View
27 release.sh
@@ -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
View
6 spec/dm_lucene_adapter_spec.rb
@@ -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
View
8 src/main/java/de/saumya/lucene/LuceneIndexer.java
@@ -23,14 +23,6 @@
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();
View
59 src/main/java/de/saumya/lucene/LuceneReader.java
@@ -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;
@@ -58,22 +60,58 @@
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;
@@ -102,10 +140,13 @@
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("~"));
}
}
View
7 src/main/java/de/saumya/lucene/LuceneService.java
@@ -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 {
@@ -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),
@@ -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));
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.