Last active
December 9, 2015 18:08
-
-
Save mocobeta/4307903 to your computer and use it in GitHub Desktop.
Lucene indexing sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 以下は、Apache Softoware Licence v2.0 の元にで頒布されているコードに一部改変を加えたものです。 | |
* http://www.apache.org/licenses/LICENSE-2.0.txt | |
*/ | |
import static org.junit.Assert.*; | |
import java.io.IOException; | |
import org.apache.lucene.analysis.core.WhitespaceAnalyzer; | |
import org.apache.lucene.document.Document; | |
import org.apache.lucene.document.StoredField; | |
import org.apache.lucene.document.StringField; | |
import org.apache.lucene.document.Field.Store; | |
import org.apache.lucene.document.TextField; | |
import org.apache.lucene.index.DirectoryReader; | |
import org.apache.lucene.index.IndexReader; | |
import org.apache.lucene.index.IndexWriter; | |
import org.apache.lucene.index.IndexWriterConfig; | |
import org.apache.lucene.index.Term; | |
import org.apache.lucene.search.IndexSearcher; | |
import org.apache.lucene.search.Query; | |
import org.apache.lucene.search.TermQuery; | |
import org.apache.lucene.store.Directory; | |
import org.apache.lucene.store.RAMDirectory; | |
import org.apache.lucene.util.Version; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class IndexingTest { | |
protected String[] ids = { "1", "2" }; | |
protected String[] unindexed = { "Netherlands", "Italy" }; | |
protected String[] unstored = { "Amsterdam has lots of bridges", | |
"Venice has lots of canals" }; | |
protected String[] text = { "Amsterdam", "Venice" }; | |
protected Directory directory; | |
@Before | |
public void setUp() throws Exception { | |
directory = new RAMDirectory(); | |
IndexWriter writer = getWriter(); | |
for (int i = 0; i < ids.length; i++) { | |
Document doc = new Document(); | |
doc.add(new StringField("id", ids[i], Store.YES)); | |
doc.add(new StoredField("country", unindexed[i])); | |
doc.add(new TextField("contents", unstored[i], Store.NO)); | |
doc.add(new StringField("city", text[i], Store.YES)); | |
writer.addDocument(doc); | |
} | |
writer.close(); | |
} | |
private IndexWriter getWriter() throws IOException { | |
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, | |
new WhitespaceAnalyzer(Version.LUCENE_40)); | |
return new IndexWriter(directory, config); | |
} | |
protected int getHitCount(String fieldName, String searchString) throws IOException { | |
IndexReader reader = DirectoryReader.open(directory); | |
IndexSearcher searcher = new IndexSearcher(reader); | |
Term t = new Term(fieldName, searchString); | |
Query query = new TermQuery(t); | |
int hitCount = searcher.search(query, 1).totalHits; | |
reader.close(); | |
return hitCount; | |
} | |
@Test | |
public void testIndexWriter() throws IOException { | |
IndexWriter writer = getWriter(); | |
assertEquals(ids.length, writer.numDocs()); | |
writer.close(); | |
} | |
@Test | |
public void testIndexReader() throws IOException { | |
IndexReader reader = DirectoryReader.open(directory); | |
assertEquals(ids.length, reader.maxDoc()); | |
assertEquals(ids.length, reader.numDocs()); | |
reader.close(); | |
} | |
@Test | |
public void testDelete() throws IOException { | |
IndexWriter writer = getWriter(); | |
assertEquals(2, writer.numDocs()); | |
writer.deleteDocuments(new Term("id", "1")); | |
writer.commit(); | |
assertTrue(writer.hasDeletions()); | |
assertEquals(2, writer.maxDoc()); | |
assertEquals(1, writer.numDocs()); | |
writer.close(); | |
} | |
@Test | |
public void testUpdate() throws IOException { | |
assertEquals(1, getHitCount("city", "Amsterdam")); | |
IndexWriter writer = getWriter(); | |
Document doc = new Document(); | |
doc.add(new StringField("id", "1", Store.YES)); | |
doc.add(new StoredField("country", "Netherlands")); | |
doc.add(new TextField("contents", "Den Haag has a lot of museums", Store.NO)); | |
doc.add(new StringField("city", "Den Haag", Store.YES)); | |
writer.updateDocument(new Term("id", "1"), doc); | |
writer.commit(); | |
writer.close(); | |
assertEquals(0, getHitCount("city", "Amsterdam")); | |
assertEquals(1, getHitCount("city", "Den Haag")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment