Search in sources :

Example 46 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project che by eclipse.

the class LuceneSearcher method createLuceneQuery.

private Query createLuceneQuery(QueryExpression query) throws ParseException {
    final BooleanQuery luceneQuery = new BooleanQuery();
    final String name = query.getName();
    final String path = query.getPath();
    final String text = query.getText();
    if (path != null) {
        luceneQuery.add(new PrefixQuery(new Term(PATH_FIELD, path)), BooleanClause.Occur.MUST);
    }
    if (name != null) {
        QueryParser qParser = new QueryParser(NAME_FIELD, makeAnalyzer());
        qParser.setAllowLeadingWildcard(true);
        luceneQuery.add(qParser.parse(name), BooleanClause.Occur.MUST);
    }
    if (text != null) {
        QueryParser qParser = new QueryParser(TEXT_FIELD, makeAnalyzer());
        qParser.setAllowLeadingWildcard(true);
        luceneQuery.add(qParser.parse(text), BooleanClause.Occur.MUST);
    }
    return luceneQuery;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) PrefixQuery(org.apache.lucene.search.PrefixQuery) Term(org.apache.lucene.index.Term)

Example 47 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project gitblit by gitblit.

the class LuceneService method deleteBlob.

/**
	 * Delete a blob from the specified branch of the repository index.
	 *
	 * @param repositoryName
	 * @param branch
	 * @param path
	 * @throws Exception
	 * @return true, if deleted, false if no record was deleted
	 */
public boolean deleteBlob(String repositoryName, String branch, String path) throws Exception {
    String pattern = MessageFormat.format("{0}:'{'0} AND {1}:\"'{'1'}'\" AND {2}:\"'{'2'}'\"", FIELD_OBJECT_TYPE, FIELD_BRANCH, FIELD_PATH);
    String q = MessageFormat.format(pattern, SearchObjectType.blob.name(), branch, path);
    StandardAnalyzer analyzer = new StandardAnalyzer();
    QueryParser qp = new QueryParser(FIELD_SUMMARY, analyzer);
    BooleanQuery query = new BooleanQuery.Builder().add(qp.parse(q), Occur.MUST).build();
    IndexWriter writer = getIndexWriter(repositoryName);
    int numDocsBefore = writer.numDocs();
    writer.deleteDocuments(query);
    writer.commit();
    int numDocsAfter = writer.numDocs();
    if (numDocsBefore == numDocsAfter) {
        logger.debug(MessageFormat.format("no records found to delete {0}", query.toString()));
        return false;
    } else {
        logger.debug(MessageFormat.format("deleted {0} records with {1}", numDocsBefore - numDocsAfter, query.toString()));
        return true;
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) IndexWriter(org.apache.lucene.index.IndexWriter) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer)

Example 48 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project gitblit by gitblit.

the class LuceneService method search.

/**
	 * Searches the specified repositories for the given text or query
	 *
	 * @param text
	 *            if the text is null or empty, null is returned
	 * @param page
	 *            the page number to retrieve. page is 1-indexed.
	 * @param pageSize
	 *            the number of elements to return for this page
	 * @param repositories
	 *            a list of repositories to search. if no repositories are
	 *            specified null is returned.
	 * @return a list of SearchResults in order from highest to the lowest score
	 *
	 */
public List<SearchResult> search(String text, int page, int pageSize, String... repositories) {
    if (StringUtils.isEmpty(text)) {
        return null;
    }
    if (ArrayUtils.isEmpty(repositories)) {
        return null;
    }
    Set<SearchResult> results = new LinkedHashSet<SearchResult>();
    StandardAnalyzer analyzer = new StandardAnalyzer();
    try {
        // default search checks summary and content
        BooleanQuery.Builder bldr = new BooleanQuery.Builder();
        QueryParser qp;
        qp = new QueryParser(FIELD_SUMMARY, analyzer);
        qp.setAllowLeadingWildcard(true);
        bldr.add(qp.parse(text), Occur.SHOULD);
        qp = new QueryParser(FIELD_CONTENT, analyzer);
        qp.setAllowLeadingWildcard(true);
        bldr.add(qp.parse(text), Occur.SHOULD);
        IndexSearcher searcher;
        if (repositories.length == 1) {
            // single repository search
            searcher = getIndexSearcher(repositories[0]);
        } else {
            // multiple repository search
            List<IndexReader> readers = new ArrayList<IndexReader>();
            for (String repository : repositories) {
                IndexSearcher repositoryIndex = getIndexSearcher(repository);
                readers.add(repositoryIndex.getIndexReader());
            }
            IndexReader[] rdrs = readers.toArray(new IndexReader[readers.size()]);
            MultiSourceReader reader = new MultiSourceReader(rdrs);
            searcher = new IndexSearcher(reader);
        }
        BooleanQuery query = bldr.build();
        Query rewrittenQuery = searcher.rewrite(query);
        logger.debug(rewrittenQuery.toString());
        TopScoreDocCollector collector = TopScoreDocCollector.create(5000);
        searcher.search(rewrittenQuery, collector);
        int offset = Math.max(0, (page - 1) * pageSize);
        ScoreDoc[] hits = collector.topDocs(offset, pageSize).scoreDocs;
        int totalHits = collector.getTotalHits();
        for (int i = 0; i < hits.length; i++) {
            int docId = hits[i].doc;
            Document doc = searcher.doc(docId);
            SearchResult result = createSearchResult(doc, hits[i].score, offset + i + 1, totalHits);
            if (repositories.length == 1) {
                // single repository search
                result.repository = repositories[0];
            } else {
                // multi-repository search
                MultiSourceReader reader = (MultiSourceReader) searcher.getIndexReader();
                int index = reader.getSourceIndex(docId);
                result.repository = repositories[index];
            }
            String content = doc.get(FIELD_CONTENT);
            result.fragment = getHighlightedFragment(analyzer, query, content, result);
            results.add(result);
        }
    } catch (Exception e) {
        logger.error(MessageFormat.format("Exception while searching for {0}", text), e);
    }
    return new ArrayList<SearchResult>(results);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) TopScoreDocCollector(org.apache.lucene.search.TopScoreDocCollector) ArrayList(java.util.ArrayList) SearchResult(com.gitblit.models.SearchResult) Document(org.apache.lucene.document.Document) ParseException(java.text.ParseException) InvalidTokenOffsetsException(org.apache.lucene.search.highlight.InvalidTokenOffsetsException) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) IndexReader(org.apache.lucene.index.IndexReader)

