Given a json-property in a document, I need to fetch all the distinct values that are present for that property across all the documents present in the collection. Is this possible using the Marklogic Java Client API?
For e.g., I have 3 documents of type "MyDocument" with property "myProperty" as -
MyDocument1.json
{
"myProperty":"val1"
}
MyDocument2.json
{
"myProperty":"val1"
}
MyDocument3.json
{
"myProperty":"val2"
}
I want to search all the distinct values for "myProperty", i.e., the result should be "val1" and "val2".
And also if I can group the documents by those distinct values. E.g.,
{
"val1": [
{
"myProperty":"val1"
},
{
"myProperty":"val1"
}
],
"val2": [
{
"myProperty":"val2"
}
]
}
I'd appreciate any help or nudge in the right direction. Thanks in advance!
Update:
I was able to use the qConsole to get the result I was looking for, but using XQuery. Here's what I did -
xquery version "1.0-ml";
import module namespace search = "http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
let $options :=
<options xmlns="http://marklogic.com/appservices/search">
<values name="myProperty">
<range type="string" facet="true">
<json-property>myProperty</json-property>
</range>
</values>
</options>
return search:values("myProperty", $options)
And the result I got was -
<search:values-response name="myProperty" type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:search="http://marklogic.com/appservices/search">
<search:distinct-value frequency="2">val1</search:distinct-value>
<search:distinct-value frequency="1">val2</search:distinct-value>
</search:values>
Now I need to achieve this using the Java Client API.