Ejemplo n.º 1
0
  public static SimpleOrderedMap<Object> getIndexInfo(IndexReader reader, boolean countTerms)
      throws IOException {
    Directory dir = reader.directory();
    SimpleOrderedMap<Object> indexInfo = new SimpleOrderedMap<Object>();

    indexInfo.add("numDocs", reader.numDocs());
    indexInfo.add("maxDoc", reader.maxDoc());

    if (countTerms) {
      TermEnum te = null;
      try {
        te = reader.terms();
        int numTerms = 0;
        while (te.next()) {
          numTerms++;
        }
        indexInfo.add("numTerms", numTerms);
      } finally {
        if (te != null) te.close();
      }
    }

    indexInfo.add(
        "version",
        reader.getVersion()); // TODO? Is this different then: IndexReader.getCurrentVersion( dir )?
    indexInfo.add("optimized", reader.isOptimized());
    indexInfo.add("current", reader.isCurrent());
    indexInfo.add("hasDeletions", reader.hasDeletions());
    indexInfo.add("directory", dir);
    indexInfo.add("lastModified", new Date(IndexReader.lastModified(dir)));
    return indexInfo;
  }
  /**
   * Checks if the index is optimized.
   *
   * @return true if it is optimized
   */
  public final boolean isOptimized() {
    boolean ret = false;
    IndexAccessor indexAccessor = this.getAccessor();
    IndexReader reader = null;
    try {
      reader = indexAccessor.getReader(false);
      ret = reader.isOptimized();
    } catch (IOException ex) {
      log.error("IOException happened during test of index. ", ex);
    } finally {
      indexAccessor.release(reader, false);
    }

    return ret;
  }
  @Override
  public boolean reload(String collectionName, String topRankingField) {
    if (collectionName == null) {
      return false;
    }

    CrescentCollectionHandler collectionHandler =
        SpringApplicationContext.getBean(
            "crescentCollectionHandler", CrescentCollectionHandler.class);

    CrescentCollection collection =
        collectionHandler.getCrescentCollections().getCrescentCollection(collectionName);

    if (collection == null) {
      logger.debug("doesn't Collection Info => {}", collectionName);
      init(View.Overview);
      return false;
    }

    if (topRankingField == null) {
      if (collection.getDefaultSearchFields().get(0) != null) {
        topRankingField = collection.getDefaultSearchFields().get(0).getName();
      } else {
        logger.debug("doesn't defaultSearchField => {}", collectionName);
        init(View.Overview);
        return false;
      }
    }

    List<String> fieldName = new ArrayList<String>();
    for (CrescentCollectionField field : collection.getFields()) fieldName.add(field.getName());
    TopRankingQueue topRankingQueue =
        new TopRankingQueue(DEFAULT_TOPRANKING_TERM, new RankingTermComparator());

    try {
      Directory directory = FSDirectory.open(new File(collection.getIndexingDirectory()));
      IndexReader reader = IndexReader.open(directory);

      TermEnum terms = reader.terms();

      int termFreq = 0;
      int termCount = 0;
      Term beforeTerm = null;
      // init term count
      fieldTermCount.clear();
      for (CrescentCollectionField field : collection.getFields())
        fieldTermCount.put(field.getName(), 0);
      topRankingQueue.clear();

      while (terms.next()) {
        Term currTerm = terms.term();
        if (beforeTerm == null) {
          beforeTerm = currTerm;
        }

        if (beforeTerm.field() == currTerm.field()) {
          termCount++;
        } else {
          fieldTermCount.put(beforeTerm.field(), termCount);
          termCount = 1;
          beforeTerm = currTerm;
        }

        TermDocs termDocs = reader.termDocs(currTerm);

        while (termDocs.next()) {
          if (currTerm.field().equals(topRankingField)) {
            RankingTerm e = new RankingTerm(currTerm.text(), currTerm.field(), termDocs.freq());
            topRankingQueue.add(e);
          }
        }
        termFreq++;
      }
      if (beforeTerm != null) fieldTermCount.put(beforeTerm.field(), termCount);

      terms.close();
      result.put("numOfTerm", termFreq);
      result.put("numOfDoc", reader.numDocs());
      result.put("hasDel", reader.hasDeletions());
      result.put("isOptimize", reader.isOptimized());
      result.put("indexVersion", reader.getVersion());
      result.put("lastModify", new Date(IndexReader.lastModified(directory)));
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
    if (topRankingQueue.size() != 0) {
      topRankingTerms = topRankingQueue.toArray();
      Arrays.sort(topRankingTerms);
    }
    result.put("collectionName", collectionName);
    result.put("indexName", collection.getIndexingDirectory());
    result.put("numOfField", collection.getFields().size());
    result.put("termCount", fieldTermCount);
    result.put("topRanking", topRankingTerms);
    result.put("fieldName", fieldName);

    return true;
  }
Ejemplo n.º 4
0
 public IndexInfo(String indexName, Directory directory, Analyzer analyzer)
     throws CorruptIndexException, IOException {
   IndexWriter iwriter = null;
   IndexReader indexReader = null;
   try {
     iwriter = new IndexWriter(directory, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
     indexReader = iwriter.getReader();
     this.index_name = indexName;
     this.index_version = indexReader.getVersion();
     this.optimized = indexReader.isOptimized();
     this.numDocs = indexReader.numDocs();
     Date date = new Date(IndexReader.lastModified(directory));
     DateFormat formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy, hh:mm:ss a");
     this.lastModifyDate = formatter.format(date);
     this.files = new HashMap<String, String>();
     String[] files = directory.listAll();
     for (int i = 0; i < files.length; i++) {
       if (files[i].startsWith("_"))
         this.files.put(
             files[i],
             Long.toString(
                     Math.round(
                         FileSizeConverter.convertSize(
                             directory.fileLength(files[i]), FileSizeConverter.KB)))
                 + " KB");
     }
     indexReader.close();
     iwriter.close();
     iwriter = null;
     directory.clearLock(directory.getLockID());
   } catch (CorruptIndexException e) {
     if (iwriter != null) {
       iwriter.close();
       iwriter = null;
     }
     if (indexReader != null) {
       indexReader.close();
       indexReader = null;
     }
     throw e;
   } catch (LockObtainFailedException e) {
     if (iwriter != null) {
       iwriter.close();
       iwriter = null;
     }
     if (indexReader != null) {
       indexReader.close();
       indexReader = null;
     }
     throw e;
   } catch (IOException e) {
     if (iwriter != null) {
       iwriter.close();
       iwriter = null;
     }
     if (indexReader != null) {
       indexReader.close();
       indexReader = null;
     }
     throw e;
   }
 }