Commit b0aea8ef authored by mohamad.alturky's avatar mohamad.alturky

edit sources

parent d28e1d02
...@@ -31,7 +31,7 @@ public class Lucene { ...@@ -31,7 +31,7 @@ public class Lucene {
} }
private static void createIndex() throws IOException { private static void createIndex() throws IOException {
indexer = new TextFileIndexer(indexDir, new TextFileFilter()); indexer = new TextFileIndexer(indexDir);
int numIndexed; int numIndexed;
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
numIndexed = indexer.createIndex(dataDir); numIndexed = indexer.createIndex(dataDir);
......
package com.search.lucene.documents.representers.implementations; package com.search.lucene.documents.representers.implementations;
import com.search.lucene.documents.representers.abstractions.IDocumentRepresenter; import com.search.lucene.documents.representers.abstractions.IDocumentRepresenter;
import com.search.lucene.settings.Constants;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
public class CSVDocumentStringRepresenter implements IDocumentRepresenter<String> { public class CSVDocumentStringRepresenter implements IDocumentRepresenter<String> {
@Override @Override
public String Represent(Document indexedDocument) { public String Represent(Document indexedDocument) {
return null; return String.format("""
file path = %s\s
file name = %s\s
in row = %s\s
in column = %s\s
with content = %s\s
""",
indexedDocument.get(Constants.FILE_PATH),
indexedDocument.get(Constants.FILE_NAME),
indexedDocument.get(Constants.ROW),
indexedDocument.get(Constants.COLUMN),
indexedDocument.get(Constants.VALUE));
} }
} }
package com.search.lucene.documents.representers.implementations; package com.search.lucene.documents.representers.implementations;
import com.search.lucene.documents.representers.abstractions.IDocumentRepresenter; import com.search.lucene.documents.representers.abstractions.IDocumentRepresenter;
import com.search.lucene.settings.Constants;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
public class PDFDocumentStringRepresenter implements IDocumentRepresenter<String> { public class PDFDocumentStringRepresenter implements IDocumentRepresenter<String> {
@Override @Override
public String Represent(Document indexedDocument) { public String Represent(Document indexedDocument) {
return null; return String.format("""
file path = %s\s
file name = %s\s
with content = %s\s
""",
indexedDocument.get(Constants.FILE_PATH),
indexedDocument.get(Constants.FILE_NAME),
indexedDocument.get(Constants.CONTENTS));
} }
} }
package com.search.lucene.documents.representers.implementations; package com.search.lucene.documents.representers.implementations;
import com.search.lucene.documents.representers.abstractions.IDocumentRepresenter; import com.search.lucene.documents.representers.abstractions.IDocumentRepresenter;
import com.search.lucene.settings.Constants;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
public class TextDocumentStringRepresenter implements IDocumentRepresenter<String> { public class TextDocumentStringRepresenter implements IDocumentRepresenter<String> {
@Override @Override
public String Represent(Document indexedDocument) { public String Represent(Document indexedDocument) {
return null; return String.format("""
file path = %s\s
file name = %s\s
with content = %s\s
""",
indexedDocument.get(Constants.FILE_PATH),
indexedDocument.get(Constants.FILE_NAME),
indexedDocument.get(Constants.CONTENTS));
} }
} }
package com.search.lucene.engine.builder.abstractions;
import com.search.lucene.engine.abstractions.ISearchEngine;
import java.io.IOException;
public interface IEngineBuilder {
ISearchEngine build() throws IOException;
}
package com.search.lucene.engine.builder.implementations;
import com.search.lucene.engine.abstractions.ISearchEngine;
import com.search.lucene.engine.builder.abstractions.IEngineBuilder;
import com.search.lucene.engine.implementations.LuceneEngine;
import com.search.lucene.indexers.abstractions.IFileIndexer;
import com.search.lucene.indexers.implementations.CSVFileIndexer;
import com.search.lucene.indexers.implementations.PDFFileIndexer;
import com.search.lucene.indexers.implementations.TextFileIndexer;
import com.search.lucene.searchers.abstractions.ISearcher;
import com.search.lucene.searchers.implementations.CSVFileSearcher;
import com.search.lucene.searchers.implementations.PDFFileSearcher;
import com.search.lucene.searchers.implementations.TextFileSearcher;
import com.search.lucene.settings.IndexPaths;
import java.io.IOException;
import java.util.ArrayList;
public class LuceneEngineBuilder implements IEngineBuilder {
@Override
public ISearchEngine build() throws IOException {
ArrayList<IFileIndexer> indexers = new ArrayList<>();
indexers.add(new CSVFileIndexer(IndexPaths.CSV_INDEX_FOLDER_PATH));
indexers.add(new PDFFileIndexer(IndexPaths.PDF_INDEX_FOLDER_PATH));
indexers.add(new TextFileIndexer(IndexPaths.TEXT_INDEX_FOLDER_PATH));
ArrayList<ISearcher> searchers = new ArrayList<>();
searchers.add(new CSVFileSearcher(IndexPaths.CSV_INDEX_FOLDER_PATH));
searchers.add(new PDFFileSearcher(IndexPaths.PDF_INDEX_FOLDER_PATH));
searchers.add(new TextFileSearcher(IndexPaths.TEXT_INDEX_FOLDER_PATH));
return new LuceneEngine(indexers,searchers);
}
}
...@@ -20,8 +20,9 @@ public class LuceneEngine implements ISearchEngine { ...@@ -20,8 +20,9 @@ public class LuceneEngine implements ISearchEngine {
@Override @Override
public void createIndexForDirectory(String directoryPath) throws IOException { public void createIndexForDirectory(String directoryPath) throws IOException {
for (var indexer : indexers){ for (IFileIndexer indexer : indexers) {
indexer.createIndex(directoryPath); indexer.createIndex(directoryPath);
indexer.close();
} }
} }
......
package com.search.lucene.indexers.abstractions; package com.search.lucene.indexers.abstractions;
import org.apache.lucene.index.CorruptIndexException;
import java.io.FileFilter; import java.io.FileFilter;
import java.io.IOException; import java.io.IOException;
public interface IFileIndexer { public interface IFileIndexer {
int createIndex(String dataDirectoryPath) throws IOException; int createIndex(String dataDirectoryPath) throws IOException;
void close() throws CorruptIndexException, IOException;
} }
...@@ -2,6 +2,7 @@ package com.search.lucene.indexers.implementations; ...@@ -2,6 +2,7 @@ package com.search.lucene.indexers.implementations;
import com.opencsv.CSVReader; import com.opencsv.CSVReader;
import com.search.lucene.file.filters.abstractions.IFileFilter; import com.search.lucene.file.filters.abstractions.IFileFilter;
import com.search.lucene.file.filters.implementations.CSVFileFilter;
import com.search.lucene.indexers.abstractions.IFileIndexer; import com.search.lucene.indexers.abstractions.IFileIndexer;
import com.search.lucene.settings.Constants; import com.search.lucene.settings.Constants;
import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer;
...@@ -26,8 +27,8 @@ public class CSVFileIndexer implements IFileIndexer { ...@@ -26,8 +27,8 @@ public class CSVFileIndexer implements IFileIndexer {
private final IndexWriter writer; private final IndexWriter writer;
private final IFileFilter filter; private final IFileFilter filter;
public CSVFileIndexer(String indexDirectoryPath, IFileFilter filter) throws IOException { public CSVFileIndexer(String indexDirectoryPath) throws IOException {
this.filter = filter; this.filter = new CSVFileFilter();
//this directory will contain the indexes //this directory will contain the indexes
Directory indexDirectory = Directory indexDirectory =
FSDirectory.open(Paths.get(indexDirectoryPath)); FSDirectory.open(Paths.get(indexDirectoryPath));
...@@ -36,6 +37,7 @@ public class CSVFileIndexer implements IFileIndexer { ...@@ -36,6 +37,7 @@ public class CSVFileIndexer implements IFileIndexer {
writer = new IndexWriter(indexDirectory, new IndexWriterConfig(new StandardAnalyzer())); writer = new IndexWriter(indexDirectory, new IndexWriterConfig(new StandardAnalyzer()));
} }
@Override
public void close() throws CorruptIndexException, IOException { public void close() throws CorruptIndexException, IOException {
writer.close(); writer.close();
} }
......
package com.search.lucene.indexers.implementations; package com.search.lucene.indexers.implementations;
import com.search.lucene.file.filters.abstractions.IFileFilter; import com.search.lucene.file.filters.abstractions.IFileFilter;
import com.search.lucene.file.filters.implementations.PDFFileFilter;
import com.search.lucene.indexers.abstractions.IFileIndexer; import com.search.lucene.indexers.abstractions.IFileIndexer;
import com.search.lucene.settings.Constants; import com.search.lucene.settings.Constants;
import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer;
...@@ -25,15 +26,15 @@ public class PDFFileIndexer implements IFileIndexer { ...@@ -25,15 +26,15 @@ public class PDFFileIndexer implements IFileIndexer {
private final IndexWriter writer; private final IndexWriter writer;
private final IFileFilter filter; private final IFileFilter filter;
public PDFFileIndexer(String indexDirectoryPath, IFileFilter fileFilter) throws IOException { public PDFFileIndexer(String indexDirectoryPath) throws IOException {
//this directory will contain the indexes //this directory will contain the indexes
Directory indexDirectory = Directory indexDirectory =
FSDirectory.open(Paths.get(indexDirectoryPath)); FSDirectory.open(Paths.get(indexDirectoryPath));
this.filter = fileFilter; this.filter = new PDFFileFilter();
//create the indexer //create the indexer
writer = new IndexWriter(indexDirectory, new IndexWriterConfig(new StandardAnalyzer())); writer = new IndexWriter(indexDirectory, new IndexWriterConfig(new StandardAnalyzer()));
} }
@Override
public void close() throws CorruptIndexException, IOException { public void close() throws CorruptIndexException, IOException {
writer.close(); writer.close();
} }
...@@ -49,7 +50,7 @@ public class PDFFileIndexer implements IFileIndexer { ...@@ -49,7 +50,7 @@ public class PDFFileIndexer implements IFileIndexer {
writer.addDocument(document); writer.addDocument(document);
pdDocument.close(); pdDocument.close();
return document; return document;
} }
private void indexFile(File file) throws IOException { private void indexFile(File file) throws IOException {
......
...@@ -7,6 +7,7 @@ import java.io.IOException; ...@@ -7,6 +7,7 @@ import java.io.IOException;
import java.nio.file.Paths; import java.nio.file.Paths;
import com.search.lucene.file.filters.abstractions.IFileFilter; import com.search.lucene.file.filters.abstractions.IFileFilter;
import com.search.lucene.file.filters.implementations.TextFileFilter;
import com.search.lucene.indexers.abstractions.IFileIndexer; import com.search.lucene.indexers.abstractions.IFileIndexer;
import com.search.lucene.settings.Constants; import com.search.lucene.settings.Constants;
import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer;
...@@ -22,8 +23,8 @@ public class TextFileIndexer implements IFileIndexer { ...@@ -22,8 +23,8 @@ public class TextFileIndexer implements IFileIndexer {
private final IndexWriter writer; private final IndexWriter writer;
private final IFileFilter filter; private final IFileFilter filter;
public TextFileIndexer(String indexDirectoryPath, IFileFilter filter) throws IOException { public TextFileIndexer(String indexDirectoryPath) throws IOException {
this.filter = filter; this.filter = new TextFileFilter();
//this directory will contain the indexes //this directory will contain the indexes
Directory indexDirectory = Directory indexDirectory =
FSDirectory.open(Paths.get(indexDirectoryPath)); FSDirectory.open(Paths.get(indexDirectoryPath));
...@@ -31,7 +32,7 @@ public class TextFileIndexer implements IFileIndexer { ...@@ -31,7 +32,7 @@ public class TextFileIndexer implements IFileIndexer {
//create the indexer //create the indexer
writer = new IndexWriter(indexDirectory, new IndexWriterConfig(new StandardAnalyzer())); writer = new IndexWriter(indexDirectory, new IndexWriterConfig(new StandardAnalyzer()));
} }
@Override
public void close() throws CorruptIndexException, IOException { public void close() throws CorruptIndexException, IOException {
writer.close(); writer.close();
} }
......
package com.search.lucene.settings;
public final class IndexPaths {
public static final String CSV_INDEX_FOLDER_PATH = "csvIndex";
public static final String TEXT_INDEX_FOLDER_PATH = "textIndex";
public static final String PDF_INDEX_FOLDER_PATH = "pdfIndex";
}
package com.search.lucene.settings; package com.search.lucene.settings;
public enum IndexType { public final class IndexType {
CSV, public static final String CSV = "CSV";
PDF, public static final String PDF = "PDF";
TEXT public static final String TEXT = "TEXT";
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment