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