下面列出了org.apache.lucene.search.Query#toString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
}
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." );
}
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." );
}
}
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." );
}
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());
}
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);
}
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
+ "/");
}
}
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 + "/");
}
}
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
+ "/");
}
}
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
+ "/");
}
}
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
+ "/");
}
}
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 + "/");
}
}
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
+ "/");
}
}
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 + "/");
}
}
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
+ "/");
}
}
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 + "/");
}
}
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 + "/");
}
}
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();
}
@Override
protected String getDescriptionFromElement(Query query) {
return query.toString();
}
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));
}