You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Christopher Condit <co...@sdsc.edu> on 2010/05/01 01:05:37 UTC
RE: Modify TermQueries or Tokens
> 2) if I have to accept whole input string with all logic (AND, OR, ..) inside,
> I think it is easier to change TermQuery afterwards than parsing the string,
> since final result from query parser should be BooleanQuery (in your
> example),
> then we iterate through each BooleanClause, if the clause is still
> BooleanQuery,
> recursively go deep, if TermQuery, we may try to convert to
> WildCardQuery?
protected static void wildcardQuery(Query query) {
if (query instanceof BooleanQuery) {
BooleanQuery bQuery = (BooleanQuery)query;
for (BooleanClause clause: bQuery.getClauses()) {
if (clause.getQuery() instanceof TermQuery) {
Term term = ((TermQuery)clause.getQuery()).getTerm();
clause.setQuery(new WildcardQuery(new Term(term.field(), "*" + term.text() + "*")));
}
else
wildcardQuery(clause.getQuery());
}
}
}
Does the trick (as long as the original query is a BooleanQuery). Thanks for the suggestion!
-Chris
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org
RE: Modify TermQueries or Tokens
Posted by Christopher Condit <co...@sdsc.edu>.
> It looks good to me, but I did not test, when testing, we may print out both
>
> initialQuery.toString() // query produced by QueryParser
> finalQuery.toString() // query after your new function
>
> as comparison, besides testing the query result.
Yes - it's exactly what I wanted:
Test Input: "foo OR baz AND (\"bar trumpet test\" OR (cheese AND nacho~.8))"
Initialquery.toString(): (test1:foo test2:foo) +(test1:baz test2:baz) +((test1:"bar trumpet test" test2:"bar trumpet test") (+(test1:cheese test2:cheese) +(test1:nacho~0.5 test2:nacho~0.5) (test1:8 test2:8)))
Finaqueryl.toString(): (test1:*foo* test2:*foo*) +(test1:*baz* test2:*baz*) +((test1:"bar trumpet test" test2:"bar trumpet test") (+(test1:*cheese* test2:*cheese*) +(test1:nacho~0.5 test2:nacho~0.5) (test1:*8* test2:*8*)))
Thanks again,
-Chris
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org
RE: Modify TermQueries or Tokens
Posted by "Zhang, Lisheng" <Li...@BroadVision.com>.
Hi,
It looks good to me, but I did not test, when testing,
we may print out both
initialQuery.toString() // query produced by QueryParser
finalQuery.toString() // query after your new function
as comparison, besides testing the query result.
Best regards, Lisheng
-----Original Message-----
From: Christopher Condit [mailto:condit@sdsc.edu]
Sent: Friday, April 30, 2010 4:06 PM
To: java-user@lucene.apache.org
Subject: RE: Modify TermQueries or Tokens
> 2) if I have to accept whole input string with all logic (AND, OR, ..) inside,
> I think it is easier to change TermQuery afterwards than parsing the string,
> since final result from query parser should be BooleanQuery (in your
> example),
> then we iterate through each BooleanClause, if the clause is still
> BooleanQuery,
> recursively go deep, if TermQuery, we may try to convert to
> WildCardQuery?
protected static void wildcardQuery(Query query) {
if (query instanceof BooleanQuery) {
BooleanQuery bQuery = (BooleanQuery)query;
for (BooleanClause clause: bQuery.getClauses()) {
if (clause.getQuery() instanceof TermQuery) {
Term term = ((TermQuery)clause.getQuery()).getTerm();
clause.setQuery(new WildcardQuery(new Term(term.field(), "*" + term.text() + "*")));
}
else
wildcardQuery(clause.getQuery());
}
}
}
Does the trick (as long as the original query is a BooleanQuery). Thanks for the suggestion!
-Chris
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org