Java Code Examples for org.apache.lucene.search.Query#toString()

The following examples show how to use org.apache.lucene.search.Query#toString() . 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
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 2
Source File: FieldQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
void saveTerms( Collection<Query> flatQueries, IndexReader reader ) throws IOException{
  for( Query query : flatQueries ){
    while (query instanceof BoostQuery) {
      query = ((BoostQuery) query).getQuery();
    }
    Set<String> termSet = getTermSet( query );
    if( query instanceof TermQuery )
      termSet.add( ((TermQuery)query).getTerm().text() );
    else if( query instanceof PhraseQuery ){
      for( Term term : ((PhraseQuery)query).getTerms() )
        termSet.add( term.text() );
    }
    else if (query instanceof MultiTermQuery && reader != null) {
      BooleanQuery mtqTerms = (BooleanQuery) query.rewrite(reader);
      for (BooleanClause clause : mtqTerms) {
        termSet.add (((TermQuery) clause.getQuery()).getTerm().text());
      }
    }
    else
      throw new RuntimeException( "query \"" + query.toString() + "\" must be flatten first." );
  }
}
 
Example 3
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 4
Source File: IndexManager.java    From spacewalk with GNU General Public License v2.0 6 votes vote down vote up
private void debugExplainResults(String indexName, Hits hits, IndexSearcher searcher,
        Query q, Set<Term> queryTerms)
    throws IOException {
    log.debug("Parsed Query is " + q.toString());
    log.debug("Looking at index:  " + indexName);
    for (int i = 0; i < hits.length(); i++) {
        if ((i < 10)) {
            Document doc = hits.doc(i);
            Float score = hits.score(i);
            Explanation ex = searcher.explain(q, hits.id(i));
            log.debug("Looking at hit<" + i + ", " + hits.id(i) + ", " + score +
                    ">: " + doc);
            log.debug("Explanation: " + ex);
            MatchingField match = new MatchingField(q.toString(), doc, queryTerms);
            String fieldName = match.getFieldName();
            String fieldValue = match.getFieldValue();
            log.debug("Guessing that matched fieldName is " + fieldName + " = " +
                    fieldValue);
        }
    }
}
 
Example 5
Source File: TestPrecedenceQueryParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void assertQueryEqualsDOA(String query, Analyzer a, String result)
    throws Exception {
  Query q = getQueryDOA(query, a);
  String s = q.toString("field");
  if (!s.equals(result)) {
    fail("Query /" + query + "/ yielded /" + s + "/, expecting /" + result
        + "/");
  }
}
 
Example 6
Source File: IndexManager.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
public String parseQuery(String table, org.apache.blur.thrift.generated.Query simpleQuery) throws ParseException,
    BlurException {
  TableContext context = getTableContext(table);
  FieldManager fieldManager = context.getFieldManager();
  Filter preFilter = QueryParserUtil.parseFilter(table, simpleQuery.recordFilter, false, fieldManager, _filterCache,
      context);
  Filter postFilter = QueryParserUtil.parseFilter(table, simpleQuery.rowFilter, true, fieldManager, _filterCache,
      context);
  Query userQuery = QueryParserUtil.parseQuery(simpleQuery.query, simpleQuery.rowQuery, fieldManager, postFilter,
      preFilter, getScoreType(simpleQuery.scoreType), context);
  return userQuery.toString();
}
 
Example 7
Source File: QueryParserTestBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void assertQueryEquals(CommonQueryParserConfiguration cqpC, String field, String query, String result) 
  throws Exception {
  Query q = getQuery(query, cqpC);
  String s = q.toString(field);
  if (!s.equals(result)) {
    fail("Query /" + query + "/ yielded /" + s
         + "/, expecting /" + result + "/");
  }
}
 
Example 8
Source File: QueryParserTestBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void assertQueryEquals(String query, Analyzer a, String result) throws Exception {
  Query q = getQuery(query, a);
  String s = q.toString("field");
  if (!s.equals(result)) {
    fail("Query /" + query + "/ yielded /" + s
         + "/, expecting /" + result + "/");
  }
}
 
Example 9
Source File: TestQPHelper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void assertQueryEqualsDOA(String query, Analyzer a, String result)
    throws Exception {
  Query q = getQueryDOA(query, a);
  String s = q.toString("field");
  if (!s.equals(result)) {
    fail("Query /" + query + "/ yielded /" + s + "/, expecting /" + result
        + "/");
  }
}
 
