简体   繁体   English

使用Marklogic的Java搜索API和XQuery / XSLT API对XPath进行搜索

[英]Document XPath searching with Marklogic's Java search API vs XQuery/XSLT API

I have the following Marklogic query that, when run in the query console, allows me to retrieve my system user's who have admin privileges: 我有以下Marklogic查询,当在查询控制台中运行时,允许我检索具有管理员权限的系统用户:

xquery version "1.0-ml";
import schema namespace bfa="http://bitfood.org/auth" at "schema/auth/bitfood-auth.xsd";
cts:search(/bfa:AppUser[bfa:appAccess/@appRole = "ROLE_SYS_ADMIN"], cts:and-query(()))

Pardon my ignorance, but is it possible to implement this query using the Java client API only? 请原谅我的无知,但是是否可以仅使用Java客户端API实现此查询?

I know I could use the raw query via XCC but I'm trying to avoid that as much as possible. 我知道我可以通过XCC使用原始查询,但我尽量避免这种情况。

I've been digging through the Java client API's documentation which, unfortunately, deals briefly with other search methods and have found nothing hinting that this is possible. 我一直在深入研究Java客户端API的文档,遗憾的是,这些文档简要介绍了其他搜索方法,并没有发现任何暗示这是可能的。

UPDATE 1: Guys, I think I've hit a showstopper here. 更新1:伙计们,我想我在这里打了一场比赛。

According to this question , the Query Options building facilities of the Java client API are marked as deprecated. 根据此问题 ,Java客户端API的查询选项构建工具被标记为已弃用。

@ehennum suggests an alternative which uses either XML or JSON query options. @ehennum建议使用XML或JSON查询选项的替代方案。 However, such query option specifications are still essentially defined as raw Strings in the code : 但是,此类查询选项规范仍然在代码中基本上定义为原始字符串

String xmlOptions =
  "<search:options "+
        "xmlns:search='http://marklogic.com/appservices/search'>"+
      "<search:constraint name='industry'>"+
        "<search:value>"+
          "<search:element name='industry' ns=''/>"+
        "</search:value>"+
      "</search:constraint>"+
  "</search:options>";

Granted, I could use JAXB, JDOM, files or some other tool to create the XML, but my personal opinion is that we are still resorting to storing queries in resource files in the code , which is not a bad thing in itself, but without careful consideration could lead to code maintenance nightmares. 当然,我可以使用JAXB,JDOM,文件或其他工具来创建XML,但我的个人意见是我们仍然依赖于在代码中的资源文件中存储查询 ,这本身并不是坏事,但没有仔细考虑可能导致代码维护噩梦。

Furthermore, the fact that these options need to be persisted via REST at the server introduces one more layer of potential problems in case this is mandatory since the options might need to be synchronized with every database instance that is meant to work with a code base. 此外,这些选项需要通过服务器上的REST持久化这一事实引入了另外一层潜在问题,以防这是必需的,因为选项可能需要与每个用于代码库的数据库实例同步。

So, if the Marklogic devs are listening, I think the Java client API isn't as mature or documented as I expected it to be. 因此,如果Marklogic开发人员正在监听,我认为Java客户端API并不像我预期的那样成熟或记录。

I have two options so far: 到目前为止我有两个选择:

1) I could hardcode my XCC queries in String files and try to use parameter placeholders to insert the data that I need and retrieve via an XCC session. 1)我可以在String文件中硬编码我的XCC查询,并尝试使用参数占位符来插入我需要的数据并通过XCC会话检索。 Or. 要么。

2) See if there are any XSD files for the http://marklogic.com/appservices/search namespace and create static JAXB objects from them in order to build some sort of "criteria" classes a la Hibernate or QueryDSL which, sadly I must say, already supports some level of MongoDB querying. 2)查看http://marklogic.com/appservices/search命名空间是否有任何XSD文件,并从中创建静态JAXB对象,以构建某种“标准”类, 即HibernateQueryDSL ,遗憾的是我必须说,已经支持某种级别的MongoDB查询。

Guys, really, bring some sort of fluent querying facilities like QueryDSL to Marklogic and get rid of this Rube Goldberg querying machine . 伙计们,真的,带来一些流利的查询设施,如QueryDSL到Marklogic,并摆脱这个Rube Goldberg查询机器

Update 2: Seems as if this might be addressed in ML7. 更新2:似乎可以在ML7中解决这个问题。 Looking forward to it. 期待它。

Thanks! 谢谢!

Background: the Java API is a layer on the REST API is a layer on the Search API is a layer on cts:search 背景: Java API是REST API上的一个层,是Search API上的一个层,是cts:search上的一个层

You can express this query in the Search API. 您可以在Search API中表达此查询。

Using the Java API, you can search using the query options and structured query as a RawCombinedQueryDefinition. 使用Java API,您可以使用查询选项和结构化查询作为RawCombinedQueryDefinition进行搜索。

All that said, you could just also search for an element-query on bfa:AppUser containing a value constraint query on bfa:appAccess/@appRole of "ROLE_SYS_ADMIN" 总而言之,您还可以在bfa上搜索元素查询:AppUser在bfa上包含值约束查询:appAccess / @ appRole“ROLE_SYS_ADMIN”

While XPath can be convenient, it's a good idea to become conversant in query expressions to understand the full power and flexibility of the database. 虽然XPath很方便,但最好是熟悉查询表达式,以了解数据库的全部功能和灵活性。

Update 1: 更新1:

A few things to consider: 需要考虑的一些事项:

  • A query in the Search API has two parts: the query (expressed either with Google-style string search or with JSON or XML structured search) and the query options. Search API中的查询包含两部分:查询(使用Google风格的字符串搜索或使用JSON或XML结构化搜索表示)和查询选项。 The StructuredQueryBuilder builds structured search. StructuredQueryBuilder构建结构化搜索。 The QueryOptionsBuilder only built the query options. QueryOptionsBuilder仅构建了查询选项。

  • In ML 6.0-3, the REST API introduced support for combined search, which provides both parts in a single request. 在ML 6.0-3中,REST API引入了对组合搜索的支持,组合搜索在单个请求中提供了两个部分。 The Java API added support for such requests through the RawCombinedQueryDefinition class. Java API通过RawCombinedQueryDefinition类添加了对此类请求的支持。

  • ML7 is expanding structured search to reduce or eliminate the need for query options with structured search. ML7正在扩展结构化搜索,以减少或消除对结构化搜索的查询选项的需求。 StructuredQueryBuilder is enhanced in ML7 to support the new features of structured search. StructuredQueryBuilder在ML7中得到增强,以支持结构化搜索的新功能。 In ML7, you'll be able to write your example query above entirely in StructuredQueryBuilder without any need for query options. 在ML7中,您将能够在StructuredQueryBuilder中完全编写上面的示例查询,而无需任何查询选项。

  • When we compared the code constructing query options with QueryOptionsBuilder and with JDOM or XOM, it was hard to see much LOC benefit in the builder. 当我们将构造查询选项的代码与QueryOptionsBuilder以及JDOM或XOM进行比较时,很难在构建器中看到很多LOC优势。

  • You don't have to hardcode query options in a string. 您不必在字符串中对查询选项进行硬编码。 For separation of concerns, you can read JSON or XML query options from a file or from many other sources. 为了分离关注点,您可以从文件或许多其他来源读取JSON或XML查询选项。 (Please see the implementations of the http://docs.marklogic.com/javadoc/client/com/marklogic/client/io/marker/QueryOptionsReadHandle.html marker interface.) (请参阅http://docs.marklogic.com/javadoc/client/com/marklogic/client/io/marker/QueryOptionsReadHandle.html标记界面的实现。)

  • Query options provide a declaration instead of executable code. 查询选项提供声明而不是可执行代码。 The parallel would be with Spring configuration files rather than with SPL files. 并行将使用Spring配置文件而不是SPL文件。

BTW, while JAXB is great for designs sourced in Java classes, JAXB can be quite painful for designs sourced in a complex XML schema. 顺便说一句,虽然JAXB非常适合源自Java类的设计,但JAXB对于采用复杂XML模式的设计来说非常痛苦。 The search schema takes advantage of the powerful capabilities of XML schemas. 搜索模式利用了XML模式的强大功能。 When we explored this route, we concluded that JAXB was not not going to help provide an interface to query options. 当我们探索这条路线时,我们得出结论,JAXB并不会帮助提供查询选项的接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM