0

我在 java 中使用 JackRabbit JCR 时很头疼。这是关于制作一个 xpath 表达式来搜索我的存储库中的实体。

让我们简要介绍一下存储什么样的数据,我们有 3 个名为“Entry”的节点类,它扩展了另一个名为“BaseEntry”的节点类,并扩展了另一个名为“BaseNode”的节点类。Entry 类代表我们 JCR 系统中的一个 Node,并具有一组属性(映射为相应类中的属性),并且还继承了映射在其超类中的属性。“BaseEntry”类聚合了许多(零个或多个)附件。这表示与条目关联的原始文件(.doc、.xls、.pdf、.txt、.etc :D)。

这些是类定义和感兴趣的属性的一部分......

“入门”课

@Node(jcrType = "entry",  extend = BaseEntry.class)
public class Entry extends BaseEntry {

  ... // nothing really important here
}

“BaseEntry”类

@Node(jcrType = "baseEntry", extend = BaseNode.class, isAbstract = true)
public abstract class BaseEntry extends BaseNode {

  @Collection (jcrType = "attachment",
      collectionConverter = NTCollectionConverterImpl.class)
  protected List<Attachment> attachments = new ArrayList<Attachment>();

  ...

}

“基节点”类

@Node(jcrType = "baseNode", isAbstract = true)
public abstract class BaseNode {

  @Field(jcrName = "name", id = true)
  protected String name;

  @Field(jcrName = "creationDate")
  protected Date creationDate;

  ...
}

“附件”类

@Node(jcrType = "attachment", discriminator = true)
public class Attachment extends BaseNode implements Comparable<Attachment> {

  /** The attachment's content resource. It cannot be null. */
  @Bean(jcrType = "skl:resource", autoUpdate = false)
  private Resource content;
  ...
}

“资源”类@Node(jcrType = "skl:resource", discriminator = true)

public class Resource extends BaseNode {

  /** Resource's MIME type. It cannot be null or empty. */
  @Field(jcrName="jcr:mimeType", jcrDefaultValue = "")
  private String mimeType;

  /** Resource's size (bytes). */
  @Field(jcrName="skl:size")
  private long size;

  /** Resource's content data as stream. It cannot be null. */
  @Field(jcrName="jcr:data")
  private InputStream data;

  ...
}

custom_nodes.xml 对这些节点具有以下定义:

<!-- Base node type definition -->
  <nodeType name="docs:baseNode"
            isMixin="false"
            hasOrderableChildNodes="false" >
    <supertypes>
      <supertype>nt:hierarchyNode</supertype>
    </supertypes>
    <propertyDefinition name="docs:name"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="true"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <propertyDefinition name="docs:searchPath"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="false"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <propertyDefinition name="docs:creationDate"
                        requiredType="Date"
                        autoCreated="false"
                        mandatory="true"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <propertyDefinition name="docs:lastModified"
                        requiredType="Date"
                        autoCreated="false"
                        mandatory="true"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <childNodeDefinition name="*"
                         defaultPrimaryType="docs:baseNode"
                         autoCreated="false"
                         mandatory="false"
                         onParentVersion="COPY"
                         protected="false"
                         sameNameSiblings="false">
      <requiredPrimaryTypes>
        <requiredPrimaryType>docs:baseNode</requiredPrimaryType>
      </requiredPrimaryTypes>
    </childNodeDefinition>
  </nodeType>



  <!-- Resource node type definition -->
  <nodeType name="skl:resource"
            isMixin="false"
            hasOrderableChildNodes="false" >
    <supertypes>
      <supertype>docs:baseNode</supertype>
      <supertype>nt:resource</supertype>
    </supertypes>
    <propertyDefinition name="skl:size"
                        requiredType="Long"
                        autoCreated="false"
                        mandatory="true"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <propertyDefinition name="skl:externalUri"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="false"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
  </nodeType>

  <!-- Attachment node type definition -->
  <nodeType name="skl:attachment"
            isMixin="false"
            hasOrderableChildNodes="false" >
    <supertypes>
      <supertype>docs:baseNode</supertype>
    </supertypes>
    <propertyDefinition name="skl:requestId"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="false"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <propertyDefinition name="skl:contentExternalUri"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="false"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <propertyDefinition name="skl:multiPagePreviewUrl"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="false"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
  </nodeType>

  <!-- Base Entry node type definition -->
  <nodeType name="skl:baseEntry"
            isMixin="false"
            hasOrderableChildNodes="false" >
    <supertypes>
      <supertype>docs:baseNode</supertype>
    </supertypes>
    <propertyDefinition name="skl:title"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="true"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <propertyDefinition name="skl:description"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="false"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <propertyDefinition name="skl:author"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="false"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <propertyDefinition name="skl:creator"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="true"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <propertyDefinition name="skl:creatorUnique"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="true"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <propertyDefinition name="skl:creatorMail"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="true"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <propertyDefinition name="skl:office"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="true"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <propertyDefinition name="skl:tags"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="false"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="true" />
  </nodeType>

  <!-- SKL Entry node type definition -->
  <nodeType name="skl:entry"
            isMixin="false"
            hasOrderableChildNodes="false" >
    <supertypes>
      <supertype>docs:baseNode</supertype>
      <supertype>skl:baseEntry</supertype>
    </supertypes>
    <propertyDefinition name="skl:languageName"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="true"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <propertyDefinition name="skl:rating"
                        requiredType="Long"
                        autoCreated="false"
                        mandatory="true"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    <propertyDefinition name="skl:urls"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="false"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="true" />
    <propertyDefinition name="skl:visitors"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="false"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="true" />
    <propertyDefinition name="skl:datePublished"
                        requiredType="String"
                        autoCreated="false"
                        mandatory="false"
                        onParentVersion="COPY"
                        protected="false"
                        multiple="false" />
    </nodeType>

所以我期待 make 和 xpath 语句来搜索那些在附件中包含某种文本的条目。所以基本上,这个想法是搜索具有包含特定文本或关键字的文件的条目。

到目前为止,我尝试使用这样的东西......

String xPathQuery = "<BASE PATH>//element(*, skl:entry) [jcr:contains(*//content,'*<keyword>*')]";
String xPathQuery = "<BASE PATH>//element(*, skl:entry) [jcr:contains(*//@jcr:data,'*<keyword>*')]";

但是这些东西,并不像你猜的那样好用……

我希望,一个慈善灵魂可以帮助我完成这个任务.. 不太好:S。感谢您提前向大家介绍如何看到这个!

问候!!

胜利者

4

1 回答 1

3

Jackrabbit 常见问题解答解释了您的问题:

为什么 //*[jcr:contains(@jcr:data, 'foo')] 不返回二进制内容的匹配项?

从二进制内容中提取的文本仅在 @jcr:data 属性的父节点上编制索引。在 nt:resource 节点上使用 jcr:contains()。

所以在你的情况下,我会使用类似的东西:

String xPathQuery = "<BASE PATH>//element(*, skl:resource)[jcr:contains(.,'*<keyword>*')]";

或者

String xPathQuery = "<BASE PATH>//element(*, skl:entry)//element(*, skl:resource)[jcr:contains(.,'*<keyword>*')]";

我也强烈反对在您的 contains 语句开头使用 * 通配符。由于 Lucene 是倒排索引,因此效率极低。

于 2012-08-13T21:08:13.347 回答