@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}); }
private TermSpanQueryBuilder(final String fieldName, final String term) { final Term t = new Term(fieldName, term); tsq = new TermSpanQuery(t); // Add default datatype tsq.setDatatype(XSDDatatype.XSD_STRING); }
@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()); }
@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 String toString(final String field) { final StringBuilder builder = new StringBuilder(); final CharSequence text = term.text(); if (text.length() != 0) { builder.append("'").append(text).append("'"); } builder.append(ToStringUtils.boost(this.getBoost())); return this.wrapToStringWithDatatype(builder).toString(); }
@Override public NodeQueryBuilder setDatatype(final String datatype) { tsq.setDatatype(datatype); return this; }
@Override public boolean equals(final Object o) { if (!(o instanceof TermSpanQuery)) return false; final TermSpanQuery other = (TermSpanQuery) o; return (this.getBoost() == other.getBoost()) && this.term.equals(other.term) && this.levelConstraint == other.levelConstraint && this.lowerBound == other.lowerBound && this.upperBound == other.upperBound && StringUtils.equals(this.datatype, other.datatype); }
@Override public int hashCode() { return Float.floatToIntBits(this.getBoost()) ^ term.hashCode() ^ levelConstraint ^ upperBound ^ lowerBound; }
@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}); }
public NodeQuery build(QueryNode queryNode) throws QueryNodeException { final FieldQueryNode fieldNode = (FieldQueryNode) queryNode; // if it is tagged as a span query if (fieldNode.getTag(QueryTypeProcessor.QUERYTYPE_TAG) == QueryTypeProcessor.SPAN_QUERYTYPE) { // create the term span query TermSpanQuery tsq = new TermSpanQuery(new Term(fieldNode.getFieldAsString(), fieldNode.getTextAsString())); // assign the datatype. We must always have a datatype assigned. tsq.setDatatype((String) queryNode.getTag(DatatypeQueryNode.DATATYPE_TAGID)); return tsq; } else { // create the node term query NodeTermQuery ntq = new NodeTermQuery(new Term(fieldNode.getFieldAsString(), fieldNode.getTextAsString())); // assign the datatype. We must always have a datatype assigned. ntq.setDatatype((String) queryNode.getTag(DatatypeQueryNode.DATATYPE_TAGID)); return ntq; } }
public TermSpanWeight(final IndexSearcher searcher, final TermContext termStates) throws IOException { assert termStates != null : "TermContext must not be null"; this.termStates = termStates; this.similarity = searcher.getSimilarity(); this.stats = similarity.computeWeight( TermSpanQuery.this.getBoost(), searcher.collectionStatistics(term.field()), searcher.termStatistics(term, termStates)); }
@Test public void testSpanExactNested() throws Exception { this.addDocuments(this.generateDocuments(2000)); TermSpanQuery term1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "three")); TermSpanQuery term2 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "hundred")); NearSpanQuery near1 = new NearSpanQuery(new TermSpanQuery[] {term1, term2}, 0, true); TermSpanQuery term3 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "thirty")); TermSpanQuery term4 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "three")); NearSpanQuery near2 = new NearSpanQuery(new TermSpanQuery[] {term3, term4}, 0, true); NearSpanQuery query = new NearSpanQuery(new SpanQuery[] {near1, near2}, 0, true); checkHits(query, new int[] {333, 1333}); // assertTrue(searcher.explain(query, 333).getValue() > 0.0f); }
@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 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}); }
@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 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 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 testSpanNearOrdered() throws Exception { this.addDocuments(this.generateDocuments(1000)); TermSpanQuery term1 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "nine")); TermSpanQuery term2 = new TermSpanQuery(new Term(DEFAULT_TEST_FIELD, "six")); NearSpanQuery query = new NearSpanQuery(new TermSpanQuery[] {term1, term2}, 4, true); checkHits(query, 906, 926, 936, 946, 956, 966, 976, 986, 996); }