lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adrien Grand <jpou...@gmail.com>
Subject Re: how to get max value of a long field?
Date Fri, 17 May 2013 09:36:26 GMT
Hi,

On Fri, May 17, 2013 at 11:10 AM, Hu Jing <huj.nju@gmail.com> wrote:
> I want to know the max value of a long field.
> I read lucene api , but don't find any api about this?
> does someone can supply any hits about how to implement this.

To do this efficiently, your field needs to have doc values[1].

First, iterate over your DirectoryReader leaves[2]. Then for every
AtomicReaderContext.reader(), get a NumericDocValues instance for your
field[3]. Then iterate over the values to compute the maximum value:

    IndexReader rd;
    long max = Long.MIN_VALUE;
    for (AtomicReaderContext ctx : rd.leaves()) {
      final NumericDocValues longs =
ctx.reader().getNumericDocValues("my_long_field");
      final Bits liveDocs = ctx.reader().getLiveDocs();
      for (int i = 0; i < ctx.reader().maxDoc(); ++i) {
        if (liveDocs != null || liveDocs.get(i)) {
          max = Math.max(max, longs.get(i));
        }
      }
    }

[1] http://lucene.apache.org/core/4_3_0/core/org/apache/lucene/document/NumericDocValuesField.html
[2] http://lucene.apache.org/core/4_3_0/core/org/apache/lucene/index/IndexReader.html#leaves()
[3] http://lucene.apache.org/core/4_3_0/core/org/apache/lucene/index/AtomicReader.html#getNumericDocValues(java.lang.String)

--
Adrien

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message