kelvint 2004/03/06 00:22:05 Modified: contributions/javascript/queryConstructor luceneQueryConstructor.js Log: Added doANDTerms method to construct Google-like queries. Revision Changes Path 1.8 +44 -2 jakarta-lucene-sandbox/contributions/javascript/queryConstructor/luceneQueryConstructor.js Index: luceneQueryConstructor.js =================================================================== RCS file: /home/cvs/jakarta-lucene-sandbox/contributions/javascript/queryConstructor/luceneQueryConstructor.js,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- luceneQueryConstructor.js 23 Jan 2004 16:59:38 -0000 1.7 +++ luceneQueryConstructor.js 6 Mar 2004 08:22:05 -0000 1.8 @@ -1,5 +1,6 @@ // Lucene Search Query Constructor // Author: Kelvin Tan (kelvint at apache.org) +// Version: $Id$ // Change this according to what you use to name the field modifiers in your form. // e.g. with the field "name", the modifier will be called "nameModifier" @@ -29,6 +30,7 @@ // Constructs the query // @param query Form field to represent the constructed query to be submitted // @param debug Turn on debugging? +// @return Submits the form if submitOnConstruction=true, else returns the query param function doMakeQuery( query, dbg ) { if(typeof(dbg) != "undefined") @@ -45,7 +47,7 @@ { var element = formElements[i]; var elementName = element.name; - if(!contains(dict, elementName)) + if(elementName != "" && !contains(dict, elementName)) { dict[dict.length] = elementName; @@ -90,6 +92,32 @@ { frm.submit(); } + else + { + return query; + } +} + +// Constructs a Google-like query (all terms are ANDed) +// @param query Form field to represent the constructed query to be submitted +// @return Submits the form if submitOnConstruction=true, else returns the query param +function doANDTerms(query) +{ + var temp = ''; + splitStr = query.value.split(" "); + query.value = ''; + for(var i=0;i<splitStr.length;i++) + { + if(splitStr[i].length > 0) addModifier(query, AND_MODIFIER, splitStr[i]); + } + if(submitOnConstruction) + { + frm.submit(); + } + else + { + return query; + } } function contains(array, s) @@ -144,6 +172,20 @@ return r.join(VALUE_DELIMITER); } +function addModifier(query, modifier, value) +{ + value = trim(value); + + if(query.value.length == 0) + { + query.value = modifier + '(' + value + ')'; + } + else + { + query.value = query.value + ' ' + modifier + '(' + value + ')'; + } +} + function addFieldWithModifier(query, modifier, field, fieldValue) { fieldValue = trim(fieldValue); @@ -171,4 +213,4 @@ pattern = /^(\s*)([\w\W]*)(\b\s*$)/; if (pattern.test(temp)) { temp = temp.replace(pattern, "$2"); } return temp; // Return the trimmed string back to the user -} \ No newline at end of file +}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]