0

I want to run the following query using MarkLogic's Java Client API but can't seem to find the correct way. The query is as follows :

xquery version "1.0-ml";

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

declare variable $searchQuery as xs:string external ;
declare variable $aggregateFunc as xs:string external ;

let $searchQuery := 'ticker:CSGN.VX AND (dateRange GE "2015-08-07" AND  dateRange LE "2015-08-21")'
let $query :=
  search:parse( $searchQuery,

    <options xmlns="http://marklogic.com/appservices/search">
      <constraint name="dateRange">
        <range type="xs:date" facet="false">
          <element ns="" name="date"/>
        </range>
      </constraint>
      <constraint name="ticker">
        <range type="xs:string" >
          <element ns="" name="ticker"/>
        </range>
      </constraint>
    </options>,

    "search:query"
  )

let $r :=
  search:values(
    'closingPrice', 

    <options xmlns="http://marklogic.com/appservices/search">
      <values name="closingPrice">
        <range type="xs:double">
          <element ns="" name="closingPrice"/>
        </range>
        <aggregate apply="avg"/>
      </values>
    </options>,

    $query
  )

return <result>{$r}</result>

I have implemented the following query using MarkLogic XCC but need to know how to implement the same using Client API.

As per discussion i formed the following query however it still doesn't return any results, let me know if something is wrong.

QueryManager queryMgr = client.newQueryManager();

ServerConfigurationManager configManager = client.newServerConfigManager();
configManager.newNamespacesManager();
QueryOptionsManager optionsMgr = configManager.newQueryOptionsManager();

optionsMgr.writeOptions(VALUE_OPTION, new StringHandle(VALUE_OPTIONS));
StringQueryDefinition qd = queryMgr.newStringDefinition(VALUE_OPTION);
qd.setCriteria("ticker:CSGN.VX AND (dateRange GE \"2015-08-07\" AND dateRange LE \"2015-08-21\")");

ValuesDefinition vdef = 
    queryMgr.newValuesDefinition("closingPrice", VALUE_OPTION);


vdef.setQueryDefinition(qd);

vdef.setAggregate("avg");

ValuesHandle results = queryMgr.values(vdef, new ValuesHandle());
StringHandle strResults=queryMgr.values(vdef, new StringHandle());

Where the value options are :

  <options xmlns="http://marklogic.com/appservices/search">
  <constraint name="dateRange">
    <range type="xs:date" facet="false">
       <element ns="" name="date"/>
    </range>
  </constraint>
  <constraint name="ticker">
    <range type="xs:string" >
       <element ns="" name="ticker"/>
    </range>
  </constraint>
<values name="closingPrice">
  <range type="xs:double">
    <element ns="" name="closingPrice"/>
  </range>
</values>
</options>
2
  • To clarify, am I right in guessing you're referring to the Java Client API? (I assume so, given that you started with XCC, but please confirm.) Commented Aug 28, 2015 at 11:03
  • Yes @DaveCassel i am reffering to Java Client API. Commented Aug 31, 2015 at 3:13

1 Answer 1

1

Ankita:

Using the Java API,

http://docs.marklogic.com/guide/java/searches#id_83836

First persist the query options that define the range constraints and the values definition:

<options xmlns="http://marklogic.com/appservices/search">
<constraint name="dateRange">
  <range type="xs:date" facet="false">
   <element ns="" name="date"/>
  </range>
</constraint>
<constraint name="ticker">
  <range type="xs:string" >
   <element ns="" name="ticker"/>
  </range>
</constraint>
<values name="closingPrice">
  <range type="xs:double">
     <element ns="" name="closingPrice"/>
  </range>
</values>
</options>

https://github.com/marklogic/java-client-api/blob/master/src/main/java/com/marklogic/client/example/cookbook/StringSearch.java#L73-L91

http://docs.marklogic.com/javadoc/client/com/marklogic/client/admin/QueryOptionsManager.html#writeOptions%28java.lang.String,%20com.marklogic.client.io.marker.QueryOptionsWriteHandle%29

Then, construct a string query for the range constraints, a values definition with the string query, and execute a values query on the values definition -- something like the following:

QueryManager queryMgr = client.newQueryManager();

StringQueryDefinition querydef = queryMgr.newStringDefinition(
    OPTIONS_NAME);
querydef.setCriteria(QUERY_TEXT);

ValuesDefinition valuesdef = queryMgr.newValuesDefinition(
    "closingPrice", OPTIONS_NAME);
valuesdef.setQueryDefinition(querydef);

// or any other handle
StringHandle readHandle =
    queryMgr.values(valuesdef, new StringHandle());

http://docs.marklogic.com/javadoc/client/com/marklogic/client/query/QueryManager.html#newStringDefinition%28java.lang.String%29

http://docs.marklogic.com/javadoc/client/com/marklogic/client/query/QueryManager.html#newValuesDefinition%28java.lang.String,%20java.lang.String%29

http://docs.marklogic.com/javadoc/client/com/marklogic/client/query/ValuesDefinition.html#setQueryDefinition%28com.marklogic.client.query.ValueQueryDefinition%29

http://docs.marklogic.com/javadoc/client/com/marklogic/client/query/QueryManager.html#values%28com.marklogic.client.query.ValuesDefinition,%20T%29

Hoping that helps,

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

3 Comments

Since I am new to Marklogic I have never tried the Values Search however i tried what you said and i failed it keeps giving me REST-INVALIDPARAM: (err:FOER0000) Invalid parameter: No configured options: <options xmlns="marklogic.com/appservices/search"><values name="closingPrice"><range type="xs:double"><element ns="" name="closingPrice"/></range><aggregate apply="avg"/></values></options> It would be really helpful if you could give me an example related to my query.
Hi @ehennum did exactly as specified by you but still doesn't seem to return the results also i set the querydef criteria to ticker:CSGN.VX AND (dateRange GE \"2015-08-07\" AND dateRange LE \"2015-08-21\") and also i set valuesDef.setAggregated to "avg" to get the average but it still doesn't return any result.
I did test the code in my answer before posting it; however, I'm not spotting any issue in your code. You might try starting simple and incrementally enhancing to the full query you want to execute to try to isolate the issue. For instance: 1) execute a string query from the Java API without using any constraint. 2) execute a values query from the Java API without any string query. 3) add the string query to the values query. 4) continue to enhance with constraints and aggregates. Hoping that helps.

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.