javamarklogicquery-by-examplemarklogic-9

Replace Deprecated KeyValueQueryDefinition in MarkLogic to use Query By Example


I have a document saved in MarkLogic like below :

<?xml  version="1.0" encoding="UTF-8"?>
<user>
  <userName>Vikram</userName>
  <password>password</password>
  <firstName>Vikram</firstName>
  <lastName>Swaminathan</lastName>
  <emailAddress>vikram@gmail.com</emailAddress>
</user>

The code that works with deprecated (KeyValueQueryDefinition) is like below :

    // create a manager for searching
    QueryManager queryMgr = client.newQueryManager();

    KeyValueQueryDefinition query = queryMgr.newKeyValueDefinition();
    query.put(queryMgr.newElementLocator(new QName("emailAddress")),"vikram@gmail.com");

    // create a handle for the search results
    SearchHandle resultsHandle = new SearchHandle();

    // run the search
    queryMgr.search(query, resultsHandle);        

    // Get the list of matching documents in this page of results
    MatchDocumentSummary[] results = resultsHandle.getMatchResults();

    // Iterate over the results
    for (MatchDocumentSummary result: results) {

        // get the list of match locations for this result
        MatchLocation[] locations = result.getMatchLocations();
        System.out.println("Matched "+locations.length+" locations in "+result.getUri()+":");

        // iterate over the match locations
        for (MatchLocation location: locations) {

            // iterate over the snippets at a match location
            for (MatchSnippet snippet : location.getSnippets()) {
                boolean isHighlighted = snippet.isHighlighted();

                if (isHighlighted)
                    System.out.print("[");
                System.out.print(snippet.getText());
                if (isHighlighted)
                    System.out.print("]");
            }
            System.out.println();
        }
        System.out.println();
    }
}

and the output i am getting is :

Matched 1 locations in user/vikram:
[vikram@gmail.com]

I took the help from the below link to make it work with the new Marklogic 9 version as KeyValueQueryDefinition is deprecated on the latest version.

https://docs.marklogic.com/guide/relnotes/chap4#id_22988

Is there a Variant to change the KeyValueQueryDefinition code here

 KeyValueQueryDefinition query = queryMgr.newKeyValueDefinition();
        query.put(queryMgr.newElementLocator(new QName("emailAddress")),"vikram@gmail.com");

to use a QBE to search the documents as mentioned here in the below link :

https://docs.marklogic.com/guide/java/searches#id_69351

Any Suggestions on how to approach this ??


Solution

  • I may not understand the question. Wouldn't you simply follow the instructions in the link you provided?

    String rawXMLQuery =
      "<q:qbe xmlns:q='http://marklogic.com/appservices/querybyexample'>"+
        "<q:query>" +
          "<emailAddress>vikram@gmail.com</emailAddress>" +
        "</q:query>" +
      "</q:qbe>";
    StringHandle rawHandle = 
        new StringHandle(rawXMLQuery).withFormat(Format.XML);
    RawQueryByExampleDefinition query =
        queryMgr.newRawQueryByExampleDefinition(rawHandle);