-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearcher.java
More file actions
101 lines (81 loc) · 3.06 KB
/
Searcher.java
File metadata and controls
101 lines (81 loc) · 3.06 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
package edu.ucr.cs172.project.partB;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
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.MultiFieldQueryParser;
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.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
public class Searcher
{
private IndexSearcher indexSearcher;
private DirectoryReader indexReader;;
private Directory indexDirectory;
private Analyzer analyzer;
private MultiFieldQueryParser parser;
//private boolean isParserInitialized;
private Map<String,Float> scoreWeights;
private final String[] FIELDS = {"title", "content"};
// Paramater 1: String - File path to directory of indexes
// Parameter 2: float - weight for results found in the title of the document
// Parameter 3: float - weight for results found in the body of the document
public Searcher(String indexDirectoryPath, float titleWeight, float contentWeight) throws IOException
{
//this directory will contain the indexes
indexDirectory = FSDirectory.open(new File(indexDirectoryPath).toPath());
indexReader = DirectoryReader.open(indexDirectory);
indexSearcher = new IndexSearcher(indexReader);
scoreWeights = new HashMap<>();
//isParserInitialized = false;
scoreWeights.put("title", titleWeight);
scoreWeights.put("content", contentWeight);
analyzer = new StandardAnalyzer();
parser = new MultiFieldQueryParser(FIELDS, analyzer, scoreWeights);
}
public List<Document> search(String queryString, int numberOfTopResults) throws IOException, ParseException
{
Query query = parser.parse(queryString);
ScoreDoc[] hits = indexSearcher.search(query, numberOfTopResults).scoreDocs;
List<Document> rankedDocuments = new ArrayList<Document>();
for (int rank = 0; rank < hits.length; ++rank) {
Document hitDoc = indexSearcher.doc(hits[rank].doc);
rankedDocuments.add(hitDoc);
}
return rankedDocuments;
}
public void close() throws IOException
{
indexReader.close();
indexDirectory.close();
}
/*
public void setTitleWeight(float weight)
{
scoreWeights.put("title", weight);
}
public void setContentWeight(float weight)
{
scoreWeights.put("content", weight);
}
public void initializeParser
{
parse = new MultiFieldQueryParser(fields, analyzer, boosts);
isParserInitialized = true;
}
*/
}