001package org.apache.lucene.demo.facet; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020import java.io.IOException; 021import java.util.ArrayList; 022import java.util.List; 023 024import org.apache.lucene.analysis.core.WhitespaceAnalyzer; 025import org.apache.lucene.document.Document; 026import org.apache.lucene.facet.DrillDownQuery; 027import org.apache.lucene.facet.FacetResult; 028import org.apache.lucene.facet.Facets; 029import org.apache.lucene.facet.FacetsCollector; 030import org.apache.lucene.facet.FacetsConfig; 031import org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState; 032import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts; 033import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField; 034import org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState; 035import org.apache.lucene.index.DirectoryReader; 036import org.apache.lucene.index.IndexWriter; 037import org.apache.lucene.index.IndexWriterConfig; 038import org.apache.lucene.search.IndexSearcher; 039import org.apache.lucene.search.MatchAllDocsQuery; 040import org.apache.lucene.store.Directory; 041import org.apache.lucene.store.RAMDirectory; 042 043/** Shows simple usage of faceted indexing and search, 044 * using {@link SortedSetDocValuesFacetField} and {@link 045 * SortedSetDocValuesFacetCounts}. */ 046 047public class SimpleSortedSetFacetsExample { 048 049 private final Directory indexDir = new RAMDirectory(); 050 private final FacetsConfig config = new FacetsConfig(); 051 052 /** Empty constructor */ 053 public SimpleSortedSetFacetsExample() { 054 } 055 056 /** Build the example index. */ 057 private void index() throws IOException { 058 IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER, 059 new WhitespaceAnalyzer())); 060 Document doc = new Document(); 061 doc.add(new SortedSetDocValuesFacetField("Author", "Bob")); 062 doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010")); 063 indexWriter.addDocument(config.build(doc)); 064 065 doc = new Document(); 066 doc.add(new SortedSetDocValuesFacetField("Author", "Lisa")); 067 doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010")); 068 indexWriter.addDocument(config.build(doc)); 069 070 doc = new Document(); 071 doc.add(new SortedSetDocValuesFacetField("Author", "Lisa")); 072 doc.add(new SortedSetDocValuesFacetField("Publish Year", "2012")); 073 indexWriter.addDocument(config.build(doc)); 074 075 doc = new Document(); 076 doc.add(new SortedSetDocValuesFacetField("Author", "Susan")); 077 doc.add(new SortedSetDocValuesFacetField("Publish Year", "2012")); 078 indexWriter.addDocument(config.build(doc)); 079 080 doc = new Document(); 081 doc.add(new SortedSetDocValuesFacetField("Author", "Frank")); 082 doc.add(new SortedSetDocValuesFacetField("Publish Year", "1999")); 083 indexWriter.addDocument(config.build(doc)); 084 085 indexWriter.close(); 086 } 087 088 /** User runs a query and counts facets. */ 089 private List<FacetResult> search() throws IOException { 090 DirectoryReader indexReader = DirectoryReader.open(indexDir); 091 IndexSearcher searcher = new IndexSearcher(indexReader); 092 SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(indexReader); 093 094 // Aggregatses the facet counts 095 FacetsCollector fc = new FacetsCollector(); 096 097 // MatchAllDocsQuery is for "browsing" (counts facets 098 // for all non-deleted docs in the index); normally 099 // you'd use a "normal" query: 100 FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc); 101 102 // Retrieve results 103 Facets facets = new SortedSetDocValuesFacetCounts(state, fc); 104 105 List<FacetResult> results = new ArrayList<>(); 106 results.add(facets.getTopChildren(10, "Author")); 107 results.add(facets.getTopChildren(10, "Publish Year")); 108 indexReader.close(); 109 110 return results; 111 } 112 113 /** User drills down on 'Publish Year/2010'. */ 114 private FacetResult drillDown() throws IOException { 115 DirectoryReader indexReader = DirectoryReader.open(indexDir); 116 IndexSearcher searcher = new IndexSearcher(indexReader); 117 SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(indexReader); 118 119 // Now user drills down on Publish Year/2010: 120 DrillDownQuery q = new DrillDownQuery(config); 121 q.add("Publish Year", "2010"); 122 FacetsCollector fc = new FacetsCollector(); 123 FacetsCollector.search(searcher, q, 10, fc); 124 125 // Retrieve results 126 Facets facets = new SortedSetDocValuesFacetCounts(state, fc); 127 FacetResult result = facets.getTopChildren(10, "Author"); 128 indexReader.close(); 129 130 return result; 131 } 132 133 /** Runs the search example. */ 134 public List<FacetResult> runSearch() throws IOException { 135 index(); 136 return search(); 137 } 138 139 /** Runs the drill-down example. */ 140 public FacetResult runDrillDown() throws IOException { 141 index(); 142 return drillDown(); 143 } 144 145 /** Runs the search and drill-down examples and prints the results. */ 146 public static void main(String[] args) throws Exception { 147 System.out.println("Facet counting example:"); 148 System.out.println("-----------------------"); 149 SimpleSortedSetFacetsExample example = new SimpleSortedSetFacetsExample(); 150 List<FacetResult> results = example.runSearch(); 151 System.out.println("Author: " + results.get(0)); 152 System.out.println("Publish Year: " + results.get(0)); 153 154 System.out.println("\n"); 155 System.out.println("Facet drill-down example (Publish Year/2010):"); 156 System.out.println("---------------------------------------------"); 157 System.out.println("Author: " + example.runDrillDown()); 158 } 159}