ubuntuでsolr動かしてみる。
install
# $ sudo apt-get install openjdk-6-jdk
$ sudo apt-get install solr-common solr-jetty
jettyの設定変更
jettyを/etc/init.dから起動できるようにする。ついでにadmin画面にアクセスしてsolrがjetty上で動いているか確認
$ sudoedit /etc/default/jetty # NO_START=0に変える ## jettyを立ち上げる $ sudo /etc/init.d/jetty start ## browserで見てみる(defaultは以下のurl) $ x-www-browser http://localhost:8080/solr
schema.xmlを変更
schema.xmlについて
solrでどのようなフィールドを格納するかの設定を記述するファイル
変更手順
ubuntuのaptでインストールした場合、schema.xmlは/etc/solr/conf/schema.xmlに置かれる。
(これは,/var/lib/dpkg/info/solr*.listの中を見ればわかる。)
schema.xmlは以下のように変更した.
<?xml version="1.0" encoding="UTF-8" ?> <schema name="example" version="1.4"> <types> <!-- The StrField type is not analyzed, but indexed/stored verbatim. --> <fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/> <fieldType name="long" class="solr.TrieLongField" precisionStep="0" omitNorms="true" positionIncrementGap="0"/> <fieldType name="text_ja" class="solr.TextField" positionIncrementGap="100"> <analyzer> <tokenizer class="solr.CJKTokenizerFactory"/> </analyzer> </fieldType> </types> <fields> <field name="id" type="string" indexed="true" stored="true" required="true" /> <field name="title" type="text_ja" indexed="true" stored="true" /> <field name="body" type="text_ja" indexed="true" stored="true" /> </fields> <uniqueKey>id</uniqueKey> <defaultSearchField>body</defaultSearchField> <solrQueryParser defaultOperator="AND"/> </schema>
上記の設定は以下のようなフィールドを登録可能にしている。
field name | type | - | |
id | int | id | |
title | text_ja | 日本語の文章タイトル(text_jaはn-gram) | |
body | text_ja | 日本語の文章タイトル(text_jaはn-gram) |
$ sudo /etc/init.d/jetty restart # $ sudo less /var/log/jetty/*.stderrout.logなどを見てエラーが起きていないことを確認する
pythonからsolrの機能を利用。
solrpyを使った。
## install $ easy_install solrpy ## try it $ python >>> import solr >>> s = solr.SolrConnection("http://localhost:8080/solr") >>> s.add(id=1, title=u"ja", body=u"日本語通る?") '<?xml version="1.0" encoding="UTF-8"?>\n<response>\n<lst name="responseHeader"><int name="status">0</int><int name="QTime">5</int></lst>\n</response>\n' >>> s.commit() '<?xml version="1.0" encoding="UTF-8"?>\n<response>\n<lst name="responseHeader"><int name="status">0</int><int name="QTime">41</int></lst>\n</response>\n' >>> s.query(u"body:日本語").results [{u'body': u'\u65e5\u672c\u8a9e\u901a\u308b\uff1f', u'score': 1.8472979, u'id': u'1', u'title': u'ja'}] >>> s.query(u"title:fo").results [] >>> s.query(u"title:ja").results