DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Curious about the future of data-driven systems? Join our Data Engineering roundtable and learn how to build scalable data platforms.

Data Engineering: The industry has come a long way from organizing unstructured data to adopting today's modern data pipelines. See how.

Threat Detection: Learn core practices for managing security risks and vulnerabilities in your organization — don't regret those threats!

Managing API integrations: Assess your use case and needs — plus learn patterns for the design, build, and maintenance of your integrations.

Related

  • Jakarta WebSocket Essentials: A Guide to Full-Duplex Communication in Java
  • High-Performance Reactive REST API and Reactive DB Connection Using Java Spring Boot WebFlux R2DBC Example
  • Java Is Greener on Arm
  • How to Get Plain Text From Common Documents in Java

Trending

  • Filtering Java Collections via Annotation-Driven Introspection
  • Leveraging Apache Flink Dashboard for Real-Time Data Processing in AWS Apache Flink Managed Service
  • Exploring AI-Powered Web Development: OpenAI, Node.js, and Dynamic UI Creation
  • Implementing LSM Trees in Golang: A Comprehensive Guide
  1. DZone
  2. Coding
  3. Java
  4. Java: Lucene: Simple In-Memory Search Example

Java: Lucene: Simple In-Memory Search Example

By 
Snippets Manager user avatar
Snippets Manager
·
May. 15, 07 · Code Snippet
Likes (0)
Comment
Save
Tweet
Share
5.8K Views

Join the DZone community and get the full member experience.

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


/**
 * A simple example of an in-memory search using Lucene.
 */
import java.io.IOException;
import java.io.StringReader;

import org.apache.lucene.search.Hits;
import org.apache.lucene.search.Query;
import org.apache.lucene.document.Field;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.document.Document;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;

public class InMemoryExample
{

  public static void main(String[] args)
  {
    // Construct a RAMDirectory to hold the in-memory representation
    // of the index.
    RAMDirectory idx = new RAMDirectory();

    try
    {
      // Make an writer to create the index
      IndexWriter writer = new IndexWriter(idx, new StandardAnalyzer(), true);

      // Add some Document objects containing quotes
      writer.addDocument(createDocument("Theodore Roosevelt",
          "It behooves every man to remember that the work of the "
              + "critic, is of altogether secondary importance, and that, "
              + "in the end, progress is accomplished by the man who does "
              + "things."));
      writer.addDocument(createDocument("Friedrich Hayek",
          "The case for individual freedom rests largely on the "
              + "recognition of the inevitable and universal ignorance "
              + "of all of us concerning a great many of the factors on "
              + "which the achievements of our ends and welfare depend."));
      writer.addDocument(createDocument("Ayn Rand",
          "There is nothing to take a man’s freedom away from "
              + "him, save other men. To be free, a man must be free "
              + "of his brothers."));
      writer.addDocument(createDocument("Mohandas Gandhi",
          "Freedom is not worth having if it does not connote "
              + "freedom to err."));

      // Optimize and close the writer to finish building the index
      writer.optimize();
      writer.close();

      // Build an IndexSearcher using the in-memory index
      Searcher searcher = new IndexSearcher(idx);

      // Run some queries
      search(searcher, "freedom");
      search(searcher, "free");
      search(searcher, "progress or achievements");

      searcher.close();
    }
    catch (IOException ioe)
    {
      // In this example we aren’t really doing an I/O, so this
      // exception should never actually be thrown.
      ioe.printStackTrace();
    }
    catch (ParseException pe)
    {
      pe.printStackTrace();
    }
  }

  /**
   * Make a Document object with an un-indexed title field and an indexed
   * content field.
   */
  private static Document createDocument(String title, String content)
  {
    Document doc = new Document();

    // Add the title as an unindexed field…
    doc.add(new Field("title", title, Field.Store.YES, Field.Index.NO));

    // …and the content as an indexed field. Note that indexed
    // Text fields are constructed using a Reader. Lucene can read
    // and index very large chunks of text, without storing the
    // entire content verbatim in the index. In this example we
    // can just wrap the content string in a StringReader.
    doc.add(new Field("content", new StringReader(content)));

    return doc;
  }

  /**
   * Searches for the given string in the "content" field
   */
  private static void search(Searcher searcher, String queryString)
      throws ParseException, IOException
  {

    // Build a Query object
    QueryParser parser = new QueryParser("content", new StandardAnalyzer());
    Query query = parser.parse(queryString);

    // Search for the query
    Hits hits = searcher.search(query);

    // Examine the Hits object to see if there were any matches
    int hitCount = hits.length();
    if (hitCount == 0)
    {
      System.out.println("No matches were found for \"" + queryString + "\"");
    }
    else
    {
      System.out.println("Hits for \"" + queryString
          + "\" were found in quotes by:");

      // Iterate over the Documents in the Hits object
      for (int i = 0; i < hitCount; i++)
      {
        Document doc = hits.doc(i);

        // Print the value that we stored in the "title" field. Note
        // that this Field was not indexed, but (unlike the
        // "contents" field) was stored verbatim and can be
        // retrieved.
        System.out.println(" " + (i + 1) + ". " + doc.get("title"));
      }
    }
    System.out.println();
  }
}
Java (programming language) Lucene

Opinions expressed by DZone contributors are their own.

Related

  • Jakarta WebSocket Essentials: A Guide to Full-Duplex Communication in Java
  • High-Performance Reactive REST API and Reactive DB Connection Using Java Spring Boot WebFlux R2DBC Example
  • Java Is Greener on Arm
  • How to Get Plain Text From Common Documents in Java

Partner Resources


Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: