MarkLogic Search API - How to do sort-order on same element as searchable-expression
marklogic, xquery
Solution
You can use `<name>` as the target element of the searchable expression, but then restrict the query to only look inside `<value>` by adding an `<additional-query>`:
<options xmlns="http://marklogic.com/appservices/search">
<term>
<term-option>case-insensitive</term-option>
</term>
<debug>true</debug>
<searchable-expression>/summary/name</searchable-expression>
<additional-query>
<cts:element-query xmlns:cts="http://marklogic.com/cts">
<cts:element>value</cts:element>
<cts:and-query/>
</cts:element-query>
</additional-query>
<sort-order type="xs:string" direction="ascending">
<element ns="" name="value"/>
<annotation>options for search institutions by name</annotation>
</sort-order>
</options>
Problem
I am attempting to do a search on a specific element so I have a searchable-expression within my options. I also want to sort by the values of this element so I have created an element range index on the value element. Here are my search options which hopefully makes things clear: ``` <options xmlns="http://marklogic.com/appservices/search"> <term> <term-option>case-insensitive</term-option> </term> <debug>true</debug> <searchable-expression>/summary/name/value</searchable-expression> <sort-order type="xs:string" direction="ascending"> <element ns="" name="value"/> <annotation>options for search institutions by name</annotation> </sort-order> </options> ``` The problem is that when it does the sort it adds another value node (Taken from search:report id="SEARCH-FLWOR") ``` ...order by xs:string(($result//value)[1]) ascending return $result)[1 to 50] ``` Instead of: ``` ...order by xs:string(($result)[1]) ascending return $result)[1 to 50] ``` How do I prevent it doing this? I cannot change the searchable expression as the "name" element has another child element which I do not wish to search across. I also I can not leave the sort-order element name blank or set it to current node. It seems this would be simple, but I have not found anything to get this working. Help would be greatly appreciated.