How can I verify that a Lucene query embedded in a larger XQuery does not contain a syntax error before launching the complete XQuery I want to run? -
i have application need allow user perform full text search on documents, , use lucene query parser syntax if desired. exist database queried django backend uses eulexistdb talk exist.
the problem when user uses incorrect syntax full text search, discovered late in game. django application has query sql database determine of parameters of search. time complete xquery built , exist accessed, sql query has run, means cost of sql query has been spent. (i know marshal data queried on sql side exist exist queried. it's not option now.)
i'd know ahead of time whether lucene query has syntactical error can avoid starting querying sql database nothing.
i've checked documentation of exist, i've not found in api simple function checks whether full-text query syntactically valid or not.
here simple function return true
if lucene query fine, or false
if there syntax error in query. db
must instance of eulexistdb.db.existdb
, query
lucene query:
def check(db, query): try: db.query(safe_interpolate("ft:query(<doc/>, {lucene_query})", lucene_query=query)) except existdbexception ex: if ex.message().startswith( "exerr:error syntax error in lucene query string"): return false raise ex # don't swallow other problems may occur. return true
this should adaptable language there library provides access exist. idea run query of interest against bogus document (<doc/>
). using bogus document avoids having search database. (an empty node sequence might seem better, we're not running ft:query
against empty node sequence because xquery optimizer skip trying parse , run lucene query since valid query on empty sequence return empty sequence, irrespective of actual lucene query.) not matter whether returns results or not. if query has no errors, there won't exception. if query has syntax error, exception raised. i've not found more robust way checking error message stored exception detect whether lucene syntax error or else.
(the safe_interpolate
function function should interpolate lucene_query
avoid injections. decide need in application.)