2
    ....
    StringHandle rawHandle = new StringHandle(jsonString);
    RawQueryByExampleDefinition querydef = queryManager.newRawQueryByExampleDefinition(rawHandle);
    querydef.setCollections(collection);
    StringHandle report = queryManager.validate(querydef, new StringHandle());

    LOGGER.info("Query Def valididity: {}",report.toString());

    StringHandle resultsHandle = new StringHandle().withFormat(Format.JSON);
    queryManager.setPageLength(size);
    queryManager.search(querydef, resultsHandle, startIndex);
    ....

I'm using the code above for MarkLogic search Query By Example, my question is how could you pass in a "sort by" criteria into the RawQueryByExampleDefinition to sort or order the resultset. For example I want the result to be sorted by emailAddress similar to the below query:

{
 "$query":
   {
      "identifier":"user",
      "$sort-by":"emailAddress"
   }
}

How do I achieve the "sortby" as well as specifying desc or asc?

1 Answer 1

1

I think that would be done using a "combined query" from https://docs.marklogic.com/guide/java/searches#id_76144 with the sort-order option from https://docs.marklogic.com/search:search

Sign up to request clarification or add additional context in comments.

3 Comments

How do you add the option (sort-by) to the QBE above in JSON format? I want to pass in the query as follows: { "$query": { "identifier":"user" } } and use the Java code in my above post. How would I add the sort-by options to that JSON string query?
As stated I think you have to use a combined query not QBE. You can transform a QBE into a combined query: docs.marklogic.com/guide/java/searches#id_51859 — and docs.marklogic.com/guide/rest-dev/search#id_84389 shows the syntax of the query and options together.
I'm not sure how to add the options after transforming my QBE to a combined query... Do I have to recreate the whole String again and insert the wanted sort options? This is a bit painful to do considering that the sortby values can be any attribute of my object... Is there an easier way to do this retaining the QBE approach because this is how I implemented my search endpoint - client/users will pass in a QBE and they want to sort it based on the attribute/s they like to input...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.