@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")); NotSpanQuery not1 = new NotSpanQuery(term1, term2); TermSpanQuery term3 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "aaa")); TermSpanQuery term4 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "bbb")); NotSpanQuery not2 = new NotSpanQuery(term3, term4); assertEquals(not1, not2); NotSpanQuery not3 = new NotSpanQuery(term3, term4); not3.setLevelConstraint(3); assertNotEquals(not1, not3); NotSpanQuery not4 = new NotSpanQuery(term3, term4); not4.setNodeConstraint(5); assertNotEquals(not1, not4); }
@Test public void testNpeInSpanNearWithSpanNot() 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 hun = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "hundred")); TermSpanQuery term3 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "forty")); NearSpanQuery exclude = new NearSpanQuery(new TermSpanQuery[]{hun, term3}, 1, true); NotSpanQuery query = new NotSpanQuery(near, exclude); checkHits(query, new int[]{801, 821, 831, 851, 861, 871, 881, 891, 1801, 1821, 1831, 1851, 1861, 1871, 1881, 1891}); }
@Test public void testSpanNotWindowDoubleExcludesBefore() throws Exception { // test hitting two excludes before an include this.addDocuments(this.generateDocuments(2000)); TermSpanQuery term1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "forty")); TermSpanQuery term2 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "two")); NearSpanQuery near = new NearSpanQuery(new TermSpanQuery[]{term1, term2}, 2, true); TermSpanQuery exclude = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "one")); SpanQuery query = new NotSpanQuery(near, exclude, 4, 1); checkHits(query, new int[]{42, 242, 342, 442, 542, 642, 742, 842, 942}); }
@Test public void testSpanNot() 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 query = new NotSpanQuery(near, term3); checkHits(query, new int[]{801, 821, 831, 851, 861, 871, 881, 891, 1801, 1821, 1831, 1851, 1861, 1871, 1881, 1891}); }
@Test public void testSpanNotWindowOne() 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, "forty")); NearSpanQuery near = new NearSpanQuery(new TermSpanQuery[]{term1, term2}, 4, true); TermSpanQuery term3 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "one")); SpanQuery query = new NotSpanQuery(near, term3, 1, 1); checkHits(query, new int[]{840, 842, 843, 844, 845, 846, 847, 848, 849, 1840, 1842, 1843, 1844, 1845, 1846, 1847, 1848, 1849}); }
@Test public void testSpanNotWindowTwoBefore() 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, "forty")); NearSpanQuery near = new NearSpanQuery(new TermSpanQuery[]{term1, term2}, 4, true); TermSpanQuery term3 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "one")); NotSpanQuery query = new NotSpanQuery(near, term3, 2, 0); checkHits(query, new int[]{840, 841, 842, 843, 844, 845, 846, 847, 848, 849}); }
@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 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}); }
@Test public void testNonExistingExcludeTerm() throws IOException { this.addDocuments( doc(token("aaa", node(1)), token("bbb", node(1))) ); TermSpanQuery term1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "aaa")); TermSpanQuery term2 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "bbb")); TermSpanQuery term3 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "ccc")); NearSpanQuery span = new NearSpanQuery(new TermSpanQuery[]{ term1, term2 }, 0, true); Query query = new LuceneProxyNodeQuery(new NotSpanQuery(span, term3)); TopDocs hits = searcher.search(query, 100); assertEquals(1, hits.totalHits); }
@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}); }
@Test public void testSetAncestorPointer() { TermSpanQuery term1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "aaa")); TermSpanQuery term2 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "bbb")); NotSpanQuery not1 = new NotSpanQuery(term1, term2); final TwigQuery twig = new TwigQuery(); not1.setAncestorPointer(twig); assertSame(twig, not1.getAncestorPointer()); // clauses must have been updated assertSame(twig, term1.getAncestorPointer()); }
@Test public void testNotQueryMatchesNearInclude() throws IOException { this.addDocuments(doc(token("fish", node(1)), token("chips", node(1)))); NearSpanQuery fishandchips = new NearSpanQuery(new TermSpanQuery[]{fish, chips}, 0, true); Query query = new LuceneProxyNodeQuery(new NotSpanQuery(fishandchips, fish)); TopDocs hits = searcher.search(query, 100); assertEquals(0, hits.totalHits); }
@Test public void testSetLevelConstraint() { TermSpanQuery term1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "aaa")); TermSpanQuery term2 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "bbb")); NotSpanQuery not1 = new NotSpanQuery(term1, term2); not1.setLevelConstraint(3); assertEquals(3, not1.getLevelConstraint()); // Level constraint must have been transferred to the clauses assertEquals(3, not1.getInclude().getLevelConstraint()); assertEquals(3, not1.getExclude().getLevelConstraint()); }
@Test public void testNotQueryMatchesNearExclude() throws IOException { this.addDocuments(doc(token("fish", node(1)), token("and", node(1)), token("chips", node(1)), token("somethink", node(1)))); NearSpanQuery fishandchips = new NearSpanQuery(new TermSpanQuery[]{fish, chips}, 1, true); Query query = new LuceneProxyNodeQuery(new NotSpanQuery(fish, fishandchips)); TopDocs hits = searcher.search(query, 100); assertEquals(0, hits.totalHits); }
@Test public void testNotQueryMatchesButThereIsAnotherPosition() throws IOException { this.addDocuments(doc(token("somethink", node(1)), token("fish", node(1)), token("chips", node(1)), token("rise", node(1)), token("and", node(1)), token("fish", node(1)))); NearSpanQuery fishandchips = new NearSpanQuery(new TermSpanQuery[]{fish, chips}, 0, true); Query query = new LuceneProxyNodeQuery(new NotSpanQuery(fish, fishandchips)); TopDocs hits = searcher.search(query, 100); assertEquals(1, hits.totalHits); }
@Test public void testNotQueryMatchesNearExcludeTwoTimes() throws IOException { this.addDocuments(doc(token("flash", node(1)), token("chips", node(2)), token("fish", node(2)), token("and", node(2)), token("chips", node(2)), token("somethink", node(2)), token("and", node(2)), token("fish", node(2)), token("chips", node(2)))); NearSpanQuery fishandchips = new NearSpanQuery(new TermSpanQuery[]{fish, chips}, 1, false); Query query = new LuceneProxyNodeQuery(new NotSpanQuery(chips, fishandchips)); TopDocs hits = searcher.search(query, 100); assertEquals(0, hits.totalHits); }
@Test public void testNotQueryMatchesRangeNotFilter() throws IOException { this.addDocuments(doc(token("fish", node(2)), token("and", node(2)), token("chips", node(2)), token("is", node(2)), token("not", node(2)), token("good", node(2)), token("fish", node(2)))); this.addDocuments(doc(token("good", node(2)), token("fish", node(2)), token("is", node(2)), token("not", node(2)), token("fish", node(2)), token("and", node(2)), token("chips", node(2)))); NearSpanQuery fishandchips = new NearSpanQuery(new TermSpanQuery[]{fish, chips}, 1, false); NearSpanQuery goodFish = new NearSpanQuery(new TermSpanQuery[]{ new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "good")), fish}, 0, true); Query query = new LuceneProxyNodeQuery(new NotSpanQuery(goodFish, fishandchips, 2)); TopDocs hits = searcher.search(query, 100); assertEquals(2, hits.totalHits); }
@Test public void testNotQueryMatchesRange() throws IOException { this.addDocuments(doc(token("fish", node(2)), token("and", node(2)), token("chips", node(2)), token("is", node(2)), token("not", node(2)), token("good", node(2)), token("fish", node(2)))); this.addDocuments(doc(token("good", node(2)), token("fish", node(2)), token("is", node(2)), token("not", node(2)), token("fish", node(2)), token("and", node(2)), token("chips", node(2)))); NearSpanQuery fishandchips = new NearSpanQuery(new TermSpanQuery[]{fish, chips}, 1, false); NearSpanQuery goodFish = new NearSpanQuery(new TermSpanQuery[]{ new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "good")), fish}, 0, true); Query query = new LuceneProxyNodeQuery(new NotSpanQuery(goodFish, fishandchips, 3)); TopDocs hits = searcher.search(query, 100); assertEquals(0, hits.totalHits); }
@Test public void testNotQueryMatchesRangePrePost() throws IOException { this.addDocuments(doc(token("fish", node(2)), token("and", node(2)), token("chips", node(2)), token("is", node(2)), token("not", node(2)), token("good", node(2)), token("fish", node(2)))); this.addDocuments(doc(token("good", node(2)), token("fish", node(2)), token("is", node(2)), token("definitely", node(2)), token("not", node(2)), token("fish", node(2)), token("and", node(2)), token("chips", node(2)))); NearSpanQuery fishandchips = new NearSpanQuery(new TermSpanQuery[]{fish, chips}, 1, false); NearSpanQuery goodFish = new NearSpanQuery(new TermSpanQuery[]{ new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "good")), fish,}, 0, true); Query query = new LuceneProxyNodeQuery(new NotSpanQuery(goodFish, fishandchips, 3, 4)); TopDocs hits = searcher.search(query, 100); assertEquals(0, hits.totalHits); }
@Test public void testSpanNot() throws Exception { this.addDocuments(this.generateDocuments(2000)); NodeSpanQuery term1 = new NodeSpanQuery(ntq("eight").getQuery()); NodeSpanQuery term2 = new NodeSpanQuery(ntq("one").getQuery()); NearSpanQuery near = new NearSpanQuery(new SpanQuery[]{term1, term2}, 4, true); NodeSpanQuery term3 = new NodeSpanQuery(ntq("forty").getQuery()); SpanQuery query = new NotSpanQuery(near, term3); checkHits(query, new int[]{801, 821, 831, 851, 861, 871, 881, 891, 1801, 1821, 1831, 1851, 1861, 1871, 1881, 1891}); }