Java Code Examples for org.apache.lucene.search.PhraseQuery#getTerms()

The following examples show how to use org.apache.lucene.search.PhraseQuery#getTerms() . 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: TestQPHelper.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testPositionIncrement() throws Exception {
  StandardQueryParser qp = new StandardQueryParser();
  qp.setAnalyzer(
      new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET));

  qp.setEnablePositionIncrements(true);

  String qtxt = "\"the words in poisitions pos02578 are stopped in this phrasequery\"";
  // 0 2 5 7 8
  int expectedPositions[] = { 1, 3, 4, 6, 9 };
  PhraseQuery pq = (PhraseQuery) qp.parse(qtxt, "a");
  // System.out.println("Query text: "+qtxt);
  // System.out.println("Result: "+pq);
  Term t[] = pq.getTerms();
  int pos[] = pq.getPositions();
  for (int i = 0; i < t.length; i++) {
    // System.out.println(i+". "+t[i]+"  pos: "+pos[i]);
    assertEquals("term " + i + " = " + t[i] + " has wrong term-position!",
        expectedPositions[i], pos[i]);
  }
}
 
Example 2
Source File: LumongoMultiFieldQueryParser.java    From lumongo with Apache License 2.0 6 votes vote down vote up
private Query applySlop(Query q, int slop) {
	if (q instanceof PhraseQuery) {
		PhraseQuery.Builder builder = new PhraseQuery.Builder();
		builder.setSlop(slop);
		PhraseQuery pq = (PhraseQuery) q;
		org.apache.lucene.index.Term[] terms = pq.getTerms();
		int[] positions = pq.getPositions();
		for (int i = 0; i < terms.length; ++i) {
			builder.add(terms[i], positions[i]);
		}
		q = builder.build();
	}
	else if (q instanceof MultiPhraseQuery) {
		MultiPhraseQuery mpq = (MultiPhraseQuery) q;

		if (slop != mpq.getSlop()) {
			q = new MultiPhraseQuery.Builder(mpq).setSlop(slop).build();
		}
	}
	return q;
}
 
Example 3
Source File: QueryTransformer.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Query handle(Query query, QueryTransformer queryTransformer) {
    PhraseQuery phraseQuery = (PhraseQuery) query;
    SpanNearQuery.Builder builder = new SpanNearQuery.Builder(phraseQuery.getTerms()[0].field(), true);
    int i = 0;
    int position = -1;
    for (Term term : phraseQuery.getTerms()) {
        if (i > 0) {
            int gap = (phraseQuery.getPositions()[i] - position) - 1;
            if (gap > 0) {
                builder.addGap(gap);
            }
        }
        position = phraseQuery.getPositions()[i];
        builder.addClause(new CustomSpanPayloadCheckQuery(new SpanTermQuery(term), Collections.singletonList(null)));
        i++;
    }
    return builder.setSlop(phraseQuery.getSlop()).build();
}
 
Example 4
Source File: QueryParserTestBase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testPositionIncrement() throws Exception {
  CommonQueryParserConfiguration qp = getParserConfig( new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET));
  qp.setEnablePositionIncrements(true);
  String qtxt = "\"the words in poisitions pos02578 are stopped in this phrasequery\"";
  //               0         2                      5           7  8
  int expectedPositions[] = {1,3,4,6,9};
  PhraseQuery pq = (PhraseQuery) getQuery(qtxt,qp);
  //System.out.println("Query text: "+qtxt);
  //System.out.println("Result: "+pq);
  Term t[] = pq.getTerms();
  int pos[] = pq.getPositions();
  for (int i = 0; i < t.length; i++) {
    //System.out.println(i+". "+t[i]+"  pos: "+pos[i]);
    assertEquals("term "+i+" = "+t[i]+" has wrong term-position!",expectedPositions[i],pos[i]);
  }
}
 
Example 5
Source File: MultiFieldQueryParser.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private Query applySlop(Query q, int slop) {
  if (q instanceof PhraseQuery) {
    PhraseQuery.Builder builder = new PhraseQuery.Builder();
    builder.setSlop(slop);
    PhraseQuery pq = (PhraseQuery) q;
    org.apache.lucene.index.Term[] terms = pq.getTerms();
    int[] positions = pq.getPositions();
    for (int i = 0; i < terms.length; ++i) {
      builder.add(terms[i], positions[i]);
    }
    q = builder.build();
  } else if (q instanceof MultiPhraseQuery) {
    MultiPhraseQuery mpq = (MultiPhraseQuery)q;
    
    if (slop != mpq.getSlop()) {
      q = new MultiPhraseQuery.Builder(mpq).setSlop(slop).build();
    }
  }
  return q;
}
 
