Apache Lucene open-source search software
LUCENE-5345: move the distance example to demo
Michael McCandlessc1523b8b1c14
diff --git a/lucene/demo/src/java/org/apache/lucene/demo/facet/DistanceFacetsExample.java b/lucene/demo/src/java/org/apache/lucene/demo/facet/DistanceFacetsExample.javanew file mode 100644index 00000000000..1b67f00210e--- /dev/null+++ b/lucene/demo/src/java/org/apache/lucene/demo/facet/DistanceFacetsExample.java@@ -0,0 +1,140 @@+package org.apache.lucene.demo.facet;++/*+ * 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.+ */++import java.io.Closeable;+import java.io.IOException;+import java.text.ParseException;+import java.util.List;++import org.apache.lucene.analysis.core.WhitespaceAnalyzer;+import org.apache.lucene.document.Document;+import org.apache.lucene.document.DoubleField;+import org.apache.lucene.document.Field;+import org.apache.lucene.document.LongField;+import org.apache.lucene.document.NumericDocValuesField;+import org.apache.lucene.expressions.Expression;+import org.apache.lucene.expressions.SimpleBindings;+import org.apache.lucene.expressions.js.JavascriptCompiler;+import org.apache.lucene.facet.DoubleRange;+import org.apache.lucene.facet.DoubleRangeFacetCounts;+import org.apache.lucene.facet.DrillDownQuery;+import org.apache.lucene.facet.FacetResult;+import org.apache.lucene.facet.Facets;+import org.apache.lucene.facet.FacetsCollector;+import org.apache.lucene.facet.FacetsConfig;+import org.apache.lucene.facet.LongRange;+import org.apache.lucene.index.DirectoryReader;+import org.apache.lucene.index.IndexWriter;+import org.apache.lucene.index.IndexWriterConfig;+import org.apache.lucene.search.IndexSearcher;+import org.apache.lucene.search.MatchAllDocsQuery;+import org.apache.lucene.search.NumericRangeQuery;+import org.apache.lucene.search.SortField;+import org.apache.lucene.search.TopDocs;+import org.apache.lucene.store.Directory;+import org.apache.lucene.store.RAMDirectory;++++/** Shows simple usage of dynamic range faceting, using the+ * expressions module to calculate distance. */+public class DistanceFacetsExample implements Closeable {++ private final Directory indexDir = new RAMDirectory();+ private IndexSearcher searcher;++ /** Empty constructor */+ public DistanceFacetsExample() {}++ /** Build the example index. */+ public void index() throws IOException {+ IndexWriter writer = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER,+ new WhitespaceAnalyzer(FacetExamples.EXAMPLES_VER)));++ // Add documents with latitude/longitude location:+ Document doc = new Document();+ doc.add(new DoubleField("latitude", 40.759011, Field.Store.NO));+ doc.add(new DoubleField("longitude", -73.9844722, Field.Store.NO));+ writer.addDocument(doc);++ doc = new Document();+ doc.add(new DoubleField("latitude", 40.718266, Field.Store.NO));+ doc.add(new DoubleField("longitude", -74.007819, Field.Store.NO));+ writer.addDocument(doc);++ doc = new Document();+ doc.add(new DoubleField("latitude", 40.7051157, Field.Store.NO));+ doc.add(new DoubleField("longitude", -74.0088305, Field.Store.NO));+ writer.addDocument(doc);++ // Open near-real-time searcher+ searcher = new IndexSearcher(DirectoryReader.open(writer, true));+ writer.close();+ }++ /** User runs a query and counts facets. */+ public FacetResult search() throws IOException {++ Expression distance;+ try {+ distance = JavascriptCompiler.compile("haversin(40.7143528,-74.0059731,latitude,longitude)");+ } catch (ParseException pe) {+ // Should not happen+ throw new RuntimeException(pe);+ }+ SimpleBindings bindings = new SimpleBindings();+ bindings.add(new SortField("latitude", SortField.Type.DOUBLE));+ bindings.add(new SortField("longitude", SortField.Type.DOUBLE));++ FacetsCollector fc = new FacetsCollector();++ searcher.search(new MatchAllDocsQuery(), fc);++ Facets facets = new DoubleRangeFacetCounts("field", distance.getValueSource(bindings), fc,+ new DoubleRange("< 1 km", 0.0, true, 1.0, false),+ new DoubleRange("< 2 km", 0.0, true, 2.0, false),+ new DoubleRange("< 5 km", 0.0, true, 5.0, false),+ new DoubleRange("< 10 km", 0.0, true, 10.0, false),+ new DoubleRange("< 20 km", 0.0, true, 20.0, false),+ new DoubleRange("< 50 km", 0.0, true, 50.0, false));++ return facets.getTopChildren(10, "field");+ }++ // nocommit how to show drillDown?++ @Override+ public void close() throws IOException {+ searcher.getIndexReader().close();+ indexDir.close();+ }++ /** Runs the search and drill-down examples and prints the results. */+ @SuppressWarnings("unchecked")+ public static void main(String[] args) throws Exception {+ RangeFacetsExample example = new RangeFacetsExample();+ example.index();++ System.out.println("Dirance facet counting example:");+ System.out.println("-----------------------");+ System.out.println(example.search());++ example.close();+ }+}diff --git a/lucene/demo/src/java/org/apache/lucene/demo/facet/RangeFacetsExample.java b/lucene/demo/src/java/org/apache/lucene/demo/facet/RangeFacetsExample.javaindex 8a7d4b4be48..3647327341e 100644--- a/lucene/demo/src/java/org/apache/lucene/demo/facet/RangeFacetsExample.java+++ b/lucene/demo/src/java/org/apache/lucene/demo/facet/RangeFacetsExample.java@@ -26,13 +26,13 @@ import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.LongField;import org.apache.lucene.document.NumericDocValuesField;-import org.apache.lucene.facet.Facets;-import org.apache.lucene.facet.FacetsConfig;-import org.apache.lucene.facet.LongRange;-import org.apache.lucene.facet.RangeFacetCounts;import org.apache.lucene.facet.DrillDownQuery;import org.apache.lucene.facet.FacetResult;+import org.apache.lucene.facet.Facets;import org.apache.lucene.facet.FacetsCollector;+import org.apache.lucene.facet.FacetsConfig;+import org.apache.lucene.facet.LongRange;+import org.apache.lucene.facet.LongRangeFacetCounts;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;@@ -96,10 +96,10 @@ public class RangeFacetsExample implements Closeable {// you'd use a "normal" query:FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);- Facets facets = new RangeFacetCounts("timestamp", fc,- PAST_HOUR,- PAST_SIX_HOURS,- PAST_DAY);+ Facets facets = new LongRangeFacetCounts("timestamp", fc,+ PAST_HOUR,+ PAST_SIX_HOURS,+ PAST_DAY);return facets.getTopChildren(10, "timestamp");}diff --git a/lucene/demo/src/test/org/apache/lucene/demo/facet/TestDistanceFacetsExample.java b/lucene/demo/src/test/org/apache/lucene/demo/facet/TestDistanceFacetsExample.javanew file mode 100644index 00000000000..fd4cd9e474f--- /dev/null+++ b/lucene/demo/src/test/org/apache/lucene/demo/facet/TestDistanceFacetsExample.java@@ -0,0 +1,37 @@+package org.apache.lucene.demo.facet;++/*+ * 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.+ */++import java.util.List;++import org.apache.lucene.facet.FacetResult;+import org.apache.lucene.search.TopDocs;+import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;+import org.apache.lucene.util.LuceneTestCase;++@SuppressCodecs("Lucene3x")+public class TestDistanceFacetsExample extends LuceneTestCase {++ public void testSimple() throws Exception {+ DistanceFacetsExample example = new DistanceFacetsExample();+ example.index();+ FacetResult result = example.search();+ assertEquals("value=3 childCount=6\n < 1 km (1)\n < 2 km (2)\n < 5 km (2)\n < 10 km (3)\n < 20 km (3)\n < 50 km (3)\n", result.toString());+ example.close();+ }+}diff --git a/lucene/facet/build.xml b/lucene/facet/build.xmlindex b6c7e904eec..d6ed7dec27b 100644--- a/lucene/facet/build.xml+++ b/lucene/facet/build.xml@@ -32,11 +32,9 @@<path id="classpath"><path refid="base.classpath"/><pathelement path="${queries.jar}"/>- <pathelement path="${expressions.jar}"/>- <fileset dir="../expressions/lib"/></path>- <target name="compile-core" depends="jar-queries,jar-expressions,common.compile-core" />+ <target name="compile-core" depends="jar-queries,common.compile-core" /><target name="javadocs" depends="javadocs-queries,compile-core"><invoke-module-javadoc>diff --git a/lucene/facet/src/test/org/apache/lucene/facet/TestRangeFacetCounts.java b/lucene/facet/src/test/org/apache/lucene/facet/TestRangeFacetCounts.javaindex 6752d6de5af..da1a84946c4 100644--- a/lucene/facet/src/test/org/apache/lucene/facet/TestRangeFacetCounts.java+++ b/lucene/facet/src/test/org/apache/lucene/facet/TestRangeFacetCounts.java@@ -29,16 +29,17 @@ import org.apache.lucene.document.FloatDocValuesField;import org.apache.lucene.document.FloatField;import org.apache.lucene.document.LongField;import org.apache.lucene.document.NumericDocValuesField;-import org.apache.lucene.expressions.Expression;-import org.apache.lucene.expressions.SimpleBindings;-import org.apache.lucene.expressions.js.JavascriptCompiler;import org.apache.lucene.facet.DrillSideways.DrillSidewaysResult;import org.apache.lucene.facet.taxonomy.TaxonomyReader;import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;+import org.apache.lucene.index.AtomicReaderContext;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.index.RandomIndexWriter;+import org.apache.lucene.queries.function.FunctionValues;+import org.apache.lucene.queries.function.ValueSource;+import org.apache.lucene.queries.function.docvalues.DoubleDocValues;import org.apache.lucene.queries.function.valuesource.FloatFieldSource;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.MatchAllDocsQuery;@@ -536,45 +537,62 @@ public class TestRangeFacetCounts extends FacetTestCase {IOUtils.close(w, r, d);}- public void testDistanceRangeFaceting() throws Exception {+ public void testCustomDoublesValueSource() throws Exception {Directory dir = newDirectory();RandomIndexWriter writer = new RandomIndexWriter(random(), dir);Document doc = new Document();- doc.add(new DoubleField("latitude", 40.759011, Field.Store.NO));- doc.add(new DoubleField("longitude", -73.9844722, Field.Store.NO));writer.addDocument(doc);doc = new Document();- doc.add(new DoubleField("latitude", 40.718266, Field.Store.NO));- doc.add(new DoubleField("longitude", -74.007819, Field.Store.NO));writer.addDocument(doc);doc = new Document();- doc.add(new DoubleField("latitude", 40.7051157, Field.Store.NO));- doc.add(new DoubleField("longitude", -74.0088305, Field.Store.NO));writer.addDocument(doc);-- Expression distance = JavascriptCompiler.compile("haversin(40.7143528,-74.0059731,latitude,longitude)");- SimpleBindings bindings = new SimpleBindings();- bindings.add(new SortField("latitude", SortField.Type.DOUBLE));- bindings.add(new SortField("longitude", SortField.Type.DOUBLE));+ writer.forceMerge(1);++ ValueSource vs = new ValueSource() {+ @Override+ public FunctionValues getValues(Map ignored, AtomicReaderContext ignored2) {+ return new DoubleDocValues(null) {+ public double doubleVal(int doc) {+ return doc+1;+ }+ };+ }++ @Override+ public boolean equals(Object o) {+ throw new UnsupportedOperationException();+ }++ @Override+ public int hashCode() {+ throw new UnsupportedOperationException();+ }++ @Override+ public String description() {+ throw new UnsupportedOperationException();+ }+ };+FacetsCollector fc = new FacetsCollector();IndexReader r = writer.getReader();IndexSearcher s = newSearcher(r);s.search(new MatchAllDocsQuery(), fc);- Facets facets = new DoubleRangeFacetCounts("field", distance.getValueSource(bindings), fc,- new DoubleRange("< 1 km", 0.0, true, 1.0, false),- new DoubleRange("< 2 km", 0.0, true, 2.0, false),- new DoubleRange("< 5 km", 0.0, true, 5.0, false),- new DoubleRange("< 10 km", 0.0, true, 10.0, false),- new DoubleRange("< 20 km", 0.0, true, 20.0, false),- new DoubleRange("< 50 km", 0.0, true, 50.0, false));+ Facets facets = new DoubleRangeFacetCounts("field", vs, fc,+ new DoubleRange("< 1", 0.0, true, 1.0, false),+ new DoubleRange("< 2", 0.0, true, 2.0, false),+ new DoubleRange("< 5", 0.0, true, 5.0, false),+ new DoubleRange("< 10", 0.0, true, 10.0, false),+ new DoubleRange("< 20", 0.0, true, 20.0, false),+ new DoubleRange("< 50", 0.0, true, 50.0, false));- assertEquals("value=3 childCount=6\n < 1 km (1)\n < 2 km (2)\n < 5 km (2)\n < 10 km (3)\n < 20 km (3)\n < 50 km (3)\n", facets.getTopChildren(10, "field").toString());+ assertEquals("value=3 childCount=6\n < 1 (0)\n < 2 (1)\n < 5 (3)\n < 10 (3)\n < 20 (3)\n < 50 (3)\n", facets.getTopChildren(10, "field").toString());IOUtils.close(r, writer, dir);}}