[MarkLogic Dev General] Referencing a string variable withincts:search

Danny Sokolsky dsokolsky at marklogic.com
Tue Jun 26 11:59:44 PDT 2007


Hi Mattio,

I think the key is that you need to define your external variable as
type cts:query.  Passing in the external variable is definitely the
preferred technique over using xdmp:eval.  Here is a simple example:

Suppose you have a file on your app server root named invoke.xqy with
the following contents:

   define variable $query as cts:query external

   xdmp:estimate(cts:search(doc(), $query))

You can then invoke this XQuery module, passing it a cts:query, by
issuing a query as follows:

   xdmp:invoke("invoke.xqy", (xs:QName("query"), cts:word-query("foo")))

You can have some logic in this module to construct the cts:query to
make it dynamic (based on user inputs, for example).  For a brief
example of how you might do that, see the "Creating a cts:query Parser"
section in the "Composing cts:query Expressions" chapter of the
Developer's Guide.

Does that help?
-Danny

-----Original Message-----
From: general-bounces at developer.marklogic.com
[mailto:general-bounces at developer.marklogic.com] On Behalf Of Mattio
Valentino
Sent: Tuesday, June 26, 2007 10:48 AM
To: General Mark Logic Developer Discussion
Subject: [MarkLogic Dev General] Referencing a string variable
withincts:search


I'm curious about something and I was wondering if anyone has some
feedback.

I have a query saved on the file system so I can call it via XCC from my
web application.  The parts of interest are:

let $PARSED_QUERY := cts:and-query( ( cts:word-query("christmas",
("case-insensitive", "diacritic-insensitive")),
cts:word-query("holiday", ("case-insensitive",
"diacritic-insensitive")) ) )
let $SEARCH := cts:search( //(entry | section), $PARSED_QUERY) let
$ESTIMATE := cts:remainder($SEARCH[1]) ...go on to do other
processing...

This works perfectly fine as is.

I plan on changing $PARSED_QUERY so it's a variable with an external
source.  I attempted this by changing the definition to define variable
$PARSED_QUERY as xs:string external, but I started getting errors.

After a little debugging, I went back to my test query above and changed
the let definition to be:

let $PARSED_QUERY := 'cts:and-query( ( cts:word-query("christmas",
("case-insensitive", "diacritic-insensitive")),
cts:word-query("holiday", ("case-insensitive",
"diacritic-insensitive")) ) )'

Turns out that treating $PARSED_QUERY as a string throws an error. That
makes sense after looking at the definition for cts:search.

By chance, I tried wrapping $PARSED_QUERY in xdmp:eval() and then I
could pass the value in as a string and have the search execute
properly.  All is well again in my little corner of the world.

But, is there a more proper or efficient way to pass in this variable so
it can be referenced inside of cts:search?  Or is there a way to convert
it properly using XQuery?

Thanks!
_______________________________________________
General mailing list
General at developer.marklogic.com
http://xqzone.com/mailman/listinfo/general


More information about the General mailing list