Last active
January 21, 2024 07:12
-
-
Save serihiro/7ba609f8b488a0d4c66e3ba4cb289d9e to your computer and use it in GitHub Desktop.
Lucene in 5 minutes (https://lucenetutorial.com/lucene-in-5-minutes.html) with lucene 9.9.1
This file contains hidden or 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
package org.example; | |
import java.io.IOException; | |
import org.apache.lucene.analysis.standard.StandardAnalyzer; | |
import org.apache.lucene.document.Document; | |
import org.apache.lucene.document.Field; | |
import org.apache.lucene.document.StringField; | |
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.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.store.ByteBuffersDirectory; | |
public class Main { | |
private static final int hitsPerPage = 10; | |
public static void main(String[] args) { | |
ByteBuffersDirectory index = null; | |
try { | |
index = initializeIndex(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
Query query = null; | |
try { | |
var queryString = args.length > 0 ? args[0] : "lucene"; | |
var queryAnalyzer = new StandardAnalyzer(); | |
query = new QueryParser("title", queryAnalyzer).parse(queryString); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
try (var reader = DirectoryReader.open(index)) { | |
var searcher = new IndexSearcher(reader); | |
var docs = searcher.search(query, hitsPerPage); | |
var hits = docs.scoreDocs; | |
System.out.println("Found " + hits.length + " hits."); | |
var storeFields = searcher.storedFields(); | |
for (int i = 0; i < hits.length; ++i) { | |
int docId = hits[i].doc; | |
var d = storeFields.document(docId); | |
System.out.println((i + 1) + ". " + d.get("isbn") + "\t" + d.get("title")); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
} | |
private static ByteBuffersDirectory initializeIndex() throws IOException { | |
var index = new ByteBuffersDirectory(); | |
var indexAnalyzer = new StandardAnalyzer(); | |
var config = new IndexWriterConfig(indexAnalyzer); | |
try (var w = new IndexWriter(index, config)) { | |
addDoc(w, "Lucene in Action", "193398817"); | |
addDoc(w, "Lucene for Dummies", "55320055Z"); | |
addDoc(w, "Managing Gigabytes", "55063554A"); | |
addDoc(w, "The Art of Computer Science", "9900333X"); | |
} catch (IOException e) { | |
throw e; | |
} | |
return index; | |
} | |
private static void addDoc(IndexWriter w, String title, String isbn) throws IOException { | |
var doc = new Document(); | |
doc.add(new TextField("title", title, Field.Store.YES)); | |
doc.add(new StringField("isbn", isbn, Field.Store.YES)); | |
w.addDocument(doc); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment