I need to create a schema which will containt few enums. I'm trying to do that using SolrJ. I've found this link DefininganEnumFieldinschema but I couldn't find any examples using Schema API or SolrJ.
Here is my enum:
public enum Attributes {
SPONSORED("sponsored"),
TOP_RATED("top-rated"),
GENERIC("generic"),
PROMOTION("promotion"),
QUICK_ORDER("quick-order");
private String value;
Attributes(String value) {
this.value = value;
}
@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}
@JsonCreator
public static Attributes fromValue(String text) {
for (Attributes b : Attributes.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}
And I want to add a field to my schema using SolrJ:
Map<String, Object> fieldAttributes = new LinkedHashMap<>();
fieldAttributes.put("name", "address.**attributes**");
fieldAttributes.put("type", "**attributesEnum**");
fieldAttributes.put("stored", true);
SchemaRequest.AddField addFieldRequest = new SchemaRequest.AddField(fieldAttributes);
addFieldRequest.process(client);
First you need to specify your enum field in schema, like in the documentation:
<fieldType name="myEnumField" class="solr.EnumField" enumsConfig="enumsConfig.xml" enumName="attribute"/>
in the enumsConfig.xml you will specify all your enum values, like:
<?xml version="1.0" ?>
<enumsConfig>
<enum name="attribute">
<value>sponsored</value>
<value>generic</value>
</enum>
</enumsConfig>
Alternatively, you could create this fieldType dynamically via Schema API as follows:
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-field-type" : {
"name":"myEnumField",
"class":"solr.EnumField",
"enumsConfig":"enumsConfig.xml",
"enumName" : "attribute"}
}' http://localhost:8983/solr/demo/schema
You could do this as well in SolrJ fashion, by using org.apache.solr.client.solrj.request.schema.SchemaRequest.AddFieldType
, you need to specify FieldTypeDefinition, method setAttributes(Map<String,Object> attributes)
will be helpful