Adding user defined types in SOLR schema

2019-08-06 04:21发布

问题:

Iam using SOLR as my search query database. My current requirement is to post this Document on SOLR database.

namespace SOLRApp
{
    public class Table1Document
    {
        public Table1Document()
        {
            MobileNos = new List<int>();
            EducationalDetails = new List<EducationalDetails>();
        }

        [SolrUniqueKey("ID")]
        public string ID { get; set; }

        [SolrField("FirstName")]
        public string FirstName { get; set; }

        [SolrField("LastName")]
        public string LastName{ get; set; }

        [SolrField("MobileNos")]
        public List<int> MobileNos { get; set; }

        [SolrField("EducationalDetails")]
        public List<EducationalDetails> EducationalDetails { get; set; }

    }

    public class EducationalDetails
    {
        public EducationalDetails()
        {
            Details = new List<Details>();
        }

        public List<Details> Details { get; set; }
    }

    public class Details
    {
        [SolrField("IntitutionName")]
        public string IntitutionName { get; set; }

        [SolrField("EnrollDate")]
        public DateTime EnrollDate { get; set; }

        [SolrField("PassoutDate")]
        public DateTime PassoutDate { get; set; }

        [SolrField("InstituteRating")]
        public int InstituteRating { get; set; }
    }
}

And for that I have added this schema file with field as follows.

        <field name="ID" type="int" indexed="true" stored="true"/>
        <field name="FirstName" type="string" indexed="true" stored="true"/>
        <field name="LastName" type="string" indexed="true" stored="true" />
        <field name="MobileNos" type="string" indexed="true" stored="true" multiValued="true" />
        <field name="EducationalDetails" type="EducationalDetails" indexed="true" stored="true" multiValued="true">
            <field name="Details" type="string" indexed="true" stored="true" multiValued="true">
                <field name="IntitutionName" type="string" indexed="true" stored="true"/>
                <field name="EnrollDate" type="date" indexed="true" stored="true"/>
                <field name="PassoutDate" type="date" indexed="true" stored="true"/>
                <field name="InstituteRating" type="string" indexed="true" stored="true"/>
            </field>
        </field>

I would like to know how can we add user defined datatypes in SOLR. Like in my example, 'EducationalDetails' is a user defined datatype (class). Can any one tell me how to add that in SOLR.

Thank you in advance.

回答1:

You can create your own types with the <fieldType /> tag, for example:

<fieldType name="cstring" class="solr.TextField" sortMissingLast="true" omitNorms="true">
    <analyzer>
        <tokenizer class="solr.KeywordTokenizerFactory" />
        <filter class="solr.TrimFilterFactory" updateOffsets="true" />
        <filter class="solr.PatternReplaceFilterFactory" pattern="\s+$" replacement="" />
        <filter class="solr.ASCIIFoldingFilterFactory" />
    </analyzer>
</fieldType>

However, as you can notice it's nothing like you want. The stuff you want (subfields, ...) is not possible with Solr. Solr is a document based store, not an "object" store or how you want to see it.

The best solution I can come up with is to create multiple "types" of documents. You scheme should look like:

<field name="ID" type="int" indexed="true" stored="true"/>
<field name="FirstName" type="string" indexed="true" stored="true"/>
<field name="LastName" type="string" indexed="true" stored="true" />
<field name="MobileNos" type="string" indexed="true" stored="true" multiValued="true" />

<!-- Education details -->
<field name="IntitutionName" type="string" indexed="true" stored="true"/>
<field name="EnrollDate" type="date" indexed="true" stored="true"/>
<field name="PassoutDate" type="date" indexed="true" stored="true"/>
<field name="InstituteRating" type="string" indexed="true" stored="true"/>
<field name="DocumentID" type="int" indexed="true" stored="true"/>

<field name="Type" type="string" indexed="true" stored="true"/>

If you add a Table1Document to Solr, you only provide the ID, FirstName, LastName and MobileNos fields (and additionally you can use the Type field and add "Table1Document"

If you add an EducationDetails object, you only provide the InstitutionName, EnrollDate, PassoutDate and InstituteRating field. In the DocumentID field you put a reference to the ID of the Table1Document they should belong to. In the Type field you can add "EducationDetails".

If you now want to retrieve your Table1Document you use a query like:

type:Table1Document AND id:1

To get the EducationDetails that belong to it you can use a query like:

type:EducationDetails AND DocumentID:1

Afterwards you can still construct an object with the structure you like.



标签: solr solrnet