使用MarkLogic Java Client API

时间:2015-08-28 10:26:20

标签: marklogic

我想使用MarkLogic的Java Client API运行以下查询,但似乎无法找到正确的方法。 查询如下:

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>

我使用MarkLogic XCC实现了以下查询,但需要知道如何使用客户端API实现相同的。

根据讨论,我形成了以下查询,但它仍然没有返回任何结果,如果出现问题,请告诉我。

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());

值选项为:

  <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>

1 个答案:

答案 0 :(得分:1)

安基塔:

使用Java API,

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

首先保留定义范围约束和值定义的查询选项:

<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

然后,构造范围约束的字符串查询,使用字符串查询的值定义,并对值定义执行值查询 - 如下所示:

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

希望有帮助,