Search in sources :

Example 1 with SearchException

use of net.jforum.exceptions.SearchException in project jforum2 by rafaelsteil.

the class LuceneSearch method performSearch.

private SearchResult performSearch(SearchArgs args, LuceneResultCollector resultCollector, Filter filter) {
    SearchResult result;
    try {
        StringBuffer criteria = new StringBuffer(256);
        this.filterByForum(args, criteria);
        this.filterByKeywords(args, criteria);
        this.filterByDateRange(args, criteria);
        Query query = new QueryParser("", new StandardAnalyzer()).parse(criteria.toString());
        if (logger.isDebugEnabled()) {
            logger.debug("Generated query: " + query);
        }
        Hits hits = filter == null ? this.search.search(query, this.getSorter(args)) : this.search.search(query, filter, this.getSorter(args));
        if (hits != null && hits.length() > 0) {
            result = new SearchResult(resultCollector.collect(args, hits, query), hits.length());
        } else {
            result = new SearchResult(new ArrayList(), 0);
        }
    } catch (Exception e) {
        throw new SearchException(e);
    }
    return result;
}
Also used : QueryParser(org.apache.lucene.queryParser.QueryParser) Hits(org.apache.lucene.search.Hits) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ArrayList(java.util.ArrayList) SearchException(net.jforum.exceptions.SearchException) IOException(java.io.IOException) SearchException(net.jforum.exceptions.SearchException)

Example 2 with SearchException

use of net.jforum.exceptions.SearchException in project jforum2 by rafaelsteil.

the class LuceneSearch method analyzeKeywords.

private String[] analyzeKeywords(String contents) {
    try {
        TokenStream stream = this.settings.analyzer().tokenStream("contents", new StringReader(contents));
        List tokens = new ArrayList();
        while (true) {
            Token token = stream.next();
            if (token == null) {
                break;
            }
            tokens.add(token.termText());
        }
        return (String[]) tokens.toArray(new String[0]);
    } catch (IOException e) {
        throw new SearchException(e);
    }
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) SearchException(net.jforum.exceptions.SearchException) ArrayList(java.util.ArrayList) List(java.util.List) Token(org.apache.lucene.analysis.Token) IOException(java.io.IOException)

Example 3 with SearchException

use of net.jforum.exceptions.SearchException in project jforum2 by rafaelsteil.

the class LuceneSearch method newDocumentAdded.

/**
 * @see net.jforum.search.NewDocumentAdded#newDocumentAdded()
 */
public void newDocumentAdded() {
    try {
        this.search.close();
        this.openSearch();
    } catch (Exception e) {
        throw new SearchException(e);
    }
}
Also used : SearchException(net.jforum.exceptions.SearchException) IOException(java.io.IOException) SearchException(net.jforum.exceptions.SearchException)

Example 4 with SearchException

use of net.jforum.exceptions.SearchException in project jforum2 by rafaelsteil.

the class LuceneIndexer method flushRAMDirectory.

public void flushRAMDirectory() {
    synchronized (MUTEX) {
        IndexWriter writer = null;
        try {
            writer = new IndexWriter(this.settings.directory(), this.settings.analyzer());
            writer.addIndexes(new Directory[] { this.ramDirectory });
            writer.optimize();
            this.createRAMWriter();
        } catch (IOException e) {
            throw new SearchException(e);
        } finally {
            if (writer != null) {
                try {
                    writer.flush();
                    writer.close();
                    this.notifyNewDocumentAdded();
                } catch (Exception e) {
                }
            }
        }
    }
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) SearchException(net.jforum.exceptions.SearchException) IOException(java.io.IOException) IOException(java.io.IOException) SearchException(net.jforum.exceptions.SearchException)

Example 5 with SearchException

use of net.jforum.exceptions.SearchException in project jforum2 by rafaelsteil.

the class LuceneIndexer method createRAMWriter.

private void createRAMWriter() {
    try {
        if (this.ramWriter != null) {
            this.ramWriter.close();
        }
        this.ramDirectory = new RAMDirectory();
        this.ramWriter = new IndexWriter(this.ramDirectory, this.settings.analyzer(), true);
        this.ramNumDocs = SystemGlobals.getIntValue(ConfigKeys.LUCENE_INDEXER_RAM_NUMDOCS);
    } catch (IOException e) {
        throw new SearchException(e);
    }
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) SearchException(net.jforum.exceptions.SearchException) IOException(java.io.IOException) RAMDirectory(org.apache.lucene.store.RAMDirectory)

Aggregations

IOException (java.io.IOException)6 SearchException (net.jforum.exceptions.SearchException)6 ArrayList (java.util.ArrayList)2 IndexWriter (org.apache.lucene.index.IndexWriter)2 StringReader (java.io.StringReader)1 List (java.util.List)1 Token (org.apache.lucene.analysis.Token)1 TokenStream (org.apache.lucene.analysis.TokenStream)1 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)1 Document (org.apache.lucene.document.Document)1 QueryParser (org.apache.lucene.queryParser.QueryParser)1 Hits (org.apache.lucene.search.Hits)1 Query (org.apache.lucene.search.Query)1 TermQuery (org.apache.lucene.search.TermQuery)1 RAMDirectory (org.apache.lucene.store.RAMDirectory)1