If anyone needs an (old) programmer I am available :)
package com.RadioCity.Lucene;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.Vector;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.FilterIndexReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.pdfbox.searchengine.lucene.LucenePDFDocument;
import com.RadioCity.Joms.util.JomsConstants;
public class FullTextUtil
{
final String indexDir = JomsConstants.IndexLocation;
final boolean debug=true;
public void mergeIndexes(String indexToMerge)
{
if (debug)
System.out.println("FullTextUtil:mergeIndexes with " + indexToMerge);
IndexWriter writer = null;
IndexReader reader = null;
File INDEX_DIR = new File(indexDir);
try
{
reader = IndexReader.open(indexToMerge);
writer = new IndexWriter(INDEX_DIR, new StandardAnalyzer());
writer.addIndexes(new IndexReader[] {reader}); writer.close();
reader.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void indexDoc(String filename, String title, String itemId, String ftype) throws Exception
{
indexDoc(filename, title, itemId, "0", ftype);
}
public void indexDoc(String filename, String title, String itemId, String systemId, String ftype) throws Exception
{
if(debug)
System.out.println("FullTextUtil:indexDoc with " + filename + " " + title + " " + itemId + " " + systemId);
int cnt = deleteItem(systemId);
if (cnt > 0)
{
if (debug)
System.out.println("Deleted previous Version of " + filename + " from full text system");
}
else
{
if (debug)
System.out.println("Previous Version of " + filename + " not found");
}
File INDEX_DIR = new File(indexDir);
IndexWriter writer = null;
try
{
writer = new IndexWriter(INDEX_DIR, new StandardAnalyzer());
File file = new File(filename);
JomsDocument kcmiDoc = new KcmiDocument(itemId, systemId, title, ftype);
writer.addDocument(JomsDoc.document(file));
writer.optimize();
writer.close();
}
catch(Exception e)
{
System.out.println("In indexDoc with an Exception, Oh No");
writer.close();
throw e;
}
}
public int deleteItem(String systemId)
{
if (systemId.equals("0")) return 0;
if(debug)
System.out.println("FullTextUtil:deleteItem with " + systemId);
int cntDeleted=0;
IndexReader reader = null;
IndexWriter writer = null;
try
{
Term term= new Term("systemId", systemId);
reader = IndexReader.open(indexDir);
cntDeleted = reader.deleteDocuments(term);
reader.close();
writer = new IndexWriter(indexDir, new StandardAnalyzer());
//writer.optimize();
writer.close();
}
catch (Exception e)
{
e.printStackTrace();
try
{
reader.close();
}
catch(Exception e2)
{
e2.printStackTrace();
return cntDeleted;
}
return cntDeleted;
}
return cntDeleted;
}
public Vector search(String s, String userId) throws Exception
{
if(debug)
System.out.println("FullTextUtil:search with " + s); if(debug) System.out.println("indexDir is " + indexDir);
String field = "contents";
String queries = null;
int repeat = 0;
int oldSmId=-1;
boolean raw = false;
String normsField = null;
IndexReader reader = IndexReader.open(indexDir);
Searcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer();
QueryParser parser = new QueryParser(field, analyzer);
Query query = parser.parse(s);
if(debug) System.out.println("Searching for: " + query.toString(field));
Hits hits = searcher.search(query);
if (repeat > 0)
{
Date start = new Date();
for (int i = 0; i < repeat; i++)
{
hits = searcher.search(query);
}
Date end = new Date();
if(debug) System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms");
}
if(debug) System.out.println(hits.length() + " total matching documents");
final int HITS_PER_PAGE = 100000;
Vector results = new Vector(hits.length());
for (int start = 0; start < hits.length(); start += HITS_PER_PAGE)
{
int end = Math.min(hits.length(), start + HITS_PER_PAGE);
for (int i = start; i < end; i++)
{
Document doc = hits.doc(i);
String title = doc.get("kcmititle");
String objectId = doc.get("objectId");
if(debug)
System.out.println(" Title:" + title + "<");
if(debug)
System.out.println(" ObjectId: " + objectId);
SearchResult sr = new SearchResult(title, objectId);
results.add((Object) sr);
}
}
reader.close();
return results;
}
}
I have offered several time to help make it a REAL database and search engine... Hopefully they will take your code and do something with it
I am worried JimRob will take the keys to the kingdom with him when he goes to his great reward (Hopefully MANY MANY years from now, JimRob!) and leave FR to its demise too...
Look at stupid sites like “MoveOn.org” they make MILLIONS - You would think the most powerful FREEDOM loving site would be able to go commercial without “selling out to ‘da man’”
We like capitalism here, after all.