Skip to content

Apache Lucene open-source search software

Initial commit of a javascript lib as a web-based GUI for boolean query construction. Based on the file posted at http://marc.theaimsgroup.com/?l=lucene-user&m=101366456309906&w=2.

Kelvin Tan9b57a1288f1c
diff --git a/sandbox/contributions/javascript/queryConstructor/luceneQueryConstructor.html b/sandbox/contributions/javascript/queryConstructor/luceneQueryConstructor.html
new file mode 100644
index 00000000000..573aa559948
--- /dev/null
+++ b/sandbox/contributions/javascript/queryConstructor/luceneQueryConstructor.html
@@ -0,0 +1,28 @@
+<html>
+ <head>
+ <title>Demo Lucene Query Constructor</title>
+ <script type="text/javascript" src="luceneQueryConstructor.js"></script>
+ </head>
+
+ <body>
+ <form>
+ <input type="hidden" name="query">
+ Name:
+ <input type="text" name="name">
+ <select name="nameModifier">
+ <option value="And" />And
+ <option value="Or" selected/>Or
+ <option value="Not" />Not
+ </select>
+ <p>
+ Description:
+ <input type="text" name="description">
+ <select name="descriptionModifier">
+ <option value="And" />And
+ <option value="Or" selected/>Or
+ <option value="Not" />Not
+ </select>
+ <input type="button" name="Search" value="Search" onClick="doMakeQuery(this.form.query)" />
+ </form>
+ </body>
+</html>
\ No newline at end of file
diff --git a/sandbox/contributions/javascript/queryConstructor/luceneQueryConstructor.js b/sandbox/contributions/javascript/queryConstructor/luceneQueryConstructor.js
new file mode 100644
index 00000000000..229eb84baae
--- /dev/null
+++ b/sandbox/contributions/javascript/queryConstructor/luceneQueryConstructor.js
@@ -0,0 +1,98 @@
+// Lucene Search Query Constructor
+// Author: Kelvin Tan ([email protected])
+// Date: 14/02/2002
+// Version: 1.1
+
+// 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;
+
+// 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')
+ {
+ addAnd(query, elementName, elementValue);
+ }
+ else if(subElementValue == 'Not')
+ {
+ addNot(query, elementName, elementValue);
+ }
+ else if(subElementValue == 'Or')
+ {
+ addOr(query, elementName, elementValue);
+ }
+ }
+ }
+ }
+ }
+
+ if(debug)
+ {
+ alert('Query:' + query.value);
+ }
+
+ if(submitOnConstruction)
+ {
+ frm.submit();
+ }
+}
+
+function addOr(query, field, fieldValue)
+{
+ if(query.value.length == 0)
+ {
+ query.value = '(' + field + ':(' + fieldValue + '))';
+ }
+ else
+ {
+ query.value = query.value + ' (' + field + ':(' + fieldValue + '))';
+ }
+}
+
+function addAnd(query, field, fieldValue)
+{
+ if(query.value.length == 0)
+ {
+ query.value = '+(' + field + ':(' + fieldValue + '))';
+ }
+ else
+ {
+ query.value = query.value + ' +(' + field + ':(' + fieldValue + '))';
+ }
+}
+
+function addNot(query, field, fieldValue)
+{
+ if(query.value.length == 0)
+ {
+ query.value = '-(' + field + ':(' + fieldValue + '))';
+ }
+ else
+ {
+ query.value = query.value + ' -(' + field + ':(' + fieldValue + '))';
+ }
+}
\ No newline at end of file