Package org.apache.lucene.index

Examples of org.apache.lucene.index.IndexReader


  }
 
  @Override
  public synchronized IndexReader reopen() throws CorruptIndexException,
    IOException {
  IndexReader newInner = null;

  SegmentInfos sinfos = new SegmentInfos();
  sinfos.read(_dir);
  int size = sinfos.size();
   
  if (in instanceof MultiReader){
    // setup current reader list
    List<IndexReader> boboReaderList = new LinkedList<IndexReader>();
    ReaderUtil.gatherSubReaders((List<IndexReader>)boboReaderList, in);
    Map<String,BoboIndexReader> readerMap = new HashMap<String,BoboIndexReader>();
    for (IndexReader reader : boboReaderList){
      BoboIndexReader boboReader = (BoboIndexReader)reader;
      SegmentReader sreader = (SegmentReader)(boboReader.in);
      readerMap.put(sreader.getSegmentName(),boboReader);
    }
   
    ArrayList<BoboIndexReader> currentReaders = new ArrayList<BoboIndexReader>(size);
    boolean isNewReader = false;
    for (int i=0;i<size;++i){
      SegmentInfo sinfo = (SegmentInfo)sinfos.get(i);
      BoboIndexReader breader = readerMap.remove(sinfo.name);
      if (breader!=null){
        // should use SegmentReader.reopen
        // TODO: see LUCENE-2559
        BoboIndexReader newReader = (BoboIndexReader)breader.reopen(true);
        if (newReader!=breader){
          isNewReader = true;
        }
        if (newReader!=null){
          currentReaders.add(newReader);
        }
      }
      else{
        isNewReader = true;
        SegmentReader newSreader = SegmentReader.get(true, sinfo, 1);
        breader = BoboIndexReader.getInstanceAsSubReader(newSreader,this._facetHandlers,this._runtimeFacetHandlerFactories);
        breader._dir = _dir;
        currentReaders.add(breader);
      }
    }
    isNewReader = isNewReader || (readerMap.size() != 0);
    if (!isNewReader){
      return this;
    }
    else{
      MultiReader newMreader = new MultiReader(currentReaders.toArray(new BoboIndexReader[currentReaders.size()]),false);
      BoboIndexReader newReader = BoboIndexReader.getInstanceAsSubReader(newMreader,this._facetHandlers,this._runtimeFacetHandlerFactories);
      newReader._dir = _dir;
      return newReader;
    }
  }
  else if (in instanceof SegmentReader){
     // should use SegmentReader.reopen
    // TODO: see LUCENE-2559
   
    SegmentReader sreader = (SegmentReader)in;
    int numDels = sreader.numDeletedDocs();
   
    SegmentInfo sinfo = null;
    boolean sameSeg = false;
    //get SegmentInfo instance
    for (int i=0;i<size;++i){
    SegmentInfo sinfoTmp = (SegmentInfo)sinfos.get(i);
    if (sinfoTmp.name.equals(sreader.getSegmentName())){
      int numDels2 = sinfoTmp.getDelCount();
      sameSeg = numDels==numDels2;
      sinfo = sinfoTmp;
      break;
    }
    }
  
    if (sinfo == null){
      // segment no longer exists
      return null;
    }
    if (sameSeg){
      return this
    }
    else{
    SegmentReader newSreader = SegmentReader.get(true, sinfo, 1);
    return BoboIndexReader.getInstanceAsSubReader(newSreader,this._facetHandlers,this._runtimeFacetHandlerFactories);
    }
  }
  else{
    // should not reach here, a catch-all default case
    IndexReader reader = in.reopen(true);
    if (in!=reader){
      return BoboIndexReader.getInstance(newInner, _facetHandlers, _runtimeFacetHandlerFactories, _workArea);
    }
    else{
    return this;
View Full Code Here


    profile.setSearchSuggestDataFile("src/test/resources/dictionary/default.txt");
    profile.setSuggestPopularTagsEnabled(false);
    indexer = new SearchSuggestIndexer();
    indexer.setSearchIndexProfile(profile);
    indexer.init();
    IndexReader reader = indexer.openSuggestIndexReader();
    assertEquals(13, reader.numDocs());
  }
View Full Code Here

    profile.setSuggestPopularTagsEnabled(true);
    indexer = new SearchSuggestIndexer();
    indexer.setGnizrDao(getGnizrDao());
    indexer.setSearchIndexProfile(profile);
    indexer.init();
    IndexReader reader = indexer.openSuggestIndexReader();
    assertEquals(4, reader.numDocs());
  }
View Full Code Here

  public String[] suggest(String queryString, IndexReader idxReader, String field){
    List<String> results = new ArrayList<String>(maxSuggestionSize);
    if(queryString != null && queryString.length() >= minQueryStringLength){
      Term term = new Term("t", queryString);
      PrefixQuery prefixQuery = new PrefixQuery(term);
      IndexReader indexReader = null;
      IndexSearcher indexSearch = null;
      try{
        indexReader = searchSuggestIndexer.openSuggestIndexReader();
        indexSearch = new IndexSearcher(indexReader);
        Hits hits = indexSearch.search(prefixQuery);
        int maxNumCandidate = maxSuggestionSize;
        if(idxReader != null && field != null){
          maxNumCandidate = maxSuggestionSize * 10;
        }
        PriorityQueue<SuggestWord> suggestQueue = new PriorityQueue<SuggestWord>(maxNumCandidate);
        for(int i = 0; i < hits.length() && i < maxNumCandidate; i++){
          String sugWord = hits.doc(i).get("t");
          // check if the 'sugWord' matches at least one doc in the
          // source index database (idxReader)
          if(idxReader != null && field != null){
            int freq = idxReader.docFreq(new Term(field,sugWord));
            if(freq > 0){
              suggestQueue.add(new SuggestWord(sugWord,freq));
            }
          }else{
            suggestQueue.add(new SuggestWord(sugWord,0));
          }         
        }
        int qSize = suggestQueue.size();
        for(int i = 0; i < maxSuggestionSize && i < qSize; i++){
          SuggestWord word = suggestQueue.poll();
          results.add(word.getWord());
        }
      }catch(Exception e){
        logger.error(e);
      }finally{
        try {
          if(indexSearch != null){
            indexSearch.close();
          }
        } catch (IOException e) {
          logger.error(e);
        }
        if(indexReader != null){
          try {
            indexReader.close();
          } catch (IOException e) {
            logger.error(e);
          }
        }
      }   
View Full Code Here

   *            a URL MD5 Hash.
   *
   * @return a Lucene document of the representative bookmark
   */
  public Document findLeadDocument(String urlHash) {
    IndexReader reader = null;
    TermDocs termDocs = null;
    Document leadDoc = null;
    try {
      boolean exists = IndexReader.indexExists(indexDirectory);
      if (exists == true) {
        reader = IndexReader.open(indexDirectory);
        Term key = new Term(DocumentCreator.FIELD_URL_MD5, urlHash);
        termDocs = reader.termDocs(key);
        boolean found = false;
        while (termDocs.next() && found == false) {
          int pos = termDocs.doc();
          // use FieldSelector for more efficient loading of Fields.
          // load only what's needed to determine a leading document
          Document d = reader.document(pos, new FieldSelector() {
            private static final long serialVersionUID = 1426724242925499003L;

            public FieldSelectorResult accept(String field) {
              if (field.equals(DocumentCreator.FIELD_INDEX_TYPE)) {
                return FieldSelectorResult.LOAD_AND_BREAK;
              } else {
                return FieldSelectorResult.NO_LOAD;
              }
            }
          });
          String[] values = d
              .getValues(DocumentCreator.FIELD_INDEX_TYPE);
          if (values != null) {
            List<String> vList = Arrays.asList(values);
            if (vList.contains(DocumentCreator.INDEX_TYPE_LEAD) == true) {
              leadDoc = reader.document(pos);
              found = true;
            }
          }
        }
      }
    } catch (Exception e) {
      logger.error("FindLeadDocument failed to find doc: " + urlHash
          + ", exception=" + e);
    } finally {
      try {
        if (termDocs != null) {
          termDocs.close();
        }
        if (reader != null) {
          reader.close();
        }
      } catch (Exception e) {
        logger
            .error("FindLeadDocument can't close reader or termDocs: "
                + e);
View Full Code Here

   *            a URL MD5 Hash.
   *
   * @return a Lucene document of a non-representative bookmark
   */
  public Document findNonLeadDocument(String urlHash) {
    IndexReader reader = null;
    TermDocs termDocs = null;
    Document leadDoc = null;
    try {
      boolean exists = IndexReader.indexExists(indexDirectory);
      if (exists == true) {
        reader = IndexReader.open(indexDirectory);
        Term key = new Term(DocumentCreator.FIELD_URL_MD5, urlHash);
        termDocs = reader.termDocs(key);
        boolean found = false;
        while (termDocs.next() && found == false) {
          int pos = termDocs.doc();
          // use FieldSelector for more efficient loading of Fields.
          // load only what's needed to determine a leading document
          Document d = reader.document(pos, new FieldSelector() {
            private static final long serialVersionUID = 1426724242925499003L;

            public FieldSelectorResult accept(String field) {
              if (field.equals(DocumentCreator.FIELD_INDEX_TYPE)) {
                return FieldSelectorResult.LOAD_AND_BREAK;
              } else {
                return FieldSelectorResult.NO_LOAD;
              }
            }

          });
          String[] values = d
              .getValues(DocumentCreator.FIELD_INDEX_TYPE);
          if (values != null) {
            List<String> vList = Arrays.asList(values);
            if (vList.contains(DocumentCreator.INDEX_TYPE_LEAD) == false) {
              leadDoc = reader.document(pos);
              found = true;
            }
          } else {
            leadDoc = reader.document(pos);
            found = true;
          }
        }
      }
    } catch (Exception e) {
      logger.error("FindLeadDocument failed to find doc hash: " + urlHash
          + ", exception=" + e);
    } finally {
      try {
        if (termDocs != null) {
          termDocs.close();
        }
        if (reader != null) {
          reader.close();
        }
      } catch (Exception e) {
        logger
            .error("FindLeadDocument can't close reader or termDocs: "
                + e);
View Full Code Here

          return SUCCESS;
        }
      }
       
      String[] results = new String[0];
      IndexReader idxReader = null;
      try{       
        idxReader = getIndexReader();     
        String field = termQuery.getTerm().field();
        String value = termQuery.getTerm().text();       
        results = searchTermSuggestion.suggest(value,idxReader,field);
        if(results != null && results.length > 0){
          if(field.equals(DocumentCreator.FIELD_TAG)){
            keywords = formatToKeywords(results,DocumentCreator.FIELD_TAG);
          }else{
            keywords = formatToKeywords(results,null);
          }
        }
      }catch(Exception e){
        logger.error(e);
      }finally{
        if(idxReader != null){
          try{
            idxReader.close();
          }catch(Exception e){
            logger.error("Unable to close IndexReader for bookmark search index");
          }
        }
      }
View Full Code Here

  //*-- should occur only for modified documents
  else
  { try 
     { synchronized(this)
       { FSDirectory fsd = FSDirectory.getDirectory(new File(Constants.getINDEXDIR()), false);
         IndexReader ir = IndexReader.open(fsd);
         ir.deleteDocuments(new Term("key", iDocument ) );
         ir.close();
       }
      }
      catch (IOException ie) { logger.error("Failed to delete " + iDocument + " " + ie.getMessage() ); }
     }
  } 
View Full Code Here

    ++count;
   } //*-- end of while

   //*-- clean up the Lucene index
   FSDirectory fsd = FSDirectory.getDirectory(new File(Constants.getINDEXDIR()), false);
   IndexReader ir = IndexReader.open(fsd);
   for (int i = 0; i < delFiles.size(); i++)
    ir.deleteDocuments(new Term("key", (String) delFiles.get(i)) );
   ir.close();
  }
  catch (DatabaseException dbe)
  { logger.error("Could not open cursor to browse File index" + dbe.getMessage() ); }
  catch (UnsupportedEncodingException ue)
  { logger.error("Could not decode key" + ue.getMessage() ); }
View Full Code Here

         * won the race for the lock, so we reopen the searcher. If flushed is
         * true, but currentSearcher != fSearcher it means that another thread
         * has reopened the reader while we were blocked waiting for the lock.
         */
        if (flushed && currentSearcher == fSearcher) {
          IndexReader currentReader = fSearcher.getIndexReader();
          IndexReader newReader = currentReader.reopen();
          if (newReader != currentReader) {

            IndexSearcher newSearcher = new IndexSearcher(newReader);
            fSearchers.put(newSearcher, new AtomicInteger(1));

View Full Code Here

TOP

Related Classes of org.apache.lucene.index.IndexReader

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.