Starting Apache Solr #1

スキーマ

http://wiki.apache.org/solr/SchemaXml

The schema.xml file contains all of the details about which fields your documents can contain, and how those fields should be dealt with when adding documents to the index, or when querying those fields.

ドキュメントのフィールドおよび、それらがインデックス作成・クエリ処理時にどう扱われるかを定義するものがschema.xmlである。$SOLR_HOME/conf/schema.xmlに配置される。

基本フォーマット

Solrに付属しているschema.xmlから抜粋。

    <?xml version="1.0" encoding="UTF-8" ?>
    <schema name="example" version="1.5">
    
      <types>
        <fieldType name="string" class="solr.StrField" sortMissingLast="true" />
        <fieldType name="int" class="solr.TrieIntField" precisionStep="0" />
        <fieldType name="text_cjk" class="solr.TextField">
          <analyzer>
            <tokenizer class="solr.StandardTokenizerFactory"/>
            <filter class="solr.CJKWidthFilterFactory"/>
            <filter class="solr.LowerCaseFilterFactory"/>
            <filter class="solr.CJKBigramFilterFactory"/>
          </analyzer>
        </fieldType>
     </types>
    
     <fields>
       <field name="id" type="string" indexed="true" stored="true" required="true" /> 
       <field name="popularity" type="int" indexed="true" stored="true" />
     </fields>
    
     <uniqueKey>id</uniqueKey>
    </schema>

<types>でフィールドの型を定義して、<fields>でフィールド名やその型を指定する。 <uniqueKey>はドキュメントを特定するキー。

types

TextFieldと非TextFieldの2つに分類できる。 非TextFieldは例えばstring、int、boolean、double、dateなど。

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

他方、TextFieldは以下のような構造になる。

         <fieldType name="text_cjk" class="solr.TextField">
          <analyzer>
            <charFilter class="solr.MappingCharFilterFactory" mapping="mapping-ISOLatin1Accent.txt"/> 
            <tokenizer class="solr.StandardTokenizerFactory"/>
            <filter class="solr.CJKBigramFilterFactory"/>
          </analyzer>
        </fieldType>

fields

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

indexed=true|false

True if this field should be "indexed". If (and only if) a field is indexed, then it is searchable, sortable, and facetable.

stored=true|false

True if the value of the field should be retrievable during a search.

ドキュメントを追加

<add>
<doc>
<field name="id">JPY</field>
<field name="name">One Yen</field>
<field name="manu">Bank of Japan</field>
<field name="manu_id_s">boj</field> 
<field name="cat">currency</field>
<fieldname="features">Coinsandnotes</field>
<field name="price_c">1,JPY</field>
<field name="inStock">true</field>
</doc>
</add>

を追加するにはSolrのAPIにHTTP Requestを投げればよい。

 $ curl http://localhost:8983/solr/update --data-binary @/tmp/jpy.xml -H 'Content-type:text/xml; charset=utf-8'
 <?xml version="1.0" encoding="UTF-8"?>
 <response>
 <lst name="responseHeader"><int name="status">0</int><int name="QTime">194</int></lst>
 </response>

コミットが必要。

$ curl http://localhost:8983/solr/update?commit=true
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader"><int name="status">0</int><int name="QTime">60</int></lst>
</response>

まとめ

  • schema.xmlの基本構造がわかった
  • ドキュメントを更新する方法がわかった