Hi, Oleksii:

Can you explain what you are trying to accomplish?

There may be better ways of doing the same thing than creating a custom 
grammar, which is really a tool of last resort.

For instance, a custom constraint can map a term to a custom query.

For other cases, it's often useful to do postprocessing on the generated query.

If a custom grammar really is unavoidable, in many cases a special-purpose 
third-party parsing tool may provide a faster and more flexible alternative to 
the limited custom grammar in the Search API.

For instance, the Jison.js and Peg.js parsers work with server-side JavaScript. 
(A nearly.js parser is also available, though I've heard no reports about it 
yet.)


Hoping that helps,


Erik Hennum


________________________________
From: [email protected] 
[[email protected]] on behalf of Oleksii Segeda 
[[email protected]]
Sent: Monday, January 30, 2017 8:51 AM
To: [email protected]
Subject: [MarkLogic Dev General] Custom search grammar

Hi there,

I’m trying to declare a custom search grammar. I declared a custom function via 
search options, which supposed to parse “BOOST” keyword:

<joiner strength="2" apply="custom-boost" 
ns="http://worldbankgroup.org/search/grammar"; at="/lib/grammar-boost.xqy" 
tokenize="word">BOOST</joiner>

I declared this function and just copied existing implementation from 
impl:joiner-boost function in /MarkLogic/appservices/search/search-impl.xqy :

declare function grammar:custom-boost($ps as map:map, $left as element()?, 
$opts as element()?) as schema-element(cts:query) {
    let $symbol := impl:symbol-lookup($ps)
    let $_ := tdop:advance($ps)
    let $expr1 := tdop:expression($ps, $symbol/@strength)
    return
        if (empty($left))
        then ($left, impl:msg($ps, <cts:annotation 
warning="SEARCH-IGNOREDQTEXT:[{string($symbol)} {$expr1}]: expected two 
arguments"/>))
        else
            element { xs:QName($symbol/@element) } {
                attribute qtextjoin {concat($symbol/string())},
                attribute strength {$symbol/@strength},
                attribute qtextgroup { 
impl:opts($ps)/opt:grammar/opt:starter[@apply eq "grouping"]/(string(), 
@delimiter/string()) },
                for $opt in 
$symbol/@options/tokenize(normalize-space(.)<mailto:$symbol/@options/tokenize(normalize-space(.)>,
 "\s") return <cts:option>{$opt}</cts:option>,
                element cts:matching-query {
                    attribute qtextref { "schema-element(cts:query)" },
                    $left },
                element cts:boosting-query {
                    attribute qtextref { "schema-element(cts:query)" },
                    $expr1 }
            }
};

Unfortunately this doesn’t work, because for some reason impl:symbol-lookup 
returns an empty sequence.
Any ideas what went wrong here?


Oleksii Segeda

IT Analyst

Information and Technology Solutions

[http://siteresources.worldbank.org/NEWS/Images/spacer.png]

[http://siteresources.worldbank.org/NEWS/Images/WBG_Information_and_Technology_Solutions.png]



_______________________________________________
General mailing list
[email protected]
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to