Using dynamic fields

Using dynamic fields, you can index content in fields that are not explicitly defined by the schema.

Using dynamic fields, you can index content in fields that not explicitly defined by the schema. Using dynamic fields, you can also process multiple Solr fields the same way by using a generic prefix or suffix to reference the field. A common use case for dynamic fields is to catch fields that should not be indexed or to implement a schema-less index. As previously mentioned, in CQL-backed Solr cores Solr schema fields that are dynamic and multivalued are not supported.

To use a dynamic field:

  • Include a Solr dynamic field in the schema.xml.

    Name the field using wildcard at the beginning or end of the field. For example, an asterisk prefix or suffix in the field name in the schema designates a dynamic field.

    • dyna_*
    • *_s
  • In CQL, to define the map collection column, use the same base name (no asterisk) as you used for the field in the schema.xml.

    For example, use dyna_* in the schema.xml and dyna_ for the name of the CQL map colleciton.

  • Using CQL, insert data into the map using the base name as a prefix or suffix in the first component of each map pair. The format of the map using a prefix is:

    { prefix_literal : literal, prefix_literal : literal, . . . }

    DSE Search maps the Solr dynamic field to a Cassandra map collection column

Example: A multilingual music database 

Procedure

This example demonstrates how to use dynamic fields to organize song data based on language into a collection map.

  1. Use the keyspace from the collection set example.
  2. In cqlsh, create the following table:
    CREATE TABLE hits (
      song uuid,
      lang_ map<text, text>,
      PRIMARY KEY (song)
    );
  3. If you did not already create a directory named solr_CQL3tutorial that contains a schema.xml and solrconfig.xml, do so now. You can use the schema.xml and solrconfig.xml from the demos/wikipedia directory by copying these files to solr_CQL3tutorial.
  4. Change the schema.xml in the solr_CQL3tutorial directory to contain this schema:
    <schema name="topHits" version="1.1">
      <types>
        <fieldType name="uuid" class="solr.UUIDField" />
        <fieldType name="text" class="solr.TextField">
        </fieldType>
      </types>
      <fields>
        <field name="song" type="uuid" indexed="true" stored="true"/>
        <dynamicField name="lang_*" type="text" indexed="true" stored="true"/>
      </fields>
      <defaultSearchField>song</defaultSearchField>
      <uniqueKey>song</uniqueKey>
    </schema>
  5. Create a search index using the cURL utility. On the operating system command line in the solr_CQL3tutorial directory, post the configuration file, the schema file, and create a Solr core.
    curl http://localhost:8983/solr/resource/mykeyspace.hits/solrconfig.xml --data-binary @solrconfig.xml -H 'Content-type:text/xml; charset=utf-8'
    
    curl http://localhost:8983/solr/resource/mykeyspace.hits/schema.xml --data-binary @schema.xml -H 'Content-type:text/xml; charset=utf-8'
    
    curl "http://localhost:8983/solr/admin/cores?action=CREATE&name=mykeyspace.hits"
    
  6. Using CQL, insert the following data about Italian and Hawaiian songs into the hits table. Use the lang_ to prefix the first component of each map pair.
    INSERT INTO hits (song, lang_) VALUES ( 62c36092-82a1-3a00-93d1-46196ee77204, { 'lang_i-title'  : 'La Vita E La Felicita', 'lang_i-artist' : 'Michele Bravi' });
    INSERT INTO hits (song, lang_) VALUES ( 8a172618-b121-4136-bb10-f665cfc469eb, { 'lang_h-title'  : 'Blew it', 'lang_h-artist' : 'Maoli f/ Fiji' });
    INSERT INTO hits (song, lang_) VALUES ( a3e64f8f-bd44-4f28-b8d9-6938726e34d4, { 'lang_i-title'  : 'Dimmi Che Non Passa Felicita', 'lang_i-artist' : 'Violetta' });
  7. To find data about hit songs in Italy, query on either of the prefixed map literals, lang_i-title or lang_i-artist.
    http://localhost:8983/solr/mykeyspace.hits/select?q=lang_i-title%3A*&wt=xml&indent=true

    Results are:

    <result name="response" numFound="2" start="0">
      <doc>
        <str name="song">62c36092-82a1-3a00-93d1-46196ee77204</str>
        <str name="lang_i-artist">Michele Bravi</str>
        <str name="lang_i-title">La Vita E La Felicita</str.</doc>
      <doc>
        <str name="song">a3e64f8f-bd44-4f28-b8d9-6938726e34d4</str>
        <str name="lang_i-artist">Violetta</str>
        <str name="lang_i-title">Dimmi Che Non Passa Felicita</str></doc>
    </result>