/* Reminder: The database must be full-text indexed to use these functions */ function buildSearchQuery() { /* Set up the sessionScope variables you want to use for your search. Each search field should be bound to a sessionScope variable. Checking for an existing instance of the the sessionScope variable keeps the search criteria if you open a view document then return to the view. */ sessionScope.put("sAllWords", sessionScope.get("sAllWords") || ""); sessionScope.put("sExactPhrase", sessionScope.get("sExactPhrase") || ""); sessionScope.put("sAnyWords", sessionScope.get("sAnyWords") || ""); sessionScope.put("sField1", sessionScope.get("sField1") || ""); sessionScope.put("sField2", sessionScope.get("sField2") || ""); /* Search for words or phrases that appear in any of the view documents. The "addToQuery" function assumes words are separated by spaces. */ var wordArray = []; /* All the search words in this field must appear in a document. */ var all = sessionScope.get("sAllWords"); if (all != "") { addToQuery(all,"AND",wordArray); } /* The exact phrase in this field must appear in a document. */ var exact = sessionScope.get("sExactPhrase"); if (exact != "") { wordArray.push("\"" + exact + "\""); } /* And of these words in this field may appear in a document. */ var any = sessionScope.get("sAnyWords"); if (any != "") { addToQuery(any,"OR",wordArray); } var wordQuery = ""; if (wordArray.length > 0) { if (wordArray.length == 1) { wordQuery += wordArray[0]; } else { wordQuery += "(" + wordArray.join(" AND ") + ")"; } } /* Use this to find words within specific fields */ var fieldArray = []; var val1 = sessionScope.get("sField1"); if (val1 != "") { addToQuery(val1,"AND",fieldArray,"Field1Name"); } var val2 = sessionScope.get("sField2"); if (val2 != "") { addToQuery(val2,"AND",fieldArray,"field2Name"); } var fieldQuery = ""; if (fieldArray.length > 0) { if (fieldArray.length == 1) { fieldQuery += fieldArray[0]; } else { fieldQuery += "(" + fieldArray.join(" AND ") + ")"; } } /* Combine to make the "Grand Unified Query" */ var finalQuery = ""; if ((wordQuery + fieldQuery) != "") { if (wordQuery == "") { finalQuery = fieldQuery; } else { if (fieldQuery == "") { finalQuery = wordQuery; } else { finalQuery = "(" + wordQuery + " AND " + fieldQuery + ")"; } } } return finalQuery; } function addToQuery(str,connector,targetArray,fieldName) { var ret = ""; if ((fieldName != null) && (fieldName != "")) { ret = "[" + fieldName + "]="; } if (@Contains(str, " ")) { var arr = str.split(" "); targetArray.push(ret + "(" + arr.join(" " + connector + " ") + ")"); } else { targetArray.push("(" + ret + str + ")"); } }