cassiomolin / lucene-example

Example of application for indexing and searching with Apache Lucene.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example of indexing and searching with Apache Lucene

Build Status MIT Licensed

Apache Lucene is a high-performance text search engine library written entirely in Java.

This example application demonstrates how to perform some operations with Apache Lucene: This application parses some JSON files with Jackson, indexes their content with Lucene and performs some searches.

Building and running this application

  1. Open a command line window or terminal.
  2. Navigate to the root directory of the project, where the pom.xml resides.
  3. Compile the project: mvn clean compile.
  4. Package the application: mvn package.
  5. Change into the target directory: cd target
  6. You should see a file with the following or a similar name: index-and-search-with-lucene-1.0.jar.
  7. Execute the JAR: java -jar index-and-search-with-lucene-1.0.jar.
  8. The application should be executed and the result should be displayed in the console.

Indexing and searching with Apache Lucene

A Lucene index (Directory) is a collection of entries (Document) that contains properties (Field).

A writer (IndexWriter) allows you to add entries to the index while a searcher (IndexSearcher) allows you to execute queries (Query) against the index and get the results (TopDocs).

For a better on understanding how Apache Lucene works, here's an example on how to perform some simple indexing and searching operations:

Create an index

// Create an index in memory
Directory index = new RAMDirectory();

Create an index writer

// Create an index writer
StandardAnalyzer analyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(index, config);

Add entries to the index

// Add a document to the index
Document document = new Document();
document.add(new TextField("name", "John Doe", Field.Store.YES));
document.add(new TextField("address", "80 Summer Hill", Field.Store.YES));
indexWriter.addDocument(document);

// Add a document to the index
document = new Document();
document.add(new TextField("name", "Jane Doe", Field.Store.YES));
document.add(new TextField("address", "9 Main Circle", Field.Store.YES));
indexWriter.addDocument(document);

// Add a document to the index
document = new Document();
document.add(new TextField("name", "John Smith", Field.Store.YES));
document.add(new TextField("address", "9 Dexter Avenue", Field.Store.YES));
indexWriter.addDocument(document);
    
indexWriter.close();

Create an index searcher

// Create an index searcher
IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);

Create a query

// Create a query to look for people with "doe" in the name
Query query = new TermQuery(new Term("name", "doe"));

Execute the query and display the results

// Execute que query and show the results
TopDocs topDocs = searcher.search(query, 10);

// Display addresses
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
    document = searcher.doc(scoreDoc.doc);
    System.out.println(document.get("address"));
}

About

Example of application for indexing and searching with Apache Lucene.

License:MIT License


Languages

Language:Java 100.0%