Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>[email protected]</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")),"[email protected]");

    // 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:
[[email protected]]

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")),"[email protected]");

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

like image 483
Vikram Avatar asked Feb 25 '26 12:02

Vikram


1 Answers

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>[email protected]</emailAddress>" +
    "</q:query>" +
  "</q:qbe>";
StringHandle rawHandle = 
    new StringHandle(rawXMLQuery).withFormat(Format.XML);
RawQueryByExampleDefinition query =
    queryMgr.newRawQueryByExampleDefinition(rawHandle);
like image 135
Sam Mefford Avatar answered Feb 28 '26 02:02

Sam Mefford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!