Example 6
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 7
Source File: FieldQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private String getKey( Query query ){
  if( !fieldMatch ) return null;
  while (query instanceof BoostQuery) {
    query = ((BoostQuery) query).getQuery();
  }
  if( query instanceof TermQuery )
    return ((TermQuery)query).getTerm().field();
  else if ( query instanceof PhraseQuery ){
    PhraseQuery pq = (PhraseQuery)query;
    Term[] terms = pq.getTerms();
    return terms[0].field();
  }
  else if (query instanceof MultiTermQuery) {
    return ((MultiTermQuery)query).getField();
  }
  else
    throw new RuntimeException( "query \"" + query.toString() + "\" must be flatten first." );
}
 
Example 8
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 9
Source File: MapperQueryParser.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private Query applySlop(Query q, int slop) {
    if (q instanceof PhraseQuery) {
        PhraseQuery pq = (PhraseQuery) q;
        PhraseQuery.Builder builder = new PhraseQuery.Builder();
        builder.setSlop(slop);
        final Term[] terms = pq.getTerms();
        final int[] positions = pq.getPositions();
        for (int i = 0; i < terms.length; ++i) {
            builder.add(terms[i], positions[i]);
        }
        pq = builder.build();
        pq.setBoost(q.getBoost());
        return pq;
    } else if (q instanceof MultiPhraseQuery) {
        ((MultiPhraseQuery) q).setSlop(slop);
        return q;
    } else {
        return q;
    }
}
 
Example 10
Source File: TestUnifiedHighlighterStrictPhrases.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private PhraseQuery setSlop(PhraseQuery query, int slop) {
  PhraseQuery.Builder builder = new PhraseQuery.Builder();
  Term[] terms = query.getTerms();
  int[] positions = query.getPositions();
  for (int i = 0; i < terms.length; i++) {
    builder.add(terms[i], positions[i]);
  }
  builder.setSlop(slop);
  return builder.build();
}
 
Example 11
Source File: SlopQueryNodeBuilder.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Query build(QueryNode queryNode) throws QueryNodeException {
  SlopQueryNode phraseSlopNode = (SlopQueryNode) queryNode;

  Query query = (Query) phraseSlopNode.getChild().getTag(
      QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);

  if (query instanceof PhraseQuery) {
    PhraseQuery.Builder builder = new PhraseQuery.Builder();
    builder.setSlop(phraseSlopNode.getValue());
    PhraseQuery pq = (PhraseQuery) query;
    org.apache.lucene.index.Term[] terms = pq.getTerms();
    int[] positions = pq.getPositions();
    for (int i = 0; i < terms.length; ++i) {
      builder.add(terms[i], positions[i]);
    }
    query = builder.build();

  } else {
    MultiPhraseQuery mpq = (MultiPhraseQuery)query;
    
    int slop = phraseSlopNode.getValue();
    
    if (slop != mpq.getSlop()) {
      query = new MultiPhraseQuery.Builder(mpq).setSlop(slop).build();
    }
  }

  return query;

}
 
Example 12
Source File: NGramQuery.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 * @param pq PhraseQuery to break up and convert to NGramQuery
 * Forms a BooleanQuery with each term in the original PhraseQuery OR'd.
 * Note:  Assumes that each term has already been tokenized into a ngram,
 * this method will not re-tokenize terms.
 * @param useMust controls if BooleanClause.Occur SHOULD or MUST is used.
 */
public NGramQuery(PhraseQuery pq, boolean useMust) {
    Term[] terms = pq.getTerms();
    for (int i = 0; i < terms.length; i++) {
        BooleanClause.Occur occur = BooleanClause.Occur.SHOULD;
        if (useMust) {
            occur = BooleanClause.Occur.MUST;
        }
        add(new TermQuery(terms[i]), occur);
    }
}
 
Example 13
Source File: FieldQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void checkOverlap( Collection<Query> expandQueries, PhraseQuery a, float aBoost, PhraseQuery b, float bBoost ){
  if( a.getSlop() != b.getSlop() ) return;
  Term[] ats = a.getTerms();
  Term[] bts = b.getTerms();
  if( fieldMatch && !ats[0].field().equals( bts[0].field() ) ) return;
  checkOverlap( expandQueries, ats, bts, a.getSlop(), aBoost);
  checkOverlap( expandQueries, bts, ats, b.getSlop(), bBoost );
}
 
