From 99a6c0f8904611a1286c8907b43eae08779c9987 Mon Sep 17 00:00:00 2001 From: Kevin Roast Date: Wed, 11 Aug 2010 14:48:37 +0000 Subject: [PATCH] Share Advanced Search - Basics of Form based search working - Search by type and form based configured fields for that type - Still a bit rough round the edges git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@21741 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../slingshot/search/search.get.desc.xml | 2 +- .../alfresco/slingshot/search/search.get.js | 18 ++-- .../alfresco/slingshot/search/search.lib.js | 93 +++++++++++++++---- 3 files changed, 87 insertions(+), 26 deletions(-) diff --git a/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/search.get.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/search.get.desc.xml index 0479d83f91..7d453429c0 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/search.get.desc.xml +++ b/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/search.get.desc.xml @@ -1,7 +1,7 @@ search Site Search Component Data Webscript - /slingshot/search?term={term?}&tag={tag?}&site={site?}&container={container?}&sort={sort?} + /slingshot/search?term={term?}&tag={tag?}&site={site?}&container={container?}&sort={sort?}&query={query?} argument user required diff --git a/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/search.get.js b/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/search.get.js index 32a1fe1cd4..9c5dd38d0c 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/search.get.js +++ b/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/search.get.js @@ -1,14 +1,18 @@ function main() { - var siteId = (args.site !== undefined) ? args.site : null; - var containerId = (args.container !== undefined) ? args.container : null; - var term = (args.term !== undefined) ? args.term : null; - var tag = (args.tag !== undefined) ? args.tag : null; - var sort = (args.sort !== undefined) ? args.sort : null; - var maxResults = (args.maxResults !== undefined) ? parseInt(args.maxResults, 10) : DEFAULT_MAX_RESULTS; + var params = + { + siteId: (args.site !== null) ? args.site : null, + containerId: (args.container !== null) ? args.container : null, + term: (args.term !== null) ? args.term : null, + tag: (args.tag !== null) ? args.tag : null, + query: (args.query !== null) ? args.query : null, + sort: (args.sort !== null) ? args.sort : null, + maxResults: (args.maxResults !== null) ? parseInt(args.maxResults, 10) : DEFAULT_MAX_RESULTS + }; - model.data = getSearchResults(term, tag, maxResults, siteId, containerId, sort); + model.data = getSearchResults(params); } main(); \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/search.lib.js b/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/search.lib.js index 32320e4660..6c0c31d204 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/search.lib.js +++ b/config/alfresco/templates/webscripts/org/alfresco/slingshot/search/search.lib.js @@ -1,17 +1,22 @@ /** * Search Component * - * Inputs: - * optional: site = the site to search into, null for all sites - * optional: container = the component the search in, null for all components in the site - * optional: term = search terms, should be supplied if tag is not - * optional: tag = search tag, should be supplied if term is not - * maxResults = maximum results to return + * Takes the following object as Input: + * params + * { + * siteId: the site identifier to search into, null for all sites + * containerId: the component the search in, null for all components in the site + * term: search terms + * tag: search tag + * query: advanced search query json + * sort: sort parameter + * maxResults: maximum results to return + * }; * * Outputs: * items - Array of objects containing the search results */ -const DEFAULT_MAX_RESULTS = 100; +const DEFAULT_MAX_RESULTS = 250; const SITES_SPACE_QNAME_PATH = "/app:company_home/st:sites/"; const QUERY_TEMPLATES = [ {field: "keywords", template: "%(cm:name cm:title cm:description ia:whatEvent ia:descriptionEvent lnk:title lnk:description TEXT)"}]; @@ -499,12 +504,18 @@ function processResults(nodes, maxResults) * * "or" is the default operator, AND and NOT are also supported - as is any other valid fts-alfresco * elements such as "quoted terms" and (bracket terms) and also propname:propvalue syntax. + * + * @param params Object containing search parameters - see API description above */ -function getSearchResults(term, tag, maxResults, siteId, containerId, sort) +function getSearchResults(params) { - var nodes; + var nodes, + ftsQuery = "", + term = params.term, + tag = params.tag, + formData = params.query; - var ftsQuery = ""; + // Simple keyword search and tag specific search if (term !== null && term.length !== 0) { ftsQuery = "(" + term + ") PATH:\"/cm:taggable/cm:" + search.ISO9075Encode(term) + "/member\""; @@ -514,35 +525,81 @@ function getSearchResults(term, tag, maxResults, siteId, containerId, sort) ftsQuery = "PATH:\"/cm:taggable/cm:" + search.ISO9075Encode(tag) + "/member\""; } + // Advanced search form data search. + // Supplied as json in the standard Alfresco Forms data structure: + // prop_:value|assoc_:value + // name = namespace_propertyname|pseudopropertyname + // value = string value - comma separated for multi-value, no escaping yet! + // - underscore represents colon character in name + // - pseudo property is one of any cm:content url property: mimetype|encoding|size + // - always string values - interogate DD for type data + if (formData !== null && formData.length !== 0) + { + var formJson = jsonUtils.toObject(formData); + + // extract form data and generate search query + for (var p in formJson) + { + // retrieve value and check there is someting to search for + // currently all values are returned as strings + var propValue = formJson[p]; + if (propValue.length !== 0) + { + if (p.indexOf("prop_") === 0) + { + // found a property - is it namespace_propertyname or pseudo property format? + var propName = p.substr(5); + if (propName.indexOf("_") !== -1) + { + // property name - convert to DD property name format + propName = propName.replace("_", ":"); + // TODO: inspect DD type - handle boolean, number etc? + ftsQuery += ' ' + propName + ':"' + propValue + '"'; + } + else + { + // pseudo cm:content property + // TODO: andyh to fix query parser impl + ftsQuery += ' cm:content.' + propName + ':' + propValue; + } + } + } + } + + // extract data type for this search + ftsQuery = 'TYPE:"' + formJson.datatype + '" AND (' + ftsQuery + ')'; + } + if (ftsQuery.length !== 0) { // we processed the search terms, so suffix the PATH query var path = SITES_SPACE_QNAME_PATH; - if (siteId !== null && siteId.length > 0) + if (params.siteId !== null && params.siteId.length > 0) { - path += "cm:" + search.ISO9075Encode(siteId) + "/"; + path += "cm:" + search.ISO9075Encode(params.siteId) + "/"; } else { path += "*/"; } - if (containerId !== null && containerId.length > 0) + if (params.containerId !== null && params.containerId.length > 0) { - path += "cm:" + search.ISO9075Encode(containerId) + "/"; + path += "cm:" + search.ISO9075Encode(params.containerId) + "/"; } else { path += "*/"; } - ftsQuery = "PATH:\"" + path + "/*\" AND (" + ftsQuery + ") "; - ftsQuery += "AND -TYPE:\"{http://www.alfresco.org/model/content/1.0}thumbnail\""; + ftsQuery = 'PATH:"' + path + '/*" AND (' + ftsQuery + ') '; + ftsQuery += 'AND -TYPE:"cm:thumbnail"'; // sort field - expecting field to in one of the following formats: // - short QName form such as: cm:name // - pseudo cm:content field starting with "." such as: .size // - any other directly supported search field such as: TYPE var sortColumns = []; + var sort = params.sort; if (sort != null && sort.length != 0) { var asc = true; @@ -579,7 +636,7 @@ function getSearchResults(term, tag, maxResults, siteId, containerId, sort) var queryDef = { query: ftsQuery, language: "fts-alfresco", - page: {maxItems: maxResults}, + page: {maxItems: params.maxResults}, templates: QUERY_TEMPLATES, defaultField: "keywords", onerror: "no-results", @@ -593,5 +650,5 @@ function getSearchResults(term, tag, maxResults, siteId, containerId, sort) nodes = []; } - return processResults(nodes, maxResults); + return processResults(nodes, params.maxResults); } \ No newline at end of file