Package org.apache.lucene.search

Examples of org.apache.lucene.search.Hits


      ArrayList results = new ArrayList();
      IndexReader reader = IndexReader.open(remoteStoreIndex);
      Searcher searcher = new IndexSearcher(reader);
      Analyzer analyzer = new StandardAnalyzer();
      Query query = QueryParser.parse(item,"RemoteFileContent",analyzer);
      Hits hits = searcher.search(query);
      for(int i = 0; i < hits.length() && (localSearch || i < QueryManager.resultSize); i++){
        Document doc = hits.doc(i);
        String hash = doc.get("RemoteFileHash");
        if(!results.contains(hash))
          results.add(hash);
      }
      reader.close();
View Full Code Here


                        "content", analyzer);

                Sort sort = new Sort();
                sort.setSort(sortField, sortDescMode);

                Hits hits = searcher.search(query, sort);
                logger.debug("Query is " + query.toString() + " hits :" +
                    hits.length());

                LinkedList listDocs = new LinkedList();
                int countErrors = 0;

                for (int i = 0;
                        (i < hits.length()) && (listDocs.size() < maxDocNo);
                        i++) {
                    try {
                        //get the document and add it to the list
                        Document doc = hits.doc(i);
                        logger.debug("Doc path" +
                            doc.get(DocumStruct.FIELD_PATH));
                        logger.debug("CategName " +
                            doc.get(DocumStruct.FIELD_CATEGORY_NAME));
                        logger.debug("CalcScore " +
                            doc.get(DocumStruct.FIELD_CALCSCORE));
                        logger.debug("CategScore " +
                            doc.get(DocumStruct.FIELD_CATEGORY_SCORE));
                        logger.debug("GeneralScore " + doc.get(sortField));

                        //translation from the found it lucene document to the RPDocument to the return documents
                        DocumStruct docStruct = new DocumStruct();
                        docStruct.setTitle(doc.get(DocumStruct.FIELD_TITLE));
                        docStruct.setPath(doc.get(DocumStruct.FIELD_PATH));
                        docStruct.setDescription(doc.get(
                                DocumStruct.FIELD_DESCRIPTION));
                        docStruct.setLastUpdate(doc.get(
                                DocumStruct.FIELD_LASTUPDATE));
                        docStruct.setId(doc.get(DocumStruct.FIELD_DOCUMENTID));

                        docStruct.setCategoryName(doc.get(
                                DocumStruct.FIELD_CATEGORY_NAME));

                        try {
                            docStruct.setCategoryScore(Double.valueOf(doc.get(
                                        DocumStruct.FIELD_CATEGORY_SCORE))
                                                             .doubleValue());
                        } catch (Throwable e) {
                            //set the default category score
                            docStruct.setCategoryScore(1.00);
                        }

                        docStruct.setCategoryLocation(doc.get(
                                DocumStruct.FIELD_CATEGORY_LOCATION));

                        listDocs.add(docStruct);
                    } catch (IOException e) {
                        countErrors++;
                    }
                }

                //create the result
                result = new ResultPages(listDocs, hits.length(), countErrors);
            } catch (ParseException e) {
                logger.warn("Error in searching in the specified index");
            } catch (IOException e) {
                logger.warn("Error in searching in the specified index");
            }
View Full Code Here

                //Local Variables
                Searcher searcher = null; //index to search
                Analyzer analyzer = new RpAnalyzer(); //analysis of this index

                Query query = null; //the (parsed) query
                Hits hits = null; //the hits against the database

                Document doc = null; //ID of current Document

                log.debug(" Original Query: " + searchFor);

                try
                 {
                        //Get a handle to the index
                        searcher = new IndexSearcher(Constants.INDEX_NAME);

                        //Build the query into Lucene specific format
                        query = QueryParser.parse(searchFor.getSearchString(), "contents", analyzer);

                        log.debug("Query Class:" + query.getClass().getName());
                        log.debug("Searching for: " + query.toString("contents"));
                        log.debug("Searching for: " + query.toString());

                        //Do the Search
                        hits = searcher.search(query);

                        log.debug("Number of Results:" + hits.length());

                        //Loop and display the results
                        for (int counter = 0; counter < hits.length(); counter++)
                         {
                                doc = hits.doc(counter);
                                log.debug("Rank:" + hits.score(counter) + " Info:" + doc.toString());
                        }

                        searcher.close();
                       
                } catch (java.io.IOException iex)
