enhanced sample, but validator is acting up it seems. kelvin - am i doing something wrong?

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150938 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2004-01-23 16:59:38 +00:00
parent 5320408426
commit 8c1b134a7a
2 changed files with 67 additions and 26 deletions

View File

@ -2,27 +2,67 @@
<head> <head>
<title>Demo Lucene Query Constructor</title> <title>Demo Lucene Query Constructor</title>
<script type="text/javascript" src="luceneQueryConstructor.js"></script> <script type="text/javascript" src="luceneQueryConstructor.js"></script>
<script type="text/javascript" src="../queryValidator/luceneQueryValidator.js"></script>
</head> </head>
<body> <body>
<form> <form>
<p>
<input type="hidden" name="query"> <input type="hidden" name="query">
Name: Name:
<input type="text" name="name"> <input type="text" name="name">
<select name="nameModifier"> <select name="nameModifier">
<option value="And" />And <option value="And">And</option>
<option value="Or" selected/>Or <option value="Or" selected="selected">Or</option>
<option value="Not" />Not <option value="Not">Not</option>
</select> </select>
</p>
<p> <p>
Description: Description:
<input type="text" name="description"> <input type="text" name="description">
<select name="descriptionModifier"> <select name="descriptionModifier">
<option value="And" />And <option value="And">And</option>
<option value="Or" selected/>Or <option value="Or" selected="selected">Or</option>
<option value="Not" />Not <option value="Not">Not</option>
</select> </select>
<input type="button" name="Search" value="Search" onClick="doMakeQuery(this.form.query)" /> </p>
<p>
Required Select:
<select name="choice">
<option value="one">One</option>
<option value="two">Two</option>
</select>
<input type="hidden" name="choiceModifier" value="And"/>
</p>
<p>
Multi-select:
<select name="multi" multiple="true" size="5">
<option value="multi1">multi1</option>
<option value="multi2">multi2</option>
<option value="multi3">multi3</option>
<option value="multi4">multi4</option>
</select>
<input type="hidden" name="multiModifier" value="Or"/>
</p>
<p>
Radio:
<!-- TODO: something fishy happens if none selected -->
<input type="radio" name="radio" value="wnrn" checked="checked" />WNRN
<input type="radio" name="radio" value="klpx" />KLPX
<input type="hidden" name="radioModifier" value="And"/>
</p>
<p>
Current Query:
<pre id="curQuery"></pre>
</p>
<input type="button" name="Update" value="Update Query" onClick="doMakeQuery(this.form.query); curQuery.innerText = this.form.query.value" />
<input type="button" name="Validate" value="Validate" onClick="doCheckLuceneQuery(this.form.query);" />
</form> </form>
</body> </body>
</html> </html>

View File

@ -33,14 +33,14 @@ function doMakeQuery( query, dbg )
{ {
if(typeof(dbg) != "undefined") if(typeof(dbg) != "undefined")
debug = dbg; debug = dbg;
var frm = query.form; var frm = query.form;
var formElements = frm.elements; var formElements = frm.elements;
query.value = ''; query.value = '';
// keep track of the fields we've examined // keep track of the fields we've examined
var dict = new Array(); var dict = new Array();
for(var i=0; i<formElements.length; i++) for(var i=0; i<formElements.length; i++)
{ {
var element = formElements[i]; var element = formElements[i];
@ -48,10 +48,11 @@ function doMakeQuery( query, dbg )
if(!contains(dict, elementName)) if(!contains(dict, elementName))
{ {
dict[dict.length] = elementName; dict[dict.length] = elementName;
// ensure we get the whole group (of checkboxes, radio, etc), if applicable // ensure we get the whole group (of checkboxes, radio, etc), if applicable
var elementValue = getFieldValue(frm[element.name]); var elementValue = trim(getFieldValue(frm[element.name]));
if(elementValue.length > 0)
if(elementValue.length > 0 && elementValue != ' ')
{ {
var subElement = frm[elementName + modifierSuffix]; var subElement = frm[elementName + modifierSuffix];
if(typeof(subElement) != "undefined") // found a field/fieldModifier pair if(typeof(subElement) != "undefined") // found a field/fieldModifier pair
@ -62,7 +63,7 @@ function doMakeQuery( query, dbg )
if(logic == 'And') if(logic == 'And')
{ {
addFieldWithModifier(query, AND_MODIFIER, elementName, elementValue); addFieldWithModifier(query, AND_MODIFIER, elementName, elementValue);
} }
else if(logic == 'Not') else if(logic == 'Not')
{ {
addFieldWithModifier(query, NOT_MODIFIER, elementName, elementValue); addFieldWithModifier(query, NOT_MODIFIER, elementName, elementValue);
@ -79,12 +80,12 @@ function doMakeQuery( query, dbg )
} }
} }
} }
if(debug) if(debug)
{ {
alert('Query:' + query.value); alert('Query:' + query.value);
} }
if(submitOnConstruction) if(submitOnConstruction)
{ {
frm.submit(); frm.submit();
@ -107,9 +108,9 @@ function getFieldValue(field)
return getCheckedValues(field); return getCheckedValues(field);
if(typeof(field[0]) != "undefined" && field[0].type=="radio") if(typeof(field[0]) != "undefined" && field[0].type=="radio")
return getRadioValue(field); return getRadioValue(field);
if(field.type.match("select*")) if(field.type.match("select*"))
return getSelectedValues(field); return getSelectedValues(field);
return field.value; return field.value;
} }
@ -146,7 +147,7 @@ function getSelectedValues (select) {
function addFieldWithModifier(query, modifier, field, fieldValue) function addFieldWithModifier(query, modifier, field, fieldValue)
{ {
fieldValue = trim(fieldValue); fieldValue = trim(fieldValue);
if(query.value.length == 0) if(query.value.length == 0)
{ {
query.value = modifier + '(' + field + ':(' + fieldValue + '))'; query.value = modifier + '(' + field + ':(' + fieldValue + '))';
@ -154,19 +155,19 @@ function addFieldWithModifier(query, modifier, field, fieldValue)
else else
{ {
query.value = query.value + ' ' + modifier + '(' + field + ':(' + fieldValue + '))'; query.value = query.value + ' ' + modifier + '(' + field + ':(' + fieldValue + '))';
} }
} }
function trim(inputString) { function trim(inputString) {
if (typeof inputString != "string") { return inputString; } if (typeof inputString != "string") { return inputString; }
var temp = inputString; var temp = inputString;
// Replace whitespace with a single space // Replace whitespace with a single space
var pattern = /\s+/ig; var pattern = /\s+/ig;
temp = temp.replace(pattern, " "); temp = temp.replace(pattern, " ");
// Trim // Trim
pattern = /^(\s*)([\w\W]*)(\b\s*$)/; pattern = /^(\s*)([\w\W]*)(\b\s*$)/;
if (pattern.test(temp)) { temp = temp.replace(pattern, "$2"); } if (pattern.test(temp)) { temp = temp.replace(pattern, "$2"); }
return temp; // Return the trimmed string back to the user return temp; // Return the trimmed string back to the user