Example 49 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project gitblit by gitblit.

the class TicketIndexer method queryFor.

/**
	 * Search for tickets matching the query.  The returned tickets are
	 * shadows of the real ticket, but suitable for a results list.
	 *
	 * @param text
	 * @param page
	 * @param pageSize
	 * @param sortBy
	 * @param desc
	 * @return
	 */
public List<QueryResult> queryFor(String queryText, int page, int pageSize, String sortBy, boolean desc) {
    if (StringUtils.isEmpty(queryText)) {
        return Collections.emptyList();
    }
    Set<QueryResult> results = new LinkedHashSet<QueryResult>();
    StandardAnalyzer analyzer = new StandardAnalyzer();
    try {
        QueryParser qp = new QueryParser(Lucene.content.name(), analyzer);
        Query query = qp.parse(queryText);
        IndexSearcher searcher = getSearcher();
        Query rewrittenQuery = searcher.rewrite(query);
        log.debug(rewrittenQuery.toString());
        Sort sort;
        if (sortBy == null) {
            sort = new Sort(Lucene.created.asSortField(desc));
        } else {
            sort = new Sort(Lucene.fromString(sortBy).asSortField(desc));
        }
        int maxSize = 5000;
        TopFieldDocs docs = searcher.search(rewrittenQuery, maxSize, sort, false, false);
        int size = (pageSize <= 0) ? maxSize : pageSize;
        int offset = Math.max(0, (page - 1) * size);
        ScoreDoc[] hits = subset(docs.scoreDocs, offset, size);
        for (int i = 0; i < hits.length; i++) {
            int docId = hits[i].doc;
            Document doc = searcher.doc(docId);
            QueryResult result = docToQueryResult(doc);
            result.docId = docId;
            result.totalResults = docs.totalHits;
            results.add(result);
        }
    } catch (Exception e) {
        log.error(MessageFormat.format("Exception while searching for {0}", queryText), e);
    }
    return new ArrayList<QueryResult>(results);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) ArrayList(java.util.ArrayList) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) Document(org.apache.lucene.document.Document) ParseException(java.text.ParseException) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Sort(org.apache.lucene.search.Sort)

Example 50 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project gitblit by gitblit.

the class TicketIndexer method deleteAll.

/**
	 * Deletes all tickets for the the repository from the index.
	 */
public boolean deleteAll(RepositoryModel repository) {
    try {
        IndexWriter writer = getWriter();
        StandardAnalyzer analyzer = new StandardAnalyzer();
        QueryParser qp = new QueryParser(Lucene.rid.name(), analyzer);
        BooleanQuery query = new BooleanQuery.Builder().add(qp.parse(repository.getRID()), Occur.MUST).build();
        int numDocsBefore = writer.numDocs();
        writer.deleteDocuments(query);
        writer.commit();
        closeSearcher();
        int numDocsAfter = writer.numDocs();
        if (numDocsBefore == numDocsAfter) {
            log.debug(MessageFormat.format("no records found to delete in {0}", repository));
            return false;
        } else {
            log.debug(MessageFormat.format("deleted {0} records in {1}", numDocsBefore - numDocsAfter, repository));
            return true;
        }
    } catch (Exception e) {
        log.error("error", e);
    }
    return false;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) IndexWriter(org.apache.lucene.index.IndexWriter) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ParseException(java.text.ParseException) IOException(java.io.IOException)

Aggregations

QueryParser (org.apache.lucene.queryparser.classic.QueryParser)73 Query (org.apache.lucene.search.Query)50 IndexSearcher (org.apache.lucene.search.IndexSearcher)32 Document (org.apache.lucene.document.Document)26 IOException (java.io.IOException)24 Analyzer (org.apache.lucene.analysis.Analyzer)21 TopDocs (org.apache.lucene.search.TopDocs)21 IndexReader (org.apache.lucene.index.IndexReader)18 ScoreDoc (org.apache.lucene.search.ScoreDoc)18 ArrayList (java.util.ArrayList)16 ParseException (org.apache.lucene.queryparser.classic.ParseException)16 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)14 BooleanQuery (org.apache.lucene.search.BooleanQuery)14 TermQuery (org.apache.lucene.search.TermQuery)13 ScoredDocuments (io.anserini.rerank.ScoredDocuments)6 Term (org.apache.lucene.index.Term)6 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)6 WildcardQuery (org.apache.lucene.search.WildcardQuery)6 EnglishAnalyzer (org.apache.lucene.analysis.en.EnglishAnalyzer)5 IndexWriter (org.apache.lucene.index.IndexWriter)5