SearchEngine.java revision 405
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/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
405N/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
0N/A/*
0N/A * Copyright 2005 Trond Norbye. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/A
376N/Apackage org.opensolaris.opengrok.search;
0N/A
0N/Aimport java.io.BufferedReader;
234N/Aimport java.io.File;
0N/Aimport java.io.FileInputStream;
0N/Aimport java.io.FileNotFoundException;
0N/Aimport java.io.FileReader;
0N/Aimport java.io.IOException;
0N/Aimport java.io.InputStreamReader;
0N/Aimport java.io.Reader;
234N/Aimport java.util.ArrayList;
234N/Aimport java.util.Iterator;
0N/Aimport java.util.List;
0N/Aimport org.apache.lucene.document.Document;
350N/Aimport org.apache.lucene.document.Fieldable;
0N/Aimport org.apache.lucene.index.IndexReader;
0N/Aimport org.apache.lucene.queryParser.QueryParser;
0N/Aimport org.apache.lucene.search.Hits;
0N/Aimport org.apache.lucene.search.IndexSearcher;
0N/Aimport org.apache.lucene.search.Query;
0N/Aimport org.apache.lucene.search.Searcher;
0N/Aimport org.opensolaris.opengrok.analysis.CompatibleAnalyser;
350N/Aimport org.opensolaris.opengrok.analysis.Definitions;
0N/Aimport org.opensolaris.opengrok.analysis.TagFilter;
234N/Aimport org.opensolaris.opengrok.configuration.Project;
125N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
0N/Aimport org.opensolaris.opengrok.search.Summary.Fragment;
0N/Aimport org.opensolaris.opengrok.search.context.Context;
0N/Aimport org.opensolaris.opengrok.search.context.HistoryContext;
226N/Aimport org.opensolaris.opengrok.web.Util;
0N/A
0N/A/**
0N/A * This is an encapsulation of the details on how to seach in the index
0N/A * database.
0N/A *
0N/A * @author Trond Norbye
0N/A */
0N/Apublic class SearchEngine {
0N/A /**
0N/A * Holds value of property definition.
0N/A */
0N/A private String definition;
0N/A
0N/A /**
0N/A * Holds value of property file.
0N/A */
0N/A private String file;
0N/A
0N/A /**
0N/A * Holds value of property freetext.
0N/A */
0N/A private String freetext;
0N/A
0N/A /**
0N/A * Holds value of property history.
0N/A */
0N/A private String history;
0N/A
0N/A /**
0N/A * Holds value of property symbol.
0N/A */
0N/A private String symbol;
234N/A
0N/A /**
0N/A * Holds value of property indexDatabase.
0N/A */
0N/A private Query query;
0N/A private CompatibleAnalyser analyzer;
0N/A private QueryParser qparser;
0N/A private Context sourceContext;
0N/A private HistoryContext historyContext;
0N/A private Summarizer summer;
234N/A private List<org.apache.lucene.search.Hit> hits;
0N/A private char[] content = new char[1024*8];
234N/A private String source;
234N/A private String data;
234N/A
0N/A /**
0N/A * Creates a new instance of SearchEngine
0N/A */
0N/A public SearchEngine() {
0N/A analyzer = new CompatibleAnalyser();
0N/A qparser = new QueryParser("full", analyzer);
99N/A qparser.setDefaultOperator(QueryParser.AND_OPERATOR);
125N/A qparser.setAllowLeadingWildcard(RuntimeEnvironment.getInstance().isAllowLeadingWildcard());
234N/A hits = new ArrayList<org.apache.lucene.search.Hit>();
234N/A }
236N/A
236N/A public boolean isValidQuery() {
236N/A boolean ret = false;
236N/A String qry = Util.buildQueryString(freetext, definition, symbol, file, history);
236N/A if (qry.length() > 0) {
236N/A try {
236N/A qparser.parse(qry);
236N/A ret = true;
236N/A } catch (Exception e) {
236N/A }
236N/A }
236N/A
236N/A return ret;
236N/A }
234N/A
234N/A private void searchSingleDatabase(File root) throws Exception {
235N/A IndexReader ireader = IndexReader.open(root);
235N/A Searcher searcher = new IndexSearcher(ireader);
235N/A Hits res = searcher.search(query);
235N/A if (res.length() > 0) {
235N/A Iterator iter = res.iterator();
235N/A while (iter.hasNext()) {
235N/A org.apache.lucene.search.Hit h = (org.apache.lucene.search.Hit) iter.next();
235N/A hits.add(h);
235N/A }
235N/A }
235N/A
235N/A }
235N/A
235N/A public String getQuery() {
235N/A return query.toString();
0N/A }
0N/A
0N/A /**
0N/A * Execute a search. Before calling this function, you must set the
0N/A * appropriate seach critera with the set-functions.
0N/A *
234N/A * @return The number of hits
0N/A */
0N/A public int search() {
235N/A source = RuntimeEnvironment.getInstance().getSourceRootPath();
235N/A data = RuntimeEnvironment.getInstance().getDataRootPath();
234N/A hits.clear();
235N/A
226N/A String qry = Util.buildQueryString(freetext, definition, symbol, file, history);
226N/A if (qry.length() > 0) {
0N/A try {
226N/A query = qparser.parse(qry);
234N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
234N/A File root = new File(env.getDataRootFile(), "index");
234N/A
234N/A if (env.hasProjects()) {
234N/A // search all projects
234N/A for (Project project : env.getProjects()) {
234N/A searchSingleDatabase(new File(root, project.getPath()));
234N/A }
234N/A } else {
234N/A // search the index database
234N/A searchSingleDatabase(root);
0N/A }
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A }
0N/A }
234N/A if (hits.size() > 0) {
0N/A sourceContext = null;
0N/A summer = null;
0N/A try {
0N/A sourceContext = new Context(query);
4N/A if(sourceContext.isEmpty()) {
0N/A sourceContext = null;
4N/A }
0N/A summer = new Summarizer(query, analyzer);
0N/A } catch (Exception e) {
0N/A }
0N/A
0N/A historyContext = null;
0N/A try {
0N/A historyContext = new HistoryContext(query);
4N/A if(historyContext.isEmpty()) {
0N/A historyContext = null;
4N/A }
0N/A } catch (Exception e) {
0N/A }
0N/A }
234N/A return hits.size();
0N/A }
0N/A
0N/A public void more(int start, int end, List<Hit> ret) {
234N/A if (end > hits.size()) {
234N/A end = hits.size();
234N/A }
0N/A for (int ii = start; ii < end; ++ii) {
0N/A boolean alt = (ii % 2 == 0);
0N/A boolean hasContext = false;
0N/A try {
234N/A Document doc = hits.get(ii).getDocument();
0N/A String filename = doc.get("path");
0N/A String genre = doc.get("t");
350N/A Definitions tags = null;
350N/A Fieldable tagsField = doc.getFieldable("tags");
350N/A if (tagsField != null) {
350N/A tags = Definitions.deserialize(tagsField.binaryValue());
350N/A }
234N/A int nhits = hits.size();
0N/A
0N/A if(sourceContext != null) {
0N/A try {
234N/A if ("p".equals(genre) && (source != null)) {
234N/A hasContext = sourceContext.getContext(new InputStreamReader(new FileInputStream(source +
0N/A filename)), null, null, null, filename,
0N/A tags, nhits > 100, ret);
234N/A } else if("x".equals(genre) && data != null && summer != null){
380N/A int len = 0;
234N/A Reader r = new TagFilter(new BufferedReader(new FileReader(data + "/xref" + filename)));
380N/A try {
380N/A len = r.read(content);
380N/A } finally {
380N/A r.close();
380N/A }
0N/A Summary sum = summer.getSummary(new String(content, 0, len));
0N/A Fragment fragments[] = sum.getFragments();
0N/A for (int jj = 0; jj < fragments.length; ++jj) {
0N/A String match = fragments[jj].toString();
0N/A if (match.length() > 0) {
0N/A if (!fragments[jj].isEllipsis()) {
0N/A Hit hit = new Hit(filename, fragments[jj].toString(), "", true, alt);
0N/A ret.add(hit);
0N/A }
0N/A hasContext = true;
0N/A }
0N/A }
0N/A// } else if("h".equals(genre) && indexDatabase.getSource() != null && summer != null){
0N/A// Reader r = new TagFilter(new BufferedReader(new FileReader(srcRoot + rpath)));
0N/A// int len = r.read(content);
0N/A// out.write(summer.getSummary(new String(content, 0, len)).toString());
0N/A } else {
0N/A System.out.println(genre);
0N/A hasContext |= sourceContext.getContext(null, null, null, null, filename, tags, false, ret);
0N/A }
0N/A } catch (FileNotFoundException exp) {
0N/A hasContext |= sourceContext.getContext(null, null, null, null, filename, tags, false, ret);
0N/A }
0N/A }
0N/A if (historyContext != null) {
234N/A hasContext |= historyContext.getContext(source + filename, filename, ret);
0N/A }
0N/A if(!hasContext) {
0N/A ret.add(new Hit(filename, "...", "", false, alt));
0N/A }
0N/A } catch (IOException e) {
32N/A e.printStackTrace();
350N/A } catch (ClassNotFoundException e) {
350N/A e.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Getter for property definition.
0N/A *
0N/A * @return Value of property definition.
0N/A */
0N/A public String getDefinition() {
0N/A return this.definition;
0N/A }
0N/A
0N/A /**
0N/A * Setter for property definition.
0N/A *
0N/A * @param definition New value of property definition.
0N/A */
0N/A public void setDefinition(String definition) {
0N/A this.definition = definition;
0N/A }
0N/A
0N/A /**
0N/A * Getter for property file.
0N/A *
0N/A * @return Value of property file.
0N/A */
0N/A public String getFile() {
0N/A return this.file;
0N/A }
0N/A
0N/A /**
0N/A * Setter for property file.
0N/A *
0N/A * @param file New value of property file.
0N/A */
0N/A public void setFile(String file) {
0N/A this.file = file;
0N/A }
0N/A
0N/A /**
0N/A * Getter for property freetext.
0N/A *
0N/A * @return Value of property freetext.
0N/A */
0N/A public String getFreetext() {
0N/A return this.freetext;
0N/A }
0N/A
0N/A /**
0N/A * Setter for property freetext.
0N/A *
0N/A * @param freetext New value of property freetext.
0N/A */
0N/A public void setFreetext(String freetext) {
0N/A this.freetext = freetext;
0N/A }
0N/A
0N/A /**
0N/A * Getter for property history.
0N/A *
0N/A * @return Value of property history.
0N/A */
0N/A public String getHistory() {
0N/A return this.history;
0N/A }
0N/A
0N/A /**
0N/A * Setter for property history.
0N/A *
0N/A * @param history New value of property history.
0N/A */
0N/A public void setHistory(String history) {
0N/A this.history = history;
0N/A }
0N/A
0N/A /**
0N/A * Getter for property symbol.
0N/A *
0N/A * @return Value of property symbol.
0N/A */
0N/A public String getSymbol() {
0N/A return this.symbol;
0N/A }
0N/A
0N/A /**
0N/A * Setter for property symbol.
0N/A *
0N/A * @param symbol New value of property symbol.
0N/A */
0N/A public void setSymbol(String symbol) {
0N/A this.symbol = symbol;
0N/A }
0N/A
0N/A boolean onlyFilnameSearch() {
0N/A return sourceContext == null && historyContext == null;
0N/A }
0N/A}