関口宏司のLuceneブログ

OSS検索ライブラリのLuceneおよびそのサブプロジェクト(Solr/Tika/Mahoutなど)について
<< メールの署名欄を取り除く方法(MLネタ) | main | (メモ)IR本 >>
スポンサーサイト

一定期間更新がないため広告を表示しています

| スポンサードリンク | - | | - | - |
ReverseStringFilter(2.9)
Lucene 2.9のcontrib/analyzersに、これまでありそうでなかったReverseStringFilterというTokenFilterが導入された。これは名前の通り、トークンの文字列を逆転させたトークンを出力するTokenFilterである。

たとえば"ABCD"というトークンは"DCBA"と変換されて出力される。

https://issues.apache.org/jira/browse/LUCENE-1398

これはどのような時に使えばいいかというと、単語の「後方一致検索」をしたいときに使うとよい。

たとえば、"ing"で終わる単語を検索したいとする。WildcardQueryを使って"*ing"を検索することはできるが、このような検索は転置索引の当該フィールドの単語をすべてチェックすることになってしまい検索に時間がかかってしまう。

そこで単語の「後方一致検索」をしたいフィールドに関してはReverseStringFilterを使って"looking"という単語なら"gnikool"というように登録する。そうしておいてWildcardQueryで"gni*"と検索すると転置索引のうち、"gni"で始まる範囲だけを見ればよいので格段に検索速度があがる。

以下に使用例を示す:



public class TestReverseStringFilter {

static Directory dir = new RAMDirectory();
static Analyzer analyzer = new ReverseStringAnalyzer();
static final String F = "f";

public static void main(String[] args) throws Exception {
makeIndex();
searchIndex( "*ing" );
searchIndex( "*.com" );
}

static void makeIndex() throws Exception {
IndexWriter writer = new IndexWriter( dir, analyzer, true, MaxFieldLength.LIMITED );
addDoc( writer, "Apache" );
addDoc( writer, "Lucene" );
addDoc( writer, "looking" );
addDoc( writer, "hacking" );
addDoc( writer, "lucene.apache.org" );
addDoc( writer, "www.rondhuit.com" );
writer.close();
}

static void addDoc( IndexWriter writer, String val ) throws Exception {
Document doc = new Document();
doc.add( new Field( F, val, Store.YES, Index.ANALYZED ) );
writer.addDocument( doc );
}

static void searchIndex( String q ) throws Exception {
System.out.println( "¥nsearching ¥"" + q + "¥" ..." );
IndexSearcher searcher = new IndexSearcher( dir );
Query query = new WildcardQuery( new Term( F, ReverseStringFilter.reverse( q ) ) );
TopDocs docs = searcher.search( query, 10 );
for( ScoreDoc scoreDoc : docs.scoreDocs ){
Document doc = searcher.doc( scoreDoc.doc );
System.out.println( doc.get( F ) );
}
searcher.close();
}

static class ReverseStringAnalyzer extends Analyzer {
public TokenStream tokenStream( String field, Reader input ) {
TokenStream ts = new KeywordTokenizer( input );
return new ReverseStringFilter( ts );
}
}
}



そして、実行結果は次の通りである:



searching "*ing" ...
looking
hacking

searching "*.com" ...
www.rondhuit.com


| 関口宏司 | Luceneクラス解説 | 00:25 | comments(1) | trackbacks(0) |
スポンサーサイト
| スポンサードリンク | - | 00:25 | - | - |
http://www.pandorajewelryrings.us.com/ pandora necklace
http://www.birkenstocksandalssale.us.com/ birkenstock sandals
http://www.adidas-ultraboost.us.com/ ultra boost
http://www.salomonspeedcross3.us.com/ salomon speedcross 3
http://www.pandora-jewelrysale.us.com/ pandora jewelry
http://www.pumafentyrihannashoes.us.com/ puma sneakers womens
http://www.kyrie-4.us.com/ kyrie 3s
http://www.kd10-shoes.us.com/ kd 10
http://www.jordan11spacejams.us.com/ space jam 11
http://www.adidasyeezy-350.us.com/ ultra boost
http://www.louboutinredbottoms.us.com/ louboutin shoes
http://www.yeezyboost350shoes.us.com/ yeezy boost 350 v2
http://www.hermesbirkin-handbags.us.com/ hermes bag
http://www.fitflops-sale.us.com/ fitflop shoes
http://www.jordans11shoes.us.com/ jordan retro 12
http://www.lebron15-shoes.us.com/ lebron soldier 11
http://www.airmax90shoes.us.com/ nike shoes
http://www.nikeairmax-90.us.com/ nike air max 95
http://www.katespadepurses.us.com/ kate spade crossbody
http://www.nikeairvapormaxflyknit.us.com/ air max 90
http://www.adidasnmdrunnerr1.us.com/ adidas nmd womens
http://www.fitflopsshoes.us.com/ fitflops clearance
http://www.longchampbag.us.com/ longchamp bag
http://www.pandorajewelryscharms.us.com/ pandora bracelet
http://www.curry4-shoes.us.com/ kyrie irving shoes
ドリフトliuyuzhen
| kate spade | 2018/03/26 3:45 AM |









トラックバック機能は終了しました。
+ Solrによるブログ内検索
+ PROFILE
     12
3456789
10111213141516
17181920212223
24252627282930
31      
<< March 2024 >>
+ LINKS
検索エンジン製品 - 比較のポイント
商用検索エンジンを購入した企業担当者は読まないでください。ショックを受けますから・・・
>>製品比較 10のポイント
+ Lucene&Solrデモ
+ ThinkIT記事
+ RECOMMEND
Apache Solr入門 ―オープンソース全文検索エンジン
Apache Solr入門 ―オープンソース全文検索エンジン (JUGEMレビュー »)
関口 宏司,三部 靖夫,武田 光平,中野 猛,大谷 純
+ RECOMMEND
Lucene in Action
Lucene in Action (JUGEMレビュー »)
Erik Hatcher,Otis Gospodnetic,Mike McCandless
FastVectorHighlighterについて解説記事を寄稿しました。
+ RECOMMEND
+ SELECTED ENTRIES
+ RECENT COMMENTS
+ RECENT TRACKBACK
+ CATEGORIES
+ ARCHIVES
+ MOBILE
qrcode
+ SPONSORED LINKS