Table of contents
No headers

BuildSubQuery

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

    USAGE: buildSubQuery(terms, conjunction, include, termModifier)
        Builds a lucene sub-query
    
    terms : list
        A list of terms to include

    conjunction : str (default: AND)
        What to join the terms with. 

    include : bool (default: true)
        if true a '+' sign will be added to the terms, otherwise a '-' will be used.

    termModifier : str
        custom termModifier.

***/

var terms = $terms ?? $0 ?? [ ];
var conjunction = $conjunction ?? $1 ?? 'AND';
var include = $include ?? $2 ?? true;
var termModifier = $termModifier ?? $3 ?? nil;

var termModifiers = {
    'content': '',
    'title': '10.0',
    'path.title': '4.0',
    'description': '3.0'
};
terms = [ term foreach var term in terms where #term ];
if(#terms) {
    var queries = [ ];
    if(termModifier != nil) {
        foreach(var term in terms) {
            queries ..= [termModifier .. ':' .. string.quote(string.searchescape(term))];
        }
    } else {
        foreach(var term in terms) {
            var subQueries = [ ];
            foreach(var modifier in map.keys(termModifiers)) {
                var q = modifier .. ':' .. string.quote(string.searchescape(term));
                if(#termModifiers[modifier] > 0) {
                    q ..= '^' .. termModifiers[modifier];
                }
                subQueries ..= [q];
            }
            var subQuery = string.join(subQueries, ' OR ');
            if(#subQuery > 0) {
                queries ..= ['(' .. subQuery .. ')'];
            }
        }
    }
    var sign = include ? '+' : '-';
    var query = sign .. '(' .. string.join(queries, ' ' .. conjunction .. ' ') .. ')';
} else {
    var query = '';
}

// return
export = query;
Page statistics
163 view(s) and 5 edit(s)

Tags

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

Comments

You must to post a comment.

Attachments