Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
L
lucene
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mohamad.alturky
lucene
Commits
6b7314e1
Commit
6b7314e1
authored
Mar 24, 2024
by
mohamad.alturky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding searchers
parent
214a7bb9
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
128 additions
and
11 deletions
+128
-11
Lucene.java
src/main/java/com/search/lucene/Lucene.java
+5
-5
CSVFileSearcher.java
...ain/java/com/search/lucene/searchers/CSVFileSearcher.java
+8
-6
PDFFileSearcher.java
...ain/java/com/search/lucene/searchers/PDFFileSearcher.java
+50
-0
TextFileSearcher.java
...in/java/com/search/lucene/searchers/TextFileSearcher.java
+50
-0
ISearcher.java
...a/com/search/lucene/searchers/abstractions/ISearcher.java
+15
-0
No files found.
src/main/java/com/search/lucene/Lucene.java
View file @
6b7314e1
...
@@ -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.
TextFile
Searcher
;
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
s
earcher
;
private
static
TextFileSearcher
textFileS
earcher
;
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
TextFile
Searcher
(
indexDir
);
long
startTime
=
System
.
currentTimeMillis
();
long
startTime
=
System
.
currentTimeMillis
();
TopDocs
hits
=
s
earcher
.
search
(
searchQuery
);
TopDocs
hits
=
textFileS
earcher
.
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
=
s
earcher
.
getDocument
(
scoreDoc
);
Document
doc
=
textFileS
earcher
.
getDocument
(
scoreDoc
);
System
.
out
.
println
(
"File: "
System
.
out
.
println
(
"File: "
+
doc
.
get
(
LuceneConstants
.
FILE_PATH
));
+
doc
.
get
(
LuceneConstants
.
FILE_PATH
));
}
}
...
...
src/main/java/com/search/lucene/searchers/Searcher.java
→
src/main/java/com/search/lucene/searchers/
CSVFile
Searcher.java
View file @
6b7314e1
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
CSVFile
Searcher
(
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
);
...
...
src/main/java/com/search/lucene/searchers/PDFFileSearcher.java
0 → 100644
View file @
6b7314e1
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
src/main/java/com/search/lucene/searchers/TextFileSearcher.java
0 → 100644
View file @
6b7314e1
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
src/main/java/com/search/lucene/searchers/abstractions/ISearcher.java
0 → 100644
View file @
6b7314e1
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
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment