marklogicmarklogic-8marklogic-9

Key Value Pair Search using Marklogic Java API


I have One XML Stored in Marklogic as Mentioned Below

<employee>
    <name>Shiv</name>
    <address>.....</address>
    <phone>......</phone>
    <email>...</email>
</employee>

So my requirement is, I want to search <name> tag and its value as Shiv using marklogic java api.

I tried using KeyValueQueryDefinition and it is working fine but KeyValueQueryDefinition is now deprecated so i can't go with this.

QueryManager queryMgr = client.newQueryManager();
KeyValueQueryDefinition query = queryMgr.newKeyValueDefinition();
query.put(queryMgr.newElementLocator(new QName("name")), "Shiv");
SearchHandle resultsHandle = new SearchHandle();
queryMgr.search(query, resultsHandle);

So I need alternative to KeyValueQueryDefinition which satisfy my above requirement and also i don't want to create Element Range Index for <name> tag.


Solution

  • A value query is equivalent to a KeyValueQueryDefinition. Something along the following lines should work:

    QueryManager queryMgr = client.newQueryManager();
    StructuredQueryBuilder qryBldr = new StructuredQueryBuilder();
    StructuredQueryDefinition query = qryBldr.value(qryBldr.element("name"), "Shiv");
    SearchHandle resultsHandle = new SearchHandle();
    queryMgr.search(query, resultsHandle);
    

    See:

    http://docs.marklogic.com/javadoc/client/com/marklogic/client/query/StructuredQueryBuilder.html#value-com.marklogic.client.query.StructuredQueryBuilder.TextIndex-java.lang.String...-

    Hoping that helps,