Whooshの機能についての調査をある程度備忘録的に記載しておく。
まずUNIQUEなフィールドについて
Schema定義の際にフィールドのインスタンスにunique=Trueを渡すことで、
同じ値が入った時に上書きがされるようだ。
Solrなどで使われる複数のフィールドを横断して検索できる
DisMax系のモジュールがWhooshでも存在し、結構簡単に使える。
今回の検証スクリプトは下記
# -*- coding: utf-8 -*-
import sys
from whoosh.index import create_in
from whoosh.fields import *
from whoosh.scoring import *
from whoosh.query import *
from whoosh.qparser import DisMaxParser
#idフィールドをユニークキーとする
schema = Schema(id=ID(stored=True,unique=True),
name=NGRAM(stored=True),
ruby=NGRAM(stored=True),
address=NGRAM(stored=True),
telephone=TEXT(stored=True))
ix = create_in("indexdir", schema)
writer = ix.writer()
writer.add_document(id=u"001",
name=u"コンビニA",
ruby=u"こんびにえー",
address=u"東京都",
telephone=u"0312345678")
writer.add_document(id=u"002",
name=u"コンビニB",
ruby=u"こんびにびー",
address=u"千葉県",
telephone=u"0471234567")
writer.commit()
from whoosh.qparser import QueryParser
searcher = ix.searcher()
q = sys.argv[1].decode('utf-8')
#nameとrubyとaddressとtelephoneフィールドを横断検索
parser = DisMaxParser({"name":0.5,"ruby": 0.5,"address": 0.2,"telephone": 0.1},schema)
query = parser.parse(q)
results = searcher.search(query)
for result in results:
print result
ちょっとずつWhooshに慣れてきた。
かなりLuceneに近い使われ方を想定している模様。
Luceneを使っている人であれば結構簡単に使えるのでは。
形態素系のAnalyzerがないので、きついところもあるかもしれないが、
結構Pluggableに作られているようだし、
MecabなどのAnalyzerも簡単に作れるかも。
次はそこら辺を調べる予定。