Example 10
Source File: TestQPHelper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void assertWildcardQueryEquals(String query,
    String result, boolean allowLeadingWildcard) throws Exception {
  StandardQueryParser qp = getParser(null);
  qp.setAllowLeadingWildcard(allowLeadingWildcard);
  Query q = qp.parse(query, "field");
  String s = q.toString("field");
  if (!s.equals(result)) {
    fail("WildcardQuery /" + query + "/ yielded /" + s + "/, expecting /"
        + result + "/");
  }
}
 
Example 11
Source File: TestQPHelper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void assertQueryEquals(StandardQueryParser qp, String field,
    String query, String result) throws Exception {
  Query q = qp.parse(query, field);
  String s = q.toString(field);
  if (!s.equals(result)) {
    fail("Query /" + query + "/ yielded /" + s + "/, expecting /" + result
        + "/");
  }
}
 
Example 12
Source File: QueryParserTestBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void assertQueryEqualsDOA(String query, Analyzer a, String result)
  throws Exception {
  Query q = getQueryDOA(query, a);
  String s = q.toString("field");
  if (!s.equals(result)) {
    fail("Query /" + query + "/ yielded /" + s
         + "/, expecting /" + result + "/");
  }
}
 
Example 13
Source File: TestQPHelper.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void assertQueryEquals(String query, Analyzer a, String result)
    throws Exception {
  Query q = getQuery(query, a);
  String s = q.toString("field");
  if (!s.equals(result)) {
    fail("Query /" + query + "/ yielded /" + s + "/, expecting /" + result
        + "/");
  }
}
 
Example 14
Source File: TestPrecedenceQueryParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void assertQueryEquals(PrecedenceQueryParser qp, String field, String query,
    String result) throws Exception {
  Query q = qp.parse(query, field);
  String s = q.toString(field);
  if (!s.equals(result)) {
    fail("Query /" + query + "/ yielded /" + s + "/, expecting /" + result
        + "/");
  }
}
 
Example 15
Source File: TestPrecedenceQueryParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void assertWildcardQueryEquals(String query, String result) throws Exception {
  PrecedenceQueryParser qp = getParser(null);
  Query q = qp.parse(query, "field");
  String s = q.toString("field");
  if (!s.equals(result)) {
    fail("WildcardQuery /" + query + "/ yielded /" + s + "/, expecting /"
        + result + "/");
  }
}
 
Example 16
Source File: TestPrecedenceQueryParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void assertQueryEquals(String query, Analyzer a, String result)
    throws Exception {
  Query q = getQuery(query, a);
  String s = q.toString("field");
  if (!s.equals(result)) {
    fail("Query /" + query + "/ yielded /" + s + "/, expecting /" + result
        + "/");
  }
}
 
Example 17
Source File: SpanNearClauseFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 18
Source File: AbstractAlfrescoSolrIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void waitForDocCount(Query query, long expectedNumFound, long waitMillis)
        throws Exception
{
    Date date = new Date();
    long timeout = date.getTime() + waitMillis;

    RefCounted<SolrIndexSearcher> ref = null;
    int totalHits = 0;
    while(new Date().getTime() < timeout)
    {
        try
        {
            ref = getCore().getSearcher();
            SolrIndexSearcher searcher = ref.get();
            TopDocs topDocs = searcher.search(query, 10);
            totalHits = topDocs.totalHits;
            if (topDocs.totalHits == expectedNumFound)
            {
                LOG.warn("Query \"" + query + "\" returned " + totalHits + " as expected");
                return;
            }
            else
            {
                LOG.warn("Query \"" + query + "\" returned " + totalHits + ", expected " + expectedNumFound);
                Thread.sleep(2000);
            }
        }
        finally
        {
            ref.decref();
        }
    }
    throw new Exception("Wait error expected "+expectedNumFound+" found "+totalHits+" : "+query.toString());
}
 
Example 19
Source File: InternalQueryProfileTree.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
protected String getDescriptionFromElement(Query query) {
    return query.toString();
}
 
Example 20
Source File: CharLenQPPredictor.java    From lucene4ir with Apache License 2.0 3 votes vote down vote up
public double scoreQuery(String qno, Query q) {

        String qstr = q.toString();
        String[] terms = q.toString().split(" ");

        int w = terms.length;
        int c = qstr.length();

        return (double) (c - (w - 1));
    }