|
| 1 | +package com.baeldung.lucene; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileReader; |
| 5 | +import java.io.IOException; |
| 6 | +import java.net.URISyntaxException; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.nio.file.Paths; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import org.apache.lucene.analysis.standard.StandardAnalyzer; |
| 13 | +import org.apache.lucene.document.Document; |
| 14 | +import org.apache.lucene.document.Field; |
| 15 | +import org.apache.lucene.document.StringField; |
| 16 | +import org.apache.lucene.document.TextField; |
| 17 | +import org.apache.lucene.index.DirectoryReader; |
| 18 | +import org.apache.lucene.index.IndexReader; |
| 19 | +import org.apache.lucene.index.IndexWriter; |
| 20 | +import org.apache.lucene.index.IndexWriterConfig; |
| 21 | +import org.apache.lucene.queryparser.classic.ParseException; |
| 22 | +import org.apache.lucene.queryparser.classic.QueryParser; |
| 23 | +import org.apache.lucene.search.IndexSearcher; |
| 24 | +import org.apache.lucene.search.Query; |
| 25 | +import org.apache.lucene.search.ScoreDoc; |
| 26 | +import org.apache.lucene.search.TopDocs; |
| 27 | +import org.apache.lucene.store.Directory; |
| 28 | + |
| 29 | +public class LuceneFileSearch { |
| 30 | + |
| 31 | + private Directory indexDirectory; |
| 32 | + private StandardAnalyzer analyzer; |
| 33 | + |
| 34 | + public LuceneFileSearch(Directory fsDirectory, StandardAnalyzer analyzer) { |
| 35 | + super(); |
| 36 | + this.indexDirectory = fsDirectory; |
| 37 | + this.analyzer = analyzer; |
| 38 | + } |
| 39 | + |
| 40 | + public void addFileToIndex(String filepath) throws IOException, URISyntaxException { |
| 41 | + |
| 42 | + Path path = Paths.get(getClass().getClassLoader().getResource(filepath).toURI()); |
| 43 | + File file = path.toFile(); |
| 44 | + IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer); |
| 45 | + IndexWriter indexWriter = new IndexWriter(indexDirectory, indexWriterConfig); |
| 46 | + Document document = new Document(); |
| 47 | + |
| 48 | + FileReader fileReader = new FileReader(file); |
| 49 | + document.add(new TextField("contents", fileReader)); |
| 50 | + document.add(new StringField("path", file.getPath(), Field.Store.YES)); |
| 51 | + document.add(new StringField("filename", file.getName(), Field.Store.YES)); |
| 52 | + |
| 53 | + indexWriter.addDocument(document); |
| 54 | + |
| 55 | + indexWriter.close(); |
| 56 | + } |
| 57 | + |
| 58 | + public List<Document> searchFiles(String inField, String queryString) { |
| 59 | + try { |
| 60 | + Query query = new QueryParser(inField, analyzer).parse(queryString); |
| 61 | + |
| 62 | + IndexReader indexReader = DirectoryReader.open(indexDirectory); |
| 63 | + IndexSearcher searcher = new IndexSearcher(indexReader); |
| 64 | + TopDocs topDocs = searcher.search(query, 10); |
| 65 | + List<Document> documents = new ArrayList<>(); |
| 66 | + for (ScoreDoc scoreDoc : topDocs.scoreDocs) { |
| 67 | + documents.add(searcher.doc(scoreDoc.doc)); |
| 68 | + } |
| 69 | + |
| 70 | + return documents; |
| 71 | + } catch (IOException | ParseException e) { |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + return null; |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | + |
0 commit comments