ngeek
ngeek

Reputation: 7893

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

With MarkLogic server (version 8), I am trying to define a field configuration (suggest) which should use the contents/values of a JSON array (descriptions), the definition for the database fields configuration looks like:

        <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>

And a sample document instance:

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

Unfortunately the content seems not to be indexed, and therefore no results are given back for suggest queries. Please advise on how you have to define the configuration to index the JSON array values.

The following sections in the documentation I read, but could not find info on this specific topic:

Upvotes: 3

Views: 260

Answers (1)

mblakele
mblakele

Reputation: 7842

Try adding a field range index on that field.

The field itself is already indexed, but only as a hash index for word and value queries. For search suggestions you want a range index, which stores a list of values rather than hashes. This is sometimes called a value lexicon.

For example I added your suggest field and a field range index on suggest as string, with the unicode codepoint collation. The config XML looks like:

<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>

I used the codepoint collation out of habit, but you could use whatever collation you prefer. That range index can be tested in XQuery: the collation in the XQuery must match the configuration.

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

That produces a sequence of string values:

ipsum
lorem

Assuming you're using the REST API, you should be able to do something similar with the /suggest endpoint.

Upvotes: 4

Related Questions