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