Java Code Examples for org.apache.lucene.search.PrefixQuery#setRewriteMethod()

The following examples show how to use org.apache.lucene.search.PrefixQuery#setRewriteMethod() . 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: HighlighterTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testGetBestFragmentsMultiTerm() throws Exception {
  TestHighlightRunner helper = new TestHighlightRunner() {

    @Override
    public void run() throws Exception {
      numHighlights = 0;
      BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
      booleanQuery.add(new TermQuery(new Term(FIELD_NAME, "john")), Occur.SHOULD);
      PrefixQuery prefixQuery = new PrefixQuery(new Term(FIELD_NAME, "kenn"));
      prefixQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
      booleanQuery.add(prefixQuery, Occur.SHOULD);

      doSearching(booleanQuery.build());
      doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
      assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
          numHighlights == 5);
    }
  };

  helper.start();
}
 
Example 2
Source File: PrefixWildcardQueryNodeBuilder.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public PrefixQuery build(QueryNode queryNode) throws QueryNodeException {    

  PrefixWildcardQueryNode wildcardNode = (PrefixWildcardQueryNode) queryNode;

  String text = wildcardNode.getText().subSequence(0, wildcardNode.getText().length() - 1).toString();
  PrefixQuery q = new PrefixQuery(new Term(wildcardNode.getFieldAsString(), text));
  
  MultiTermQuery.RewriteMethod method = (MultiTermQuery.RewriteMethod)queryNode.getTag(MultiTermRewriteMethodProcessor.TAG_ID);
  if (method != null) {
    q.setRewriteMethod(method);
  }
  
  return q;
}
 
Example 3
Source File: MyCrazyCustomField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Query getPrefixQuery(QParser parser, SchemaField sf, String termStr) {
  if(termStr.equals("foo")) {
    termStr = "bar";
  } else if (termStr.equals("bar")) {
    termStr = "foo";
  }

  PrefixQuery query = new PrefixQuery(new Term(sf.getName(), termStr));
  query.setRewriteMethod(sf.getType().getRewriteMethod(parser, sf));
  return query;
}
 
Example 4
Source File: StringFieldType.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, QueryShardContext context) {
    failIfNotIndexed();
    PrefixQuery query = new PrefixQuery(new Term(name(), indexedValueForSearch(value)));
    if (method != null) {
        query.setRewriteMethod(method);
    }
    return query;
}
 
Example 5
Source File: Queries.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Query createPrefixQuery(final @NonNull String name, final @NonNull String value) {
    final PrefixQuery pq = new PrefixQuery(new Term(name, value));
    pq.setRewriteMethod(PrefixQuery.CONSTANT_SCORE_FILTER_REWRITE);
    return pq;
}
 
Example 6
Source File: FieldType.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a Query instance for doing prefix searches on this field type.
 * Also, other QueryParser implementations may have different semantics.
 * <p>
 * Sub-classes should override this method to provide their own range query implementation.
 *
 * @param parser       the {@link org.apache.solr.search.QParser} calling the method
 * @param sf           the schema field
 * @param termStr      the term string for prefix query, if blank then this query should match all docs with this field
 * @return a Query instance to perform prefix search
 */
public Query getPrefixQuery(QParser parser, SchemaField sf, String termStr) {
  if ("".equals(termStr)) {
    return getExistenceQuery(parser, sf);
  }
  PrefixQuery query = new PrefixQuery(new Term(sf.getName(), termStr));
  query.setRewriteMethod(sf.getType().getRewriteMethod(parser, sf));
  return query;
}