替换 MarkLogic 中弃用的 KeyValueQueryDefinition 以使用 Query By Example
Replace Deprecated KeyValueQueryDefinition in MarkLogic to use Query By Example
我在 MarkLogic 中保存了一个文档,如下所示:
<?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>
使用已弃用 (KeyValueQueryDefinition) 的代码如下所示:
// 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();
}
}
我得到的输出是:
Matched 1 locations in user/vikram:
[vikram@gmail.com]
我从下面 link 那里得到了帮助,使其可以与新的 Marklogic 9 版本一起使用,因为 KeyValueQueryDefinition
在最新版本中已被弃用。
是否有变体来更改此处的 KeyValueQueryDefinition 代码
KeyValueQueryDefinition query = queryMgr.newKeyValueDefinition();
query.put(queryMgr.newElementLocator(new QName("emailAddress")),"vikram@gmail.com");
使用 QBE 搜索下面提到的文档 link :
关于如何解决这个问题的任何建议??
我可能没看懂你的问题。难道你不会简单地按照 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);
我在 MarkLogic 中保存了一个文档,如下所示:
<?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>
使用已弃用 (KeyValueQueryDefinition) 的代码如下所示:
// 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();
}
}
我得到的输出是:
Matched 1 locations in user/vikram:
[vikram@gmail.com]
我从下面 link 那里得到了帮助,使其可以与新的 Marklogic 9 版本一起使用,因为 KeyValueQueryDefinition
在最新版本中已被弃用。
是否有变体来更改此处的 KeyValueQueryDefinition 代码
KeyValueQueryDefinition query = queryMgr.newKeyValueDefinition();
query.put(queryMgr.newElementLocator(new QName("emailAddress")),"vikram@gmail.com");
使用 QBE 搜索下面提到的文档 link :
关于如何解决这个问题的任何建议??
我可能没看懂你的问题。难道你不会简单地按照 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);