Index: src/java/org/apache/lucene/demo/core/SimpleCoreExample.java =================================================================== --- src/java/org/apache/lucene/demo/core/SimpleCoreExample.java (revision 0) +++ src/java/org/apache/lucene/demo/core/SimpleCoreExample.java (working copy) @@ -0,0 +1,117 @@ +package org.apache.lucene.demo.core; + +import java.io.IOException; + +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.demo.DemoConstants; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.TextField; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.StoredDocument; +import org.apache.lucene.queryparser.classic.ParseException; +import org.apache.lucene.queryparser.classic.QueryParser; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.TopDocs; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.RAMDirectory; + +/** + * + * @author mandy + * + */ +public class SimpleCoreExample { + + private final Directory indexDir = new RAMDirectory(); + + /** + * Empty Constructor + */ + public SimpleCoreExample() { + + } + + private void addDocs(IndexWriter indexWriter, String value) throws IOException { + // Add fields to document + Document doc = new Document(); + doc.add(new TextField("Contents", value, Field.Store.YES)); + + // Create index + indexWriter.addDocument(doc); + } + + /** + * Create Index + * + * @throws IOException + */ + public void index() throws IOException { + // Create index writer + IndexWriterConfig config = new IndexWriterConfig(DemoConstants.DEMO_VER, new StandardAnalyzer(DemoConstants.DEMO_VER)); + IndexWriter indexWriter = new IndexWriter(indexDir, config); + + addDocs(indexWriter, "Welcome to Apache Lucene, high-performance, full-featured text search engine library written entirely in Java"); + addDocs(indexWriter, "Lucene is an Opensource project"); + addDocs(indexWriter, "This is Lucene Core Demo"); + addDocs(indexWriter, "Apache Lucene"); + addDocs(indexWriter, "Lucene Indexing & Searching APIs"); + + indexWriter.close(); + } + + /** + * Search for custom query + * + * @param keyword + * @return no. of hits + * @throws IOException + * @throws ParseException + */ + public int search(String keyword) throws IOException, ParseException { + // Create index searcher + DirectoryReader indexReader = DirectoryReader.open(indexDir); + IndexSearcher indexSearcher = new IndexSearcher(indexReader); + + // Build a query + QueryParser parser = new QueryParser(DemoConstants.DEMO_VER, "Contents", new StandardAnalyzer(DemoConstants.DEMO_VER)); + Query query = parser.parse(keyword); + + TopDocs topDocs = indexSearcher.search(query, 6); + + // Iterate the score docs + ScoreDoc[] hits = topDocs.scoreDocs; + System.out.println("No. of Hits ---> " + hits.length); + if (hits.length > 0) { + System.out.println("Search result for *" + keyword + "* --->"); + for (int i = 0; i < hits.length; i++) { + int docID = hits[i].doc; + StoredDocument doc = indexSearcher.doc(docID); + System.out.println(doc.get("Contents")); + } + } else { + System.out.println("No Results for... " + keyword); + } + + return hits.length; + } + + /** + * Runs simple example for index creation & search + * + * @param args + * @throws Exception + */ + public static void main(String args[]) throws Exception { + SimpleCoreExample example = new SimpleCoreExample(); + // Create index + example.index(); + // Invoke custom search + example.search("Apache"); + } + +} Index: src/java/org/apache/lucene/demo/DemoConstants.java =================================================================== --- src/java/org/apache/lucene/demo/DemoConstants.java (revision 0) +++ src/java/org/apache/lucene/demo/DemoConstants.java (working copy) @@ -0,0 +1,15 @@ +package org.apache.lucene.demo; + +import org.apache.lucene.util.Version; + +/** + * + * @author mandy + * + */ +public class DemoConstants { + + /** The Lucene {@link Version} used by the example code. */ + public static final Version DEMO_VER = Version.LUCENE_50; + +} Index: src/test/org/apache/lucene/demo/core/TestSimpleCoreExample.java =================================================================== --- src/test/org/apache/lucene/demo/core/TestSimpleCoreExample.java (revision 0) +++ src/test/org/apache/lucene/demo/core/TestSimpleCoreExample.java (working copy) @@ -0,0 +1,36 @@ +package org.apache.lucene.demo.core; + +import org.apache.lucene.demo.core.SimpleCoreExample; +import org.apache.lucene.util.LuceneTestCase; + +/** + * + * @author mandy + * + */ +public class TestSimpleCoreExample extends LuceneTestCase { + + SimpleCoreExample example; + + private void assertExpectedCounts(String keyword, int expectedHitCount) throws Exception { + assertEquals("Count", expectedHitCount, example.search(keyword)); + } + + /** + * Simple test + * + * @throws Exception + */ + public void testSimple() throws Exception { + example = new SimpleCoreExample(); + // Create index + example.index(); + + // Test no. of Hits + assertExpectedCounts("Apache", 2); + assertExpectedCounts("Lucene", 5); + assertExpectedCounts("Opensource", 1); + assertExpectedCounts("Search", 1); + } + +}