View Full Code Here

            {
                log.info("Lucene not yet ready; indexing not started",ex);
                return null;
            }

            Hits hits = searcher.search(luceneQuery);

            list = new ArrayList<SearchResult>(hits.length());
            for ( int curr = 0; curr < hits.length(); curr++ )
            {
                Document doc = hits.doc(curr);
                String pageName = doc.get(LUCENE_ID);
                WikiPage page = m_engine.getPage(pageName, WikiPageProvider.LATEST_VERSION);

                if(page != null)
                {
                    if(page instanceof Attachment)
                    {
                        // Currently attachments don't look nice on the search-results page
                        // When the search-results are cleaned up this can be enabled again.
                    }

                    int score = (int)(hits.score(curr) * 100);


                    // Get highlighted search contexts
                    String text = doc.get(LUCENE_PAGE_CONTENTS);

View Full Code Here

                    booleanQuery.add(participantQuery, BooleanClause.Occur.MUST);
                    query = booleanQuery;
                }
            }

            Hits hits = searcher.search(query, filter, sort);

            int startIndex = search.getStartIndex();
            int endIndex = startIndex + search.getNumResults() - 1;

            // The end index can't be after the end of the results.
            if (endIndex > hits.length() - 1) {

               // endIndex = hits.length() - 1;
                // TODO: We need to determine if this is necessary.
            }
