Hello,
I have a problem when i search with lucene, but i dont know if this is a
problem of my Index or my Search.
At Index, I have like parameter a list of data of an individual
The list has values like "Name:PeterRooney", "Age:25", "From:NY" if the
individual is a person
but if the individual is an Event it has "Date:20/09/2014".
However, the Docs have differente keys for the type of individual.
public void indexDocs(IndexWriter writer, List<String> listInd)
throws IOException {
try {
Document doc = new Document();
String aux;
int pos;
for(int i=0;i<listInd.size();i++){
aux = listInd.get(i);
pos = aux.indexOf(":");
doc.add(new StringField(aux.substring(0,
pos),aux.substring(pos+1, aux.length()),Field.Store.YES));
}
if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {
writer.addDocument(doc);
} else {
// Existing index (an old copy of this document may have been
indexed) so
// we use updateDocument instead to replace the old one
matching the exact
// path, if present:
// System.out.println("updating " + file);
// writer.updateDocument(new StringBuffer("","",doc);
}
} finally {
// fis.close();
}
}
When i make the search i have this code:
public class SearchEngine {
// private IndexSearcher searcher = null;
// private QueryParser parser = null;
public SearchEngine(String line) throws IOException, ParseException {
String index = "index";
String field = "Name";
String queries = null;
int repeat = 1;
boolean raw = false;
String queryString = null;
int hitsPerPage = 10;
IndexReader reader = DirectoryReader.open(
FSDirectory.open(new File(index)));
IndexSearcher searcher = new IndexSearcher(reader);
// :Post-Release-Update-Version.LUCENE_XY:
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_10_0);
QueryParser parser = new QueryParser(Version.LUCENE_4_10_0, field,
analyzer);
Query query = parser.parse(line);
if (repeat > 0) { // repeat & time as
benchmark
Date start = new Date();
for (int i = 0; i < repeat; i++) {
searcher.search(query, null, 100);
}
}
doPagingSearch(searcher, query, hitsPerPage, raw, queries == null &&
queryString == null);
reader.close();
}
public List<String> doPagingSearch(IndexSearcher searcher, Query query,
int hitsPerPage, boolean raw, boolean
interactive) throws IOException {
// Collect enough docs to show 5 pages
TopDocs results = searcher.search(query, 1 * hitsPerPage);
ScoreDoc[] hits = results.scoreDocs;
List<String> result = new ArrayList<String>();
int numTotalHits = results.totalHits;
int start = 0;
int end = Math.min(numTotalHits, hitsPerPage);
while (true) {
end = Math.min(hits.length, start + hitsPerPage);
for (int i = start; i < end; i++) {
if (raw) { // output raw format
continue;
}
Document doc = searcher.doc(hits[i].doc);
String path = doc.get("Name");
result.add(path);
System.out.println(path);
}
if (!interactive || end == 0) {
break;
}
end = Math.min(numTotalHits, start + hitsPerPage);
}
return result;
}
}