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