jpountz commented on code in PR #14204: URL: https://github.com/apache/lucene/pull/14204#discussion_r1954925602
########## lucene/facet/src/java/org/apache/lucene/facet/histogram/HistogramCollector.java: ########## @@ -0,0 +1,252 @@ +/* + * 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, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.facet.histogram; + +import java.io.IOException; +import org.apache.lucene.index.DocValues; +import org.apache.lucene.index.DocValuesSkipper; +import org.apache.lucene.index.DocValuesType; +import org.apache.lucene.index.FieldInfo; +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.NumericDocValues; +import org.apache.lucene.index.SortedNumericDocValues; +import org.apache.lucene.internal.hppc.LongIntHashMap; +import org.apache.lucene.search.CollectionTerminatedException; +import org.apache.lucene.search.Collector; +import org.apache.lucene.search.LeafCollector; +import org.apache.lucene.search.Scorable; +import org.apache.lucene.search.ScoreMode; + +final class HistogramCollector implements Collector { Review Comment: Ok it seems that I was confused, I thought that the densification of ordinals was the responsibility of the cutter, but it seems to happen on the recorder, so producing non-dense ordinals should be fine (?) Then it would be nice to make ordinals `long`s rather than `int`s since we can't guarantee that `Math.floorDiv(value, bucketWidth)` would always return a value in the `int` range? Separately I played with the quick/dirty benchmark I had created, which seems to have got a bit more than 2x slower. I guess this is the cost of the additional abstractions. I'm including it for reference, it's certainly crude. ```patch diff --git a/src/extra/perf/IndexGeoNames.java b/src/extra/perf/IndexGeoNames.java index aa98b20..6db0837 100644 --- a/src/extra/perf/IndexGeoNames.java +++ b/src/extra/perf/IndexGeoNames.java @@ -44,6 +44,7 @@ import org.apache.lucene.document.IntField; import org.apache.lucene.document.IntPoint; import org.apache.lucene.document.KeywordField; import org.apache.lucene.document.LongField; +import org.apache.lucene.document.NumericDocValuesField; import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; //import org.apache.lucene.index.IndexReader; @@ -52,6 +53,8 @@ import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.IndexWriterConfig.OpenMode; import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.NoMergePolicy; +import org.apache.lucene.search.Sort; +import org.apache.lucene.search.SortField; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.PrintStreamInfoStream; @@ -75,7 +78,10 @@ public class IndexGeoNames { Directory dir = FSDirectory.open(indexPath); //IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_48, new StandardAnalyzer(Version.LUCENE_48)); - IndexWriterConfig iwc = new IndexWriterConfig(new StandardAnalyzer()); + SortField sortField = new SortField("elevation", SortField.Type.LONG); + sortField.setMissingValue(Long.MIN_VALUE); + Sort sort = new Sort(sortField); + IndexWriterConfig iwc = new IndexWriterConfig(new StandardAnalyzer()).setIndexSort(sort); iwc.setOpenMode(OpenMode.CREATE); //iwc.setRAMBufferSizeMB(350); iwc.setInfoStream(new PrintStreamInfoStream(System.out)); @@ -144,8 +150,8 @@ public class IndexGeoNames { baseDoc.add(admin3Field); StringField admin4Field = new StringField("admin4", "", store); baseDoc.add(admin4Field); - LongField population = new LongField("population", 0, store); - LongField elevation = new LongField("elevation", 0, store); + NumericDocValuesField population = NumericDocValuesField.indexedField("population", 0); + NumericDocValuesField elevation = NumericDocValuesField.indexedField("elevation", 0); IntField dem = new IntField("dem", 0, store); KeywordField tzField = new KeywordField("timezone", "", store); baseDoc.add(tzField); @@ -282,11 +288,11 @@ public class IndexGeoNames { if (values[14].isEmpty() == false) { long v = Long.parseLong(values[14]); - doc.add(new LongField("population", v, store)); + doc.add(NumericDocValuesField.indexedField("population", v)); } if (values[15].isEmpty() == false) { long v = Long.parseLong(values[15]); - doc.add(new LongField("elevation", v, store)); + doc.add(NumericDocValuesField.indexedField("elevation", v)); } if (values[16].isEmpty() == false) { doc.add(new IntField("dem", Integer.parseInt(values[16]), store)); diff --git a/src/extra/perf/SearchGeoNames.java b/src/extra/perf/SearchGeoNames.java index 743c422..87e98f4 100644 --- a/src/extra/perf/SearchGeoNames.java +++ b/src/extra/perf/SearchGeoNames.java @@ -20,8 +20,6 @@ package perf; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; -import java.text.ParsePosition; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -32,7 +30,14 @@ import org.apache.lucene.document.IntPoint; import org.apache.lucene.document.LongPoint; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; +import org.apache.lucene.internal.hppc.LongIntHashMap; +import org.apache.lucene.sandbox.facet.FacetFieldCollectorManager; +import org.apache.lucene.sandbox.facet.iterators.OrdinalIterator; +import org.apache.lucene.sandbox.facet.plain.histograms.HistogramFacetCutter; +import org.apache.lucene.sandbox.facet.recorders.CountFacetRecorder; +import org.apache.lucene.search.FieldExistsQuery; import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; @@ -53,7 +58,7 @@ public class SearchGeoNames { IndexSearcher s = new IndexSearcher(r); s.setQueryCache(null); // don't bench the cache - SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd", Locale.US); + /*SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd", Locale.US); System.out.println("t=" + dateParser.parse("2014-12-01", new ParsePosition(0)).getTime()); searchOneField(s, getQueries(s, "geoNameID", 0, 10000000)); @@ -61,7 +66,32 @@ public class SearchGeoNames { searchOneField(s, getQueries(s, "longitude", -180.0, 180.0)); // 1993-12-01 to 2014-12-01: - searchOneField(s, getQueries(s, "modified", 754722000000L, 1417410000000L)); + searchOneField(s, getQueries(s, "modified", 754722000000L, 1417410000000L));*/ + + System.out.println(r.maxDoc() + " total docs"); + System.out.println(s.count(new FieldExistsQuery("elevation")) + " docs with elevation"); + for (int i = 0; i < 10000; ++i) { + long start = System.nanoTime(); + // histogram_facet branch + //LongIntHashMap counts = s.search(new MatchAllDocsQuery(), new HistogramCollectorManager("elevation", 100)); + // histogram cutter branch + CountFacetRecorder recorder = new CountFacetRecorder(); + HistogramFacetCutter cutter = new HistogramFacetCutter("elevation", 100); + FacetFieldCollectorManager<CountFacetRecorder> collectorManager = + new FacetFieldCollectorManager<>(cutter, recorder); + s.search(new MatchAllDocsQuery(), collectorManager); + long end = System.nanoTime(); + // histogram_facet branch + // System.out.println((end - start) + " " + counts); + // histogram cutter branch + LongIntHashMap counts = new LongIntHashMap(); + OrdinalIterator ords = recorder.recordedOrds(); + for (int ord = ords.nextOrd(); ord != OrdinalIterator.NO_MORE_ORDS; ord = ords.nextOrd()) { + counts.put(ord, recorder.getCount(ord)); + } + System.out.println((end - start) + " " + counts); + recorder.recordedOrds(); + } r.close(); dir.close(); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org