Collaborama About Help Contact Anonymous [login] Source: site.view [edit] Function name: searchCartoons Arguments: query,caption,tags,author,lang Description: Search the cartoon database using tags (category filters) and a query string DB is passed as an option argument Page type: webl Render function: searchCartoonsRender Module: perfectCartoon Page source: var dbg = ""; // Set this to something you want to display var allWords = fun(query) var q = ""; every s in Str_Split(query, " ") do q = q + " +" + s; end; return Str_Trim(q) end; var multiWordCheck = fun(settings, query) if Size(Str_Split(query, " ")) > 1 then var lquery = Str_ToLowerCase(query); every cat in settings.tagSet do if Str_ToLowerCase(cat) == lquery then return allWords(query) end end; every cat in settings.tagSetFr do if Str_ToLowerCase(cat) == lquery then return allWords(query) end end; every cat in settings.authorSet do if Str_ToLowerCase(cat) == lquery then return allWords(query) end end; return query else return query end; end; var main = fun() var MAX = 500; var settings = WubCall("cartoonDBSettings", []); var db = "production"; if (Size(OPTIONALARGS) == 1) and (OPTIONALARGS[0] == "staging") then db = "staging"; end; var reader = Wub_ReadLuceneIndex(settings[db]); if db == "production" then db = "" end; query = Str_Trim(query); caption = Str_Trim(caption); tags = Str_Trim(tags); author = Str_Trim(Wub_ReplaceAll(author, "%20", " ")); if (Str_IndexOf("...", query) > 0) then query = ""; end; if (Str_IndexOf("...", author) > 0) then author = ""; end; if (Str_IndexOf("...", tags) > 0) then tags = ""; end; if (Str_IndexOf("...", caption) > 0) then caption = ""; end; var quotes = []; var luceneQuery = ""; if query != "" then luceneQuery = multiWordCheck(settings, query); quotes = WubCall("searchQuotations", [query]); end; if caption != "" then if luceneQuery != "" then luceneQuery = luceneQuery + " AND " else quotes = WubCall("searchQuotations", [tags]); end; if lang == "fr" then luceneQuery = luceneQuery + `textfr:"` + caption + `"`; else luceneQuery = luceneQuery + `text:"` + caption + `"`; end end; if tags != "" then if luceneQuery != "" then luceneQuery = luceneQuery + " AND " else quotes = WubCall("searchQuotations", [tags]); end; if lang == "fr" then luceneQuery = luceneQuery + `tagsfr:"` + tags + `"`; else luceneQuery = luceneQuery + `tags:"` + tags + `"`; end; end; if author != "" then if luceneQuery != "" then luceneQuery = luceneQuery + " AND " end; luceneQuery = luceneQuery + `author:"` + author + `"`; end; var feature = nil; // Get random quote if Str_Trim(luceneQuery) == "" then var lst = ToList(settings.tagSet); var cat = lst[Wub_RandomInt(Size(lst))]; quotes = WubCall("searchQuotations", [cat]); luceneQuery = `tags:"` + cat + `"`; feature = true; end; var quote = nil; if Size(quotes) > 0 then quote = quotes[Wub_RandomInt(Size(quotes))] end; var defaultField = "search"; if (lang == "fr") then defaultField = "searchFr"; end; var docs = Wub_LuceneSearch(reader, defaultField, luceneQuery, 2000, settings.stemmer); Wub_CloseLuceneIndex(reader); var i = 0; var sz = (docs.size() ? 0); var res = []; while (i < sz) do var d = [. .]; var doc = docs.get(i).getDoc(); var handScore = 1.0; var val = (Str_Trim(doc.getField("handScore").stringValue()) ? ""); if (val != "") then handScore = ToReal(val) ? 1.0; end; // d["explanation"] := docs.get(i).getExplanation() ? ""; d["pageUrl"] := doc.getField("pageUrl").stringValue() ? ""; d["imgUrl"] := doc.getField("imgUrl").stringValue() ? ""; d["title"] := doc.getField("title").stringValue() ? ""; d["text"] := doc.getField("text").stringValue() ? ""; d["author"] := doc.getField("author").stringValue() ? ""; d["tags"] := doc.getField("tags").stringValue() ? ""; d["newYorker"] := doc.getField("newYorker").stringValue() ? ""; d["handScore"] := handScore ? "0"; d["textfr"] := doc.getField("textfr").stringValue() ? ""; d["tagsfr"] := doc.getField("tagsfr").stringValue() ? ""; res = res + [d]; i = i + 1 end; if (settings["sortByManual"] ? true) then res = Sort(res, fun(a, b) var diff = a.handScore - b.handScore; if diff < 0.0 then 1 elsif diff == 0.0 then 0 else -1 end end); end; // if Size(res) > MAX then // res = Select(res, 0, MAX-1) // end; if (feature != nil) then feature = res[Wub_RandomInt(Size(res))]; return [. q = query, luceneQuery=luceneQuery, roamOver = settings.roamOver, showTips = settings.showTips, t = tags, a = author, c = caption, tags = settings.tagSet, tagsfr = settings.tagSetFr, authors = settings.authorSet, res = [], debug=dbg, db = db, lang = lang, quote=quote, feature = nil .] // nil: disable showing image on homepage for now, until get copyright else return [. q = query, luceneQuery=luceneQuery, roamOver = settings.roamOver, showTips = settings.showTips, t = tags, a = author, c = caption, tags = settings.tagSet, tagsfr = settings.tagSetFr, authors = settings.authorSet, res = res, debug=dbg, db = db, lang = lang, quote=quote, feature = nil .] end; end; main();