2002-05-08 22:18:00 -04:00
|
|
|
// Lucene Search Query Constructor
|
2003-05-08 02:00:02 -04:00
|
|
|
// Author: Kelvin Tan (kelvint at apache.org)
|
2004-03-06 03:22:05 -05:00
|
|
|
// Version: $Id$
|
2002-05-08 22:18:00 -04:00
|
|
|
|
|
|
|
// Change this according to what you use to name the field modifiers in your form.
|
2004-05-17 09:04:42 -04:00
|
|
|
// e.g. with the search field "name", the form element of its modifier
|
|
|
|
// will be "name<modifierSuffix>"
|
2002-05-08 22:18:00 -04:00
|
|
|
var modifierSuffix = 'Modifier';
|
|
|
|
|
2004-05-17 09:04:42 -04:00
|
|
|
// If not a field-specific search
|
|
|
|
// e.g. with the arbitary form element 'foobar', its modifier will be
|
|
|
|
// <noFieldPrefix>foobarModifier and its form element
|
|
|
|
// will be <noFieldPrefix>foobar
|
|
|
|
var noFieldPrefix = 'noField-';
|
|
|
|
|
2002-05-08 22:18:00 -04:00
|
|
|
// Do you wish the query to be displayed as an alert box?
|
2002-12-03 06:48:17 -05:00
|
|
|
var debug = false;
|
2002-05-08 22:18:00 -04:00
|
|
|
|
|
|
|
// Do you wish the function to submit the form upon query construction?
|
2004-05-17 09:04:42 -04:00
|
|
|
var submitForm = false;
|
2002-05-08 22:18:00 -04:00
|
|
|
|
2002-05-08 22:39:26 -04:00
|
|
|
// prefix modifier for boolean AND queries
|
|
|
|
var AND_MODIFIER = '+';
|
|
|
|
|
|
|
|
// prefix modifier for boolean NOT queries
|
|
|
|
var NOT_MODIFIER = '-';
|
|
|
|
|
|
|
|
// prefix modifier for boolean OR queries
|
2004-05-17 09:04:42 -04:00
|
|
|
var OR_MODIFIER = ' ';
|
|
|
|
|
|
|
|
var NO_MODIFIER = 0;
|
|
|
|
|
|
|
|
// default modifier for terms
|
|
|
|
var DEFAULT_TERM_MODIFIER = AND_MODIFIER;
|
2002-05-08 22:39:26 -04:00
|
|
|
|
2004-05-17 09:04:42 -04:00
|
|
|
// default modifier for groups of terms (denoted by parantheses)
|
|
|
|
var DEFAULT_GROUP_MODIFIER = AND_MODIFIER;
|
2002-05-08 22:39:26 -04:00
|
|
|
|
2003-05-08 02:00:02 -04:00
|
|
|
// used to delimit multiple values from checkboxes and select lists
|
|
|
|
var VALUE_DELIMITER = ' ';
|
|
|
|
|
2002-05-08 22:18:00 -04:00
|
|
|
// Constructs the query
|
|
|
|
// @param query Form field to represent the constructed query to be submitted
|
2003-05-08 02:00:02 -04:00
|
|
|
// @param debug Turn on debugging?
|
|
|
|
function doMakeQuery( query, dbg )
|
2002-05-08 22:18:00 -04:00
|
|
|
{
|
2003-05-08 02:00:02 -04:00
|
|
|
if(typeof(dbg) != "undefined")
|
|
|
|
debug = dbg;
|
2004-05-17 09:04:42 -04:00
|
|
|
|
2002-05-08 22:18:00 -04:00
|
|
|
var frm = query.form;
|
|
|
|
var formElements = frm.elements;
|
|
|
|
query.value = '';
|
2004-05-17 09:04:42 -04:00
|
|
|
|
2003-05-08 02:00:02 -04:00
|
|
|
// keep track of the fields we've examined
|
|
|
|
var dict = new Array();
|
2004-05-17 09:04:42 -04:00
|
|
|
|
2002-05-08 22:18:00 -04:00
|
|
|
for(var i=0; i<formElements.length; i++)
|
|
|
|
{
|
|
|
|
var element = formElements[i];
|
|
|
|
var elementName = element.name;
|
2004-03-06 03:22:05 -05:00
|
|
|
if(elementName != "" && !contains(dict, elementName))
|
2002-05-08 22:18:00 -04:00
|
|
|
{
|
2003-05-08 02:00:02 -04:00
|
|
|
dict[dict.length] = elementName;
|
2004-05-17 09:04:42 -04:00
|
|
|
|
2003-05-08 02:00:02 -04:00
|
|
|
// ensure we get the whole group (of checkboxes, radio, etc), if applicable
|
2004-05-17 09:04:42 -04:00
|
|
|
var elementValue = getFieldValue(frm[element.name]);
|
|
|
|
if(elementValue.length > 0)
|
2002-05-08 22:18:00 -04:00
|
|
|
{
|
2003-05-08 02:00:02 -04:00
|
|
|
var subElement = frm[elementName + modifierSuffix];
|
|
|
|
if(typeof(subElement) != "undefined") // found a field/fieldModifier pair
|
2002-05-08 22:18:00 -04:00
|
|
|
{
|
2004-05-17 09:04:42 -04:00
|
|
|
var termMod, groupMod;
|
|
|
|
var modifier = getFieldValue(subElement);
|
|
|
|
// modifier field is in the form <termModifier>|<groupModifier>
|
|
|
|
if(modifier.indexOf('|') > -1)
|
2002-05-08 22:18:00 -04:00
|
|
|
{
|
2004-05-17 09:04:42 -04:00
|
|
|
var idx = modifier.indexOf('|');
|
|
|
|
termMod = modifier.substring(0, idx);
|
|
|
|
if(termMod == '') termMod = DEFAULT_TERM_MODIFIER;
|
|
|
|
groupMod = modifier.substring(idx + 1);
|
|
|
|
if(groupMod == '') groupMod = DEFAULT_GROUP_MODIFIER;
|
2002-05-08 22:39:26 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-05-17 09:04:42 -04:00
|
|
|
termMod = modifier;
|
|
|
|
if(termMod == '') termMod = DEFAULT_TERM_MODIFIER;
|
|
|
|
groupMod = DEFAULT_GROUP_MODIFIER;
|
2002-05-08 22:18:00 -04:00
|
|
|
}
|
2004-05-17 09:04:42 -04:00
|
|
|
appendTerms(query, termMod, elementValue, elementName, groupMod);
|
2002-05-08 22:18:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-01-23 11:59:38 -05:00
|
|
|
|
2004-05-17 09:04:42 -04:00
|
|
|
if(debug) {alert('Query:' + query.value);}
|
|
|
|
|
|
|
|
if(submitForm)
|
2002-05-08 22:18:00 -04:00
|
|
|
{
|
|
|
|
frm.submit();
|
|
|
|
}
|
2004-03-06 03:22:05 -05:00
|
|
|
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)
|
|
|
|
{
|
2004-05-17 09:04:42 -04:00
|
|
|
appendTerms(query, AND_MODIFIER, query.value);
|
|
|
|
if(submitForm)
|
2004-03-06 03:22:05 -05:00
|
|
|
{
|
|
|
|
frm.submit();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return query;
|
|
|
|
}
|
2002-05-08 22:18:00 -04:00
|
|
|
}
|
|
|
|
|
2004-05-17 09:04:42 -04:00
|
|
|
function buildTerms(termModifier, fieldValue)
|
2003-05-08 02:00:02 -04:00
|
|
|
{
|
2004-05-17 09:04:42 -04:00
|
|
|
fieldValue = trim(fieldValue);
|
|
|
|
var splitStr = fieldValue.split(" ");
|
|
|
|
fieldValue = '';
|
|
|
|
var inQuotes = false;
|
|
|
|
for(var i=0;i<splitStr.length;i++)
|
2003-05-08 02:00:02 -04:00
|
|
|
{
|
2004-05-17 09:04:42 -04:00
|
|
|
if(splitStr[i].length > 0)
|
|
|
|
{
|
|
|
|
if(!inQuotes)
|
|
|
|
{
|
|
|
|
fieldValue = fieldValue + termModifier + splitStr[i] + ' ';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fieldValue = fieldValue + splitStr[i] + ' ';
|
|
|
|
}
|
|
|
|
if(splitStr[i].indexOf('"') > -1) inQuotes = !inQuotes
|
|
|
|
}
|
2003-05-08 02:00:02 -04:00
|
|
|
}
|
2004-05-17 09:04:42 -04:00
|
|
|
fieldValue = trim(fieldValue);
|
|
|
|
return fieldValue;
|
2003-05-08 02:00:02 -04:00
|
|
|
}
|
|
|
|
|
2004-05-17 09:04:42 -04:00
|
|
|
// Appends terms to a query.
|
|
|
|
// @param query Form field of query
|
|
|
|
// @param termModifier Term modifier
|
|
|
|
// @param value Value to be appended. Tokenized by spaces,
|
|
|
|
// and termModifier will be applied to each token.
|
|
|
|
// @param fieldName Name of search field. Omit if not a field-specific query.
|
|
|
|
// @param groupModifier Modifier applied to each group of terms.
|
|
|
|
// @return query form field
|
|
|
|
function appendTerms(query, termModifier, value, fieldName, groupModifier)
|
|
|
|
{
|
|
|
|
if(typeof(groupModifier) == "undefined")
|
|
|
|
groupModifier = DEFAULT_GROUP_MODIFIER;
|
|
|
|
|
|
|
|
value = buildTerms(termModifier, value);
|
|
|
|
|
|
|
|
// not a field-specific search
|
|
|
|
if(fieldName == null || fieldName.indexOf(noFieldPrefix) != -1 || fieldName.length == 0)
|
|
|
|
{
|
|
|
|
if(groupModifier == NO_MODIFIER)
|
|
|
|
{
|
|
|
|
if(query.value.length == 0)
|
|
|
|
{
|
|
|
|
query.value = value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
query.value = query.value + ' ' + value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(query.value.length == 0)
|
|
|
|
{
|
|
|
|
query.value = groupModifier + '(' + value + ')';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
query.value = query.value + ' ' + groupModifier + '(' + value + ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(query.value.length == 0)
|
|
|
|
{
|
|
|
|
query.value = groupModifier + fieldName + ':(' + value + ')';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
query.value = query.value + ' ' + groupModifier +fieldName + ':(' + value + ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
query.value = trim(query.value)
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obtain the value of a form field.
|
|
|
|
// @param field Form field
|
|
|
|
// @return Array of values, or string value depending on field type,
|
|
|
|
// or empty string if field param is undefined or null
|
2003-05-08 02:00:02 -04:00
|
|
|
function getFieldValue(field)
|
|
|
|
{
|
2004-05-17 09:04:42 -04:00
|
|
|
if(field == null || typeof(field) == "undefined")
|
|
|
|
return "";
|
|
|
|
if(typeof(field) != "undefined" && typeof(field[0]) != "undefined" && field[0].type=="checkbox")
|
2003-05-08 02:00:02 -04:00
|
|
|
return getCheckedValues(field);
|
2004-05-17 09:04:42 -04:00
|
|
|
if(typeof(field) != "undefined" && typeof(field[0]) != "undefined" && field[0].type=="radio")
|
2003-05-08 02:00:02 -04:00
|
|
|
return getRadioValue(field);
|
2004-05-17 09:04:42 -04:00
|
|
|
if(typeof(field) != "undefined" && field.type.match("select*"))
|
2003-05-08 02:00:02 -04:00
|
|
|
return getSelectedValues(field);
|
2004-05-17 09:04:42 -04:00
|
|
|
if(typeof(field) != "undefined")
|
|
|
|
return field.value;
|
2003-05-08 02:00:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getRadioValue(radio)
|
|
|
|
{
|
|
|
|
for(var i=0; i<radio.length; i++)
|
|
|
|
{
|
|
|
|
if(radio[i].checked)
|
|
|
|
return radio[i].value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCheckedValues(checkbox)
|
|
|
|
{
|
|
|
|
var r = new Array();
|
|
|
|
for(var i = 0; i < checkbox.length; i++)
|
|
|
|
{
|
|
|
|
if(checkbox[i].checked)
|
|
|
|
r[r.length] = checkbox[i].value;
|
|
|
|
}
|
|
|
|
return r.join(VALUE_DELIMITER);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSelectedValues (select) {
|
|
|
|
var r = new Array();
|
|
|
|
for (var i = 0; i < select.options.length; i++)
|
|
|
|
if (select.options[i].selected)
|
|
|
|
{
|
|
|
|
r[r.length] = select.options[i].value;
|
|
|
|
}
|
|
|
|
return r.join(VALUE_DELIMITER);
|
|
|
|
}
|
|
|
|
|
2004-05-17 09:04:42 -04:00
|
|
|
function quote(value)
|
2004-03-06 03:22:05 -05:00
|
|
|
{
|
2004-05-17 09:04:42 -04:00
|
|
|
return "\"" + trim(value) + "\"";
|
2004-03-06 03:22:05 -05:00
|
|
|
}
|
|
|
|
|
2004-05-17 09:04:42 -04:00
|
|
|
function contains(array, s)
|
2002-05-08 22:18:00 -04:00
|
|
|
{
|
2004-05-17 09:04:42 -04:00
|
|
|
for(var i=0; i<array.length; i++)
|
2002-05-08 22:18:00 -04:00
|
|
|
{
|
2004-05-17 09:04:42 -04:00
|
|
|
if(s == array[i])
|
|
|
|
return true;
|
2004-01-23 11:59:38 -05:00
|
|
|
}
|
2004-05-17 09:04:42 -04:00
|
|
|
return false;
|
2002-12-04 19:21:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function trim(inputString) {
|
|
|
|
if (typeof inputString != "string") { return inputString; }
|
2004-05-17 09:04:42 -04:00
|
|
|
|
2002-12-04 19:21:45 -05:00
|
|
|
var temp = inputString;
|
2004-05-17 09:04:42 -04:00
|
|
|
|
2002-12-04 19:21:45 -05:00
|
|
|
// Replace whitespace with a single space
|
2002-12-10 22:29:05 -05:00
|
|
|
var pattern = /\s+/ig;
|
2002-12-04 19:21:45 -05:00
|
|
|
temp = temp.replace(pattern, " ");
|
2004-05-17 09:04:42 -04:00
|
|
|
|
|
|
|
// Trim
|
2002-12-04 19:21:45 -05:00
|
|
|
pattern = /^(\s*)([\w\W]*)(\b\s*$)/;
|
|
|
|
if (pattern.test(temp)) { temp = temp.replace(pattern, "$2"); }
|
2004-05-17 09:04:42 -04:00
|
|
|
// run it another time through for words which don't end with a character or a digit
|
|
|
|
pattern = /^(\s*)([\w\W]*)(\s*$)/;
|
|
|
|
if (pattern.test(temp)) { temp = temp.replace(pattern, "$2"); }
|
2002-12-04 19:21:45 -05:00
|
|
|
return temp; // Return the trimmed string back to the user
|
2004-05-17 09:04:42 -04:00
|
|
|
}
|