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; 045import org.apache.lucene.util.IOUtils; 046 047/** Shows example usage of category associations. */ 048public class AssociationsFacetsExample { 049 050 private final Directory indexDir = new ByteBuffersDirectory(); 051 private final Directory taxoDir = new ByteBuffersDirectory(); 052 private final FacetsConfig config; 053 054 /** Empty constructor */ 055 public AssociationsFacetsExample() { 056 config = new FacetsConfig(); 057 config.setMultiValued("tags", true); 058 config.setIndexFieldName("tags", "$tags"); 059 config.setMultiValued("genre", true); 060 config.setIndexFieldName("genre", "$genre"); 061 } 062 063 /** Build the example index. */ 064 private void index() throws IOException { 065 IndexWriterConfig iwc = 066 new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE); 067 IndexWriter indexWriter = new IndexWriter(indexDir, iwc); 068 069 // Writes facet ords to a separate directory from the main index 070 DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); 071 072 Document doc = new Document(); 073 // 3 occurrences for tag 'lucene' 074 doc.add(new IntAssociationFacetField(3, "tags", "lucene")); 075 // 87% confidence level of genre 'computing' 076 doc.add(new FloatAssociationFacetField(0.87f, "genre", "computing")); 077 indexWriter.addDocument(config.build(taxoWriter, doc)); 078 079 doc = new Document(); 080 // 1 occurrence for tag 'lucene' 081 doc.add(new IntAssociationFacetField(1, "tags", "lucene")); 082 // 2 occurrence for tag 'solr' 083 doc.add(new IntAssociationFacetField(2, "tags", "solr")); 084 // 75% confidence level of genre 'computing' 085 doc.add(new FloatAssociationFacetField(0.75f, "genre", "computing")); 086 // 34% confidence level of genre 'software' 087 doc.add(new FloatAssociationFacetField(0.34f, "genre", "software")); 088 indexWriter.addDocument(config.build(taxoWriter, doc)); 089 090 IOUtils.close(indexWriter, taxoWriter); 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 IOUtils.close(indexReader, taxoReader); 119 120 return results; 121 } 122 123 /** User drills down on 'tags/solr'. */ 124 private FacetResult drillDown() throws IOException { 125 DirectoryReader indexReader = DirectoryReader.open(indexDir); 126 IndexSearcher searcher = new IndexSearcher(indexReader); 127 TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); 128 129 // Passing no baseQuery means we drill down on all 130 // documents ("browse only"): 131 DrillDownQuery q = new DrillDownQuery(config); 132 133 // Now user drills down on Publish Date/2010: 134 q.add("tags", "solr"); 135 FacetsCollector fc = new FacetsCollector(); 136 FacetsCollector.search(searcher, q, 10, fc); 137 138 // Retrieve results 139 Facets facets = 140 new TaxonomyFacetFloatAssociations( 141 "$genre", taxoReader, config, fc, AssociationAggregationFunction.SUM); 142 FacetResult result = facets.getTopChildren(10, "genre"); 143 144 IOUtils.close(indexReader, taxoReader); 145 146 return result; 147 } 148 149 /** Runs summing association example. */ 150 public List<FacetResult> runSumAssociations() throws IOException { 151 index(); 152 return sumAssociations(); 153 } 154 155 /** Runs the drill-down example. */ 156 public FacetResult runDrillDown() throws IOException { 157 index(); 158 return drillDown(); 159 } 160 161 /** Runs the sum int/float associations examples and prints the results. */ 162 public static void main(String[] args) throws Exception { 163 System.out.println("Sum associations example:"); 164 System.out.println("-------------------------"); 165 List<FacetResult> results = new AssociationsFacetsExample().runSumAssociations(); 166 System.out.println("tags: " + results.get(0)); 167 System.out.println("genre: " + results.get(1)); 168 } 169}