Created
July 5, 2010 20:29
-
-
Save ariejan/464660 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package nl.kabisa.mgl.rssninja.beans; | |
import java.io.IOException; | |
import java.util.Date; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.ejb.ActivationConfigProperty; | |
import javax.ejb.MessageDriven; | |
import javax.jms.JMSException; | |
import javax.jms.Message; | |
import javax.jms.MessageListener; | |
import javax.jms.TextMessage; | |
import org.apache.lucene.analysis.standard.StandardAnalyzer; | |
import org.apache.lucene.document.Document; | |
import org.apache.lucene.document.Field; | |
import org.apache.lucene.index.CorruptIndexException; | |
import org.apache.lucene.index.IndexWriter; | |
import org.apache.lucene.queryParser.ParseException; | |
import org.apache.lucene.queryParser.QueryParser; | |
import org.apache.lucene.search.IndexSearcher; | |
import org.apache.lucene.search.Query; | |
import org.apache.lucene.search.ScoreDoc; | |
import org.apache.lucene.search.Searcher; | |
import org.apache.lucene.search.TopDocs; | |
import org.apache.lucene.store.LockObtainFailedException; | |
import org.apache.lucene.store.RAMDirectory; | |
import org.apache.lucene.util.Version; | |
/** | |
* | |
* @author ariejan | |
*/ | |
@MessageDriven(mappedName = "jms/filterQueue", activationConfig = { | |
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), | |
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") | |
}) | |
public class LuceneFilterBean implements MessageListener { | |
/** | |
* Make a Document object with an un-indexed title field and an | |
* indexed content field. | |
*/ | |
private static Document createDocument(String title, String content) { | |
Document doc = new Document(); | |
doc.add(new Field("title", title, Field.Store.YES, Field.Index.NO)); | |
doc.add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED)); | |
return doc; | |
} | |
@Override | |
public void onMessage(Message message) { | |
// Construct a RAMDirectory to hold the in-memory representation | |
// of the index. | |
long start = System.currentTimeMillis(); | |
RAMDirectory idx = new RAMDirectory(); | |
TextMessage text = (TextMessage) message; | |
try { | |
// Make an writer to create the index | |
IndexWriter writer = new IndexWriter(idx, | |
new StandardAnalyzer(Version.LUCENE_30), | |
true, | |
IndexWriter.MaxFieldLength.UNLIMITED); | |
// Add some Document objects containing quotes | |
writer.addDocument(createDocument("Theodore Roosevelt", | |
"It behooves every man to remember that the work of the " + | |
"critic, is of altogether secondary importance, and that, " + | |
"in the end, progress is accomplished by the man who does " + | |
"things.")); | |
writer.addDocument(createDocument("Friedrich Hayek", | |
"The case for individual freedom rests largely on the " + | |
"recognition of the inevitable and universal ignorance " + | |
"of all of us concerning a great many of the factors on " + | |
"which the achievements of our ends and welfare depend.")); | |
writer.addDocument(createDocument("Ayn Rand", | |
"There is nothing to take a man's freedom away from " + | |
"him, save other men. To be free, a man must be free " + | |
"of his brothers.")); | |
writer.addDocument(createDocument("Mohandas Gandhi", | |
"Freedom is not worth having if it does not connote " + | |
"freedom to err.")); | |
writer.addDocument(createDocument("Incoming message", text.getText())); | |
// Optimize and close the writer to finish building the index | |
writer.optimize(); | |
writer.close(); | |
System.out.println("== Indexed in: " + (System.currentTimeMillis() - start) + "ms."); | |
// Build an IndexSearcher using the in-memory index | |
Searcher searcher = new IndexSearcher(idx); | |
search(searcher, "freedom"); | |
search(searcher, "free"); | |
search(searcher, "progress or achievements"); | |
searcher.close(); | |
Date endTime = new Date(); | |
System.out.println("== Total elapsed: " + (System.currentTimeMillis() - start) + "ms."); | |
} catch (JMSException ex) { | |
Logger.getLogger(LuceneFilterBean.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
catch (ParseException ex) { | |
Logger.getLogger(LuceneFilterBean.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
catch (CorruptIndexException ex) { | |
Logger.getLogger(LuceneFilterBean.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
catch (LockObtainFailedException ex) { | |
Logger.getLogger(LuceneFilterBean.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
catch (IOException ex) { | |
Logger.getLogger(LuceneFilterBean.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
finally { | |
// Stuffs | |
} | |
} | |
private static void search(Searcher searcher, String queryString) | |
throws ParseException, IOException { | |
// Build a Query object | |
QueryParser parser = new QueryParser(Version.LUCENE_30, "content", new StandardAnalyzer(Version.LUCENE_30)); | |
Query query = parser.parse(queryString); | |
// Search for the query | |
// Hits hits = searcher.search(query); | |
TopDocs hits = searcher.search(query, 10); | |
// Examine the Hits object to see if there were any matches | |
int hitCount = hits.totalHits; | |
if (hitCount == 0) { | |
System.out.println( | |
"No matches were found for \"" + queryString + "\""); | |
} | |
else { | |
System.out.println("Hits for \"" + | |
queryString + "\" were found in quotes by:"); | |
// Iterate over the Documents in the Hits object | |
for (int i = 0; i < hitCount; i++) { | |
ScoreDoc doc = hits.scoreDocs[i]; | |
// Print the value that we stored in the "title" field. Note | |
// that this Field was not indexed, but (unlike the | |
// "contents" field) was stored verbatim and can be | |
// retrieved. | |
System.out.println(" " + (i + 1) + ". " + doc.toString()); | |
} | |
} | |
System.out.println(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INFO: Sending message: Shields up! Rrred Alert! | |
INFO: == Indexed in: 233ms. | |
INFO: Hits for "freedom" were found in quotes by: | |
INFO: 1. doc=3 score=0.64866984 | |
INFO: 2. doc=2 score=0.3057859 | |
INFO: 3. doc=1 score=0.22933942 | |
INFO: Hits for "free" were found in quotes by: | |
INFO: 1. doc=2 score=0.6775111 | |
INFO: Hits for "progress or achievements" were found in quotes by: | |
INFO: 1. doc=0 score=0.16937779 | |
INFO: 2. doc=1 score=0.12703334 | |
INFO: == Total elapsed: 346ms. | |
-- Obviously, the first run is slow. | |
INFO: Sending message: Shields up! Rrred Alert! | |
INFO: == Indexed in: 3ms. | |
INFO: Hits for "freedom" were found in quotes by: | |
INFO: 1. doc=3 score=0.64866984 | |
INFO: 2. doc=2 score=0.3057859 | |
INFO: 3. doc=1 score=0.22933942 | |
INFO: Hits for "free" were found in quotes by: | |
INFO: 1. doc=2 score=0.6775111 | |
INFO: Hits for "progress or achievements" were found in quotes by: | |
INFO: 1. doc=0 score=0.16937779 | |
INFO: 2. doc=1 score=0.12703334 | |
INFO: == Total elapsed: 6ms. | |
-- Next runs are pretty fast :) | |
INFO: Sending message: Shields up! Rrred Alert! | |
INFO: == Indexed in: 3ms. | |
INFO: Hits for "freedom" were found in quotes by: | |
INFO: 1. doc=3 score=0.64866984 | |
INFO: 2. doc=2 score=0.3057859 | |
INFO: 3. doc=1 score=0.22933942 | |
INFO: Hits for "free" were found in quotes by: | |
INFO: 1. doc=2 score=0.6775111 | |
INFO: Hits for "progress or achievements" were found in quotes by: | |
INFO: 1. doc=0 score=0.16937779 | |
INFO: 2. doc=1 score=0.12703334 | |
INFO: == Total elapsed: 6ms. | |
INFO: Sending message: Shields up! Rrred Alert! | |
INFO: == Indexed in: 4ms. | |
INFO: Hits for "freedom" were found in quotes by: | |
INFO: 1. doc=3 score=0.64866984 | |
INFO: 2. doc=2 score=0.3057859 | |
INFO: 3. doc=1 score=0.22933942 | |
INFO: Hits for "free" were found in quotes by: | |
INFO: 1. doc=2 score=0.6775111 | |
INFO: Hits for "progress or achievements" were found in quotes by: | |
INFO: 1. doc=0 score=0.16937779 | |
INFO: 2. doc=1 score=0.12703334 | |
INFO: == Total elapsed: 7ms. | |
INFO: Sending message: Shields up! Rrred Alert! | |
INFO: == Indexed in: 4ms. | |
INFO: Hits for "freedom" were found in quotes by: | |
INFO: 1. doc=3 score=0.64866984 | |
INFO: 2. doc=2 score=0.3057859 | |
INFO: 3. doc=1 score=0.22933942 | |
INFO: Hits for "free" were found in quotes by: | |
INFO: 1. doc=2 score=0.6775111 | |
INFO: Hits for "progress or achievements" were found in quotes by: | |
INFO: 1. doc=0 score=0.16937779 | |
INFO: 2. doc=1 score=0.12703334 | |
INFO: == Total elapsed: 6ms. | |
INFO: Sending message: Shields up! Rrred Alert! | |
INFO: == Indexed in: 3ms. | |
INFO: Hits for "freedom" were found in quotes by: | |
INFO: 1. doc=3 score=0.64866984 | |
INFO: 2. doc=2 score=0.3057859 | |
INFO: 3. doc=1 score=0.22933942 | |
INFO: Hits for "free" were found in quotes by: | |
INFO: 1. doc=2 score=0.6775111 | |
INFO: Hits for "progress or achievements" were found in quotes by: | |
INFO: 1. doc=0 score=0.16937779 | |
INFO: 2. doc=1 score=0.12703334 | |
INFO: == Total elapsed: 6ms. | |
INFO: Sending message: Shields up! Rrred Alert! | |
INFO: == Indexed in: 3ms. | |
INFO: Hits for "freedom" were found in quotes by: | |
INFO: 1. doc=3 score=0.64866984 | |
INFO: 2. doc=2 score=0.3057859 | |
INFO: 3. doc=1 score=0.22933942 | |
INFO: Hits for "free" were found in quotes by: | |
INFO: 1. doc=2 score=0.6775111 | |
INFO: Hits for "progress or achievements" were found in quotes by: | |
INFO: 1. doc=0 score=0.16937779 | |
INFO: 2. doc=1 score=0.12703334 | |
INFO: == Total elapsed: 6ms. | |
INFO: Sending message: Shields up! Rrred Alert! | |
INFO: == Indexed in: 3ms. | |
INFO: Hits for "freedom" were found in quotes by: | |
INFO: 1. doc=3 score=0.64866984 | |
INFO: 2. doc=2 score=0.3057859 | |
INFO: 3. doc=1 score=0.22933942 | |
INFO: Hits for "free" were found in quotes by: | |
INFO: 1. doc=2 score=0.6775111 | |
INFO: Hits for "progress or achievements" were found in quotes by: | |
INFO: 1. doc=0 score=0.16937779 | |
INFO: 2. doc=1 score=0.12703334 | |
INFO: == Total elapsed: 6ms. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment