Search in sources :

Example 36 with MultiFieldQueryParser

use of org.apache.lucene.queryparser.classic.MultiFieldQueryParser in project gravitee-management-rest-api by gravitee-io.

the class PageDocumentSearcher method search.

@Override
public SearchResult search(io.gravitee.rest.api.service.search.query.Query query) throws TechnicalException {
    QueryParser parser = new MultiFieldQueryParser(new String[] { "name", "name_lowercase", "content" }, analyzer);
    parser.setFuzzyMinSim(0.6f);
    try {
        final Query parse = parser.parse(QueryParserBase.escape(query.getQuery()));
        BooleanQuery.Builder pageQuery = new BooleanQuery.Builder();
        BooleanQuery.Builder pageFieldsQuery = new BooleanQuery.Builder();
        pageFieldsQuery.add(parse, BooleanClause.Occur.SHOULD);
        pageFieldsQuery.add(new WildcardQuery(new Term("name", '*' + query.getQuery() + '*')), BooleanClause.Occur.SHOULD);
        pageFieldsQuery.add(new WildcardQuery(new Term("name_lowercase", '*' + query.getQuery().toLowerCase() + '*')), BooleanClause.Occur.SHOULD);
        pageFieldsQuery.add(new WildcardQuery(new Term("content", '*' + query.getQuery() + '*')), BooleanClause.Occur.SHOULD);
        pageQuery.add(pageFieldsQuery.build(), BooleanClause.Occur.MUST);
        pageQuery.add(new TermQuery(new Term(FIELD_TYPE, FIELD_TYPE_VALUE)), BooleanClause.Occur.MUST);
        // Note: Page search does not seem to be used so for now we don't implement any filtering for organization or environment.
        return search(pageQuery.build());
    } catch (ParseException pe) {
        logger.error("Invalid query to search for page documents", pe);
        throw new TechnicalException("Invalid query to search for page documents", pe);
    }
}
Also used : MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) Term(org.apache.lucene.index.Term) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 37 with MultiFieldQueryParser

use of org.apache.lucene.queryparser.classic.MultiFieldQueryParser in project jspwiki by apache.

the class LuceneSearchProvider method findPages.

/**
 *  Searches pages using a particular combination of flags.
 *
 *  @param query The query to perform in Lucene query language
 *  @param flags A set of flags
 *  @return A Collection of SearchResult instances
 *  @throws ProviderException if there is a problem with the backend
 */
