-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSearcher.java
More file actions
124 lines (106 loc) · 4.21 KB
/
Searcher.java
File metadata and controls
124 lines (106 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.io.IOException;
import java.nio.file.Paths;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
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.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopFieldDocs;
import org.apache.lucene.search.similarities.BM25Similarity;
import org.apache.lucene.search.similarities.Similarity;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
/**
* Class for searching the entry in Lucene index
*
* @author Akshay
*/
public class Searcher {
private IndexSearcher searcher = null;
private QueryParser parser = null;
private IndexReader reader=null;
/* Creates a new instance of SearchEngine */
public Searcher() {
try{
Directory dir=FSDirectory.open(Paths.get("C:\\My files\\Eclipse Workspace\\MusicSearchEngineIndexing\\indexDirectory"));
System.out.println(dir);
DirectoryReader dirReader=DirectoryReader.open(dir);
// searcher = new IndexSearcher(DirectoryReader.open(FSDirectory
// .open(Paths.get("indexDirectory"))));
searcher=new IndexSearcher(dirReader);
parser = new QueryParser("content", new StandardAnalyzer());
}catch(Exception e){
e.printStackTrace();
}
}
/**
* Method for retrieving the document stored in Lucene index
*
* @param docId
* :Unique ID of each indexed document
* @return :Returns the results based on given Doc ID
* @throws IOException
*/
public Document getDocument(int docId) throws IOException {
return searcher.doc(docId);
}
/**
* Method for searching the index for the given input query
*
* @param queryString
* :Query term to be searched in the index
* @param sort
* :Total results to be retrieved
* @param se
* :Searcher class object
* @throws ParseException
* @throws IOException
*/
public void searchIndex(String queryString,int n, Sort sort, Searcher se)
throws ParseException, IOException {
System.out.println("Searching for:" + queryString);
Query query = parser.parse(queryString);
// BM25Similarity bm25sim=new BM25Similarity(10.2f, 1.75f);
// searcher.setSimilarity(bm25sim);
TopFieldDocs topDocs = searcher.search(query, n, sort);
System.out.println("Results found: " + topDocs.totalHits);
System.out.println("Sorted By:"+sort);
ScoreDoc[] hits = topDocs.scoreDocs;
for (int i = 0; i < hits.length; i++) {
Document doc = se.getDocument(hits[i].doc);
System.out.println("\nTitle:" + doc.get("title") + "\nArtist:"
+ doc.get("artist") + "\nAlbum: " + doc.get("album")
+ "\nAlbumArt: " + doc.get("albumArt") + "\nYear:"
+ doc.get("year") + "\nGenre:" + doc.get("genre")
+ "\nURL:" + doc.get("URL") + "\nScore Hits:" + "("
+ hits[i].score + ")"+"\nPageRank:"+doc.get("PageRank")+"\nID:"+doc.get("ID"));
}
System.out.println("Search done");
}
public void searchIndexLuceneOrder(String queryString, int n, Searcher se)
throws ParseException, IOException {
System.out.println("Searching for:" + queryString);
Query query;
query = parser.parse(queryString);
TopDocs topDocs = searcher.search(query, n);
System.out.println("Results found: " + topDocs.totalHits);
ScoreDoc[] hits = topDocs.scoreDocs;
for (int i = 0; i < hits.length; i++) {
Document doc = se.getDocument(hits[i].doc);
System.out.println("\nTitle:" + doc.get("title") + "\nArtist:"
+ doc.get("artist") + "\nAlbum: " + doc.get("album")
+ "\nAlbumArt: " + doc.get("albumArt") + "\nYear:"
+ doc.get("year") + "\nGenre:" + doc.get("genre")
+ "\nURL:" + doc.get("URL") + "\nScore Hits:" + "("
+ hits[i].score + ")"+"\nmeta:"+doc.get("pageDescription"));
}
System.out.println("Search done");
}
}