Example usage for org.apache.lucene.search Sort RELEVANCE

List of usage examples for org.apache.lucene.search Sort RELEVANCE

Introduction

In this page you can find the example usage for org.apache.lucene.search Sort RELEVANCE.

Prototype

Sort RELEVANCE

To view the source code for org.apache.lucene.search Sort RELEVANCE.

Click Source Link

Document

Represents sorting by computed relevance.

Usage

From source file:net.mojodna.searchable.AbstractSearcher.java

License:Apache License

/**
 * Search the index with the specified query.
 * //from  ww  w .  ja v a 2  s.  c  o  m
 * @param query Query to use.
 * @param offset Offset to begin result set at.
 * @param count Number of results to return.
 * @return ResultSet containing results.
 * @throws IndexException
 */
protected ResultSet doSearch(final String query, final Integer offset, final Integer count)
        throws IndexException {
    return doSearch(query, offset, count, Sort.RELEVANCE);
}

From source file:net.riezebos.thoth.content.search.Indexer.java

License:Apache License

protected void markUnusedDocuments(Map<String, List<String>> directReverseIndex)
        throws IOException, ContentManagerException {

    String indexFolder = contentManager.getIndexFolder();

    try (IndexWriter writer = getWriter(false); IndexReader reader = getIndexReader(indexFolder)) {
        IndexSearcher searcher = getIndexSearcher(reader);
        for (ContentNode node : contentManager.getUnusedFragments()) {
            TermQuery query = new TermQuery(new Term(Indexer.INDEX_PATH, node.getPath()));

            TopDocs results = searcher.search(query, 10, Sort.RELEVANCE);
            ScoreDoc[] hits = results.scoreDocs;

            for (ScoreDoc scoreDoc : hits) {
                Document document = searcher.doc(scoreDoc.doc);
                document.add(new TextField(INDEX_USED, "false", Store.YES));
                writer.updateDocument(new Term(INDEX_PATH, node.getPath()), document);
            }//from  w w w.j  av a2  s .com
        }
    }
}

From source file:net.riezebos.thoth.content.search.Searcher.java

License:Apache License

public PagedList<SearchResult> search(Identity identity, String queryExpression, int pageNumber, int pageSize)
        throws SearchException {
    try {//from w  w  w . j a  va  2 s  .  com
        IndexReader reader = getIndexReader(contentManager);
        IndexSearcher searcher = getIndexSearcher(reader);
        Analyzer analyzer = new StandardAnalyzer();

        // We might need to restrict the results to books of the user does not have access to fragments:
        AccessManager accessManager = contentManager.getAccessManager();
        boolean booksOnly = !accessManager.hasPermission(identity, "", Permission.READ_FRAGMENTS);
        if (booksOnly) {
            queryExpression = Indexer.INDEX_TYPE + ":" + Indexer.TYPE_DOCUMENT + " AND (" + queryExpression
                    + ")";
        }

        QueryParser parser = new QueryParser(Indexer.INDEX_CONTENTS, analyzer);
        Query query = parser.parse(queryExpression);

        // We add 1 to determine if there is more to be found after the current page
        int maxResults = pageSize * pageNumber + 1;
        TopDocs results = searcher.search(query, maxResults, Sort.RELEVANCE);
        ScoreDoc[] hits = results.scoreDocs;

        boolean hadMore = (hits.length == maxResults);

        List<SearchResult> searchResults = new ArrayList<>();
        int idx = 0;
        for (ScoreDoc scoreDoc : hits) {
            if (searchResults.size() == pageSize)
                break;
            idx++;
            if (idx >= (pageNumber - 1) * pageSize) {
                Document document = searcher.doc(scoreDoc.doc);
                IndexableField field = document.getField(Indexer.INDEX_PATH);
                String documentPath = field.stringValue();
                SearchResult searchResult = new SearchResult();
                searchResult.setIndexNumber((pageNumber - 1) * pageSize + idx);
                searchResult.setDocument(documentPath);

                String type = document.get(Indexer.INDEX_TYPE);
                if (Indexer.TYPE_DOCUMENT.equals(type) || Indexer.TYPE_FRAGMENT.equals(type)) {
                    searchResult.setResource(false);

                    try {
                        MarkDownDocument markDownDocument = contentManager.getMarkDownDocument(documentPath,
                                true, CriticProcessingMode.DO_NOTHING);
                        String contents = markDownDocument.getMarkdown();

                        SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter();
                        Highlighter highlighter = new Highlighter(htmlFormatter,
                                new QueryScorer(query, Indexer.INDEX_CONTENTS));
                        highlighter.setMaxDocCharsToAnalyze(Integer.MAX_VALUE);

                        TokenStream tokenStream = analyzer.tokenStream(Indexer.INDEX_CONTENTS, contents);

                        TextFragment[] frags = highlighter.getBestTextFragments(tokenStream, contents, false,
                                99999);
                        for (TextFragment frag : frags) {
                            if ((frag != null) && (frag.getScore() > 0)) {
                                String fragmentText = frag.toString();
                                searchResult.addFragment(
                                        new Fragment(ThothCoreUtil.escapeHtmlExcept("B", fragmentText)));
                            }
                        }
                    } catch (FileNotFoundException e) {
                        LOG.warn(
                                "Index contains an invalid file reference); probably need to reindex to get rid of this. File: "
                                        + e.getMessage());
                    }
                } else {
                    searchResult.setResource(true);
                    String extension = ThothUtil.getExtension(documentPath);
                    searchResult.setImage(getConfiguration().isImageExtension(extension));

                    searchResult.addFragment(new Fragment(document.get(Indexer.INDEX_TITLE)));
                }
                searchResults.add(searchResult);
            }
        }
        reader.close();
        linkBooks(searchResults);
        PagedList<SearchResult> pagedList = new PagedList<>(searchResults, hadMore);
        return pagedList;
    } catch (Exception e) {
        throw new SearchException(e);
    }
}

