PredicateIndex { public Searcher searcher() { return new Searcher(); } PredicateIndex(
            Config config,
            int[] internalToExternalIdMapping,
            byte[] minFeatureIndex,
            short[] intervalEnds,
            int highestIntervalEnd,
            SimpleIndex intervalIndex,
            SimpleIndex boundsIndex,
            SimpleIndex conjunctionIntervalIndex,
            PredicateIntervalStore intervalStore,
            ConjunctionIndex conjunctionIndex,
            int[] zeroConstraintDocuments); void rebuildPostingListCache(); Searcher searcher(); void writeToOutputStream(DataOutputStream out); static PredicateIndex fromInputStream(DataInputStream in);  }
@Test public void requireThatPredicateIndexCanSearch() { PredicateIndexBuilder builder = new PredicateIndexBuilder(10); builder.indexDocument(1, Predicate.fromString("country in ['no', 'se'] and gender in ['male']")); builder.indexDocument(0x3fffffe, Predicate.fromString("country in ['no'] and gender in ['female']")); PredicateIndex index = builder.build(); PredicateIndex.Searcher searcher = index.searcher(); PredicateQuery query = new PredicateQuery(); query.addFeature("country", "no"); query.addFeature("gender", "male"); assertEquals("[1]", searcher.search(query).collect(toList()).toString()); query.addFeature("gender", "female"); assertEquals("[1, 67108862]", searcher.search(query).collect(toList()).toString()); }
@Test public void requireThatPredicateIndexCanSearchWithNotExpression() { { PredicateIndexBuilder builder = new PredicateIndexBuilder(10); builder.indexDocument(1, Predicate.fromString("country in ['no'] and gender not in ['male']")); PredicateIndex index = builder.build(); PredicateIndex.Searcher searcher = index.searcher(); PredicateQuery query = new PredicateQuery(); query.addFeature("country", "no"); query.addFeature("gender", "female"); assertEquals("[1]", searcher.search(query).collect(toList()).toString()); } { PredicateIndexBuilder builder = new PredicateIndexBuilder(10); builder.indexDocument(DOC_ID, Predicate.fromString("country in ['no'] and gender in ['male']")); builder.indexDocument(DOC_ID + 1, Predicate.fromString("country not in ['no']")); PredicateIndex index = builder.build(); PredicateIndex.Searcher searcher = index.searcher(); PredicateQuery query = new PredicateQuery(); assertEquals("[43]", searcher.search(query).collect(toList()).toString()); query.addFeature("country", "no"); assertEquals(0, searcher.search(query).count()); } { PredicateIndexBuilder builder = new PredicateIndexBuilder(10); builder.indexDocument(DOC_ID, Predicate.fromString("country not in ['no'] and gender not in ['male']")); PredicateIndex index = builder.build(); PredicateIndex.Searcher searcher = index.searcher(); PredicateQuery query = new PredicateQuery(); assertEquals(1, searcher.search(query).count()); query.addFeature("country", "no"); assertEquals(0, searcher.search(query).count()); query.addFeature("gender", "male"); assertEquals(0, searcher.search(query).count()); query = new PredicateQuery(); query.addFeature("gender", "male"); assertEquals(0, searcher.search(query).count()); } }
@Test public void requireThatSearchesCanUseSubqueries() { PredicateIndexBuilder builder = new PredicateIndexBuilder(10); builder.indexDocument(DOC_ID, Predicate.fromString("country in [no] and gender in [male]")); PredicateIndex index = builder.build(); PredicateIndex.Searcher searcher = index.searcher(); PredicateQuery query = new PredicateQuery(); query.addFeature("country", "no", 0x3); assertEquals(0, searcher.search(query).count()); query.addFeature("gender", "male", 0x6); assertEquals("[[42,0x2]]", searcher.search(query).collect(toList()).toString()); }
@Test public void requireThatPredicateIndexCanSearchWithRange() { PredicateIndexBuilder builder = new PredicateIndexBuilder(10); builder.indexDocument(1, Predicate.fromString("gender in ['male'] and age in [20..40]")); builder.indexDocument(2, Predicate.fromString("gender in ['female'] and age in [20..40]")); PredicateIndex index = builder.build(); PredicateIndex.Searcher searcher = index.searcher(); PredicateQuery query = new PredicateQuery(); query.addFeature("gender", "male"); query.addRangeFeature("age", 36); assertEquals("[1]", searcher.search(query).collect(toList()).toString()); query.addFeature("gender", "female"); assertEquals("[1, 2]", searcher.search(query).collect(toList()).toString()); }
@Test public void requireThatPredicateIndexCanSearchWithEmptyDocuments() { PredicateIndexBuilder builder = new PredicateIndexBuilder(10); builder.indexDocument(1, Predicate.fromString("true")); builder.indexDocument(2, Predicate.fromString("false")); PredicateIndex index = builder.build(); PredicateIndex.Searcher searcher = index.searcher(); PredicateQuery query = new PredicateQuery(); assertEquals("[1]", searcher.search(query).collect(toList()).toString()); }
@Test public void requireThatPredicatesHavingMultipleIdenticalConjunctionsAreSupported() { PredicateIndexBuilder builder = new PredicateIndexBuilder(10); builder.indexDocument(DOC_ID, Predicate.fromString( "((a in ['b'] and c in ['d']) or x in ['y']) and ((a in ['b'] and c in ['d']) or z in ['w'])")); PredicateIndex index = builder.build(); PredicateIndex.Searcher searcher = index.searcher(); PredicateQuery query = new PredicateQuery(); query.addFeature("a", "b"); query.addFeature("c", "d"); assertEquals("[42]", searcher.search(query).collect(toList()).toString()); }