/** Returns true iff <code>o</code> is equal to this. */ @Override public boolean equals(final Object o) { if (!(o instanceof TwigQuery)) return false; final TwigQuery other = (TwigQuery) o; return (this.getBoost() == other.getBoost()) && this.clauses.equals(other.clauses) && this.root.equals(other.root); // root and twig query should have the same constraints, // no need to integrate them into the equality test }
@Override public int hashCode() { return Float.floatToIntBits(this.getBoost()) ^ clauses.hashCode() ^ root.hashCode(); // root and twig query should have the same constraints, // no need to integrate them into the hashcode }
@Override public float getValueForNormalization() throws IOException { float sum = 0.0f; for (int i = 0; i < weights.size(); i++) { // call sumOfSquaredWeights for all clauses in case of side effects final float s = weights.get(i).getValueForNormalization(); // sum sub weights if (!clauses.get(i).isProhibited()) { // only add to sum for non-prohibited clauses sum += s; } } // incorporate root weight sum += rootWeight.getValueForNormalization(); // boost each weight sum *= TwigQuery.this.getBoost() * TwigQuery.this.getBoost(); return sum; }
@Override public void normalize(final float norm, float topLevelBoost) { // incorporate boost topLevelBoost *= TwigQuery.this.getBoost(); for (final Weight w : weights) { // normalize all clauses, (even if prohibited in case of side affects) w.normalize(norm, topLevelBoost); } // Normalise root weight rootWeight.normalize(norm, topLevelBoost); }
@Test public void testRewriteEmptyClauses() throws IOException { final TwigQuery tq = new TwigQuery(2); tq.addRoot(new NodeTermQuery(new Term("field", "value"))); tq.setBoost(0.5f); // it must be rewritten into the root query final NodeQuery q = (NodeQuery) tq.rewrite(null); assertTrue(q instanceof NodeTermQuery); assertEquals(2, q.getLevelConstraint()); assertEquals(tq.getBoost(), q.getBoost(), 0); assertSame(tq.ancestor, q.ancestor); }
@Test public void testRewriteTwigQueryRoot() throws IOException { // if the root node of a twig query is another twig query, then they must // be merged final TwigQuery tq2 = new TwigQuery(2); tq2.addRoot(new NodeTermQuery(new Term("root2", "root2"))); tq2.addChild(new NodeTermQuery(new Term("child2", "child2")), Occur.MUST); tq2.setBoost(0.5f); final TwigQuery tq1 = new TwigQuery(); tq1.addRoot(tq2); tq1.addChild(new NodeTermQuery(new Term("child1", "child1")), Occur.MUST); tq1.setBoost(0.5f); final NodeQuery q = (NodeQuery) tq1.rewrite(null); assertTrue(q instanceof TwigQuery); final TwigQuery tq = (TwigQuery) q; // level and boosst must be the same than for tq1 assertEquals(1, tq.getLevelConstraint()); assertEquals(tq.getBoost(), tq1.getBoost(), 0); // root must not be the same than for tq2 (it has been cloned) assertNotSame(tq.getRoot(), tq2.getRoot()); // root must be equal to the root of tq2 assertEquals(tq.getRoot(), tq2.getRoot()); // clauses must be merged assertEquals(2, tq.clauses().size()); for (final NodeBooleanClause clause : tq.clauses()) { assertEquals(2, clause.getQuery().getLevelConstraint()); assertSame(tq.getRoot(), clause.getQuery().ancestor); } }
assertTrue(q instanceof AncestorFilterQuery); assertEquals(2, q.getLevelConstraint()); assertEquals(tq.getBoost(), q.getBoost(), 0);