From source file:net.semanticmetadata.lire.solr.LireRequestHandler.java

License:Open Source License

/**
 * Actual search implementation based on (i) hash based retrieval and (ii) feature based re-ranking.
 *
 * @param req           the SolrQueryRequest
 * @param rsp           the response to write the data to
 * @param searcher      the actual index searcher object to search the index
 * @param hashFieldName the name of the field the hashes can be found
 * @param maximumHits   the maximum number of hits, the smaller the faster
 * @param filterQuery   can be null//from ww w . j  av a2s  .  co  m
 * @param query         the (Boolean) query for querying the candidates from the IndexSearcher
 * @param queryFeature  the image feature used for re-ranking the results
 * @throws IOException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
private void doSearch(SolrQueryRequest req, SolrQueryResponse rsp, SolrIndexSearcher searcher,
        String hashFieldName, int maximumHits, Query filterQuery, Query query, GlobalFeature queryFeature)
        throws IOException, IllegalAccessException, InstantiationException {
    // temp feature instance
    GlobalFeature tmpFeature = queryFeature.getClass().newInstance();
    // Taking the time of search for statistical purposes.
    time = System.currentTimeMillis();

    String featureFieldName = FeatureRegistry.getFeatureFieldName(hashFieldName);
    BinaryDocValues binaryValues = MultiDocValues.getBinaryValues(searcher.getIndexReader(), featureFieldName);

    time = System.currentTimeMillis() - time;
    rsp.add("DocValuesOpenTime", time + "");

    Iterator<Integer> docIterator;
    int numberOfResults = 0;
    time = System.currentTimeMillis();
    if (filterQuery != null) {
        DocList docList = searcher.getDocList(query, filterQuery, Sort.RELEVANCE, 0, numberOfCandidateResults);
        numberOfResults = docList.size();
        docIterator = docList.iterator();
    } else {
        TopDocs docs = searcher.search(query, numberOfCandidateResults);
        numberOfResults = docs.totalHits;
        docIterator = new TopDocsIterator(docs);
    }
    time = System.currentTimeMillis() - time;
    rsp.add("RawDocsCount", numberOfResults + "");
    rsp.add("RawDocsSearchTime", time + "");
    time = System.currentTimeMillis();
    TreeSet<CachingSimpleResult> resultScoreDocs = getReRankedResults(docIterator, binaryValues, queryFeature,
            tmpFeature, maximumHits, searcher);

    // Creating response ...
    time = System.currentTimeMillis() - time;
    rsp.add("ReRankSearchTime", time + "");
    LinkedList list = new LinkedList();
    for (Iterator<CachingSimpleResult> it = resultScoreDocs.iterator(); it.hasNext();) {
        CachingSimpleResult result = it.next();
        HashMap m = new HashMap(2);
        m.put("d", result.getDistance());
        // add fields as requested:
        if (req.getParams().get("fl") == null) {
            m.put("id", result.getDocument().get("id"));
            if (result.getDocument().get("title") != null)
                m.put("title", result.getDocument().get("title"));
        } else {
            String fieldsRequested = req.getParams().get("fl");
            if (fieldsRequested.contains("score")) {
                m.put("score", result.getDistance());
            }
            if (fieldsRequested.contains("*")) {
                // all fields
                for (IndexableField field : result.getDocument().getFields()) {
                    String tmpField = field.name();

                    if (result.getDocument().getFields(tmpField).length > 1) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getValues(tmpField));
                    } else if (result.getDocument().getFields(tmpField).length > 0) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getFields(tmpField)[0].stringValue());
                    }
                }
            } else {
                StringTokenizer st;
                if (fieldsRequested.contains(","))
                    st = new StringTokenizer(fieldsRequested, ",");
                else
                    st = new StringTokenizer(fieldsRequested, " ");
                while (st.hasMoreElements()) {
                    String tmpField = st.nextToken();
                    if (result.getDocument().getFields(tmpField).length > 1) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getValues(tmpField));
                    } else if (result.getDocument().getFields(tmpField).length > 0) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getFields(tmpField)[0].stringValue());
                    }
                }
            }
        }
        //            m.put(field, result.getDocument().get(field));
        //            m.put(field.replace("_ha", "_hi"), result.getDocument().getBinaryValue(field));
        list.add(m);
    }
    rsp.add("docs", list);
    // rsp.add("Test-name", "Test-val");
}

From source file:net.sf.zekr.engine.search.lucene.QuranTextSearcher.java

public QuranTextSearcher(ZekrIndexReader indexReader, SearchScope searchScope, Analyzer analyzer) {
    this.searchScope = searchScope;
    zekrIndexReader = indexReader;/*from  ww w  .j  ava 2  s. co  m*/
    this.searchScope = searchScope;
    this.analyzer = analyzer;
    maxClauseCount = MAX_CLAUSE_COUNT;
    sortResultOrder = Sort.RELEVANCE;
    highlightFormatter = new ZekrHighlightFormatter();
    try {
        maxSearchResult = config.getProps().getInt("search.maxResult", MAX_SEARCH_RESULT);
    } catch (Exception e) {
        // silently ignore, as the variable is already initialized
    }
    pageNum = 0;
}