View Full Code Here

            writer.close();

            // Create a searcher, try to find a match.
            IndexSearcher searcher = new IndexSearcher(dir);

            Hits hits = searcher.search(booleanQuery);
            // Check to see if a match was found.
            if (hits.length() > 0) {
                foundMatch = true;
            }
            searcher.close();
        }
        catch (Exception e) {
View Full Code Here

    catch (IOException ie) { logger.error("IO Error: Could not forward to index.html: " + ie.getMessage()); }
    return;
  }

  //*-- Fetch the hit list depending on the type of request - query, question, query expansion, or a similar document
  Hits hits = null; String requestType = request.getParameter("type"  );
  try
  { if (requestType.equals("SEARCH")) hits = sQuery.getHits(q);
    else if (requestType.equals("ASK")) hits =  sQuestion.getHits(q);
    else if (requestType.equals("EXPAND")) hits = is.search( SearchTools.expandQuery(q) );
    else if (requestType.equals("SIMILAR")) hits = is.search( SearchTools.similarDocs(request.getParameter("dockey"), dbt));
View Full Code Here

                            // create query
                            QueryParser queryParser = new QueryParser(Index.FIELD_BODY, new StandardAnalyzer());
                            Query query = queryParser.parse(findText);

                            // check for date filter and search in index
                            Hits hits;
                            if (useDate.isSelected()) {
                                String dateFrom = DateTimeUtils.getDateStringForIndex(DateTimeUtils.getDateFromString(fromField.getText()));
                                String dateTo = DateTimeUtils.getDateStringForIndex(DateTimeUtils.getDateFromString(toField.getText()));
                                RangeFilter rangeFilter = new RangeFilter(Index.FIELD_MODDATE, dateFrom, dateTo, true, true);
                                if (logger.isDebugEnabled()) {
                                    logger.debug("doSearch() search with date range '" + rangeFilter + "'");
                                }

                                hits = searcher.search(query, rangeFilter);
                            }
                            else {
                                if (logger.isDebugEnabled()) {
                                    logger.debug("doSearch() search without date range");
                                }

                                hits = searcher.search(query);
                            }

                            // check for search with filesize
                            int numHits;
                            if (useSize.isSelected()) {
                                int minFilesize = 1;
                                int maxFilesize = 2;
                                try {
                                    minFilesize = Integer.parseInt(sizeFromField.getText()) * 1024;
                                    maxFilesize = Integer.parseInt(sizeToField.getText()) * 1024;
                                }
                                catch (Exception e) {
                                    setStatus(dsErrParseNums + " " + e.toString());
                                }
                                sizeList = getHitsForFilesizeRange(hits, minFilesize, maxFilesize);
                                numHits = sizeList[0].size();
                            }
                            else {
                                numHits = hits.length(); // NOT A SIZE QUERY
                            }

                            searchedIndexes.append("<li> <font color=\"blue\">");
                            searchedIndexes.append(currentIndex.getDescription());
                            searchedIndexes.append("</font> (<b>");
                            searchedIndexes.append(numHits);
                            searchedIndexes.append("</b>  &nbsp; ");
                            searchedIndexes.append(I18n.getString("documents"));
                            searchedIndexes.append(")</li>");
                            if (env.isGUIMode()) {
                                if (logger.isDebugEnabled()) {
                                    logger.debug("doSearch() " + I18n.getString("index") + ": " + currentIndex.getDescription());
                                }
                            }
                            else {
                                System.out.println(I18n.getString("index") + ": " + currentIndex.getDescription());
                            }
                            grandTotalHits += numHits;
                            tempBuf.append("<p align=\"center\"><b>");
                            tempBuf.append(numHits);
                            tempBuf.append("</b> &nbsp; ");
                            tempBuf.append(dsDocsFndInIndx);
                            tempBuf.append("<b>  &nbsp; ");
                            tempBuf.append(currentIndex.getDescription());
                            tempBuf.append("</b></p>");

                            curSrchPos++;
                            if (curSrchPos > 0) {
                                pPanel.setCurPos(curSrchPos);
                            }
                            for (int i = 0; i < numHits; i++) {
                                if (i > maxNumHitsShown) {
                                    setStatus(dsMxNumHits + " (" + maxNumHitsShown + ") " + I18n.getString("exceeded") + " (" + numHits + ").");
                                    break;
                                }

                                // get document and score from result or special result
                                Document currentDocument;
                                float currentScore;

                                // filesize result?
                                if (useSize.isSelected()) {
                                    currentDocument = (Document) sizeList[0].get(i);
                                    currentScore = ((Float) sizeList[1].get(i)).floatValue();
                                }
                                else {
                                    currentDocument = hits.doc(i);
                                    currentScore = hits.score(i);
                                }

                                // title
                                String currentTitle = Utils.convertTextToHTML(currentDocument.get(Index.FIELD_TITLE));

View Full Code Here

     * @param  default_field            the lucene field to run the query
     * @return                          lucene Hits
     * @exception  ProcessingException  iff its not possible do run the query
     */
    public Hits search(String query_string, String default_field) throws ProcessingException {
        Hits hits = null;

        if (query_string == null) {
            query_string = defaultQueryDefault;
        }
        if (default_field == null) {
View Full Code Here

     * @throws  ProcessingException  if an error occurs
     */
    private LuceneCocoonPager buildHits() throws ProcessingException {

        if (queryString != null && queryString.length() != 0) {
            Hits hits = null;

            // TODO (VG): Move parts into compose/initialize/recycle
            try {
                lcs = (LuceneCocoonSearcher) this.manager.lookup(LuceneCocoonSearcher.ROLE);
                Analyzer analyzer = LuceneCocoonHelper.getAnalyzer("org.apache.lucene.analysis.standard.StandardAnalyzer");
                lcs.setAnalyzer(analyzer);
                // get the directory where the index resides
                Directory directory = LuceneCocoonHelper.getDirectory(index, false);
                lcs.setDirectory(directory);
                hits = lcs.search(queryString, LuceneXMLIndexer.BODY_FIELD);
            } catch (IOException ioe) {
                throw new ProcessingException("IOException in search", ioe);
            } catch (ServiceException ce) {
                throw new ProcessingException("ComponentException in search", ce);
            } finally {
                if (lcs != null) {
                    this.manager.release(lcs);
                    lcs = null;
                }
            }

            // wrap the hits by an pager help object for accessing only a range of hits
            LuceneCocoonPager pager = new LuceneCocoonPager(hits);

            int start_index = START_INDEX_DEFAULT;
            if (this.startIndex != null) {
                start_index = this.startIndex.intValue();
                if (start_index <= 0) {
                    start_index = 0;
                }
                pager.setStartIndex(start_index);
            }

            int page_length = PAGE_LENGTH_DEFAULT;
            if (this.pageLength != null) {
                page_length = this.pageLength.intValue();
                if (page_length <= 0) {
                    page_length = hits.length();
                }
                pager.setCountOfHitsPerPage(page_length);
            }

            return pager;
View Full Code Here

TOP

Related Classes of org.apache.lucene.search.Hits

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.