-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMusicLuceneIndexBuilder.java
207 lines (188 loc) · 5.96 KB
/
MusicLuceneIndexBuilder.java
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.DoubleField;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
/**
* Class for creating index using Apache Lucene
*
* @author Akshay
*/
public class MusicLuceneIndexBuilder {
public IndexWriter indexWriter = null;
/* Creates a new instance of Indexer */
public MusicLuceneIndexBuilder() {
}
/**
* Method for creating a new Indexwriter Object if index is not present
*
* @return :The indexWriter object
* @throws IOException
*/
public IndexWriter getIndexWriter() throws IOException {
if (indexWriter == null) {
File fs = new File("indexDirectory");
Directory indexDir = FSDirectory.open(Paths.get("indexDirectory"));
IndexWriterConfig config = new IndexWriterConfig(
new StandardAnalyzer());
indexWriter = new IndexWriter(indexDir, config);
}
return indexWriter;
}
/**
* Method for closing the indexwriter Object if not closed
*
* @throws IOException
*/
public void closeIndexWriter() throws IOException {
if (indexWriter != null) {
indexWriter.close();
}
}
/**
* Method for building the Lucene index
*
* @param line
* :Data from file containing song information
* @param fileName
* @return
* @throws IOException
*/
public void indexSongData(String line, String fileName) throws IOException {
String fullSearchableText = "", domain = "";
double pageRank = -1;
String[] domainSplitter;
int pgRank = 0;
//System.out.println(line);
String[] splitter = line.split(";");
IndexWriter writer = getIndexWriter();
Document doc = new Document();
pageRank = fetchPageRank(fileName);
if(!splitter[0].contains("*^*")){
doc.add(new TextField("ID", splitter[0], Field.Store.YES));
}
if (!splitter[1].contains("*^*")) {
doc.add(new TextField("title", splitter[1], Field.Store.YES));
fullSearchableText += splitter[1];
}
if (!splitter[2].contains("*^*")) {
doc.add(new TextField("artist", splitter[2], Field.Store.YES));
fullSearchableText += splitter[2];
}
if (!splitter[3].contains("*^*")) {
doc.add(new TextField("album", splitter[3], Field.Store.YES));
fullSearchableText += splitter[3];
}
if (!splitter[4].contains("*^*")) {
doc.add(new TextField("albumArt", splitter[4], Field.Store.YES));
fullSearchableText += splitter[4];
}
if (!splitter[5].contains("*^*")) {
doc.add(new TextField("year", splitter[5], Field.Store.YES));
fullSearchableText += splitter[5];
}
if (!splitter[6].contains("*^*")) {
doc.add(new TextField("genre", splitter[6], Field.Store.YES));
fullSearchableText += splitter[6];
}
if (!splitter[7].contains("*^*")) {
doc.add(new StringField("URL", splitter[7], Field.Store.YES));
fullSearchableText += splitter[7];
/*
* domainSplitter=splitter[7].split("/");
* domain=domainSplitter[0]+"//"+domainSplitter[2]; PageRankService
* prObj=new PageRankService();
* pgRank=prObj.getPR("http://gaana.com/");
* pageRank=Integer.toString(pgRank);
*
* doc.add(new TextField("PageRank",pageRank,Field.Store.YES));
*/
}
if (!splitter[8].contains("*^*")) {
doc.add(new StringField("pageTitle", splitter[8], Field.Store.YES));
}
if (!splitter[9].contains("*^*")) {
doc.add(new StringField("pageDescription", splitter[9],
Field.Store.YES));
}
/*
* PageRankService prObj=new PageRankService();
* pgRank=prObj.getPR("http://gaana.com/");
* pageRank=Integer.toString(pgRank);
* System.out.println("Page Rank:"+pageRank);
*/
doc.add(new DoubleField("PageRank", pageRank,
DOUBLE_FIELD_TYPE_STORED_SORTED));
doc.add(new TextField("content", fullSearchableText, Field.Store.NO));
writer.addDocument(doc);
}
/**
*
* @param fileName
* @return
*/
private double fetchPageRank(String fileName) {
double pR = -1;
if (fileName.contains("gaana")) {
pR = 7;
} else if (fileName.contains("sworly")) {
pR = 3;
} else if (fileName.contains("EMusic")) {
pR = 7;
} else if (fileName.contains("jamendo")) {
pR = 7;
} else if (fileName.contains("shazam")) {
pR = 6;
} else if (fileName.contains("saavn")) {
pR = 4;
} else if (fileName.contains("indishuffle")) {
pR = 5;
} else if (fileName.contains("myspace")) {
pR = 8;
}
return pR;
}
/**
*
*/
private static final FieldType DOUBLE_FIELD_TYPE_STORED_SORTED = new FieldType();
static {
DOUBLE_FIELD_TYPE_STORED_SORTED.setTokenized(false);
DOUBLE_FIELD_TYPE_STORED_SORTED.setOmitNorms(true);
DOUBLE_FIELD_TYPE_STORED_SORTED.setIndexOptions(IndexOptions.DOCS);
DOUBLE_FIELD_TYPE_STORED_SORTED
.setNumericType(FieldType.NumericType.DOUBLE);
DOUBLE_FIELD_TYPE_STORED_SORTED.setStored(true);
DOUBLE_FIELD_TYPE_STORED_SORTED.setDocValuesType(DocValuesType.NUMERIC);
DOUBLE_FIELD_TYPE_STORED_SORTED.freeze();
}
/**
* Method for creating a new index object and indexing data from each line
* of file
*
* @param fileDataList
* :ArrayList containing each line read from file
* @param fileName
* @throws IOException
*/
public void createIndex(ArrayList<String> fileDataList, String fileName)
throws IOException {
getIndexWriter();
for (String line : fileDataList) {
indexSongData(line, fileName);
}
closeIndexWriter();
}
}