public Collection<SearchResult> findPages(final String query, final int flags, final Context wikiContext) throws ProviderException {
    ArrayList<SearchResult> list = null;
    Highlighter highlighter = null;
    try (final Directory luceneDir = new NIOFSDirectory(new File(m_luceneDirectory).toPath());
        final IndexReader reader = DirectoryReader.open(luceneDir)) {
        final String[] queryfields = { LUCENE_PAGE_CONTENTS, LUCENE_PAGE_NAME, LUCENE_AUTHOR, LUCENE_ATTACHMENTS, LUCENE_PAGE_KEYWORDS };
        final QueryParser qp = new MultiFieldQueryParser(queryfields, getLuceneAnalyzer());
        final Query luceneQuery = qp.parse(query);
        final IndexSearcher searcher = new IndexSearcher(reader, searchExecutor);
        if ((flags & FLAG_CONTEXTS) != 0) {
            highlighter = new Highlighter(new SimpleHTMLFormatter("<span class=\"searchmatch\">", "</span>"), new SimpleHTMLEncoder(), new QueryScorer(luceneQuery));
        }
        final ScoreDoc[] hits = searcher.search(luceneQuery, MAX_SEARCH_HITS).scoreDocs;
        final AuthorizationManager mgr = m_engine.getManager(AuthorizationManager.class);
        list = new ArrayList<>(hits.length);
        for (final ScoreDoc hit : hits) {
            final int docID = hit.doc;
            final Document doc = searcher.doc(docID);
            final String pageName = doc.get(LUCENE_ID);
            final Page page = m_engine.getManager(PageManager.class).getPage(pageName, PageProvider.LATEST_VERSION);
            if (page != null) {
                final PagePermission pp = new PagePermission(page, PagePermission.VIEW_ACTION);
                if (mgr.checkPermission(wikiContext.getWikiSession(), pp)) {
                    final int score = (int) (hit.score * 100);
                    // Get highlighted search contexts
                    final String text = doc.get(LUCENE_PAGE_CONTENTS);
                    String[] fragments = new String[0];
                    if (text != null && highlighter != null) {
                        final TokenStream tokenStream = getLuceneAnalyzer().tokenStream(LUCENE_PAGE_CONTENTS, new StringReader(text));
                        fragments = highlighter.getBestFragments(tokenStream, text, MAX_FRAGMENTS);
                    }
                    final SearchResult result = new SearchResultImpl(page, score, fragments);
                    list.add(result);
                }
            } else {
                log.error("Lucene found a result page '" + pageName + "' that could not be loaded, removing from Lucene cache");
                pageRemoved(Wiki.contents().page(m_engine, pageName));
            }
        }
    } catch (final IOException e) {
        log.error("Failed during lucene search", e);
    } catch (final ParseException e) {
        log.info("Broken query; cannot parse query: " + query, e);
        throw new ProviderException("You have entered a query Lucene cannot process [" + query + "]: " + e.getMessage());
    } catch (final InvalidTokenOffsetsException e) {
        log.error("Tokens are incompatible with provided text ", e);
    }
    return list;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) TokenStream(org.apache.lucene.analysis.TokenStream) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) ProviderException(org.apache.wiki.api.exceptions.ProviderException) Page(org.apache.wiki.api.core.Page) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) PageManager(org.apache.wiki.pages.PageManager) InvalidTokenOffsetsException(org.apache.lucene.search.highlight.InvalidTokenOffsetsException) StringReader(java.io.StringReader) Highlighter(org.apache.lucene.search.highlight.Highlighter) Directory(org.apache.lucene.store.Directory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) SimpleHTMLEncoder(org.apache.lucene.search.highlight.SimpleHTMLEncoder) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) QueryScorer(org.apache.lucene.search.highlight.QueryScorer) SearchResult(org.apache.wiki.api.search.SearchResult) IOException(java.io.IOException) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) IndexReader(org.apache.lucene.index.IndexReader) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager) ParseException(org.apache.lucene.queryparser.classic.ParseException) SimpleHTMLFormatter(org.apache.lucene.search.highlight.SimpleHTMLFormatter) File(java.io.File) PagePermission(org.apache.wiki.auth.permissions.PagePermission)

Aggregations

MultiFieldQueryParser (org.apache.lucene.queryparser.classic.MultiFieldQueryParser)37 ParseException (org.apache.lucene.queryparser.classic.ParseException)24 Query (org.apache.lucene.search.Query)17 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)14 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)14 Term (org.apache.lucene.index.Term)11 IndexSearcher (org.apache.lucene.search.IndexSearcher)11 TermQuery (org.apache.lucene.search.TermQuery)11 IOException (java.io.IOException)10 WildcardQuery (org.apache.lucene.search.WildcardQuery)10 ModuleException (it.vige.rubia.ModuleException)8 ResultPage (it.vige.rubia.search.ResultPage)8 Searching (it.vige.rubia.search.Searching)8 SortBy (it.vige.rubia.search.SortBy)8 SortOrder (it.vige.rubia.search.SortOrder)8 EntityManager (javax.persistence.EntityManager)8 Builder (org.apache.lucene.search.BooleanQuery.Builder)8 TopDocs (org.apache.lucene.search.TopDocs)8 FullTextQuery (org.hibernate.search.FullTextQuery)8 FullTextSession (org.hibernate.search.FullTextSession)8