/*
* SearchKeyWords.java
*
* Created on April 17, 2007, 10:37 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package cometvis.index.search;
/**
*
* @author louiebagz
*/
/*
* SearchChatFile.java
*
* Created on April 10, 2007, 10:24 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.awt.Color;
import java.io.StringReader;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.Sort;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import java.io.File;
import java.io.IOException;
/**
* Search class provides a simple
* example of searching with Lucene. It looks for an entry whose
* 'msg' field contains keyword 'heart'. The index being searched
* is called "chatFile", located in a temporary directory.
*/
public class SearchKeyWords extends javax.swing.JFrame
{
public SearchKeyWords()
{
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//
private void initComponents() {
searchResultsPanel = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
searchResultsTextArea = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
searchResultsTextArea.setColumns(20);
searchResultsTextArea.setEditable(false);
searchResultsTextArea.setLineWrap(true);
searchResultsTextArea.setRows(20);
searchResultsTextArea.setInheritsPopupMenu(true);
jScrollPane1.setViewportView(searchResultsTextArea);
org.jdesktop.layout.GroupLayout searchResultsPanelLayout = new org.jdesktop.layout.GroupLayout(searchResultsPanel);
searchResultsPanel.setLayout(searchResultsPanelLayout);
searchResultsPanelLayout.setHorizontalGroup(
searchResultsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE)
);
searchResultsPanelLayout.setVerticalGroup(
searchResultsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(searchResultsPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(searchResultsPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}//
public void searchString(String strQuery)
{
//String indexDir =
//System.getProperty("java.io.tmpdir", "tmp") +
//System.getProperty("file.separator") + "chatFile";
String indexDir = "/Volumes/Bagz/My Thesis/Index/KeyWordsIndex";
//SimpleAnalyzer analyzer = new SimpleAnalyzer();
analyzer = new StandardAnalyzer();
try
{
searcher = new IndexSearcher(indexDir);
//Query query = new TermQuery(new Term("author", "tutor"));
//query = QueryParser.parse(strQuery, "msg", analyzer);
query = MultiFieldQueryParser.parse(strQuery,"kw", analyzer);
hits = searcher.search(query, Sort.INDEXORDER);
showResults(hits, strQuery);
}
catch (ParseException pe)
{
pe.getMessage();
pe.printStackTrace();
}
catch (IOException ioe)
{
ioe.getMessage();
ioe.printStackTrace();
}
}
public void showResults (Hits h, String strQuery)
{
searchResultsTextArea.append("Number of Matching Hits: " + h.length() + newline);
if (h.length() > 0)
{
for (int i = 0; i < h.length(); i++)
{
try
{
searchResultsTextArea.append(h.doc(i).get("lineNum") + " : " + h.doc(i).get("kw") + newline);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
else {
searchResultsTextArea.append("String query " + strQuery + " not found!" + newline);
}
highlight (searchResultsTextArea, strQuery);
}
// Creates highlights around all occurrences of pattern in textComp
public void highlight(JTextComponent textComp, String pattern) {
// First remove all old highlights
removeHighlights(textComp);
myHighlightPainter = new MyHighlightPainter1(Color.YELLOW);
try {
Highlighter hilite = textComp.getHighlighter();
Document doc = textComp.getDocument();
String text = doc.getText(0, doc.getLength()).toLowerCase();
int pos = 0;
// Search for pattern
while ((pos = text.indexOf(pattern, pos)) >= 0) {
// Create highlighter using private painter and apply around pattern
hilite.addHighlight(pos, pos+pattern.length(), myHighlightPainter);
pos += pattern.length();
}
} catch (BadLocationException e) {
}
}
// Removes only our private highlights
public void removeHighlights(JTextComponent textComp) {
Highlighter hilite = textComp.getHighlighter();
Highlighter.Highlight[] hilites = hilite.getHighlights();
for (int i=0; i