lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Hostetter <>
Subject RE: Building queries
Date Wed, 17 May 2006 18:34:51 GMT

: This kinda leads me back to my "best practises" question: If Im building
: up the query dynamically based off of several fields (rather than the
: user entering a Lucene Query), Im kind of wondering If I should be using
: query parser at all...?! I keep feeling that maybe I should just be
: building up the Query programatically (although dynamically) based off
: the fields.
: But all the examples I can find seem to transform already split UI
: fields back in to a single lucene query string and then put them through
: a parser.
: However, if I don't use the QueryParser, I guess I've got to manually
: run each field through the appropriate analyser as Im building up the
: query terms.

If you are parsing raw text from the user, and you want that text to
contain instructions (for things like required/prohibited clauses, range
queries, boosts and what not) then you might as well use the QueryParser.

if all of the forms are for very specific purposes (ie: one text box for
required text, one for prohibited text, pulldowns for rnages, etc) then i
wouldn't use the QUeryParser at all -- you've got it right with calling
with using the Analyzer directly.

What you really *DON'T* want to do because it's wastefull, silly, and easy
to get wrong so your users can break it is stuff like this...

       String mfgName = getStringFormInput("mfgName")
       int maxPrice = getIntFormInput("price");
       String query = getStringFormInput("q");
       Query q = parser.parse("+(" + query + ") +mfgName:("+mfgName+") "
                              + "+maxPrice:[0 TO "+ maxPrice"]");

that is certainly *NOT* a best practice.

The middle ground where things become muddy is when you wnat the user to
be able to specify *some* instrucutions (like require/prohibited clauses
and quoted phrases) but you don't want then specifying boosts, or field
names -- in that case, I think it makes a lot of sense to "partially
escape" their input to eliminate just hte special characters you don't
want to affect them (like "^" and ":") before passing itto the QueryParser
is a perfectly valid approach -- then you can add the resulting Query to a
bigger BooleanQuery along with the other Query objects you've built up
from the form...

       Query main = parser.parse(myescape(getStringFormInput("q"))
       Query mfg = parser.parse(myescape(getStringFormInput("mfgName"))
       Query price = buildPriceQuery(getIntFormInput("price"));
       BooleanQuery q = new BooleanQuery();
       q.add(main, ...);
       q.add(mfg, ...);
       q.add(price, ...);


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message