LUCENE-8367: make per-dimension drill down optional for each facet dimension

This commit is contained in:
Mike McCandless 2018-06-27 15:57:21 -04:00
parent 39fb4b8a18
commit e8d1057403
8 changed files with 151 additions and 2 deletions

View File

@ -130,6 +130,10 @@ Changes in Runtime Behavior:
* LUCENE-7976: TieredMergePolicy now respects maxSegmentSizeMB by default when executing
findForcedMerges and findForcedDeletesMerges (Erick Erickson)
Improvements
* LUCENE-8367: Make per-dimension drill down optional for each facet dimension (Mike McCandless)
Other:
* LUCENE-8366: Upgrade to ICU 62.1. Emoji handling now uses Unicode 11's

View File

@ -539,6 +539,19 @@ public final class CheckIndex implements Closeable {
return result;
}
if (infoStream != null) {
int maxDoc = 0;
int delCount = 0;
for (SegmentCommitInfo info : sis) {
maxDoc += info.info.maxDoc();
delCount += info.getDelCount();
}
infoStream.println(String.format(Locale.ROOT, "%.2f%% total deletions; %d documents; %d deleteions",
100.*delCount/maxDoc,
maxDoc,
delCount));
}
// find the oldest and newest segment versions
Version oldest = null;
Version newest = null;

View File

@ -82,6 +82,11 @@ public class FacetsConfig {
* is required, which is unusual (default is false). */
public boolean requireDimCount;
/** True if drilling down by a whole dimension, to match all
* documents that had any value for this dimension, is necessary
* (default is true)*/
public boolean requireDimensionDrillDown = true;
/** Actual field where this dimension's facet labels
* should be indexed */
public String indexFieldName = DEFAULT_INDEX_FIELD_NAME;
@ -164,6 +169,16 @@ public class FacetsConfig {
ft.indexFieldName = indexFieldName;
}
/** Specify whether drill down on just the dimension is necessary. */
public synchronized void setRequireDimensionDrillDown(String dimName, boolean v) {
DimConfig ft = fieldTypes.get(dimName);
if (ft == null) {
ft = new DimConfig();
fieldTypes.put(dimName, ft);
}
ft.requireDimensionDrillDown = v;
}
/** Returns map of field name to {@link DimConfig}. */
public Map<String,DimConfig> getDimConfigs() {
return fieldTypes;
@ -340,7 +355,13 @@ public class FacetsConfig {
}
// Drill down:
for (int i=1;i<=cp.length;i++) {
int start;
if (ft.requireDimensionDrillDown) {
start = 1;
} else {
start = 2;
}
for (int i=start;i<=cp.length;i++) {
doc.add(new StringField(indexFieldName, pathToString(cp.components, i), Field.Store.NO));
}
}
@ -368,10 +389,14 @@ public class FacetsConfig {
// For drill-down:
doc.add(new StringField(indexFieldName, fullPath, Field.Store.NO));
FacetsConfig.DimConfig ft = getDimConfig(facetField.dim);
if (ft.requireDimensionDrillDown) {
doc.add(new StringField(indexFieldName, facetField.dim, Field.Store.NO));
}
}
}
}
private void processAssocFacetFields(TaxonomyWriter taxoWriter,
Map<String,List<AssociationFacetField>> byField, Document doc)
@ -399,7 +424,15 @@ public class FacetsConfig {
System.arraycopy(field.assoc.bytes, field.assoc.offset, bytes, upto, field.assoc.length);
upto += field.assoc.length;
FacetsConfig.DimConfig ft = getDimConfig(field.dim);
// Drill down:
int start;
if (ft.requireDimensionDrillDown) {
start = 1;
} else {
start = 2;
}
for (int i = 1; i <= label.length; i++) {
doc.add(new StringField(indexFieldName, pathToString(label.components, i), Field.Store.NO));
}

View File

@ -92,6 +92,22 @@ public final class DoubleRange extends Range {
return "DoubleRange(" + label + ": " + min + " to " + max + ")";
}
@Override
public boolean equals(Object _that) {
if (_that instanceof DoubleRange == false) {
return false;
}
DoubleRange that = (DoubleRange) _that;
return that.label.equals(this.label) &&
Double.compare(that.min, this.min) == 0 &&
Double.compare(that.max, this.max) == 0;
}
@Override
public int hashCode() {
return Objects.hash(label, min, max);
}
private static class ValueSourceQuery extends Query {
private final DoubleRange range;
private final Query fastMatchQuery;

View File

@ -84,6 +84,22 @@ public final class LongRange extends Range {
return "LongRange(" + label + ": " + min + " to " + max + ")";
}
@Override
public boolean equals(Object _that) {
if (_that instanceof LongRange == false) {
return false;
}
LongRange that = (LongRange) _that;
return that.label.equals(this.label) &&
that.min == this.min &&
that.max == this.max;
}
@Override
public int hashCode() {
return Objects.hash(label, min, max);
}
private static class ValueSourceQuery extends Query {
private final LongRange range;
private final Query fastMatchQuery;

View File

@ -89,6 +89,7 @@ public class LruTaxonomyWriterCache implements TaxonomyWriterCache {
cache = null;
}
@Override
public int size() {
return cache.getSize();
}

View File

@ -252,4 +252,44 @@ public class TestDrillDownQuery extends FacetTestCase {
Query rewrite = q.rewrite(reader).rewrite(reader);
assertEquals(base, rewrite);
}
public void testRequireDimensionDrillDown() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir,
newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.KEYWORD, false)));
Directory taxoDir = newDirectory();
TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
FacetsConfig config = new FacetsConfig();
config.setRequireDimensionDrillDown("a", true);
config.setRequireDimensionDrillDown("b", false);
Document doc = new Document();
doc.add(new FacetField("a", "1"));
doc.add(new FacetField("b", "2"));
writer.addDocument(config.build(taxoWriter, doc));
taxoWriter.close();
IndexReader reader = writer.getReader();
DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
IndexSearcher searcher = newSearcher(reader);
DrillDownQuery q = new DrillDownQuery(config);
q.add("a", "1");
assertEquals(1, searcher.count(q));
q = new DrillDownQuery(config);
q.add("a");
assertEquals(1, searcher.count(q));
q = new DrillDownQuery(config);
q.add("b", "2");
assertEquals(1, searcher.count(q));
q = new DrillDownQuery(config);
q.add("b");
// no hits because we disabled dimension drill down for dimension "b":
assertEquals(0, searcher.count(q));
IOUtils.close(taxoReader, reader, writer, dir, taxoDir);
}
}

