SearchEngine.java revision 538
260N/A/*
260N/A * CDDL HEADER START
260N/A *
260N/A * The contents of this file are subject to the terms of the
260N/A * Common Development and Distribution License (the "License").
260N/A * You may not use this file except in compliance with the License.
260N/A *
260N/A * See LICENSE.txt included in this distribution for the specific
260N/A * language governing permissions and limitations under the License.
260N/A *
260N/A * When distributing Covered Code, include this CDDL HEADER in each
260N/A * file and include the License file at LICENSE.txt.
260N/A * If applicable, add the following below this CDDL HEADER, with the
260N/A * fields enclosed by brackets "[]" replaced with your own identifying
260N/A * information: Portions Copyright [yyyy] [name of copyright owner]
260N/A *
260N/A * CDDL HEADER END
260N/A */
260N/A
260N/A/*
1235N/A * Copyright 2005 Trond Norbye. All rights reserved.
1190N/A * Use is subject to license terms.
1185N/A */
260N/A
260N/Apackage org.opensolaris.opengrok.search;
260N/A
260N/Aimport java.io.BufferedReader;
260N/Aimport java.io.File;
260N/Aimport java.io.FileInputStream;
260N/Aimport java.io.FileNotFoundException;
260N/Aimport java.io.FileReader;
260N/Aimport java.io.IOException;
260N/Aimport java.io.InputStreamReader;
260N/Aimport java.io.Reader;
364N/Aimport java.util.ArrayList;
260N/Aimport java.util.Iterator;
1195N/Aimport java.util.List;
260N/Aimport java.util.logging.Level;
260N/Aimport org.apache.lucene.document.Document;
260N/Aimport org.apache.lucene.document.Fieldable;
1238N/Aimport org.apache.lucene.index.IndexReader;
1238N/Aimport org.apache.lucene.queryParser.QueryParser;
465N/Aimport org.apache.lucene.search.Hits;
344N/Aimport org.apache.lucene.search.IndexSearcher;
1190N/Aimport org.apache.lucene.search.Query;
260N/Aimport org.apache.lucene.search.Searcher;
260N/Aimport org.opensolaris.opengrok.OpenGrokLogger;
260N/Aimport org.opensolaris.opengrok.analysis.CompatibleAnalyser;
260N/Aimport org.opensolaris.opengrok.analysis.Definitions;
260N/Aimport org.opensolaris.opengrok.analysis.TagFilter;
260N/Aimport org.opensolaris.opengrok.configuration.Project;
260N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
260N/Aimport org.opensolaris.opengrok.search.Summary.Fragment;
260N/Aimport org.opensolaris.opengrok.search.context.Context;
260N/Aimport org.opensolaris.opengrok.search.context.HistoryContext;
260N/Aimport org.opensolaris.opengrok.web.Util;
260N/A
260N/A/**
260N/A * This is an encapsulation of the details on how to seach in the index
260N/A * database.
260N/A *
260N/A * @author Trond Norbye
260N/A */
260N/Apublic class SearchEngine {
260N/A /**
260N/A * Holds value of property definition.
260N/A */
260N/A private String definition;
260N/A
456N/A /**
260N/A * Holds value of property file.
260N/A */
260N/A private String file;
1185N/A
1185N/A /**
1185N/A * Holds value of property freetext.
1185N/A */
1115N/A private String freetext;
1185N/A
1185N/A /**
1185N/A * Holds value of property history.
272N/A */
1348N/A private String history;
1185N/A
1185N/A /**
1185N/A * Holds value of property symbol.
1185N/A */
1185N/A private String symbol;
1185N/A
1185N/A /**
1185N/A * Holds value of property indexDatabase.
1185N/A */
1235N/A private Query query;
1185N/A private final CompatibleAnalyser analyzer;
1185N/A private final QueryParser qparser;
1185N/A private Context sourceContext;
260N/A private HistoryContext historyContext;
894N/A private Summarizer summer;
465N/A private final List<org.apache.lucene.search.Hit> hits;
1327N/A private final char[] content = new char[1024*8];
1185N/A private String source;
1185N/A private String data;
1185N/A
1185N/A /**
1235N/A * Creates a new instance of SearchEngine
1235N/A */
1185N/A public SearchEngine() {
886N/A analyzer = new CompatibleAnalyser();
1185N/A qparser = new QueryParser("full", analyzer);
1327N/A qparser.setDefaultOperator(QueryParser.AND_OPERATOR);
1185N/A qparser.setAllowLeadingWildcard(RuntimeEnvironment.getInstance().isAllowLeadingWildcard());
260N/A hits = new ArrayList<org.apache.lucene.search.Hit>();
1185N/A }
1235N/A
1330N/A public boolean isValidQuery() {
260N/A boolean ret;
260N/A String qry = Util.buildQueryString(freetext, definition, symbol, file, history);
260N/A if (qry.length() > 0) {
260N/A try {
260N/A query = qparser.parse(qry);
260N/A ret = true;
260N/A } catch (Exception e) {
260N/A ret = false;
260N/A }
260N/A } else {
260N/A ret = false;
260N/A }
260N/A
260N/A return ret;
260N/A }
260N/A
260N/A private void searchSingleDatabase(File root) throws IOException {
260N/A IndexReader ireader = IndexReader.open(root);
260N/A Searcher searcher = new IndexSearcher(ireader);
260N/A Hits res = searcher.search(query);
260N/A if (res.length() > 0) {
260N/A Iterator iter = res.iterator();
260N/A while (iter.hasNext()) {
260N/A org.apache.lucene.search.Hit h = (org.apache.lucene.search.Hit) iter.next();
260N/A hits.add(h);
260N/A }
260N/A }
260N/A
260N/A }
260N/A
260N/A public String getQuery() {
260N/A return query.toString();
260N/A }
260N/A
260N/A /**
260N/A * Execute a search. Before calling this function, you must set the
260N/A * appropriate seach critera with the set-functions.
260N/A *
260N/A * @return The number of hits
260N/A */
260N/A public int search() {
260N/A source = RuntimeEnvironment.getInstance().getSourceRootPath();
260N/A data = RuntimeEnvironment.getInstance().getDataRootPath();
1195N/A hits.clear();
260N/A
260N/A String qry = Util.buildQueryString(freetext, definition, symbol, file, history);
260N/A if (qry.length() > 0) {
260N/A try {
260N/A query = qparser.parse(qry);
260N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
260N/A File root = new File(env.getDataRootFile(), "index");
260N/A
260N/A if (env.hasProjects()) {
1044N/A // search all projects
260N/A for (Project project : env.getProjects()) {
260N/A searchSingleDatabase(new File(root, project.getPath()));
260N/A }
260N/A } else {
260N/A // search the index database
260N/A searchSingleDatabase(root);
260N/A }
260N/A } catch (Exception e) {
260N/A OpenGrokLogger.getLogger().log(Level.WARNING, "Exception searching", e);
260N/A }
260N/A }
260N/A if (!hits.isEmpty()) {
260N/A sourceContext = null;
260N/A summer = null;
260N/A try {
260N/A sourceContext = new Context(query);
260N/A if(sourceContext.isEmpty()) {
260N/A sourceContext = null;
260N/A }
260N/A summer = new Summarizer(query, analyzer);
260N/A } catch (Exception e) {
260N/A OpenGrokLogger.getLogger().log(Level.WARNING, "An error occured while creating summary", e);
260N/A }
260N/A
260N/A historyContext = null;
260N/A try {
260N/A historyContext = new HistoryContext(query);
260N/A if(historyContext.isEmpty()) {
260N/A historyContext = null;
1195N/A }
260N/A } catch (Exception e) {
260N/A OpenGrokLogger.getLogger().log(Level.WARNING, "An error occured while getting history context", e);
260N/A }
260N/A }
260N/A return hits.size();
364N/A }
364N/A
1190N/A public void more(int start, int end, List<Hit> ret) {
364N/A int len = end;
364N/A if (end > hits.size()) {
800N/A len = hits.size();
364N/A }
364N/A for (int ii = start; ii < len; ++ii) {
1190N/A boolean alt = (ii % 2 == 0);
540N/A boolean hasContext = false;
540N/A try {
1190N/A Document doc = hits.get(ii).getDocument();
540N/A String filename = doc.get("path");
540N/A String genre = doc.get("t");
540N/A Definitions tags = null;
540N/A Fieldable tagsField = doc.getFieldable("tags");
540N/A if (tagsField != null) {
540N/A tags = Definitions.deserialize(tagsField.binaryValue());
540N/A }
540N/A int nhits = hits.size();
1185N/A
540N/A if(sourceContext != null) {
540N/A try {
260N/A 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 l = 0;
Reader r = new TagFilter(new BufferedReader(new FileReader(data + "/xref" + filename)));
try {
l = r.read(content);
} finally {
r.close();
}
Summary sum = summer.getSummary(new String(content, 0, l));
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 {
OpenGrokLogger.getLogger().warning("Unknown genre: " + 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;
}
}