<?xml version="1.0" encoding="UTF-8" ?>
<!--
 Solr Schema for StoryCove Collections Core
-->
<schema name="storycove-collections" version="1.6">

  <!-- Field Types -->

  <fieldType name="string" class="solr.StrField" sortMissingLast="true" />

  <fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
    <analyzer type="index">
      <tokenizer class="solr.StandardTokenizerFactory"/>
      <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
      <filter class="solr.LowerCaseFilterFactory"/>
    </analyzer>
    <analyzer type="query">
      <tokenizer class="solr.StandardTokenizerFactory"/>
      <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
      <filter class="solr.SynonymGraphFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
      <filter class="solr.LowerCaseFilterFactory"/>
    </analyzer>
  </fieldType>

  <fieldType name="text_enhanced" class="solr.TextField" positionIncrementGap="100">
    <analyzer type="index">
      <tokenizer class="solr.StandardTokenizerFactory"/>
      <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
      <filter class="solr.LowerCaseFilterFactory"/>
      <filter class="solr.EdgeNGramFilterFactory" minGramSize="1" maxGramSize="15"/>
    </analyzer>
    <analyzer type="query">
      <tokenizer class="solr.StandardTokenizerFactory"/>
      <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
      <filter class="solr.LowerCaseFilterFactory"/>
    </analyzer>
  </fieldType>

  <fieldType name="pint" class="solr.IntPointField" docValues="true"/>
  <fieldType name="plong" class="solr.LongPointField" docValues="true"/>
  <fieldType name="pdouble" class="solr.DoublePointField" docValues="true"/>
  <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>
  <fieldType name="pdate" class="solr.DatePointField" docValues="true"/>
  <fieldType name="strings" class="solr.StrField" sortMissingLast="true" multiValued="true" docValues="true"/>
  <fieldType name="random" class="solr.RandomSortField" indexed="true"/>

  <!-- Fields -->

  <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
  <field name="_version_" type="plong" indexed="false" stored="false"/>

  <!-- Core Collection Fields -->
  <field name="name" type="text_enhanced" indexed="true" stored="true" required="true"/>
  <field name="description" type="text_general" indexed="true" stored="true"/>
  <field name="rating" type="pint" indexed="true" stored="true"/>
  <field name="coverImagePath" type="string" indexed="false" stored="true"/>
  <field name="isArchived" type="boolean" indexed="true" stored="true"/>

  <!-- Derived Stats -->
  <field name="storyCount" type="pint" indexed="true" stored="true"/>
  <field name="totalWordCount" type="plong" indexed="true" stored="true"/>
  <field name="estimatedReadingTime" type="pint" indexed="true" stored="true"/>
  <field name="averageStoryRating" type="pdouble" indexed="true" stored="true"/>

  <!-- Tag Fields -->
  <field name="tagNames" type="strings" indexed="true" stored="true"/>

  <!-- Multi-tenant Library Separation -->
  <field name="libraryId" type="string" indexed="true" stored="true" required="false" default="default"/>

  <!-- Timestamp Fields -->
  <field name="createdAt" type="pdate" indexed="true" stored="true"/>
  <field name="updatedAt" type="pdate" indexed="true" stored="true"/>

  <!-- Combined search field for general queries -->
  <field name="text" type="text_general" indexed="true" stored="false" multiValued="true"/>

  <copyField source="name" dest="text"/>
  <copyField source="description" dest="text"/>
  <copyField source="tagNames" dest="text"/>

  <dynamicField name="random_*" type="random" indexed="true" stored="false"/>

  <uniqueKey>id</uniqueKey>

</schema>
