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.taxonomy.FloatAssociationFacetField; 030import org.apache.lucene.facet.taxonomy.IntAssociationFacetField; 031import org.apache.lucene.facet.taxonomy.TaxonomyFacetSumFloatAssociations; 032import org.apache.lucene.facet.taxonomy.TaxonomyFacetSumIntAssociations; 033import org.apache.lucene.facet.taxonomy.TaxonomyReader; 034import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader; 035import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter; 036import org.apache.lucene.index.DirectoryReader; 037import org.apache.lucene.index.IndexWriter; 038import org.apache.lucene.index.IndexWriterConfig; 039import org.apache.lucene.index.IndexWriterConfig.OpenMode; 040import org.apache.lucene.search.IndexSearcher; 041import org.apache.lucene.search.MatchAllDocsQuery; 042import org.apache.lucene.store.ByteBuffersDirectory; 043import org.apache.lucene.store.Directory; 044 045/** Shows example usage of category associations. */ 046public class AssociationsFacetsExample { 047 048 private final Directory indexDir = new ByteBuffersDirectory(); 049 private final Directory taxoDir = new ByteBuffersDirectory(); 050 private final FacetsConfig config; 051 052 /** Empty constructor */ 053 public AssociationsFacetsExample() { 054 config = new FacetsConfig(); 055 config.setMultiValued("tags", true); 056 config.setIndexFieldName("tags", "$tags"); 057 config.setMultiValued("genre", true); 058 config.setIndexFieldName("genre", "$genre"); 059 } 060 061 /** Build the example index. */ 062 private void index() throws IOException { 063 IndexWriterConfig iwc = 064 new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE); 065 IndexWriter indexWriter = new IndexWriter(indexDir, iwc); 066 067 // Writes facet ords to a separate directory from the main index 068 DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); 069 070 Document doc = new Document(); 071 // 3 occurrences for tag 'lucene' 072 doc.add(new IntAssociationFacetField(3, "tags", "lucene")); 073 // 87% confidence level of genre 'computing' 074 doc.add(new FloatAssociationFacetField(0.87f, "genre", "computing")); 075 indexWriter.addDocument(config.build(taxoWriter, doc)); 076 077 doc = new Document(); 078 // 1 occurrence for tag 'lucene' 079 doc.add(new IntAssociationFacetField(1, "tags", "lucene")); 080 // 2 occurrence for tag 'solr' 081 doc.add(new IntAssociationFacetField(2, "tags", "solr")); 082 // 75% confidence level of genre 'computing' 083 doc.add(new FloatAssociationFacetField(0.75f, "genre", "computing")); 084 // 34% confidence level of genre 'software' 085 doc.add(new FloatAssociationFacetField(0.34f, "genre", "software")); 086 indexWriter.addDocument(config.build(taxoWriter, doc)); 087 088 indexWriter.close(); 089 taxoWriter.close(); 090 } 091 092 /** User runs a query and aggregates facets by summing their association values. */ 093 private List<FacetResult> sumAssociations() throws IOException { 094 DirectoryReader indexReader = DirectoryReader.open(indexDir); 095 IndexSearcher searcher = new IndexSearcher(indexReader); 096 TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); 097 098 FacetsCollector fc = new FacetsCollector(); 099 100 // MatchAllDocsQuery is for "browsing" (counts facets 101 // for all non-deleted docs in the index); normally 102 // you'd use a "normal" query: 103 FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc); 104 105 Facets tags = new TaxonomyFacetSumIntAssociations("$tags", taxoReader, config, fc); 106 Facets genre = new TaxonomyFacetSumFloatAssociations("$genre", taxoReader, config, fc); 107 108 // Retrieve results 109 List<FacetResult> results = new ArrayList<>(); 110 results.add(tags.getTopChildren(10, "tags")); 111 results.add(genre.getTopChildren(10, "genre")); 112 113 indexReader.close(); 114 taxoReader.close(); 115 116 return results; 117 } 118 119 /** User drills down on 'tags/solr'. */ 120 private FacetResult drillDown() throws IOException { 121 DirectoryReader indexReader = DirectoryReader.open(indexDir); 122 IndexSearcher searcher = new IndexSearcher(indexReader); 123 TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); 124 125 // Passing no baseQuery means we drill down on all 126 // documents ("browse only"): 127 DrillDownQuery q = new DrillDownQuery(config); 128 129 // Now user drills down on Publish Date/2010: 130 q.add("tags", "solr"); 131 FacetsCollector fc = new FacetsCollector(); 132 FacetsCollector.search(searcher, q, 10, fc); 133 134 // Retrieve results 135 Facets facets = new TaxonomyFacetSumFloatAssociations("$genre", taxoReader, config, fc); 136 FacetResult result = facets.getTopChildren(10, "genre"); 137 138 indexReader.close(); 139 taxoReader.close(); 140 141 return result; 142 } 143 144 /** Runs summing association example. */ 145 public List<FacetResult> runSumAssociations() throws IOException { 146 index(); 147 return sumAssociations(); 148 } 149 150 /** Runs the drill-down example. */ 151 public FacetResult runDrillDown() throws IOException { 152 index(); 153 return drillDown(); 154 } 155 156 /** Runs the sum int/float associations examples and prints the results. */ 157 public static void main(String[] args) throws Exception { 158 System.out.println("Sum associations example:"); 159 System.out.println("-------------------------"); 160 List<FacetResult> results = new AssociationsFacetsExample().runSumAssociations(); 161 System.out.println("tags: " + results.get(0)); 162 System.out.println("genre: " + results.get(1)); 163 } 164}