while (query instanceof BoostQuery) { BoostQuery bq = (BoostQuery) query; boost *= bq.getBoost(); query = bq.getQuery(); while (query instanceof BoostQuery) { BoostQuery bq = (BoostQuery) query; boost *= bq.getBoost(); query = bq.getQuery(); BoostQuery boostQuery = (BoostQuery) must; must = boostQuery.getQuery(); boost = boostQuery.getBoost();
private static void planBoostQuery(final StringBuilder builder, final BoostQuery query) { toPlan(builder, query.getQuery()); builder.append(",boost=" + query.getBoost() + ")"); }
BoostQuery bq = (BoostQuery) query; query = bq.getQuery(); boost *= bq.getBoost();
BoostQuery boostQuery = (BoostQuery) subQuery; subQuery = boostQuery.getQuery(); boost *= boostQuery.getBoost(); } else { break;
while(query instanceof BoostQuery) { BoostQuery boostQuery = (BoostQuery) query; boosts.add(boostQuery.getBoost()); query = boostQuery.getQuery();
@Override public void normalize(float norm, float boost) { weight.normalize(norm, BoostQuery.this.getBoost() * boost); }
@Override public void normalize(float norm, float boost) { weight.normalize(norm, BoostQuery.this.getBoost() * boost); }
BoostQuery bq = (BoostQuery) sourceQuery; sourceQuery = bq.getQuery(); boost *= bq.getBoost(); flatten(sourceQuery, reader, flatQueries, boost); } else if (sourceQuery instanceof SpanTermQuery) {
@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 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 { final Query rewritten = query.rewrite(reader); if (getBoost() == 1f) { return rewritten; } if (rewritten.getClass() == BoostQuery.class) { BoostQuery in = (BoostQuery) rewritten; return new BoostQuery(in.query, getBoost() * in.getBoost()); } if (getBoost() == 0f && rewritten.getClass() != ConstantScoreQuery.class) { // so that we pass needScores=false return new BoostQuery(new ConstantScoreQuery(rewritten), 0f); } if (query != rewritten) { return new BoostQuery(rewritten, getBoost()); } return this; }
@Override public Query rewrite(IndexReader reader) throws IOException { final Query rewritten = query.rewrite(reader); if (getBoost() == 1f) { return rewritten; } if (rewritten.getClass() == BoostQuery.class) { BoostQuery in = (BoostQuery) rewritten; return new BoostQuery(in.query, getBoost() * in.getBoost()); } if (getBoost() == 0f && rewritten.getClass() != ConstantScoreQuery.class) { // so that we pass needScores=false return new BoostQuery(new ConstantScoreQuery(rewritten), 0f); } if (query != rewritten) { return new BoostQuery(rewritten, getBoost()); } return this; }
while (innerQuery instanceof BoostQuery) { BoostQuery bq = (BoostQuery) innerQuery; boost *= bq.getBoost(); innerQuery = bq.getQuery();
public Collection<Query> decomposeBoostQuery(BoostQuery q) { if (q.getBoost() == 1.0) return decompose(q.getQuery()); List<Query> boostedDecomposedQueries = new ArrayList<>(); for (Query subq : decompose(q.getQuery())) { boostedDecomposedQueries.add(new BoostQuery(subq, q.getBoost())); } return boostedDecomposedQueries; }
protected void flattenQuery(BoostQuery query, float pathBoost, Object sourceOverride, IndexReader reader, Callback callback) { flatten(query.getQuery(), query.getBoost() * pathBoost, sourceOverride, reader, callback); }
protected static void assertTermOrBoostQuery(Query query, String field, String value, float fieldBoost) { if (fieldBoost != AbstractQueryBuilder.DEFAULT_BOOST) { assertThat(query, instanceOf(BoostQuery.class)); BoostQuery boostQuery = (BoostQuery) query; assertThat(boostQuery.getBoost(), equalTo(fieldBoost)); query = boostQuery.getQuery(); } assertTermQuery(query, field, value); }
@Override protected Query doToQuery(QueryShardContext context) throws IOException { Query subQuery = multiTermQueryBuilder.toQuery(context); float boost = AbstractQueryBuilder.DEFAULT_BOOST; if (subQuery instanceof BoostQuery) { BoostQuery boostQuery = (BoostQuery) subQuery; subQuery = boostQuery.getQuery(); boost = boostQuery.getBoost(); } //no MultiTermQuery extends SpanQuery, so SpanBoostQuery is not supported here assert subQuery instanceof SpanBoostQuery == false; if (subQuery instanceof MultiTermQuery == false) { throw new UnsupportedOperationException("unsupported inner query, should be " + MultiTermQuery.class.getName() +" but was " + subQuery.getClass().getName()); } SpanQuery wrapper = new SpanMultiTermQueryWrapper<>((MultiTermQuery) subQuery); if (boost != AbstractQueryBuilder.DEFAULT_BOOST) { wrapper = new SpanBoostQuery(wrapper, boost); } return wrapper; }
@Override protected Query doToQuery(QueryShardContext context) throws IOException { Query subQuery = multiTermQueryBuilder.toQuery(context); float boost = AbstractQueryBuilder.DEFAULT_BOOST; if (subQuery instanceof BoostQuery) { BoostQuery boostQuery = (BoostQuery) subQuery; subQuery = boostQuery.getQuery(); boost = boostQuery.getBoost(); } //no MultiTermQuery extends SpanQuery, so SpanBoostQuery is not supported here assert subQuery instanceof SpanBoostQuery == false; if (subQuery instanceof MultiTermQuery == false) { throw new UnsupportedOperationException("unsupported inner query, should be " + MultiTermQuery.class.getName() +" but was " + subQuery.getClass().getName()); } SpanQuery wrapper = new SpanMultiTermQueryWrapper<>((MultiTermQuery) subQuery); if (boost != AbstractQueryBuilder.DEFAULT_BOOST) { wrapper = new SpanBoostQuery(wrapper, boost); } return wrapper; }
FieldQuery( Query query, IndexReader reader, boolean phraseHighlight, boolean fieldMatch ) throws IOException { this.fieldMatch = fieldMatch; Set<Query> flatQueries = new LinkedHashSet<>(); flatten( query, reader, flatQueries, 1f ); saveTerms( flatQueries, reader ); Collection<Query> expandQueries = expand( flatQueries ); for( Query flatQuery : expandQueries ){ QueryPhraseMap rootMap = getRootMap( flatQuery ); rootMap.add( flatQuery, reader ); float boost = 1f; while (flatQuery instanceof BoostQuery) { BoostQuery bq = (BoostQuery) flatQuery; flatQuery = bq.getQuery(); boost *= bq.getBoost(); } if( !phraseHighlight && flatQuery instanceof PhraseQuery ){ PhraseQuery pq = (PhraseQuery)flatQuery; if( pq.getTerms().length > 1 ){ for( Term term : pq.getTerms() ) rootMap.addTerm( term, boost ); } } } }
/** Fixes a negative query by adding a MatchAllDocs query clause. * The query passed in *must* be a negative query. */ public static Query fixNegativeQuery(Query q) { float boost = 1f; if (q instanceof BoostQuery) { BoostQuery bq = (BoostQuery) q; boost = bq.getBoost(); q = bq.getQuery(); } BooleanQuery bq = (BooleanQuery) q; BooleanQuery.Builder newBqB = new BooleanQuery.Builder(); newBqB.setMinimumNumberShouldMatch(bq.getMinimumNumberShouldMatch()); for (BooleanClause clause : bq) { newBqB.add(clause); } newBqB.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST); BooleanQuery newBq = newBqB.build(); return new BoostQuery(newBq, boost); }