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