Ravi
Ravi

Reputation: 1179

Build cts query based on search grammar

I have a use-case where in I have to write custom REST extension and in it I have to build cts:query based on the user supplied text string. Is there a way I can utilize either

Custom search constraint will not work in my case.

------------ Updated, giving more context---------------

I have two different entity types in my database. When the user queries, I have get the results from both the entity types and them combine the results.. Each entity type have a different weighting scheme for its attributes.
so I ended up writing a custom REST extension where in I do build the cts:query for each entity type and then issue multiple cts:search and then combine results.. This works but fails when user sends with Search Grammar.
As as workaround (maybe it is not).. I have an API gateway and when user issues the call, In the API gateway I issue multiple calls using out-of-box search REST API, with search options for each entity-type and then in the GATEWAY, I combine them..
Now I have to maintain logic in marklogic and one in gateway.. which is fine.. I was looking to see if I can get away having everything in Marklogic

Upvotes: 0

Views: 190

Answers (2)

ehennum
ehennum

Reputation: 7335

Consider cts:parse(), which is the modern, faster, more flexible, and more robust alternative to search:parse() for generating a cts:query in not only XQuery but SJS:

https://docs.marklogic.com/cts:parse

If you need to build a query structure on the client, one possibility is to build the serialization of cts:query using an XML or JSON DOM API on the client and to use the cts:query() constructor to parse the serialized cts:query on the enode.

You can see the XML serialization of a cts:query with:

xdmp:quote(document{...YOUR QUERY...}/*)

and the JSON serialization with:

xdmp:to-json-string(...YOUR QUERY...)

Hoping that helps,

Upvotes: 1

grtjn
grtjn

Reputation: 20414

Receiving and processing the qtext is fairly straight-forward: just use search:parse. It returns a cts:query, which can be merged with other cts queries, and passed into either search:resolve, an optic query (where you feed the cts query as argument into op:where), or into cts:search the old-fashioned way.

Processing a structured-query is more difficult. There are no official apis exposed to convert that to cts query, unfortunately..

HTH!

Upvotes: 0

Related Questions