@Override public Query rewrite(IndexReader reader) throws IOException { if (boost == 1f) { return query; } final SpanQuery rewritten = (SpanQuery) query.rewrite(reader); if (query != rewritten) { return new SpanBoostQuery(rewritten, boost); } if (query.getClass() == SpanBoostQuery.class) { SpanBoostQuery in = (SpanBoostQuery) query; return new SpanBoostQuery(in.query, boost * in.boost); } return super.rewrite(reader); }
@Override public int hashCode() { int h = classHash(); h = 31 * h + query.hashCode(); h = 31 * h + Float.floatToIntBits(boost); return h; }
@Override public boolean equals(Object other) { return sameClassAs(other) && equalsTo(getClass().cast(other)); }
@Override public Query rewrite(IndexReader reader) throws IOException { final SpanQuery rewritten = (SpanQuery) query.rewrite(reader); if (getBoost() == 1f) { return rewritten; } if (rewritten.getClass() == SpanBoostQuery.class) { SpanBoostQuery in = (SpanBoostQuery) rewritten; return new SpanBoostQuery(in.query, getBoost() * in.getBoost()); } if (query != rewritten) { return new SpanBoostQuery(rewritten, getBoost()); } return this; }
public void addSpanQuery(Query q) { if (q.getClass() == MatchNoDocsQuery.class) return; if (! (q instanceof SpanQuery)) throw new AssertionError("Expected SpanQuery: " + q.toString(getFieldName())); float boost = 1f; if (q instanceof SpanBoostQuery) { SpanBoostQuery bq = (SpanBoostQuery) q; boost = bq.getBoost(); q = bq.getQuery(); } addSpanQueryWeighted((SpanQuery)q, boost); }
@Override public void normalize(float norm, float boost) { weight.normalize(norm, SpanBoostQuery.this.getBoost() * boost); }
/** Sole constructor: wrap {@code query} in such a way that the produced * scores will be boosted by {@code boost}. */ public SpanBoostQuery(SpanQuery query, float boost) { this.query = Objects.requireNonNull(query); setBoost(boost); }
@Override public Query rewrite(IndexReader reader) throws IOException { final SpanQuery rewritten = (SpanQuery) query.rewrite(reader); if (getBoost() == 1f) { return rewritten; } if (rewritten.getClass() == SpanBoostQuery.class) { SpanBoostQuery in = (SpanBoostQuery) rewritten; return new SpanBoostQuery(in.query, getBoost() * in.getBoost()); } if (query != rewritten) { return new SpanBoostQuery(rewritten, getBoost()); } return this; }
public void addSpanQuery(Query q) { if (q.getClass() == MatchNoDocsQuery.class) return; if (! (q instanceof SpanQuery)) throw new AssertionError("Expected SpanQuery: " + q.toString(getFieldName())); float boost = 1f; if (q instanceof SpanBoostQuery) { SpanBoostQuery bq = (SpanBoostQuery) q; boost = bq.getBoost(); q = bq.getQuery(); } addSpanQueryWeighted((SpanQuery)q, boost); }
@Override public void normalize(float norm, float boost) { weight.normalize(norm, SpanBoostQuery.this.getBoost() * boost); }
/** Sole constructor: wrap {@code query} in such a way that the produced * scores will be boosted by {@code boost}. */ public SpanBoostQuery(SpanQuery query, float boost) { this.query = Objects.requireNonNull(query); setBoost(boost); }
@Override public final Query toQuery(QueryShardContext context) throws IOException { Query query = doToQuery(context); if (query != null) { if (boost != DEFAULT_BOOST) { if (query instanceof SpanQuery) { query = new SpanBoostQuery((SpanQuery) query, boost); } else { query = new BoostQuery(query, boost); } } if (queryName != null) { context.addNamedQuery(queryName, query); } } return query; }
/** * Checks the result of {@link QueryBuilder#toQuery(QueryShardContext)} given the original {@link QueryBuilder} * and {@link QueryShardContext}. Verifies that named queries and boost are properly handled and delegates to * {@link #doAssertLuceneQuery(AbstractQueryBuilder, Query, SearchContext)} for query specific checks. */ private void assertLuceneQuery(QB queryBuilder, Query query, SearchContext context) throws IOException { if (queryBuilder.queryName() != null) { Query namedQuery = context.getQueryShardContext().copyNamedQueries().get(queryBuilder.queryName()); assertThat(namedQuery, equalTo(query)); } if (query != null) { if (queryBuilder.boost() != AbstractQueryBuilder.DEFAULT_BOOST) { assertThat(query, either(instanceOf(BoostQuery.class)).or(instanceOf(SpanBoostQuery.class))); if (query instanceof SpanBoostQuery) { SpanBoostQuery spanBoostQuery = (SpanBoostQuery) query; assertThat(spanBoostQuery.getBoost(), equalTo(queryBuilder.boost())); query = spanBoostQuery.getQuery(); } else { BoostQuery boostQuery = (BoostQuery) query; assertThat(boostQuery.getBoost(), equalTo(queryBuilder.boost())); query = boostQuery.getQuery(); } } } doAssertLuceneQuery(queryBuilder, query, context); }
@Override public String toString(String field) { boolean needsParens = NO_PARENS_REQUIRED_QUERIES.contains(query.getClass()) == false; StringBuilder builder = new StringBuilder(); if (needsParens) { builder.append("("); } builder.append(query.toString(field)); if (needsParens) { builder.append(")"); } builder.append("^"); builder.append(getBoost()); return builder.toString(); }
@Override public boolean equals(Object other) { return sameClassAs(other) && equalsTo(getClass().cast(other)); }
@Override public int hashCode() { int h = classHash(); h = 31 * h + query.hashCode(); h = 31 * h + Float.floatToIntBits(boost); return h; }
return new SpanBoostQuery(spanQuery, boost);
@Override public String toString(String field) { boolean needsParens = NO_PARENS_REQUIRED_QUERIES.contains(query.getClass()) == false; StringBuilder builder = new StringBuilder(); if (needsParens) { builder.append("("); } builder.append(query.toString(field)); if (needsParens) { builder.append(")"); } builder.append("^"); builder.append(getBoost()); return builder.toString(); }
@Override public Query rewrite(IndexReader reader) throws IOException { if (boost == 1f) { return query; } final SpanQuery rewritten = (SpanQuery) query.rewrite(reader); if (query != rewritten) { return new SpanBoostQuery(rewritten, boost); } if (query.getClass() == SpanBoostQuery.class) { SpanBoostQuery in = (SpanBoostQuery) query; return new SpanBoostQuery(in.query, boost * in.boost); } return super.rewrite(reader); }
weight.normalize(1f, getBoost()); return new SpanWeight(this, searcher, terms) {