Commit 6b7314e1 authored by mohamad.alturky's avatar mohamad.alturky

Adding searchers

parent 214a7bb9
...@@ -4,7 +4,7 @@ import java.io.IOException; ...@@ -4,7 +4,7 @@ import java.io.IOException;
import com.search.lucene.file.filters.TextFileFilter; import com.search.lucene.file.filters.TextFileFilter;
import com.search.lucene.indexers.TextFileIndexer; import com.search.lucene.indexers.TextFileIndexer;
import com.search.lucene.searchers.Searcher; import com.search.lucene.searchers.TextFileSearcher;
import com.search.lucene.settings.LuceneConstants; import com.search.lucene.settings.LuceneConstants;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.ParseException;
...@@ -16,7 +16,7 @@ public class Lucene { ...@@ -16,7 +16,7 @@ public class Lucene {
private static final String indexDir = "index"; private static final String indexDir = "index";
private static final String dataDir = "data"; private static final String dataDir = "data";
private static TextFileIndexer indexer; private static TextFileIndexer indexer;
private static Searcher searcher; private static TextFileSearcher textFileSearcher;
public static void main(String[] args) { public static void main(String[] args) {
try { try {
...@@ -42,15 +42,15 @@ public class Lucene { ...@@ -42,15 +42,15 @@ public class Lucene {
} }
private static void search(String searchQuery) throws IOException, ParseException { private static void search(String searchQuery) throws IOException, ParseException {
searcher = new Searcher(indexDir); textFileSearcher = new TextFileSearcher(indexDir);
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
TopDocs hits = searcher.search(searchQuery); TopDocs hits = textFileSearcher.search(searchQuery);
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
System.out.println(hits.totalHits + System.out.println(hits.totalHits +
" documents found. Time :" + (endTime - startTime)); " documents found. Time :" + (endTime - startTime));
for(ScoreDoc scoreDoc : hits.scoreDocs) { for(ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = searcher.getDocument(scoreDoc); Document doc = textFileSearcher.getDocument(scoreDoc);
System.out.println("File: " System.out.println("File: "
+ doc.get(LuceneConstants.FILE_PATH)); + doc.get(LuceneConstants.FILE_PATH));
} }
......
package com.search.lucene.searchers; package com.search.lucene.searchers;
import java.io.IOException; import com.search.lucene.searchers.abstractions.ISearcher;
import java.nio.file.Paths;
import com.search.lucene.settings.LuceneConstants; import com.search.lucene.settings.LuceneConstants;
import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
...@@ -18,13 +16,16 @@ import org.apache.lucene.search.TopDocs; ...@@ -18,13 +16,16 @@ import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.FSDirectory;
public class Searcher { import java.io.IOException;
import java.nio.file.Paths;
public class CSVFileSearcher implements ISearcher {
IndexSearcher indexSearcher; IndexSearcher indexSearcher;
QueryParser queryParser; QueryParser queryParser;
Query query; Query query;
public Searcher(String indexDirectoryPath) public CSVFileSearcher(String indexDirectoryPath)
throws IOException { throws IOException {
Directory indexDirectory = Directory indexDirectory =
FSDirectory.open(Paths.get(indexDirectoryPath)); FSDirectory.open(Paths.get(indexDirectoryPath));
...@@ -34,12 +35,13 @@ public class Searcher { ...@@ -34,12 +35,13 @@ public class Searcher {
new StandardAnalyzer()); new StandardAnalyzer());
} }
@Override
public TopDocs search( String searchQuery) public TopDocs search( String searchQuery)
throws IOException, ParseException { throws IOException, ParseException {
query = queryParser.parse(searchQuery); query = queryParser.parse(searchQuery);
return indexSearcher.search(query, LuceneConstants.MAX_SEARCH); return indexSearcher.search(query, LuceneConstants.MAX_SEARCH);
} }
@Override
public Document getDocument(ScoreDoc scoreDoc) public Document getDocument(ScoreDoc scoreDoc)
throws CorruptIndexException, IOException { throws CorruptIndexException, IOException {
return indexSearcher.doc(scoreDoc.doc); return indexSearcher.doc(scoreDoc.doc);
......
package com.search.lucene.searchers;
import com.search.lucene.searchers.abstractions.ISearcher;
import com.search.lucene.settings.LuceneConstants;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
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.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import java.io.IOException;
import java.nio.file.Paths;
public class PDFFileSearcher implements ISearcher {
IndexSearcher indexSearcher;
QueryParser queryParser;
Query query;
public PDFFileSearcher(String indexDirectoryPath)
throws IOException {
Directory indexDirectory =
FSDirectory.open(Paths.get(indexDirectoryPath));
IndexReader reader = DirectoryReader.open(indexDirectory);
indexSearcher = new IndexSearcher(reader);
queryParser = new QueryParser(LuceneConstants.CONTENTS,
new StandardAnalyzer());
}
@Override
public TopDocs search( String searchQuery)
throws IOException, ParseException {
query = queryParser.parse(searchQuery);
return indexSearcher.search(query, LuceneConstants.MAX_SEARCH);
}
@Override
public Document getDocument(ScoreDoc scoreDoc)
throws CorruptIndexException, IOException {
return indexSearcher.doc(scoreDoc.doc);
}
}
\ No newline at end of file
package com.search.lucene.searchers;
import java.io.IOException;
import java.nio.file.Paths;
import com.search.lucene.searchers.abstractions.ISearcher;
import com.search.lucene.settings.LuceneConstants;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
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.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class TextFileSearcher implements ISearcher {
IndexSearcher indexSearcher;
QueryParser queryParser;
Query query;
public TextFileSearcher(String indexDirectoryPath)
throws IOException {
Directory indexDirectory =
FSDirectory.open(Paths.get(indexDirectoryPath));
IndexReader reader = DirectoryReader.open(indexDirectory);
indexSearcher = new IndexSearcher(reader);
queryParser = new QueryParser(LuceneConstants.CONTENTS,
new StandardAnalyzer());
}
@Override
public TopDocs search( String searchQuery)
throws IOException, ParseException {
query = queryParser.parse(searchQuery);
return indexSearcher.search(query, LuceneConstants.MAX_SEARCH);
}
@Override
public Document getDocument(ScoreDoc scoreDoc)
throws CorruptIndexException, IOException {
return indexSearcher.doc(scoreDoc.doc);
}
}
\ No newline at end of file
package com.search.lucene.searchers.abstractions;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import java.io.IOException;
public interface ISearcher {
Document getDocument(ScoreDoc scoreDoc) throws CorruptIndexException, IOException;
TopDocs search(String searchQuery) throws IOException, ParseException;
}
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