Home Ask Login Register

Developers Planet

Your answer is one click away!

Ravi February 2016

Custom sort-order in Xquery search options

I am writing Rest Extension for search, and this takes in parameters where to search in a JSON Nested document. I was able to do this by writing a custom constraint, and with this custom constraint then parse the search text and build the query..

Can we do the same for the sort-order as well? By the way, I mean that I build the sort-order for the search options based on the parameters that get passed..

I saw in one of the examples, where the do the following

let $options :=
<search:options>
  <search:operator name="sort">
    <search:state name="relevance">
      <search:sort-order>
        <search:score/>
      </search:sort-order>
    </search:state>
    <search:state name="year">
      <search:sort-order direction="descending" type="xs:gYear"
            collation="">
        <search:attribute ns="" name="year"/>
        <search:element ns="http://marklogic.com/wikipedia"
          name="nominee"/>
      </search:sort-order>
      <search:sort-order>
        <search:score/>
      </search:sort-order>
    </search:state>
  </search:operator>
</search:options>
return
search:search("lange sort:year", $options)

but even here there are kind of hard-coding which fields to sort. I want to do something like custom-constraint, where at run time I determine the sort-order columns..

Is this possible ??

Answers


David Ennis February 2016

Well, the example you give, you mention that it's sort of hard coded. At this state, it is just XML. You can use programming logic before this step to decide elements and sort-order and build your XML accordingly. Tiny example below make the sort ascending or descending for the title element. This tiny example should help you understand what you might do on a larger scale.

let $sort-direction := "descending"

let $options :=
    <search:options>
      <search:operator name="sort">
        <search:state name="relevance">
          <search:sort-order>
            <search:score/>
          </search:sort-order>
        </search:state>
        <search:state name="year">
          <search:sort-order direction="{$sort-direction}" type="xs:gYear"
                collation="">
            <search:attribute ns="" name="year"/>
            <search:element ns="http://marklogic.com/wikipedia"
              name="nominee"/>
          </search:sort-order>
          <search:sort-order>
            <search:score/>
          </search:sort-order>
        </search:state>
      </search:operator>
    </search:options>
    return
    search:search("lange sort:year", $options)

Post Status

Asked in February 2016
Viewed 1,637 times
Voted 9
Answered 1 times

Search




Leave an answer


Quote of the day: live life