From source file:net.sf.zekr.engine.search.lucene.QuranTextSearcher.java

protected SearchResultModel doSearch(String query) throws SearchException {
    rawQuery = query;/*  w  w  w. ja va 2  s .  co  m*/
    String s = SearchUtils.simplifyAdvancedSearchQuery(query);
    results = internalSearch(s);
    if (sortResultOrder.equals(Sort.RELEVANCE)) {
        ascending = !ascending; // Lucene sorts relevance descending, while natural order ascending!
    }
    // String clause = StringUtils.join(highlightedTermList, " ");
    String clause = getQuery().toString(QuranTextIndexer.CONTENTS_FIELD);
    return new SearchResultModel(zekrIndexReader.quranText, results, clause, rawQuery, matchedItemCount,
            searchResultComparator, ascending);
}

From source file:net.sf.zekr.ui.QuranForm.java

private void advancedFind() {
    String str;//  w  ww  .  j  a  v  a2  s  .  com
    if (advancedToggleMultiLine.getSelection()) {
        str = advancedSearchBox.getText();
    } else {
        str = advancedSearchCombo.getText();
    }
    if ("".equals(str.trim())) {
        return; // do nothing
    }

    LuceneIndexManager lim = config.getLuceneIndexManager();
    try {
        if (advancedQuranTargetBut.getSelection()) {
            qts = new QuranTextSearcher(lim, searchScope);
        } else {
            qts = new QuranTextSearcher(lim, searchScope, config.getTranslation().getDefault());
        }
    } catch (IndexingException e) {
        logger.implicitLog(e);
        MessageBoxUtils.showError("Indexing Error: " + e);
        return; // search failed
    }
    if (!qts.isIndexReaderOpen()) {
        return; // indexing probably interrupted
    }

    str = str.trim();
    if (!"".equals(str)) {
        if (advancedSearchCombo.getItemCount() <= 0 || !str.equals(advancedSearchCombo.getItem(0))) {
            advancedSearchCombo.add(str, 0);
        }
        if (advancedSearchCombo.getItemCount() > 40) {
            advancedSearchCombo.remove(40, advancedSearchCombo.getItemCount() - 1);
        }

        logger.info("Search started: " + str);
        Date date1 = new Date();

        int sortBy = advancedSearchOrderCombo.getSelectionIndex();
        boolean relevance = sortBy == 0 ? true : false;
        try {
            qts.setSortResultOrder(relevance ? Sort.RELEVANCE : Sort.INDEXORDER);
            qts.setAscending(advancedSortOrderButton.getData().equals("asc"));
            qts.setSearchResultComparator(SearchResultComparatorFactory
                    .getComparator((String) advancedSearchOrderCombo.getData(String.valueOf(sortBy))));
            asr = qts.search(str);
        } catch (Exception e) {
            logger.implicitLog(e);
            MessageBoxUtils.showError("Advanced Search Error: " + e);
            searchNav.setVisible(false);
            return; // search failed
        }
        Date date2 = new Date();
        logger.info("Search for " + str + " finished; took " + (date2.getTime() - date1.getTime()) + " ms.");

        int pageCount = asr.getResultPageCount();
        logger.debug("Search result has " + pageCount + " pages.");
        if (pageCount > 1) {
            advSearchNav.setVisible(true);
        } else {
            advSearchNav.setVisible(false);
            advSearchNav.nextPageBut.setEnabled(true);
        }

        advSearchNav.resetSearch(pageCount);
    }
}

