Table of contents
No headers

BuildLuceneConstraint

Last modified 18:51, 6 Nov 2014
Table of contents
No headers
/***

    USAGE: buildLuceneQuery(sortyby, ns, path, language, type, author, tags)
        Builds lucene contraints from various variables
    
    sortby : str
        Sort criteria.  One of 'rank', 'title', 'modified'.        
    
    ns : str
        Namespace to search. One of 'all', 'main', 'user', 'template'.

    path : str
        Subpath to search.

    language : str
        Language to search in.

    type : str
        Type of documents to return. One of 'wiki', 'document', 'image', 'comment'.

    author : str
        Username of the author.

    tags : list/str
        List or comma separated list of tags

***/

var sortby = $sortby ?? $0;
var ns = $ns ?? $1;
var path = $path ?? $2;
var language = $language ?? $3;
var type = $type ?? $4;
var author = $author ?? $5;
var tags = $tags ?? $6;

var paths = [];
if(ns == 'user') {
    paths ..= [ string.searchescape(user.homepage.path), string.searchescape(user.homepage.path) .. '/*' ];
}
if(#path > 0) {
    if (string.startswith(path, '/')) {
        path = string.substr(path, 1);
    }
    if(ns == 'subpages') {
        paths ..= [ path .. '/*' ];
        ns = 'all';
    } else {
    paths ..= [path];
}
}

// Construct contraints
var constraint = [ ];
if(#ns > 0 && ns != 'all') {
    constraint ..= [ '+namespace:' .. string.searchescape(ns) ];
}
if(#paths > 0) {
    web.pre("p" .. paths);
    constraint ..= [ '+' .. string.join(['path.ancestor:' .. string.searchescape(string.replace(string.trim(path), '/*', '', false)) foreach var path in paths], ' OR ') .. '' ];
}
if(#language > 0) {
    constraint ..= [ '(language:' .. string.searchescape(language) .. ' language: neutral)' ];
}
if(#type > 0) {
    constraint ..= [ '+type:' .. string.searchescape(type) ];
}
if(#tags > 0) {
    var listTags = typeof(tags) == 'list' ? tags : string.split(tags, ',');    
    constraint ..= [ string.join(['+tag:' .. string.replace('"' .. string.searchescape(string.trim(tag)) .. '"', '\\"', '') foreach var tag in listTags], ' ') ];
}
if(#author > 0) {
    constraint ..= [ '+author:' .. string.quote(string.searchescape(string.trim(author)))];
}

// Join all the constraint into a string
constraint = #constraint > 0 ? '+(' .. string.join(constraint, ' ') .. ')' : '';

export = constraint;
Page statistics
156 view(s) and 10 edit(s)

Tags

This page has no custom tags.
This page has no classifications.

Comments

You must to post a comment.

Attachments