@Override protected OrSpanQuery getTopLevelQuery(final NodeQuery query) { OrSpanQuery q = new OrSpanQuery(); // set level and node constraints q.setLevelConstraint(query.getLevelConstraint()); q.setNodeConstraint(query.getNodeConstraint()[0], query.getNodeConstraint()[1]); // set ancestor q.setAncestorPointer(query.getAncestorPointer()); return q; }
public OrSpanQuery(final SpanQuery... clauses) { // copy clauses array into an ArrayList this.clauses = new ArrayList<SpanQuery>(clauses.length); for (int i = 0; i < clauses.length; i++) { this.addClause(clauses[i]); } }
/** Returns true if <code>o</code> is equal to this. */ @Override public boolean equals(final Object o) { if (!(o instanceof OrSpanQuery)) return false; final OrSpanQuery other = (OrSpanQuery) o; return (this.getBoost() == other.getBoost()) && this.clauses.equals(other.clauses) && this.levelConstraint == other.levelConstraint && this.lowerBound == other.lowerBound && this.upperBound == other.upperBound; }
@Test public void testSetAncestorPointer() { NodeSpanQuery term1 = new NodeSpanQuery(ntq("aaa").getQuery()); NodeSpanQuery term2 = new NodeSpanQuery(ntq("bbb").getQuery()); OrSpanQuery or1 = new OrSpanQuery(new SpanQuery[] {term1, term2}); final TwigQuery twig = new TwigQuery(); or1.setAncestorPointer(twig); assertSame(twig, or1.getAncestorPointer()); // clauses must have been updated assertSame(twig, term1.getAncestorPointer()); NodeSpanQuery term3 = new NodeSpanQuery(ntq("aaa").getQuery()); or1.addClause(term3); // new clause must have been updated assertSame(twig, term3.getAncestorPointer()); }
@Test public void testSpanWithMultipleNotMany() throws Exception { this.addDocuments(this.generateDocuments(2000)); TermSpanQuery term1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "eight")); TermSpanQuery term2 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "one")); NearSpanQuery near = new NearSpanQuery(new TermSpanQuery[]{term1, term2}, 4, true); TermSpanQuery term3 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "forty")); TermSpanQuery term4 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "sixty")); TermSpanQuery term5 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "eighty")); OrSpanQuery or = new OrSpanQuery(new TermSpanQuery[]{term3, term4, term5}); NotSpanQuery query = new NotSpanQuery(near, or); checkHits(query, new int[]{801, 821, 831, 851, 871, 891, 1801, 1821, 1831, 1851, 1871, 1891}); }
@Test public void testEquality() throws Exception { TermSpanQuery term1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "aaa")); TermSpanQuery term2 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "bbb")); OrSpanQuery or1 = new OrSpanQuery(new SpanQuery[] {term1, term2}); TermSpanQuery term3 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "aaa")); TermSpanQuery term4 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "bbb")); OrSpanQuery or2 = new OrSpanQuery(new SpanQuery[] {term3, term4}); assertEquals(or1, or2); OrSpanQuery or3 = new OrSpanQuery(new SpanQuery[] {term1, term2}); or3.setLevelConstraint(3); assertNotEquals(or1, or3); OrSpanQuery or4 = new OrSpanQuery(new SpanQuery[] {term1, term2}); or4.setNodeConstraint(5); assertNotEquals(or1, or4); }
@Test public void testSetLevelConstraint() { TermSpanQuery term1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "aaa")); TermSpanQuery term2 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "bbb")); OrSpanQuery or1 = new OrSpanQuery(new SpanQuery[] {term1, term2}); or1.setLevelConstraint(3); assertEquals(3, or1.getLevelConstraint()); // Level constraint must have been transferred to the clauses assertEquals(3, or1.getClauses()[0].getLevelConstraint()); assertEquals(3, or1.getClauses()[1].getLevelConstraint()); NodeSpanQuery term3 = new NodeSpanQuery(ntq("aaa").getQuery()); NodeSpanQuery term4 = new NodeSpanQuery(ntq("bbb").getQuery()); OrSpanQuery or2 = new OrSpanQuery(new SpanQuery[] {term3, term4}); or2.setLevelConstraint(4); OrSpanQuery or3 = new OrSpanQuery(new SpanQuery[] {or1, or2}); or3.setLevelConstraint(6); // Level constraint must have been transferred to the clauses assertEquals(6, or1.getLevelConstraint()); assertEquals(6, or2.getLevelConstraint()); }
@Test public void testSpanNearOr() throws Exception { this.addDocuments(this.generateDocuments(2000)); TermSpanQuery t1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD,"six")); TermSpanQuery t3 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD,"seven")); TermSpanQuery t5 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD,"seven")); TermSpanQuery t6 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD,"six")); OrSpanQuery to1 = new OrSpanQuery(t1, t3); OrSpanQuery to2 = new OrSpanQuery(t5, t6); NearSpanQuery query = new NearSpanQuery(new SpanQuery[] {to1, to2}, 10, true); checkHits(query, new int[] { 606, 607, 626, 627, 636, 637, 646, 647, 656, 657, 666, 667, 676, 677, 686, 687, 696, 697, 706, 707, 726, 727, 736, 737, 746, 747, 756, 757, 766, 767, 776, 777, 786, 787, 796, 797, 1606, 1607, 1626, 1627, 1636, 1637, 1646, 1647, 1656, 1657, 1666, 1667, 1676, 1677, 1686, 1687, 1696, 1697, 1706, 1707, 1726, 1727, 1736, 1737, 1746, 1747, 1756, 1757, 1766, 1767, 1776, 1777, 1786, 1787, 1796, 1797}); }
@Test public void testSpanWithMultipleNotSingle() throws Exception { this.addDocuments(this.generateDocuments(2000)); TermSpanQuery term1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "eight")); TermSpanQuery term2 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "one")); NearSpanQuery near = new NearSpanQuery(new TermSpanQuery[]{term1, term2}, 4, true); TermSpanQuery term3 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "forty")); SpanQuery or = new OrSpanQuery(new TermSpanQuery[]{term3}); SpanQuery query = new NotSpanQuery(near, or); checkHits(query, new int[]{801, 821, 831, 851, 861, 871, 881, 891, 1801, 1821, 1831, 1851, 1861, 1871, 1881, 1891}); }
@Override protected void addClause(OrSpanQuery topLevel, Term term, int docFreq, float boost, TermContext states) { final TermSpanQuery q = new TermSpanQuery(term); q.setBoost(boost); topLevel.addClause(q); } };
@Override public int hashCode() { return Float.floatToIntBits(this.getBoost()) ^ clauses.hashCode() ^ levelConstraint ^ upperBound ^ lowerBound; }
@Test public void testSpanNotWindowNeg() throws Exception { this.addDocuments(this.generateDocuments(2000)); // test handling of invalid window < 0 TermSpanQuery term1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "eight")); TermSpanQuery term2 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "one")); NearSpanQuery near = new NearSpanQuery(new TermSpanQuery[]{term1, term2}, 4, true); TermSpanQuery term3 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "forty")); OrSpanQuery or = new OrSpanQuery(new TermSpanQuery[]{term3}); NotSpanQuery query = new NotSpanQuery(near, or); checkHits(query, new int[]{801, 821, 831, 851, 861, 871, 881, 891, 1801, 1821, 1831, 1851, 1861, 1871, 1881, 1891}); }
@Override public float getValueForNormalization() throws IOException { float sum = 0.0f; for (int i = 0; i < weights.size(); i++) { sum += weights.get(i).getValueForNormalization(); // sum sub weights } // boost each sub-weight sum *= OrSpanQuery.this.getBoost() * OrSpanQuery.this.getBoost(); return sum; }
@Test public void testSpanOr() throws Exception { this.addDocuments(this.generateDocuments(2000)); TermSpanQuery term1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "thirty")); TermSpanQuery term2 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "three")); NearSpanQuery near1 = new NearSpanQuery(new TermSpanQuery[] {term1, term2}, 0, true); TermSpanQuery term3 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "forty")); TermSpanQuery term4 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "seven")); NearSpanQuery near2 = new NearSpanQuery(new TermSpanQuery[] {term3, term4}, 0, true); OrSpanQuery query = new OrSpanQuery(near1, near2); checkHits(query, new int[] { 33, 47, 133, 147, 233, 247, 333, 347, 433, 447, 533, 547, 633, 647, 733, 747, 833, 847, 933, 947, 1033, 1047, 1133, 1147, 1233, 1247, 1333, 1347, 1433, 1447, 1533, 1547, 1633, 1647, 1733, 1747, 1833, 1847, 1933, 1947}); }
@Override public void normalize(final float norm, float topLevelBoost) { // incorporate boost topLevelBoost *= OrSpanQuery.this.getBoost(); for (final Weight w : weights) { // normalize all clauses w.normalize(norm, topLevelBoost); } }
@Test public void testSpanComplex1() throws Exception { this.addDocuments(this.generateDocuments(2000)); TermSpanQuery t1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD,"six")); TermSpanQuery t2 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD,"hundred")); NearSpanQuery tt1 = new NearSpanQuery(new TermSpanQuery[] {t1, t2}, 0, true); TermSpanQuery t3 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD,"seven")); TermSpanQuery t4 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD,"hundred")); NearSpanQuery tt2 = new NearSpanQuery(new TermSpanQuery[] {t3, t4}, 0, true); TermSpanQuery t5 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD,"seven")); TermSpanQuery t6 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD,"six")); OrSpanQuery to1 = new OrSpanQuery(tt1, tt2); OrSpanQuery to2 = new OrSpanQuery(t5, t6); NearSpanQuery query = new NearSpanQuery(new SpanQuery[] {to1, to2}, 100, true); checkHits(query, new int[] { 606, 607, 626, 627, 636, 637, 646, 647, 656, 657, 666, 667, 676, 677, 686, 687, 696, 697, 706, 707, 726, 727, 736, 737, 746, 747, 756, 757, 766, 767, 776, 777, 786, 787, 796, 797, 1606, 1607, 1626, 1627, 1636, 1637, 1646, 1647, 1656, 1657, 1666, 1667, 1676, 1677, 1686, 1687, 1696, 1697, 1706, 1707, 1726, 1727, 1736, 1737, 1746, 1747, 1756, 1757, 1766, 1767, 1776, 1777, 1786, 1787, 1796, 1797}); }
@Override public String toString(final String field) { StringBuilder buffer = new StringBuilder(); buffer.append("spanOr(["); Iterator<SpanQuery> i = clauses.iterator(); while (i.hasNext()) { SpanQuery clause = i.next(); buffer.append(clause.toString(field)); if (i.hasNext()) { buffer.append(", "); } } buffer.append("])"); buffer.append(ToStringUtils.boost(getBoost())); return buffer.toString(); }
SpanQuery spanNoSuch = new MultiTermSpanQuery<NodeFuzzyQuery>(fuzzyNoSuch); SpanQuery term = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "brown")); OrSpanQuery near = new OrSpanQuery(new SpanQuery[] { term, spanNoSuch }); Query query = new LuceneProxyNodeQuery(near); assertEquals(1, searcher.search(query, 10).totalHits); near = new OrSpanQuery(new SpanQuery[] { spanNoSuch, term }); query = new LuceneProxyNodeQuery(near); assertEquals(1, searcher.search(query, 10).totalHits); NodeWildcardQuery wcNoSuch = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "noSuch*")); SpanQuery spanWCNoSuch = new MultiTermSpanQuery<NodeWildcardQuery>(wcNoSuch); near = new OrSpanQuery(new SpanQuery[] { term, spanWCNoSuch }); query = new LuceneProxyNodeQuery(near); assertEquals(1, searcher.search(query, 10).totalHits); near = new OrSpanQuery(new SpanQuery[] { term, spanRgxNoSuch }); query = new LuceneProxyNodeQuery(near); assertEquals(1, searcher.search(query, 10).totalHits); near = new OrSpanQuery(new SpanQuery[] { term, spanPrfxNoSuch }); query = new LuceneProxyNodeQuery(near); assertEquals(1, searcher.search(query, 10).totalHits); near = new OrSpanQuery(new SpanQuery[] { spanPrfxNoSuch }); query = new LuceneProxyNodeQuery(near); assertEquals(0, searcher.search(query, 10).totalHits); near = new OrSpanQuery(new SpanQuery[] { spanPrfxNoSuch, spanPrfxNoSuch }); query = new LuceneProxyNodeQuery(near);
@Test public void testSpanOrSingleDocument1() throws Exception { // first test with TermSpanQuery on a single node this.addDocuments( doc( token("aaa", node(1)), token("bbb", node(1))) ); TermSpanQuery termA = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "aaa")); TermSpanQuery termB = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "bbb")); TermSpanQuery termC = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "ccc")); TermSpanQuery termD = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "ddd")); // ask for "aaa" or "bbb" - should return 2 hits 1 doc OrSpanQuery spanQuery = new OrSpanQuery(new TermSpanQuery[] {termA, termB}); Query query = new LuceneProxyNodeQuery(spanQuery); TopDocs hits = searcher.search(query, 100); assertEquals(1, hits.totalHits); }