如何使用 MarkLogic 服务器基于 JSON 数组指定数据库字段配置?

How to specify a database fields configuration based on a JSON array with MarkLogic server?

使用 MarkLogic 服务器(版本 8),我试图定义一个字段配置(suggest),它应该使用 JSON 数组的 contents/values(descriptions),数据库字段配置的定义如下:

        <fields>
            <field>
                <field-name>suggest</field-name>
                <include-root>false</include-root>
                <stemmed-searches>advanced</stemmed-searches>
                <word-searches>true</word-searches>
                <field-value-searches>true</field-value-searches>
                <field-value-positions>true</field-value-positions>
                <trailing-wildcard-searches>true</trailing-wildcard-searches>
                <trailing-wildcard-word-positions>true</trailing-wildcard-word-positions>
                <three-character-searches>true</three-character-searches>
                <three-character-word-positions>true</three-character-word-positions>
                <word-lexicons>
                    <word-lexicon>http://marklogic.com/collation/</word-lexicon>
                </word-lexicons>
                <included-elements>
                    <included-element>
                        <namespace-uri/>
                        <localname>descriptions</localname>
                        <weight>1.0</weight>
                        <attribute-namespace-uri/>
                        <attribute-localname/>
                        <attribute-value/>
                    </included-element>
                </included-elements>
                <excluded-elements/>
                <tokenizer-overrides/>
            </field>
        </fields>

以及示例文档实例:

{
   "descriptions": ["lorem", "ipsum"]
}

不幸的是,内容似乎没有被索引,因此没有返回建议查询的结果。请告知如何定义配置以索引 JSON 数组值。

我阅读了文档中的以下部分,但找不到关于此特定主题的信息:

在该字段上尝试 adding a field range index

该字段本身已经编入索引,但仅作为单词和值查询的哈希索引。 For search suggestions you want a range index,它存储值列表而不是散列。这有时被称为价值词典。

例如,我添加了您的 suggest 字段和 suggest 上的字段范围索引作为字符串,使用 unicode 代码点排序规则。配置 XML 如下所示:

<range-field-indexes>
  <range-field-index>
    <scalar-type>string</scalar-type>
    <collation>http://marklogic.com/collation/codepoint</collation>
    <field-name>suggest</field-name>
    <range-value-positions>false</range-value-positions>
    <invalid-values>reject</invalid-values>
  </range-field-index>
</range-field-indexes>

出于习惯,我使用代码点排序规则,但您可以使用您喜欢的任何排序规则。可以在 XQuery 中测试该范围索引:XQuery 中的排序规则必须 与配置匹配。

cts:values(
  cts:field-reference(
    'suggest', 'collation=http://marklogic.com/collation/codepoint'))

生成字符串值序列:

ipsum
lorem

假设您使用的是 REST API,您应该能够使用 /suggest endpoint.

执行类似的操作