Java Code Examples for org.apache.lucene.search.BoostQuery#getQuery()

The following examples show how to use org.apache.lucene.search.BoostQuery#getQuery() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FieldQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public 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 );
      }
    }
  }
}
 
Example 2
Source File: FieldQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
void add( Query query, IndexReader reader ) {
  float boost = 1f;
  while (query instanceof BoostQuery) {
    BoostQuery bq = (BoostQuery) query;
    query = bq.getQuery();
    boost = bq.getBoost();
  }
  if( query instanceof TermQuery ){
    addTerm( ((TermQuery)query).getTerm(), boost );
  }
  else if( query instanceof PhraseQuery ){
    PhraseQuery pq = (PhraseQuery)query;
    Term[] terms = pq.getTerms();
    Map<String, QueryPhraseMap> map = subMap;
    QueryPhraseMap qpm = null;
    for( Term term : terms ){
      qpm = getOrNewMap( map, term.text() );
      map = qpm.subMap;
    }
    qpm.markTerminal( pq.getSlop(), boost );
  }
  else
    throw new RuntimeException( "query \"" + query.toString() + "\" must be flatten first." );
}
 
Example 3
Source File: MoreLikeThisHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private Query getBoostedQuery(Query mltquery) {
  BooleanQuery boostedQuery = (BooleanQuery)mltquery;
  if (boostFields.size() > 0) {
    BooleanQuery.Builder newQ = new BooleanQuery.Builder();
    newQ.setMinimumNumberShouldMatch(boostedQuery.getMinimumNumberShouldMatch());
    for (BooleanClause clause : boostedQuery) {
      Query q = clause.getQuery();
      float originalBoost = 1f;
      if (q instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) q;
        q = bq.getQuery();
        originalBoost = bq.getBoost();
      }
      Float fieldBoost = boostFields.get(((TermQuery) q).getTerm().field());
      q = ((fieldBoost != null) ? new BoostQuery(q, fieldBoost * originalBoost) : clause.getQuery());
      newQ.add(q, clause.getOccur());
    }
    boostedQuery = newQ.build();
  }
  return boostedQuery;
}
 
Example 4
Source File: MoreLikeThisHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void fillInterestingTermsFromMLTQuery( Query query, List<InterestingTerm> terms )
{ 
  Collection<BooleanClause> clauses = ((BooleanQuery)query).clauses();
  for( BooleanClause o : clauses ) {
    Query q = o.getQuery();
    float boost = 1f;
    if (q instanceof BoostQuery) {
      BoostQuery bq = (BoostQuery) q;
      q = bq.getQuery();
      boost = bq.getBoost();
    }
    InterestingTerm it = new InterestingTerm();
    it.boost = boost;
    it.term = ((TermQuery) q).getTerm();
    terms.add( it );
  } 
  // alternatively we could use
  // mltquery.extractTerms( terms );
}
 
Example 5
Source File: SolrPluginUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private static void flattenBooleanQuery(BooleanQuery.Builder to, BooleanQuery from, float fromBoost) {

    for (BooleanClause clause : from.clauses()) {

      Query cq = clause.getQuery();
      float boost = fromBoost;
      while (cq instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) cq;
        cq = bq.getQuery();
        boost *= bq.getBoost();
      }

      if (cq instanceof BooleanQuery
          && !clause.isRequired()
          && !clause.isProhibited()) {

        /* we can recurse */
        flattenBooleanQuery(to, (BooleanQuery)cq, boost);

      } else {
        to.add(clause);
      }
    }
  }
 
Example 6
Source File: HighlightCustomQueryTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected void extractUnknownQuery(Query query,
    Map<String, WeightedSpanTerm> terms) throws IOException {
  float boost = 1f;
  while (query instanceof BoostQuery) {
    BoostQuery bq = (BoostQuery) query;
    boost *= bq.getBoost();
    query = bq.getQuery();
  }
  if (query instanceof CustomQuery) {
    extractWeightedTerms(terms, new TermQuery(((CustomQuery) query).term), boost);
  }
}
 
Example 7
Source File: TestMoreLikeThis.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testBoostFactor() throws Throwable {
  Map<String,Float> originalValues = getOriginalValues();
  mlt.setFieldNames(new String[] {"text"});
  mlt.setBoost(true);
  
  // this mean that every term boost factor will be multiplied by this
  // number
  float boostFactor = 5;
  mlt.setBoostFactor(boostFactor);
  
  BooleanQuery query = (BooleanQuery) mlt.like("text", new StringReader(
      "lucene release"));
  Collection<BooleanClause> clauses = query.clauses();
  
  assertEquals("Expected " + originalValues.size() + " clauses.",
      originalValues.size(), clauses.size());

  for (BooleanClause clause : clauses) {
    BoostQuery bq = (BoostQuery) clause.getQuery();
    TermQuery tq = (TermQuery) bq.getQuery();
    Float termBoost = originalValues.get(tq.getTerm().text());
    assertNotNull("Expected term " + tq.getTerm().text(), termBoost);

    float totalBoost = termBoost * boostFactor;
    assertEquals("Expected boost of " + totalBoost + " for term '"
        + tq.getTerm().text() + "' got " + bq.getBoost(), totalBoost, bq
        .getBoost(), 0.0001);
  }
}
 
Example 8
Source File: TestMoreLikeThis.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private Map<String,Float> getOriginalValues() throws IOException {
  Map<String,Float> originalValues = new HashMap<>();
  mlt.setFieldNames(new String[] {"text"});
  mlt.setBoost(true);
  BooleanQuery query = (BooleanQuery) mlt.like("text", new StringReader(
      "lucene release"));
  Collection<BooleanClause> clauses = query.clauses();

  for (BooleanClause clause : clauses) {
    BoostQuery bq = (BoostQuery) clause.getQuery();
    TermQuery tq = (TermQuery) bq.getQuery();
    originalValues.put(tq.getTerm().text(), bq.getBoost());
  }
  return originalValues;
}
 
Example 9
Source File: TestExtendedDismaxParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private boolean containsClause(Query query, String field, String value,
                               int boost, boolean fuzzy) {

  float queryBoost = 1f;
  if (query instanceof BoostQuery) {
    BoostQuery bq = (BoostQuery) query;
    query = bq.getQuery();
    queryBoost = bq.getBoost();
  }

  if(query instanceof BooleanQuery) {
    return containsClause((BooleanQuery)query, field, value, boost, fuzzy);
  }
  if(query instanceof DisjunctionMaxQuery) {
    return containsClause((DisjunctionMaxQuery)query, field, value, boost, fuzzy);
  }
  if (boost != queryBoost) {
    return false;
  }
  if(query instanceof TermQuery && !fuzzy) {
    return containsClause((TermQuery)query, field, value);
  }
  if(query instanceof FuzzyQuery && fuzzy) {
    return containsClause((FuzzyQuery)query, field, value);
  }
  return false;
}
 
Example 10
Source File: QueryTransformer.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Query handle(Query query, QueryTransformer queryTransformer) {
    BoostQuery boostQuery = (BoostQuery) query;
    Query newInnerBoostQuery = queryTransformer.transform(boostQuery.getQuery());
    if (newInnerBoostQuery != boostQuery.getQuery()) {
        return new BoostQuery(newInnerBoostQuery, boostQuery.getBoost());
    }
    return query;
}