From source file:nl.uva.expose.clustering.SimGraphMaker.java

public HashMap<String, Double> searchAndReturnResults(String queryText, String qId)
        throws IOException, ParseException {
    queryText = queryText.replaceAll("AND", "and").replaceAll("OR", "or").replaceAll("NOT", "not"); // to avoid boolean operation!
    QueryParser qParser = new QueryParser(Version.LUCENE_CURRENT, field, this.analyzer);
    BooleanQuery.setMaxClauseCount(queryText.split("\\s+").length);
    Query q = qParser.parse(QueryParser.escape(queryText));
    Similarity simFunc = new BM25Similarity();
    IndexSearcher isearcher = new IndexSearcher(this.ireader);
    isearcher.setSimilarity(simFunc);// ww w  .  ja v a2  s  .  c  om
    TopFieldCollector tfc = TopFieldCollector.create(Sort.RELEVANCE, ireader.numDocs(), true, true, true,
            false);
    //            TopFieldCollector tfc = TopFieldCollector.create(Sort.RELEVANCE,20, true, true, true, false);
    isearcher.search(q, tfc);
    TopDocs results = tfc.topDocs();
    ScoreDoc[] hits = results.scoreDocs;
    return fillQueryResultList(hits, qId);
}

From source file:nl.uva.mlc.eurovoc.irengine.Retrieval.java

public HashMap<String, Feature> searchAndReturnResults(String queryText, String qId)
        throws IOException, ParseException {
    queryText = queryText.replaceAll("AND", "and").replaceAll("OR", "or").replaceAll("NOT", "not"); // to avoid boolean operation!
    QueryParser qParser = new QueryParser(Version.LUCENE_CURRENT, field, this.analyzer);
    BooleanQuery.setMaxClauseCount(queryText.split("\\s+").length);
    Query q = qParser.parse(QueryParser.escape(queryText));
    this.simFunction = SIM_FUNCS[SimilarityFunction.valueOf(SimFName).ordinal()];
    Similarity simFunc = this.simFunction;
    IndexSearcher isearcher = new IndexSearcher(this.ireader);
    isearcher.setSimilarity(simFunc);// w w  w  .  java  2s  .c  o  m
    TopFieldCollector tfc = TopFieldCollector.create(Sort.RELEVANCE, ireader.numDocs(), true, true, true,
            false);
    isearcher.search(q, tfc);
    TopDocs results = tfc.topDocs();
    ScoreDoc[] hits = results.scoreDocs;
    return fillQueryResultList(hits, qId);
}

From source file:org.ala.dao.TaxonConceptSHDaoImpl.java

License:Open Source License

/**
 * Perform Lucene search with params for sorting and paging
 * // ww w . j a  v a2s  . c  o m
 * @param searchQuery
 * @param startIndex
 * @param pageSize
 * @param sortDirection
 * @param sortField
 * @return
 * @throws IOException
 * @throws Exception
 */
private SearchResultsDTO sortPageSearch(Query searchQuery, Integer startIndex, Integer pageSize,
        String sortField, String sortDirection) throws IOException, Exception {
    boolean direction = false;

    if (sortDirection != null && !sortDirection.isEmpty() && sortDirection.equalsIgnoreCase("desc")) {
        direction = true;
    }

    Sort sort = new Sort();

    if (sortField != null && !sortField.isEmpty() && !sortField.equalsIgnoreCase("score")) {
        SortField sf = new SortField(sortField, SortField.Type.STRING, direction);
        sort.setSort(sf);
    } else {
        sort = Sort.RELEVANCE;
    }

    TopDocs topDocs = getTcIdxSearcher().search(searchQuery, null, startIndex + pageSize, sort); // TODO ues sortField here
    logger.debug("Total hits: " + topDocs.totalHits);
    List<SearchTaxonConceptDTO> tcs = new ArrayList<SearchTaxonConceptDTO>();

    for (int i = 0; i < topDocs.scoreDocs.length; i++) {
        if (i >= startIndex) {
            ScoreDoc scoreDoc = topDocs.scoreDocs[i];
            Document doc = getTcIdxSearcher().doc(scoreDoc.doc);
            tcs.add(createTaxonConceptFromIndex(doc, scoreDoc.score));
        }
    }

    SearchResultsDTO searchResults = new SearchResultsDTO(tcs);
    searchResults.setTotalRecords(topDocs.totalHits);
    searchResults.setStartIndex(startIndex);
    searchResults.setStatus("OK");
    searchResults.setSort(sortField);
    searchResults.setDir(sortDirection);
    searchResults.setQuery(searchQuery.toString());

    return searchResults;
}