Hi Keith,

What do your documents look like, and do you have a searchable expression?  And 
what version of MarkLogic are you running?  If you do have a fragment root, you 
should ideally search at the fragment root, not above it.  It seems to do the 
right thing for me when I use a searchable expression.

Here is an example that I think gives the answer you are looking for.  For this 
example, I have div as a fragment root, a codepoint range index on div/@order,  
and a searchable expression of /foo/div.  I am using 6.0-2.2.

Here is what I did:

xquery version "1.0-ml";
xdmp:document-insert("/keith1.xml",
<foo>
  <div order="3">hello</div>
  <div order="1">hello</div>
</foo>),
xdmp:document-insert("/keith2.xml",
<foo>
  <div order="2">hello</div>
  <div order="4">hello</div>
</foo>);

import module namespace search = "http://marklogic.com/appservices/search";
    at "/MarkLogic/appservices/search/search.xqy";

let $options :=
<options xmlns="http://marklogic.com/appservices/search";>
    <searchable-expression>/foo/div</searchable-expression>
    <sort-order type="xs:string" direction="ascending"
       collation="http://marklogic.com/collation/codepoint";>
       <element ns="" name="div"/>
       <attribute ns="" name="order"/>
     </sort-order>
<debug>true</debug>
</options>
return
search:search("hello", $options),

xdmp:version()


ð   And here are the results, which seem to be ordered correctly:

<search:response snippet-format="snippet" total="4" start="1" page-length="10" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns="" 
xmlns:search="http://marklogic.com/appservices/search";>
  <search:result index="1" uri="/keith1.xml" 
path="fn:doc(&quot;/keith1.xml&quot;)/foo/div[2]" score="14336" 
confidence="0.370487" fitness="0.718278">
    <search:snippet>
      <search:match 
path="fn:doc(&quot;/keith1.xml&quot;)/foo/div[2]"><search:highlight>hello</search:highlight></search:match>
    </search:snippet>
  </search:result>
  <search:result index="2" uri="/keith2.xml" 
path="fn:doc(&quot;/keith2.xml&quot;)/foo/div[1]" score="14336" 
confidence="0.370487" fitness="0.718278">
    <search:snippet>
      <search:match 
path="fn:doc(&quot;/keith2.xml&quot;)/foo/div[1]"><search:highlight>hello</search:highlight></search:match>
    </search:snippet>
  </search:result>
  <search:result index="3" uri="/keith1.xml" 
path="fn:doc(&quot;/keith1.xml&quot;)/foo/div[1]" score="14336" 
confidence="0.370487" fitness="0.718278">
    <search:snippet>
      <search:match 
path="fn:doc(&quot;/keith1.xml&quot;)/foo/div[1]"><search:highlight>hello</search:highlight></search:match>
    </search:snippet>
  </search:result>
  <search:result index="4" uri="/keith2.xml" 
path="fn:doc(&quot;/keith2.xml&quot;)/foo/div[2]" score="14336" 
confidence="0.370487" fitness="0.718278">
    <search:snippet>
      <search:match 
path="fn:doc(&quot;/keith2.xml&quot;)/foo/div[2]"><search:highlight>hello</search:highlight></search:match>
    </search:snippet>
  </search:result>
  <search:qtext>hello</search:qtext>
  <search:report id="SEARCH-FLWOR">(for $result in cts:search(/foo/div, 
cts:word-query("hello", ("lang=en"), 1), ("score-logtfidf"), 1) order by 
xs:string(($result//div/@order)[1]) ascending collation 
"http://marklogic.com/collation/codepoint"; return $result)[1 to 
10]</search:report>
  <search:metrics>
    <search:query-resolution-time>PT0.073S</search:query-resolution-time>
    <search:facet-resolution-time>PT0S</search:facet-resolution-time>
    <search:snippet-resolution-time>PT0.007S</search:snippet-resolution-time>
    <search:total-time>PT0.081S</search:total-time>
  </search:metrics>
</search:response>
6.0-2.2

-Danny

From: [email protected] 
[mailto:[email protected]] On Behalf Of Keith Breinholt
Sent: Thursday, March 14, 2013 2:56 PM
To: General MarkLogic Developer Discussion
Subject: [MarkLogic Dev General] How do I sort by result attribute in 
search:search()?

We have a search:search() query that searches for <div> elements in a database 
fragmented on <div> tags.

We want to sort the <div> tags based on an @order attribute so we passing the 
sort-order option:

                    <sort-order type="xs:string" direction="ascending">
                        <element ns="" name="div"/>
                        <attribute ns="" name="order"/>
                    </sort-order>

But the results returned are not sorted on the order attribute.

When we turn on debugging with the <debug>true</debug> option I see in the 
SEARCH-FLWOR the order by phrase:

      order by xs:string(($result//div/@order)[1]) ascending

What we actually want is:

      order by xs:string($result/@order) ascending

If I run the cts:query from the SEARCH-FLWOR with that modified order by phrase 
it returns results sorted exactly as wanted.

Is there a way to coax search:search() to sort specifically on an attribute of 
the result?

--
Keith
"Do what you can, with what you have, where you are." - Theodore Roosevelt
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to