What is Dynamic Field?
If you cannot confirm the fields for your collection initially, or the fields of your data source may change in the future, you can use pre-defined dynamic fields. You can also define some specified for your data source. Let’s check the C:/{your_installed_path}/solr-5.4.1/server/solr/films/conf/managed-schema.xml , you can some pre-defined dynamic fields with tag “<dynamicField>”, such as:
<dynamicField name=”attr_*” type=”text_general” multiValued=”true” indexed=”true” stored=”true”/>
<dynamicField name=”*_txt” type=”text_general” indexed=”true” stored=”true”/>
Let’s try indexing this article, and see what is happening:
{
“id”: “/en/02_2016”,
“directed_by”: [“Testing”],
“initial_release_date”: “2016-02-30”,
“genre”: [“Sci”,”Action film”],
“actors”: [“Peter”,”John”],
“attr_testing”: [“dynamic”,”field”,”testing”],
“testing_txt”: “testing movie”,
“name”: “Testing”
}
Now, try to search this article by using these two fields: http://localhost:8983/solr/films/select?q=testing_txt:testing&wt=json&indent=true or http://localhost:8983/solr/films/select?q=attr_testing:testing&wt=json&indent=true , it will return you below record.
You may find that this article has a field called “artors”, it is not defined field or dynamic field. So, what will happen to this field? SOLR can dynamically add new field if it is new when indexing a new article, and it will update the schema automatically without restart. You can check again your C:/{your_installed_path}/solr-5.4.1/server/solr/films/conf/managed-schema.xml now, you will find:
<field name=”actors” type=”strings”/>
has been added.
Let’s try searching this article by using “actors” field:
http://localhost:8983/solr/films/select?q=actors:Peter&wt=json&indent=true , it will return you:
SOLR will base on your field data, assigning the related field type to the new field. Please try indexing this article:
{
“id”: “/en/03_2016”,
“directed_by”: [“Testing3”],
“initial_release_date”: “2016-02-29”,
“genre”: [“Sci”,”Action film”],
“testfield1”: [1,2,3,4,9,15],
“testfield2”: “2016-02-29”,
“testfield3”: [“testing1″,”testing2″,”testing3”],
“name”: “Testing”
}
and check managed-schema.xml now, you will find that the below fields have been added:
<field name=”testfield1″ type=”tlongs”/>
<field name=”testfield2″ type=”tdates”/>
<field name=”testfield3″ type=”strings”/>
You see… with this feature, you don’t need to worry about the SOLR schema initially.