Example 14
Source File: SolrQueryParserBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Base implementation delegates to {@link #getFieldQuery(String,String,boolean,boolean)}.
 * This method may be overridden, for example, to return
 * a SpanNearQuery instead of a PhraseQuery.
 *
 */
protected Query getFieldQuery(String field, String queryText, int slop)
      throws SyntaxError {
  Query query = getFieldQuery(field, queryText, true, false);

  // only set slop of the phrase query was a result of this parser
  // and not a sub-parser.
  if (subQParser == null) {
    if (query instanceof PhraseQuery) {
      PhraseQuery pq = (PhraseQuery) query;
      Term[] terms = pq.getTerms();
      int[] positions = pq.getPositions();
      PhraseQuery.Builder builder = new PhraseQuery.Builder();
      for (int i = 0; i < terms.length; ++i) {
        builder.add(terms[i], positions[i]);
      }
      builder.setSlop(slop);
      query = builder.build();
    } else if (query instanceof MultiPhraseQuery) {
      MultiPhraseQuery mpq = (MultiPhraseQuery)query;

      if (slop != mpq.getSlop()) {
        query = new MultiPhraseQuery.Builder(mpq).setSlop(slop).build();
      }
    }
  }

  return query;
}
 
Example 15
Source File: LuceneQueryTranslator.java    From imhotep with Apache License 2.0 5 votes vote down vote up
public static Query rewrite(PhraseQuery pq, Set<String> intFields) {
    final List<Query> termQueries = Lists.newArrayListWithCapacity(pq.getTerms().length);
    for (final org.apache.lucene.index.Term term : pq.getTerms()) {
        termQueries.add(Query.newTermQuery(rewriteTerm(term, intFields)));
    }
    return Query.newBooleanQuery(BooleanOp.AND, termQueries);
}
 
Example 16
Source File: PhraseQueryExtractor.java    From solr-redis with Apache License 2.0 5 votes vote down vote up
@Override
public void extractSubQueriesFields(final PhraseQuery q,
        final Iterable<QueryExtractor<? extends Query>> extractors,
        final Set<String> extractedFields) throws UnsupportedOperationException {
  for (final Term term : q.getTerms()) {
    extractedFields.add(term.field());
  }
}
 
Example 17
Source File: NGramQuery.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 * @param pq PhraseQuery to break up and convert to NGramQuery
 * Forms a BooleanQuery with each term in the original PhraseQuery OR'd.
 * Note:  Assumes that each term has already been tokenized into a ngram,
 * this method will not re-tokenize terms.
 * @param useMust controls if BooleanClause.Occur SHOULD or MUST is used.
 */
public NGramQuery(PhraseQuery pq, boolean useMust) {
    Term[] terms = pq.getTerms();
    for (int i = 0; i < terms.length; i++) {
        BooleanClause.Occur occur = BooleanClause.Occur.SHOULD;
        if (useMust) {
            occur = BooleanClause.Occur.MUST;
        }
        add(new TermQuery(terms[i]), occur);
    }
}
 
Example 18
Source File: ExtendedDismaxQParser.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private Query getQuery() {
  try {
    
    switch (type) {
      case FIELD:  // fallthrough
      case PHRASE:
        Query query;
        if (val == null) {
          query = super.getFieldQuery(field, vals, false);
        } else {
          query = super.getFieldQuery(field, val, type == QType.PHRASE, false);
        }
        // Boolean query on a whitespace-separated string
        // If these were synonyms we would have a SynonymQuery
        if (query instanceof BooleanQuery) {
          if (type == QType.FIELD) { // Don't set mm for boolean query containing phrase queries
            BooleanQuery bq = (BooleanQuery) query;
            query = SolrPluginUtils.setMinShouldMatch(bq, minShouldMatch, false);
          }
        } else if (query instanceof PhraseQuery) {
          PhraseQuery pq = (PhraseQuery)query;
          if (minClauseSize > 1 && pq.getTerms().length < minClauseSize) return null;
          PhraseQuery.Builder builder = new PhraseQuery.Builder();
          Term[] terms = pq.getTerms();
          int[] positions = pq.getPositions();
          for (int i = 0; i < terms.length; ++i) {
            builder.add(terms[i], positions[i]);
          }
          builder.setSlop(slop);
          query = builder.build();
        } else if (query instanceof MultiPhraseQuery) {
          MultiPhraseQuery mpq = (MultiPhraseQuery)query;
          if (minClauseSize > 1 && mpq.getTermArrays().length < minClauseSize) return null;
          if (slop != mpq.getSlop()) {
            query = new MultiPhraseQuery.Builder(mpq).setSlop(slop).build();
          }
        } else if (query instanceof SpanQuery) {
          return query;
        } else if (minClauseSize > 1) {
          // if it's not a type of phrase query, it doesn't meet the minClauseSize requirements
          return null;
        }
        return query;
      case PREFIX: return super.getPrefixQuery(field, val);
      case WILDCARD: return super.getWildcardQuery(field, val);
      case FUZZY: return super.getFuzzyQuery(field, val, flt);
      case RANGE: return super.getRangeQuery(field, val, val2, bool, bool2);
    }
    return null;
    
  } catch (Exception e) {
    // an exception here is due to the field query not being compatible with the input text
    // for example, passing a string to a numeric field.
    return null;
  }
}