我想在MarkLogic数据库中搜索包含给定元素值对的XML文档,我想改变元素值对的大小。
这是我写的查询,以查找具有ACCOUNT" 00100"和描述为"你好"。
QueryManager queryMgr = client.newQueryManager();
StructuredQueryBuilder qryBldr = new StructuredQueryBuilder();
StructuredQueryDefinition query1 =
qryBldr.value(qryBldr.element("ACCOUNT"), "00100");
StructuredQueryDefinition query2 =
qryBldr.value(qryBldr.element("DESCRIPTION"), "hello");
StructuredQueryDefinition query = qryBldr.and(query1,query2);
SearchHandle resultsHandle = new SearchHandle();
queryMgr.search(query, resultsHandle);
我想编写一个方法,它将获取包含键值对的输入映射,它将根据映射键值对形成查询并返回匹配的文档。
示例文档如下所示:
<Tax>
<ACCOUNT>00100</ACCOUNT>
<DESCRIPTION>hello</DESCRIPTION>
<AMOUNT>100</AMOUNT>
<DATE>05252018</DATE>
</Tax>
我该如何实现?
答案 0 :(得分:1)
Java 8应该能够使用类似于以下代码的流转换地图(未经测试):
StructuredQueryDefinition query = qryBldr.and(
theInputMap
.entrySet()
.stream()
.map(entry -> qryBlder.value(
qryBldr.element(entry.getKey()), entry.getValue()
))
.toArray(size -> new StructuredQueryDefinition[size])
);
如果查询始终由键值对组成,您还可以考虑按示例查询:
http://docs.marklogic.com/guide/search-dev/qbe
在这种方法中,您只需将地图序列化为JSON - 例如,使用Jackson。
希望有帮助,
答案 1 :(得分:0)
We can use the Query By Example(QBE) method to solve this issue.
Ex: document structure
<Account>
<Name>ram</Name>
<AccountNumber>999</AccountNumber>
<Amount>100<Amount>
</Account>
We can use qbe write as below
String XMLQuery =
"<q:qbe xmlns:q='http://marklogic.com/appservices/querybyexample'>"+
"<q:query>" +
"<Name>ram</Name>" +
"</q:query>" +
"</q:qbe>";
StringHandle newHandle=new StringHandle(XMLQuery).withFormat(Format.XML);
RawQueryByExampleDefinition querydef =
queryMgr.newRawQueryByExampleDefinition(newHandle);
SearchHandle resultsHandle =
queryMgr.search(querydef, new SearchHandle());
For forming the XMLQuery from Map we can use below code:
public String getMarklogicQuery(Map<String,String> map){
StringBuffer query = new StringBuffer();
query.append("<q:qbe xmlns:q='http://marklogic.com/appservices/querybyexample'>");
query.append("<q:query>");
for(String key:map.keySet()){
query.append("<"+key+">"+map.get(key)+"</"+key+">");
}
query.append("</q:query>");
query.append("</q:qbe>");
return query.toString();
}