SearchEngine.java revision 428
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
407N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * See LICENSE.txt included in this distribution for the specific
0N/A * language governing permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at LICENSE.txt.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A
1237N/A/*
1190N/A * Copyright 2005 Trond Norbye. All rights reserved.
1185N/A * Use is subject to license terms.
0N/A */
0N/A
0N/Apackage org.opensolaris.opengrok.search;
388N/A
1185N/Aimport java.io.BufferedReader;
388N/Aimport java.io.File;
388N/Aimport java.io.FileInputStream;
388N/Aimport java.io.FileNotFoundException;
388N/Aimport java.io.FileReader;
388N/Aimport java.io.IOException;
388N/Aimport java.io.InputStreamReader;
388N/Aimport java.io.Reader;
508N/Aimport java.util.ArrayList;
1327N/Aimport java.util.Iterator;
1185N/Aimport java.util.List;
388N/Aimport java.util.logging.Level;
388N/Aimport org.apache.lucene.document.Document;
1185N/Aimport org.apache.lucene.document.Fieldable;
1327N/Aimport org.apache.lucene.index.IndexReader;
819N/Aimport org.apache.lucene.queryParser.QueryParser;
350N/Aimport org.apache.lucene.search.Hits;
1185N/Aimport org.apache.lucene.search.IndexSearcher;
0N/Aimport org.apache.lucene.search.Query;
1385N/Aimport org.apache.lucene.search.Searcher;
615N/Aimport org.opensolaris.opengrok.OpenGrokLogger;
1195N/Aimport org.opensolaris.opengrok.analysis.CompatibleAnalyser;
1185N/Aimport org.opensolaris.opengrok.analysis.Definitions;
1185N/Aimport org.opensolaris.opengrok.analysis.TagFilter;
388N/Aimport org.opensolaris.opengrok.configuration.Project;
0N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
1111N/Aimport org.opensolaris.opengrok.search.Summary.Fragment;
1185N/Aimport org.opensolaris.opengrok.search.context.Context;
1111N/Aimport org.opensolaris.opengrok.search.context.HistoryContext;
456N/Aimport org.opensolaris.opengrok.web.Util;
1327N/A
456N/A/**
456N/A * This is an encapsulation of the details on how to seach in the index
456N/A * database.
1185N/A *
1111N/A * @author Trond Norbye
1185N/A */
1185N/Apublic class SearchEngine {
1185N/A /**
1185N/A * Holds value of property definition.
1185N/A */
1185N/A private String definition;
1185N/A
1111N/A /**
1111N/A * Holds value of property file.
1237N/A */
1318N/A private String file;
1190N/A
615N/A /**
1190N/A * Holds value of property freetext.
1185N/A */
1185N/A private String freetext;
1185N/A
819N/A /**
0N/A * Holds value of property history.
1185N/A */
0N/A private String history;
1185N/A
0N/A /**
0N/A * Holds value of property symbol.
0N/A */
0N/A private String symbol;
0N/A
1185N/A /**
1185N/A * Holds value of property indexDatabase.
1190N/A */
1385N/A private Query query;
1185N/A private CompatibleAnalyser analyzer;
1185N/A private QueryParser qparser;
1185N/A private Context sourceContext;
1185N/A private HistoryContext historyContext;
1385N/A private Summarizer summer;
1185N/A private List<org.apache.lucene.search.Hit> hits;
1385N/A private char[] content = new char[1024*8];
1385N/A private String source;
1185N/A private String data;
1385N/A
1185N/A /**
1185N/A * Creates a new instance of SearchEngine
1185N/A */
1185N/A public SearchEngine() {
1385N/A analyzer = new CompatibleAnalyser();
1385N/A qparser = new QueryParser("full", analyzer);
1327N/A qparser.setDefaultOperator(QueryParser.AND_OPERATOR);
1185N/A qparser.setAllowLeadingWildcard(RuntimeEnvironment.getInstance().isAllowLeadingWildcard());
1195N/A hits = new ArrayList<org.apache.lucene.search.Hit>();
1185N/A }
1185N/A
1185N/A public boolean isValidQuery() {
460N/A boolean ret = false;
1185N/A String qry = Util.buildQueryString(freetext, definition, symbol, file, history);
1185N/A if (qry.length() > 0) {
1185N/A try {
1185N/A qparser.parse(qry);
1185N/A ret = true;
1185N/A } catch (Exception e) {
1185N/A }
1185N/A }
1185N/A
1185N/A return ret;
1185N/A }
1190N/A
1185N/A private void searchSingleDatabase(File root) throws Exception {
1185N/A IndexReader ireader = IndexReader.open(root);
1190N/A Searcher searcher = new IndexSearcher(ireader);
1185N/A Hits res = searcher.search(query);
1185N/A if (res.length() > 0) {
1185N/A Iterator iter = res.iterator();
1185N/A while (iter.hasNext()) {
1190N/A org.apache.lucene.search.Hit h = (org.apache.lucene.search.Hit) iter.next();
1190N/A hits.add(h);
1190N/A }
1185N/A }
1190N/A
1185N/A }
1185N/A
1185N/A public String getQuery() {
1185N/A return query.toString();
1185N/A }
1185N/A
1185N/A /**
1185N/A * Execute a search. Before calling this function, you must set the
1185N/A * appropriate seach critera with the set-functions.
1185N/A *
158N/A * @return The number of hits
1237N/A */
1237N/A public int search() {
1185N/A source = RuntimeEnvironment.getInstance().getSourceRootPath();
1185N/A data = RuntimeEnvironment.getInstance().getDataRootPath();
1390N/A hits.clear();
1185N/A
1185N/A String qry = Util.buildQueryString(freetext, definition, symbol, file, history);
1185N/A if (qry.length() > 0) {
1185N/A try {
1185N/A query = qparser.parse(qry);
1185N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
1390N/A File root = new File(env.getDataRootFile(), "index");
1185N/A
1390N/A if (env.hasProjects()) {
1185N/A // search all projects
1185N/A for (Project project : env.getProjects()) {
1185N/A searchSingleDatabase(new File(root, project.getPath()));
0N/A }
1185N/A } else {
1390N/A // search the index database
1185N/A searchSingleDatabase(root);
1185N/A }
1185N/A } catch (Exception e) {
1185N/A OpenGrokLogger.getLogger().log(Level.WARNING, "Exception searching", e);
1185N/A }
1185N/A }
1185N/A if (hits.size() > 0) {
1185N/A sourceContext = null;
1185N/A summer = null;
1111N/A try {
1390N/A sourceContext = new Context(query);
1185N/A if(sourceContext.isEmpty()) {
1185N/A sourceContext = null;
1185N/A }
1185N/A summer = new Summarizer(query, analyzer);
1390N/A } catch (Exception e) {
1185N/A }
1185N/A
350N/A historyContext = null;
350N/A try {
350N/A historyContext = new HistoryContext(query);
928N/A if(historyContext.isEmpty()) {
350N/A historyContext = null;
1185N/A }
1385N/A } catch (Exception e) {
1185N/A }
1185N/A }
1185N/A return hits.size();
1185N/A }
1185N/A
1185N/A public void more(int start, int end, List<Hit> ret) {
1185N/A if (end > hits.size()) {
1185N/A end = hits.size();
1190N/A }
1185N/A for (int ii = start; ii < end; ++ii) {
1190N/A boolean alt = (ii % 2 == 0);
1190N/A boolean hasContext = false;
1185N/A try {
0N/A Document doc = hits.get(ii).getDocument();
0N/A String filename = doc.get("path");
1185N/A String genre = doc.get("t");
1190N/A Definitions tags = null;
1185N/A Fieldable tagsField = doc.getFieldable("tags");
0N/A if (tagsField != null) {
0N/A tags = Definitions.deserialize(tagsField.binaryValue());
0N/A }
0N/A int nhits = hits.size();
0N/A
0N/A if(sourceContext != null) {
try {
if ("p".equals(genre) && (source != null)) {
hasContext = sourceContext.getContext(new InputStreamReader(new FileInputStream(source +
filename)), null, null, null, filename,
tags, nhits > 100, ret);
} else if("x".equals(genre) && data != null && summer != null){
int len = 0;
Reader r = new TagFilter(new BufferedReader(new FileReader(data + "/xref" + filename)));
try {
len = r.read(content);
} finally {
r.close();
}
Summary sum = summer.getSummary(new String(content, 0, len));
Fragment fragments[] = sum.getFragments();
for (int jj = 0; jj < fragments.length; ++jj) {
String match = fragments[jj].toString();
if (match.length() > 0) {
if (!fragments[jj].isEllipsis()) {
Hit hit = new Hit(filename, fragments[jj].toString(), "", true, alt);
ret.add(hit);
}
hasContext = true;
}
}
// } else if("h".equals(genre) && indexDatabase.getSource() != null && summer != null){
// Reader r = new TagFilter(new BufferedReader(new FileReader(srcRoot + rpath)));
// int len = r.read(content);
// out.write(summer.getSummary(new String(content, 0, len)).toString());
} else {
System.out.println(genre);
hasContext |= sourceContext.getContext(null, null, null, null, filename, tags, false, ret);
}
} catch (FileNotFoundException exp) {
hasContext |= sourceContext.getContext(null, null, null, null, filename, tags, false, ret);
}
}
if (historyContext != null) {
hasContext |= historyContext.getContext(source + filename, filename, ret);
}
if(!hasContext) {
ret.add(new Hit(filename, "...", "", false, alt));
}
} catch (IOException e) {
OpenGrokLogger.getLogger().log(Level.WARNING, "Exception searching", e);
} catch (ClassNotFoundException e) {
OpenGrokLogger.getLogger().log(Level.WARNING, "Exception searching", e);
}
}
}
/**
* Getter for property definition.
*
* @return Value of property definition.
*/
public String getDefinition() {
return this.definition;
}
/**
* Setter for property definition.
*
* @param definition New value of property definition.
*/
public void setDefinition(String definition) {
this.definition = definition;
}
/**
* Getter for property file.
*
* @return Value of property file.
*/
public String getFile() {
return this.file;
}
/**
* Setter for property file.
*
* @param file New value of property file.
*/
public void setFile(String file) {
this.file = file;
}
/**
* Getter for property freetext.
*
* @return Value of property freetext.
*/
public String getFreetext() {
return this.freetext;
}
/**
* Setter for property freetext.
*
* @param freetext New value of property freetext.
*/
public void setFreetext(String freetext) {
this.freetext = freetext;
}
/**
* Getter for property history.
*
* @return Value of property history.
*/
public String getHistory() {
return this.history;
}
/**
* Setter for property history.
*
* @param history New value of property history.
*/
public void setHistory(String history) {
this.history = history;
}
/**
* Getter for property symbol.
*
* @return Value of property symbol.
*/
public String getSymbol() {
return this.symbol;
}
/**
* Setter for property symbol.
*
* @param symbol New value of property symbol.
*/
public void setSymbol(String symbol) {
this.symbol = symbol;
}
boolean onlyFilnameSearch() {
return sourceContext == null && historyContext == null;
}
}