`
Rocky_rup
  • 浏览: 143496 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Jackrabbit 索引配置说明

阅读更多

从1.4版本之后,jackrabbit开始支持节点的属性索引配置.换句话说,我们可以通过配置决定哪种类型节点的哪些属性应该建立索引.这样一来,我们就能对索引内容进行优化,使得搜索更为高效.

索引配置文件也是一个XML,默认名字为indexing_configuration.xml.默认情况下,jackrabbit不会有索引配置的存在,因此要想使用它就必须在repository.xml和workspace.xml中,为SearchIndex元素添加一个参数.

示例:

<param name="indexingConfiguration" value="${wsp.home}/indexing_configuration.xml"/>

上例中,value指明了配置文件存在的相对路径,它与workspace.xml位于同一个路径下.

接下来,看看哪些问题可以通过索引配置解决.

问题1 如何只给特定的属性建立索引?

示例:

<?xml version="1.0"?>

<!DOCTYPE configuration SYSTEM "http://jackrabbit.apache.org/dtd/indexing-configuration-1.0.dtd">

<configuration xmlns:nt="http://www.jcp.org/jcr/nt/1.0">

  <index-rule nodeType="nt:unstructured">

    <property>Text</property>

  </index-rule>

</configuration>

上例中,索引规则定义了nt:unstructured节点类型下,名为Text的属性需要建立索引.这条规则对nt:unstructured子类型也有效.

问题2 如何调整特定类型或属性的查询相关度?

示例:

<?xml version="1.0"?>

<!DOCTYPE configuration SYSTEM "http://jackrabbit.apache.org/dtd/indexing-configuration-1.0.dtd">

<configuration xmlns:nt="http://www.jcp.org/jcr/nt/1.0">

  <index-rule nodeType="nt:unstructured" boost="2.0">

    <property>Text</property>

  </index-rule>

</configuration>

上例中,指明nt:unstructured类型的节点的激进因子(boost)为2.0,默认值是1.0,当按jcr:score进行降序排序结果集时,boost值越高位置越考前(也可能有例外,关于score的计算方法请见这里).boost值的有效范围是1.0~5.0.

若想在特定属性上指定boost参数也是可以的.

示例:(为了节省空间,下文的示例中将省略一些不必要的内容)

  <index-rule nodeType="nt:unstructured">

    <property boost="3.0">Title</property>

    <property boost="1.5">Text</property>

  </index-rule>

问题3 上面的规则不错,但只能将规则指定给某个类型,有没有比类型更具体的指定方式呢?

借助condition值的定义实现更具体的方式,看下面示例:

  <index-rule nodeType="nt:unstructured"

              boost="2.0"

              condition="@priority = 'high'"> 

    <property>Text</property>

  </index-rule>

priority属性等于high的nt:unstructured节点,它的激进因子设置为2.0,且只有名为Text的属性会被建立索引.

注意,目前针对属性的条件,仅支持等号操作符和字符串类型.

条件也可以不在当前节点上,示例:

<index-rule nodeType="nt:unstructured"

            boost="2.0"

            condition="ancestor::*/@priority = 'high'">

  <property>Text</property>

</index-rule>

<index-rule nodeType="nt:unstructured"

            boost="0.5"

            condition="parent::foo/@priority = 'low'">

  <property>Text</property>

</index-rule>

<index-rule nodeType="nt:unstructured"

            boost="1.5"

            condition="bar/@priority = 'medium'">

  <property>Text</property>

</index-rule>

当有多规则应用于统一个类型时,最先满足条件的那个规则会被应用,其余的将被忽略.
在条件中也是可以指定节点类型的,但类型的匹配必须是精确的(exact),也就是不支持类型的子类.
示例:
  <index-rule nodeType="nt:unstructured"

              boost="2.0"

              condition="element(*, nt:unstructured)/@priority = 'high'">

    <property>Text</property>

  </index-rule>
问题4 有些属性的值(文本)不需要做全文索引,该怎么办?
默认情况下,凡STRING类型的属性以及其它可以抽取文本的属性都是进行全文索引的.
这就意味着,你可以通过类似jcr:contains(., 'foo')的条件查找到含有foo关键的文本所在的位置.
有时,类似省市地区这样的属性,就没有必要做全文索引,那么可以通过下面的方式禁用全文索引:
  <index-rule nodeType="nt:unstructured">

    <property nodeScopeIndex="false">Text</property>

  </index-rule>
问题5 可不可以用正则表达来匹配要索引的属性?
jackrabbit 1.5支持这样的特性,同时要确保使用DTD1.1的版本.
示例:
<?xml version="1.0"?>

<!DOCTYPE configuration SYSTEM "http://jackrabbit.apache.org/dtd/indexing-configuration-1.1.dtd">

<configuration xmlns:nt="http://www.jcp.org/jcr/nt/1.0">

  <index-rule nodeType="nt:unstructured">

    <property isRegexp="true">.*Text</property>

  </index-rule>

</configuration>
在属性上将useInExcerpt设置为false,可以禁用属性的文本摘要(excerpt)特性,这也是1.5的默认特性.
问题6 很多时候用nt:resource存储文件的内容,但全文查询的时候却只能查到它,而不是它的父节点,怎么办?
<?xml version="1.0"?>

<!DOCTYPE configuration SYSTEM "http://jackrabbit.apache.org/dtd/indexing-configuration-1.0.dtd">

<configuration xmlns:jcr="http://www.jcp.org/jcr/1.0"

               xmlns:nt="http://www.jcp.org/jcr/nt/1.0">

  <aggregate primaryType="nt:file">

    <include>jcr:content</include>

  </aggregate>

</configuration>

通过配置aggregate元素来解决这个问题.上例中,就指明了nt:filejcr:content子节点的索引会指向nt:file.

当然,还可以限定子节点的类型,并用"*"匹配所有子节点,示例:

<?xml version="1.0"?>

<!DOCTYPE configuration SYSTEM "http://jackrabbit.apache.org/dtd/indexing-configuration-1.0.dtd">

<configuration xmlns:jcr="http://www.jcp.org/jcr/1.0"

               xmlns:nt="http://www.jcp.org/jcr/nt/1.0">

  <aggregate primaryType="nt:file">

    <include primaryType="nt:resource">*</include>

  </aggregate>

</configuration>

还可以通过深度(相对路径)来匹配多层子节点,示例:

<?xml version="1.0"?>

<!DOCTYPE configuration SYSTEM "http://jackrabbit.apache.org/dtd/indexing-configuration-1.0.dtd">

<configuration xmlns:jcr="http://www.jcp.org/jcr/1.0"

               xmlns:nt="http://www.jcp.org/jcr/nt/1.0">

  <aggregate primaryType="nt:file">

    <include>*</include>

    <include>*/*</include>

    <include>*/*/*</include>

  </aggregate>

</configuration>
问题7 不同属性的文本值索引的分析算法不同,该怎么办?
<?xml version="1.0"?>

<!DOCTYPE configuration SYSTEM "http://jackrabbit.apache.org/dtd/indexing-configuration-1.0.dtd">

<configuration xmlns:nt="http://www.jcp.org/jcr/nt/1.0">

  <analyzers> 

        <analyzer class="org.apache.lucene.analysis.KeywordAnalyzer">

            <property>mytext</property>

        </analyzer>

        <analyzer class="org.apache.lucene.analysis.WhitespaceAnalyzer">

            <property>mytext2</property>

        </analyzer>

  </analyzers> 

</configuration>

上文的内容均参考至http://wiki.apache.org/jackrabbit/IndexingConfiguration

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics