package com.twitter.elephantbird.mapreduce.input;

import java.io.IOException;
import java.util.List;

import com.google.common.collect.ImmutableList;

import org.apache.hadoop.io.IntWritable;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Scorer;

/**
 * Only counts the number of hits for each query
 *
 * @author Alex Levenson
 */
public abstract class LuceneIndexCountHitsRecordReader
    extends LuceneIndexRecordReader<IntWritable> {

  @Override
  protected List<IntWritable> search(IndexSearcher searcher, Query query) throws IOException {
    CountHitsCollector collector = new CountHitsCollector();
    searcher.search(query, collector);
    return ImmutableList.of(new IntWritable(collector.getHits()));
  }

  private static final class CountHitsCollector extends Collector {
    private int hits = 0;

    @Override
    public void setScorer(Scorer scorer) throws IOException {
    }

    @Override
    public void collect(int i) throws IOException {
      hits++;
    }

    @Override
    public void setNextReader(AtomicReaderContext atomicReaderContext) throws IOException {
    }

    @Override
    public boolean acceptsDocsOutOfOrder() {
      return true;
    }

    public int getHits() {
      return hits;
    }
  }
}

@@ -215,33 +215,28 @@
 ene.
-index.AtomicReaderContext
+search.IndexSearcher
 ;%0Aim
@@ -265,25 +265,21 @@
 .search.
-Collector
+Query
 ;%0Aimport
@@ -300,37 +300,31 @@
 cene.search.
-IndexSearcher
+TopDocs
 ;%0Aimport org
@@ -349,52 +349,29 @@
 rch.
-Query;%0Aimport org.apache.lucene.search.Score
+TotalHitCountCollecto
 r;%0A%0A
@@ -673,25 +673,29 @@
 n %7B%0A    
+TotalHit
 Count
-Hits
 Collecto
@@ -712,25 +712,29 @@
 r = new 
+TotalHit
 Count
-Hits
 Collecto
@@ -834,16 +834,21 @@
 ctor.get
+Total
 Hits()))
@@ -852,528 +852,8 @@
 )));
-%0A  %7D%0A%0A  private static final class CountHitsCollector extends Collector %7B%0A    private int hits = 0;%0A%0A    @Override%0A    public void setScorer(Scorer scorer) throws IOException %7B%0A    %7D%0A%0A    @Override%0A    public void collect(int i) throws IOException %7B%0A      hits++;%0A    %7D%0A%0A    @Override%0A    public void setNextReader(AtomicReaderContext atomicReaderContext) throws IOException %7B%0A    %7D%0A%0A    @Override%0A    public boolean acceptsDocsOutOfOrder() %7B%0A      return true;%0A    %7D%0A%0A    public int getHits() %7B%0A      return hits;%0A    %7D
 %0A  %7D

use built in hit count collector