Lucene8:Facetの使い方サンプル、その2

int, longの場合はfacet用ディレクトリは不要のようだ。そして、intもlongもlongとして扱われるらしい(そもそもlongしか無い?)。


import static java.util.stream.Collectors.*; import static org.junit.Assert.*; import java.util.stream.*; import org.apache.lucene.document.*; import org.apache.lucene.facet.*; import org.apache.lucene.index.*; import org.apache.lucene.search.*; import org.apache.lucene.store.*; import org.junit.*; public class FacetTest2 { @Test public void test() throws Exception { Directory dir = new RAMDirectory(); try (IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig())) { for (int i = 0; i < 10; i++) { Document doc = new Document(); doc.add(new NumericDocValuesField("id", 1)); indexWriter.addDocument(doc); } for (int i = 0; i < 5; i++) { Document doc = new Document(); doc.add(new NumericDocValuesField("id", 2)); indexWriter.addDocument(doc); } } try (IndexReader indexReader = DirectoryReader.open(dir)) { IndexSearcher indexSearcher = new IndexSearcher(indexReader); FacetsCollector fc = new FacetsCollector(); FacetsCollector.search(indexSearcher, new MatchAllDocsQuery(), Integer.MAX_VALUE, fc); LongValueFacetCounts longCounts = new LongValueFacetCounts("id", fc, false); FacetResult result = longCounts.getAllChildrenSortByValue(); String s = IntStream.range(0, result.childCount) .mapToObj(i->result.labelValues[i]) .map(lv->lv.label + "=" + lv.value) .sorted() .collect(joining(",")); assertEquals("1=10,2=5", s); } } }