View File

@ -867,4 +867,30 @@ public class TestRangeFacetCounts extends FacetTestCase {
writer.close();
IOUtils.close(r, dir);
}
public void testLongRangeEquals() throws Exception {
assertEquals(new LongRange("field", -7, true, 17, false),
new LongRange("field", -7, true, 17, false));
assertEquals(new LongRange("field", -7, true, 17, false).hashCode(),
new LongRange("field", -7, true, 17, false).hashCode());
assertFalse(new LongRange("field", -7, true, 17, false).equals(new LongRange("field", -7, true, 17, true)));
assertFalse(new LongRange("field", -7, true, 17, false).hashCode() ==
new LongRange("field", -7, true, 17, true).hashCode());
assertFalse(new LongRange("field", -7, true, 17, false).equals(new LongRange("field", -7, true, 18, false)));
assertFalse(new LongRange("field", -7, true, 17, false).hashCode() ==
new LongRange("field", -7, true, 18, false).hashCode());
}
public void testDoubleRangeEquals() throws Exception {
assertEquals(new DoubleRange("field", -7d, true, 17d, false),
new DoubleRange("field", -7d, true, 17d, false));
assertEquals(new DoubleRange("field", -7d, true, 17d, false).hashCode(),
new DoubleRange("field", -7d, true, 17d, false).hashCode());
assertFalse(new DoubleRange("field", -7d, true, 17d, false).equals(new DoubleRange("field", -7d, true, 17d, true)));
assertFalse(new DoubleRange("field", -7d, true, 17d, false).hashCode() ==
new DoubleRange("field", -7d, true, 17d, true).hashCode());
assertFalse(new DoubleRange("field", -7d, true, 17d, false).equals(new DoubleRange("field", -7d, true, 18d, false)));
assertFalse(new DoubleRange("field", -7d, true, 17d, false).hashCode() ==
new DoubleRange("field", -7d, true, 18, false).hashCode());
}
}