Yash
Yash

Reputation: 530

cts:search and search:search while dealing with json docs

My json doc looks like this:

{
  "directions": ["Heat oil in heavy... "], 
  "rating": 5, 
  "title": "Mahi-Mahi in Tomato Olive Sauce", 
  "ingredients": [
    "2 tablespoons extra-virgin olive oil", 
    "1 cup chopped onion", 
    "1 cup dry white wine", 
    "1 teaspoon anchovy paste", 
  ], 
  "sodium": null
}

When I run:

cts:search(fn:doc(),"anchovy")/title/string()

I get: Mahi-Mahi in Tomato Olive Sauce, which is desired.

But when I run:

search:search("anchovy", $options)/search:result/title/string()

I get an empty sequence. Note: I have set transform-results = "raw".

P.S I observed that search:search("anchovy", $options)/search:result gives a doc that appears to be in text format rather than json.

Is it possible to obtain the desired results using search:search in this case ?

Upvotes: 1

Views: 43

Answers (1)

Tamas
Tamas

Reputation: 11214

This should do the job:

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

declare variable $options := <options xmlns="http://marklogic.com/appservices/search"><transform-results apply="raw" /><extract-metadata>
        <json-property>title</json-property>
    </extract-metadata></options>;

search:search("anchovy", $options)//title/text()

Here we are specifying a JSON property to be extracted from the document and placed into the resultset.

As a side note, you may want to look at using ServerSide JavaScript if you're working a lot with JSON documents, and in that case you could also utilise jsearch.

Upvotes: 2

Related Questions