Last active
December 17, 2015 23:38
-
-
Save mocobeta/5690309 to your computer and use it in GitHub Desktop.
Lucene API カスタムCollector サンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 以下は、Apache Softoware Licence v2.0 の元に頒布されているコードに一部改変を加えたものです。 | |
* http://www.apache.org/licenses/LICENSE-2.0.txt | |
*/ | |
package extsearch.collector; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.TreeMap; | |
import org.apache.lucene.index.AtomicReaderContext; | |
import org.apache.lucene.index.BinaryDocValues; | |
import org.apache.lucene.search.Collector; | |
import org.apache.lucene.search.FieldCache; | |
import org.apache.lucene.search.Scorer; | |
import org.apache.lucene.util.BytesRef; | |
public class PostedMonthCollector extends Collector { | |
// 投稿月とエントリ(タイトル)リストのマップ | |
private Map<String, List<String>> postedMonthMap = new TreeMap<String, List<String>>(); | |
// 登録されているドキュメントのdateフィールド値 | |
private BinaryDocValues dates; | |
// 登録されているドキュメントのtitleフィールド値 | |
private BinaryDocValues titles; | |
@Override | |
public void setNextReader(AtomicReaderContext context) throws IOException { | |
// dateフィールドとtitleフィールドの値をフィールドキャッシュから取得 | |
dates = FieldCache.DEFAULT.getTerms(context.reader(), "date"); | |
titles = FieldCache.DEFAULT.getTerms(context.reader(), "title"); | |
} | |
@Override | |
public void collect(int doc) throws IOException { | |
BytesRef bytes = new BytesRef(); | |
dates.get(doc, bytes); | |
String month = bytes.utf8ToString().substring(0, 7); // 投稿日から月を抽出 | |
titles.get(doc, bytes); | |
String title = bytes.utf8ToString(); // タイトル | |
if (!postedMonthMap.containsKey(month)) { | |
postedMonthMap.put(month, new ArrayList<String>()); | |
} | |
// マップに追加 | |
postedMonthMap.get(month).add(title); | |
} | |
public Map<String, List<String>> getPostedMonthMap() { | |
return Collections.unmodifiableMap(postedMonthMap); | |
} | |
@Override | |
public void setScorer(Scorer scorer) throws IOException { | |
// スコア値が必要な場合は実装する | |
// ここでは何もしない | |
} | |
@Override | |
public boolean acceptsDocsOutOfOrder() { | |
// ドキュメントを受け付けるソート順を意識する必要があるか | |
// ここでは特に意識しなくていい | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 以下は、Apache Softoware Licence v2.0 の元に頒布されているコードに一部改変を加えたものです。 | |
* http://www.apache.org/licenses/LICENSE-2.0.txt | |
*/ | |
import org.apache.lucene.index.Term; | |
import org.apache.lucene.search.IndexSearcher; | |
import org.apache.lucene.search.TermQuery; | |
import org.apache.lucene.store.Directory; | |
import org.apache.lucene.store.FSDirectory; | |
public class PostedMonthTest { | |
public static void main(String[] args) throws IOException { | |
Directory directory = FSDirectory.open(new File("tumblrdata")); | |
IndexReader reader = DirectoryReader.open(directory); | |
IndexSearcher searcher = new IndexSearcher(reader); | |
// "lucene" というタグをもつドキュメントを検索、 | |
// 投稿月とエントリ(タイトル)のマッピングを取得 | |
TermQuery query = new TermQuery(new Term("tag", "lucene")); | |
PostedMonthCollector collector = new PostedMonthCollector(); | |
searcher.search(query, collector); | |
Map<String, List<String>> map = collector.getPostedMonthMap(); | |
// 投稿月ごとにエントリタイトルを表示 | |
for (String month : map.keySet()) { | |
List<String> titles = map.get(month); | |
System.out.println(month); | |
for (String title : titles) { | |
System.out.println("\t" + title); | |
} | |
} | |
reader.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment