home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (DeleteByPercentTask.java)

This example Lucene source code file (DeleteByPercentTask.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

deletebypercenttask, deletebypercenttask, exception, exception, indexreader, override, override, perftask, random, random, termdocs, util

The Lucene DeleteByPercentTask.java source code

package org.apache.lucene.benchmark.byTask.tasks;

 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

import java.util.Random;

import org.apache.lucene.benchmark.byTask.PerfRunData;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.TermDocs;

 * Deletes a percentage of documents from an index randomly
 * over the number of documents.  The parameter, X, is in
 * percent.  EG 50 means 1/2 of all documents will be
 * deleted.
 * <p>NOTE: the param is an absolute percentage of
 * maxDoc().  This means if you delete 50%, and then delete
 * 50% again, the 2nd delete will do nothing.
 * <p> Parameters:
 * <ul>
 * <li> delete.percent.rand.seed - defines the seed to
 * initialize Random (default 1717)
 * </ul>
public class DeleteByPercentTask extends PerfTask {
  double percent;
  int numDeleted = 0;
  final Random random;

  public DeleteByPercentTask(PerfRunData runData) {
    random = new Random(runData.getConfig().get("delete.percent.rand.seed", 1717));
  public void setup() throws Exception {

  public void setParams(String params) {
    percent = Double.parseDouble(params)/100;

  public boolean supportsParams() {
    return true;

  public int doLogic() throws Exception {
    IndexReader r = getRunData().getIndexReader();
    int maxDoc = r.maxDoc();
    int numDeleted = 0;
    // percent is an absolute target:
    int numToDelete = ((int) (maxDoc * percent)) - r.numDeletedDocs();
    if (numToDelete < 0) {
      numToDelete = (int) (maxDoc * percent);
    while (numDeleted < numToDelete) {
      double delRate = ((double) (numToDelete-numDeleted))/r.numDocs();
      TermDocs termDocs = r.termDocs(null);
      while (termDocs.next() && numDeleted < numToDelete) {
        if (random.nextDouble() <= delRate) {
    System.out.println("--> processed (delete) " + numDeleted + " docs");
    return numDeleted;

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene DeleteByPercentTask.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.