Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Thiti V. Sintopchai http://thitiv.blogspot.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Java: Lucene: Simple Indexing and Searching Example (from javadoc)

   1  
   2  public void simpleLucene()
   3  {
   4      Analyzer analyzer = new StandardAnalyzer();
   5  
   6      // Store the index in memory:
   7      Directory directory = new RAMDirectory();
   8  
   9      // To store an index on disk, use this instead (note that the 
  10      // parameter true will overwrite the index in that directory
  11      // if one exists):
  12      // Directory directory = FSDirectory.getDirectory("/tmp/testindex", true);
  13  
  14      IndexWriter iwriter = new IndexWriter(directory, analyzer, true);
  15      iwriter.setMaxFieldLength(25000);
  16  
  17      Document doc = new Document();
  18      String text = "This is the text to be indexed.";
  19      doc.add(new Field("fieldname", text, Field.Store.YES,
  20          Field.Index.TOKENIZED));
  21      iwriter.addDocument(doc);
  22      iwriter.close();
  23      
  24      // Now search the index:
  25      IndexSearcher isearcher = new IndexSearcher(directory);
  26  
  27      // Parse a simple query that searches for "text":
  28      QueryParser parser = new QueryParser("fieldname", analyzer);
  29      Query query = parser.parse("text");
  30      Hits hits = isearcher.search(query);
  31      assertEquals(1, hits.length());
  32  
  33      // Iterate through the results:
  34      for (int i = 0; i < hits.length(); i++)
  35      {
  36        Document hitDoc = hits.doc(i);
  37        assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));
  38      }
  39  
  40      isearcher.close();
  41      directory.close();
  42  }

Java: Lucene: Simple In-Memory Search Example

// Adapted from http://javatechniques.com/blog/lucene-in-memory-text-search-example
// Works with present APIs in Lucene 2.1.0

   1  
   2  /**
   3   * A simple example of an in-memory search using Lucene.
   4   */
   5  import java.io.IOException;
   6  import java.io.StringReader;
   7  
   8  import org.apache.lucene.search.Hits;
   9  import org.apache.lucene.search.Query;
  10  import org.apache.lucene.document.Field;
  11  import org.apache.lucene.search.Searcher;
  12  import org.apache.lucene.index.IndexWriter;
  13  import org.apache.lucene.document.Document;
  14  import org.apache.lucene.store.RAMDirectory;
  15  import org.apache.lucene.search.IndexSearcher;
  16  import org.apache.lucene.queryParser.QueryParser;
  17  import org.apache.lucene.queryParser.ParseException;
  18  import org.apache.lucene.analysis.standard.StandardAnalyzer;
  19  
  20  public class InMemoryExample
  21  {
  22  
  23    public static void main(String[] args)
  24    {
  25      // Construct a RAMDirectory to hold the in-memory representation
  26      // of the index.
  27      RAMDirectory idx = new RAMDirectory();
  28  
  29      try
  30      {
  31        // Make an writer to create the index
  32        IndexWriter writer = new IndexWriter(idx, new StandardAnalyzer(), true);
  33  
  34        // Add some Document objects containing quotes
  35        writer.addDocument(createDocument("Theodore Roosevelt",
  36            "It behooves every man to remember that the work of the "
  37                + "critic, is of altogether secondary importance, and that, "
  38                + "in the end, progress is accomplished by the man who does "
  39                + "things."));
  40        writer.addDocument(createDocument("Friedrich Hayek",
  41            "The case for individual freedom rests largely on the "
  42                + "recognition of the inevitable and universal ignorance "
  43                + "of all of us concerning a great many of the factors on "
  44                + "which the achievements of our ends and welfare depend."));
  45        writer.addDocument(createDocument("Ayn Rand",
  46            "There is nothing to take a man’s freedom away from "
  47                + "him, save other men. To be free, a man must be free "
  48                + "of his brothers."));
  49        writer.addDocument(createDocument("Mohandas Gandhi",
  50            "Freedom is not worth having if it does not connote "
  51                + "freedom to err."));
  52  
  53        // Optimize and close the writer to finish building the index
  54        writer.optimize();
  55        writer.close();
  56  
  57        // Build an IndexSearcher using the in-memory index
  58        Searcher searcher = new IndexSearcher(idx);
  59  
  60        // Run some queries
  61        search(searcher, "freedom");
  62        search(searcher, "free");
  63        search(searcher, "progress or achievements");
  64  
  65        searcher.close();
  66      }
  67      catch (IOException ioe)
  68      {
  69        // In this example we aren’t really doing an I/O, so this
  70        // exception should never actually be thrown.
  71        ioe.printStackTrace();
  72      }
  73      catch (ParseException pe)
  74      {
  75        pe.printStackTrace();
  76      }
  77    }
  78  
  79    /**
  80     * Make a Document object with an un-indexed title field and an indexed
  81     * content field.
  82     */
  83    private static Document createDocument(String title, String content)
  84    {
  85      Document doc = new Document();
  86  
  87      // Add the title as an unindexed field…
  88      doc.add(new Field("title", title, Field.Store.YES, Field.Index.NO));
  89  
  90      //and the content as an indexed field. Note that indexed
  91      // Text fields are constructed using a Reader. Lucene can read
  92      // and index very large chunks of text, without storing the
  93      // entire content verbatim in the index. In this example we
  94      // can just wrap the content string in a StringReader.
  95      doc.add(new Field("content", new StringReader(content)));
  96  
  97      return doc;
  98    }
  99  
 100    /**
 101     * Searches for the given string in the "content" field
 102     */
 103    private static void search(Searcher searcher, String queryString)
 104        throws ParseException, IOException
 105    {
 106  
 107      // Build a Query object
 108      QueryParser parser = new QueryParser("content", new StandardAnalyzer());
 109      Query query = parser.parse(queryString);
 110  
 111      // Search for the query
 112      Hits hits = searcher.search(query);
 113  
 114      // Examine the Hits object to see if there were any matches
 115      int hitCount = hits.length();
 116      if (hitCount == 0)
 117      {
 118        System.out.println("No matches were found for \"" + queryString + "\"");
 119      }
 120      else
 121      {
 122        System.out.println("Hits for \"" + queryString
 123            + "\" were found in quotes by:");
 124  
 125        // Iterate over the Documents in the Hits object
 126        for (int i = 0; i < hitCount; i++)
 127        {
 128          Document doc = hits.doc(i);
 129  
 130          // Print the value that we stored in the "title" field. Note
 131          // that this Field was not indexed, but (unlike the
 132          // "contents" field) was stored verbatim and can be
 133          // retrieved.
 134          System.out.println(" " + (i + 1) + ". " + doc.get("title"));
 135        }
 136      }
 137      System.out.println();
 138    }
 139  }
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS