Creating EnumType Solr using SolrJ

你说的曾经没有我的故事 提交于 2019-12-31 04:24:26

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/47308076/creating-enumtype-solr-using-solrj

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!