2002-05-08 22:18:00 -04:00
|
|
|
// Lucene Search Query Constructor
|
|
|
|
// Author: Kelvin Tan (kelvin@relevanz.com)
|
|
|
|
|
|
|
|
// 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"
|
|
|
|
var modifierSuffix = 'Modifier';
|
|
|
|
|
|
|
|
// Do you wish the query to be displayed as an alert box?
|
|
|
|
var debug = true;
|
|
|
|
|
|
|
|
// Do you wish the function to submit the form upon query construction?
|
|
|
|
var submitOnConstruction = true;
|
|
|
|
|
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
|
|
|
|
var OR_MODIFIER = '';
|
|
|
|
|
|
|
|
// default prefix modifier for boolean queries
|
|
|
|
var DEFAULT_MODIFIER = OR_MODIFIER;
|
|
|
|
|
2002-05-08 22:18:00 -04:00
|
|
|
// Constructs the query
|
|
|
|
// @param query Form field to represent the constructed query to be submitted
|
|
|
|
function doMakeQuery( query )
|
|
|
|
{
|
|
|
|
var frm = query.form;
|
|
|
|
var formElements = frm.elements;
|
|
|
|
query.value = '';
|
|
|
|
for(var i=0; i<formElements.length; i++)
|
|
|
|
{
|
|
|
|
var element = formElements[i];
|
|
|
|
var elementName = element.name;
|
|
|
|
var elementValue = element.value;
|
|
|
|
if(elementValue.length > 0)
|
|
|
|
{
|
|
|
|
for(var j=0; j<formElements.length; j++)
|
|
|
|
{
|
|
|
|
var subElement = formElements[j];
|
|
|
|
if(subElement.name == (elementName + modifierSuffix))
|
|
|
|
{
|
|
|
|
var subElementValue = subElement.options[subElement.selectedIndex].value;
|
|
|
|
if(subElementValue == 'And')
|
|
|
|
{
|
2002-05-08 22:39:26 -04:00
|
|
|
addFieldWithModifier(query, AND_MODIFIER, elementName, elementValue);
|
2002-05-08 22:18:00 -04:00
|
|
|
}
|
|
|
|
else if(subElementValue == 'Not')
|
|
|
|
{
|
2002-05-08 22:39:26 -04:00
|
|
|
addFieldWithModifier(query, NOT_MODIFIER, elementName, elementValue);
|
2002-05-08 22:18:00 -04:00
|
|
|
}
|
|
|
|
else if(subElementValue == 'Or')
|
|
|
|
{
|
2002-05-08 22:39:26 -04:00
|
|
|
addFieldWithModifier(query, OR_MODIFIER, elementName, elementValue);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addFieldWithModifier(query, DEFAULT_MODIFIER, elementName, elementValue);
|
2002-05-08 22:18:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(debug)
|
|
|
|
{
|
|
|
|
alert('Query:' + query.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(submitOnConstruction)
|
|
|
|
{
|
|
|
|
frm.submit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-08 22:39:26 -04:00
|
|
|
function addFieldWithModifier(query, modifier, field, fieldValue)
|
2002-05-08 22:18:00 -04:00
|
|
|
{
|
|
|
|
if(query.value.length == 0)
|
|
|
|
{
|
2002-05-08 22:39:26 -04:00
|
|
|
query.value = modifier + '(' + field + ':(' + fieldValue + '))';
|
2002-05-08 22:18:00 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-05-08 22:39:26 -04:00
|
|
|
query.value = query.value + ' ' + modifier + '(' + field + ':(' + fieldValue + '))';
|
2002-05-08 22:18:00 -04:00
|
|
|
}
|
|
|
|
}
|