Sorry...looking down on the thread, I realized you did this before...I got 
engrossed with comparing this variation that came to mind to Geert's...found it 
to be faster and wanted to put it out...but, again, you had it alredy...great 
minds think alike? J

 

From: [email protected] 
[mailto:[email protected]] On Behalf Of Fernandes, Nivaldo
Sent: Wednesday, May 25, 2011 9:31 AM
To: General MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] How to specified a result root node ?

 

Or this one:

 

let $query :=
    cts:and-query((
        cts:element-value-query(xs:QName("title"),"foobar"),
        cts:element-value-query(xs:QName("lastname"),"touissant")
    ))
return
    cts:search(
        xdmp:directory("/test/"),
        $query
    )
    //*:author

 

From: [email protected] 
[mailto:[email protected]] On Behalf Of Geert Josten
Sent: Wednesday, May 25, 2011 9:00 AM
To: General MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] How to specified a result root node ?

 

Hi Stephane,

 

Might be worth to compare the performance of your search with this one (which 
should produce the same results as far as I can see):

 

let $gq := cts:element-value-query(xs:QName("title"), "foobar")
let $fq := cts:element-value-query(xs:QName("lastname"), "toussaint")
return
cts:search(
  xdmp:directory("/test/"),
  $gq
)//*:author[cts:contains(. , $fq)]

 

Kind regards,

Geert

 

Van: [email protected] 
[mailto:[email protected]] Namens Stephane Toussaint
Verzonden: woensdag 25 mei 2011 14:35
Aan: General MarkLogic Developer Discussion
Onderwerp: Re: [MarkLogic Dev General] How to specified a result root node ?

 

Damon,

 

Thank you for your answer. The use of cts:contains is a nice way of doing 
filter operation. Thank you for that.

 

To answer my last use case, I need to separate query in 2 parts. A global and a 
filtering one. Both are combined to produce the search query, and finally the 
last one is use alone for filtering

 

let $gq := cts:element-value-query(xs:QName("title"), "foobar")
let $fq := cts:element-value-query(xs:QName("lastname"), "toussaint")
let $jq := cts:and-query(($gq, $fq))
return
cts:search(
  xdmp:directory("/test/"),
  $jq
)//*:author[cts:contains(. , $fq)]

 

It is the only way I found to retrieve only the author node that match both the 
title and the lastname author. This may seem weird here because I only propose 
one doc in sample, but say I need to filter on the title too.

I would like to not need to know which part of the query is effective for 
filtering and which not, but I think that in my case I can handle it (this 
Xquery is build from the value set by a user in a form way).

 

Anyway thank you for the hint.

 

Thanks

Stephane

 

Le 24 mai 2011 à 18:30, Damon Feldman a écrit :

 

Stephane,

 

You are on the right track. To search the entire document, use the 
xdmp:directory() expression as the "searchable expression" but you will get 
entire documents back from that search.

 

To find the author you want you may use cts:contains(). This is the function 
that performs the "filtering" check referenced in our documentation, where it 
checks an in-memory XML structure to see if a query condition holds rather than 
checking the DB indexes for the query condition.  This example (namespace 
removed for convenience) should help:

 

xquery version "1.0-ml";

 

let $d :=

<doc docId="1">

    <title>foobar</title>

    <authors>

        <author>

            <firstname>stephane</firstname>

            <lastname>toussaint</lastname>

        </author>

        <author>

            <firstname>my</firstname>

            <lastname>self</lastname>

        </author>

    </authors>

</doc>  

 

return xdmp:document-insert("/test/text.xml", $d)

;

 

let $q := cts:word-query("stephane")  (: "stephane" is a shorthand for this 
word query :)

return cts:search(/doc, $q)//author[cts:contains(., $q)]

 

For more precision, you may also use cts:element-query(xs:QName("author"), 
cts:word-query("stephane")).

 

Yours,

Damon

 

From: [email protected] 
[mailto:[email protected]] On Behalf Of Stephane Toussaint
Sent: Tuesday, May 24, 2011 8:28 AM
To: General MarkLogic Developer Discussion
Subject: [MarkLogic Dev General] How to specified a result root node ?

 

Hi,

 

I'm trying to specified a result root node for my query in a customizable 
context.

 

Take this documents for instance ("/test/doc1.xml") :

 

<?xml version="1.0" encoding="UTF-8"?>
<doc docId="1" xmlns="sto:test">
    <title>foobar</title>
    <authors>
        <author>
            <firstname>stephane</firstname>
            <lastname>toussaint</lastname>
        </author>
        <author>
            <firstname>my</firstname>
            <lastname>self</lastname>
        </author>
    </authors>

</doc>   

 

I'd like to retrieve for instance doc which contains title "foobar". Easy 
enough the query will be :

 

cts:search(
  xdmp:directory("/test/"),
  cts:element-value-query(fn:QName("sto:test", "title"), "foobar")
)

 

Now if I want to retrieve docs written by toussaint.

 

cts:search(
  xdmp:directory("/test/"),
  cts:element-value-query(fn:QName("sto:test", "lastname"), "toussaint")
)

 

Ok, so far so good. Now I want to change the root of the element to return. I'd 
like to return authors instead of the doc directly.

Retrieving the authors of a particular doc is easy :

 

cts:search(
  xdmp:directory("/test/"),
  cts:element-value-query(fn:QName("sto:test", "title"), "foobar")
)//*:author

 

But now if I want only authors with 'toussaint' has lastname

 

cts:search(
  xdmp:directory("/test/"),
  cts:element-value-query(fn:QName("sto:test", "

lastname

"), "toussaint")
)//*:author

 

This query doesn't work because it return every authors for the document with 
an author named toussaint (return 2 results instead of one).

 

The thing is to change the root node of the search query :

 

cts:search(
  xdmp:directory("/test/")//*:author,
  cts:element-value-query(fn:QName("sto:test", "lastname"), "toussaint")
)

 

But how could I combined for instance title with lastname and return only the 
author ?

 

cts:search(
  xdmp:directory("/test/")//*:author,
  cts:and-query((
    cts:element-value-query(fn:QName("sto:test", "title"), "foobar"),
    cts:element-value-query(fn:QName("sto:test", "lastname"), "toussaint")
  ))
)

 

This return nothing because there is no 'title' element under 'author'

 

cts:search(
  xdmp:directory("/test/"),
  cts:and-query((
    cts:element-value-query(fn:QName("sto:test", "title"), "foobar"),
    cts:element-value-query(fn:QName("sto:test", "lastname"), "toussaint")
  ))
)//*:author

 

This returns 2 authors instead of only the matching one.

 

So briefly the question is,  whatever the constraint part of my query I want to 
be able to retrieve the closet element of the root element I choose to return 
(I hope I'm clear enough).

Is there something that can be done with the cts api ?

 

Thanks,

 

Stéphane

 

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

 

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

Reply via email to