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);
}
}
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;
}
Aggregations