Imagine that we use Solr to store information about our environment's state, error, and events related to them—a simple solution that will work as a simple log centralization solution. For our simple use case, we will store the identifier of the message, the information, what type of event it is, and the severity of the event, showing us how important the event is. However, what we will want to be sure of is that the severity
field contains only values from a given list. To achieve all this, we will use the Solr enumeration type.
To achieve our requirements, we will have to perform the following steps:
schema.xml
file will look as follows:<field name="id" type="string" indexed="true" stored="true" required="true" /> <field name="problem" type="text_general" indexed="true" stored="true" /> <field name="severity" type="enum_type" indexed="true" stored="true" />
enum_type
to be defined. To do this, we add the following entry to the schema.xml
file:<fieldType name="enum_type" class="solr.EnumField" enumsConfig="enumsConfig.xml" enumName="severity"/>
enumsConfig.xml
file to hold our enumeration values. The content of the file will look as follows:<?xml version="1.0" ?> <enumsConfig> <enum name="severity"> <value>Ignore</value> <value>Low</value> <value>Medium</value> <value>High</value> <value>Critical</value> </enum> </enumsConfig>
<doc> <field name="id">1</field> <field name="problem">Service unavailable</field> <field name="severity">Critical</field> </doc> <doc> <field name="id">2</field> <field name="problem">Logging error</field> <field name="severity">Low</field> </doc> <doc> <field name="id">3</field> <field name="problem">Disk space low on node1</field> <field name="severity">High</field> </doc> </add>
http://localhost:8983/solr/cookbook/select?q=*:*&sort=severity+desc&fq=severity:(Critical+OR+High)
<?xml version="1.0" encoding="UTF-8"?> <response> <lst name="responseHeader"> <int name="status">0</int> <int name="QTime">1</int> <lst name="params"> <str name="q">*:*</str> <str name="indent">true</str> <str name="sort">severity desc</str> <str name="fq">severity:(Critical OR High)</str> </lst> </lst> <result name="response" numFound="2" start="0"> <doc> <str name="id">1</str> <str name="problem">Service unavailable</str> <str name="severity">Critical</str> <long name="_version_">1470159603541999616</long></doc> <doc> <str name="id">3</str> <str name="problem">Disk space low on node1</str> <str name="severity">High</str> <long name="_version_">1470159603544096769</long></doc> </result> </response>
Now, let's look at how it works.
As you can see, our index structure is rather simple. It consists of three fields; the id
field holds the document identifier, the problem
field contains event description, and the severity
field contains information about the event importance. The interesting thing is the severity
field that is defined using the new enum_type
type.
Our new enum_type
type uses solr.EnumField
as its implementation class. It also tells Solr the enumeration value definition files that should be used (in our case, it is enumsConfig.xml
stored in the same directory as the rest of the configuration). Finally, we have the enumName
property, which tells Solr which section of the mentioned enumsConfig.xml
file to use.
The enumsConfig.xml
file stores our enumeration values. The root tag called <enumsConfig>
can store multiple <enum>
entries, each defined with the name
property. The value of the name
property of the <enum>
tag should be the same as the value of the enumName
property in our previously defined type. It allows us to handle multiple enumeration types in a single configuration file.
Now, if we look at our data, we can see that the values of the document severity
field contain one of the values defined in the enumsConfig.xml
file. If we try to index the document with a value that is not present in the configuration, Solr will throw an error and reject the document. Also, remember that when changing the enumsConfig.xml
file, you should reindex your data.
Finally, the query shows that the field using the new enumeration type can be used for querying, filtering, and sorting. The sort order will depend on the order of the enumeration value definitions.
18.191.206.196