/** * Determines the number of hits for each facet across the main query. * * @param facets * @param reader * @param mainQueryBits * @throws ParseException * @throws IOException */ private void processFacetCategory(Collection<FacetResult> facets, IndexReader reader, OpenBitSetDISI mainQueryBitSet, IndexSearcher searcher) throws ParseException, IOException { for(FacetResult f : facets ) { long count = 0; String searchString = f.getFacetName(); if( !searchString.trim().equals("")) { QueryParser subQueryParser = new QueryParser(Version.LUCENE_35, f.getField(), keywordAnalyzer); searchString = "\"" + searchString +"\""; Query subQuery = subQueryParser.parse(searchString); QueryWrapperFilter subQueryWrapper = new QueryWrapperFilter(subQuery); log.debug("Fixed query in process facet catagory 2 = " + subQuery + " subQueryWrapper = " + subQueryWrapper); DocIdSet subQueryBits = subQueryWrapper.getDocIdSet(reader); OpenBitSetDISI subQuerybitSet = new OpenBitSetDISI(subQueryBits.iterator(), reader.maxDoc()); count = getFacetHitCount(mainQueryBitSet, subQuerybitSet); log.debug("count = " + count); } else { log.error("bad search string " + searchString); } f.setHits(count); } }
/** * Determines the number of hits for each facet across the main query. * * @param facets * @param reader * @param mainQueryBits * @throws ParseException * @throws IOException */ private void processFacetCategory(Collection<FacetResult> facets, IndexReader reader, DocIdSet mainQueryBits) throws ParseException, IOException { for(FacetResult f : facets ) { QueryParser subQueryParser = new QueryParser(Version.LUCENE_35, f.getField(), analyzer); subQueryParser.setDefaultOperator(QueryParser.AND_OPERATOR); String fixedQuery = SearchHelper.prepareFacetSearchString(f.getFacetName(), false); fixedQuery = "\"" + fixedQuery + "\""; Query subQuery = subQueryParser.parse(fixedQuery); if(log.isDebugEnabled()) { log.debug("Fiexed query in process Facet Category = " + fixedQuery); } QueryWrapperFilter subQueryWrapper = new QueryWrapperFilter(subQuery); DocIdSet subQueryBits = subQueryWrapper.getDocIdSet(reader); OpenBitSetDISI mainQuerybitSet = new OpenBitSetDISI(mainQueryBits.iterator(), maxNumberOfMainQueryHits); OpenBitSetDISI subQuerybitSet = new OpenBitSetDISI(subQueryBits.iterator(), maxNumberOfMainQueryHits); long count = getFacetHitCount(mainQuerybitSet, subQuerybitSet); f.setHits(count); } }
/** * Delegates to each filter in the chain. * * @param context AtomicReaderContext * @param logic Logical operation * @return DocIdSet */ private DocIdSet getDocIdSet(AtomicReaderContext context, int logic, int[] index) throws IOException { OpenBitSetDISI result = initialResult(context, logic, index); for (; index[0] < chain.length; index[0]++) { // we dont pass acceptDocs, we will filter at the end using an additional filter doChain(result, logic, chain[index[0]].getDocIdSet(context, null)); } return result; }
/** * Delegates to each filter in the chain. * * @param context AtomicReaderContext * @param logic Logical operation * @return DocIdSet */ private DocIdSet getDocIdSet(AtomicReaderContext context, int[] logic, int[] index) throws IOException { if (logic.length != chain.length) { throw new IllegalArgumentException("Invalid number of elements in logic array"); } OpenBitSetDISI result = initialResult(context, logic[0], index); for (; index[0] < chain.length; index[0]++) { // we dont pass acceptDocs, we will filter at the end using an additional filter doChain(result, logic[index[0]], chain[index[0]].getDocIdSet(context, null)); } return result; }
/** * Get the facets and results * @see edu.ur.ir.institution.InstitutionalItemSearchService#executeSearchWithFacets(java.lang.String, java.lang.String, int, int, int, int) */ public FacetSearchHelper executeSearchWithFacets( String mainQueryString, String indexFolder, int numberOfHitsToProcessForFacets, int numberOfResultsToCollectForFacets, int numberOfFactsToShow, int numberOfIdsToCollect, int idsToCollectStartPosition ) throws CorruptIndexException, IOException, ParseException { log.debug("orginal query 4 = " + mainQueryString); if( searchDirectoryIsEmpty(indexFolder) || isInvalidQuery(mainQueryString)) { return new FacetSearchHelper(new HashSet<Long>(), 0, new HashMap<String, Collection<FacetResult>>(), mainQueryString); } FSDirectory directory = FSDirectory.open(new File(indexFolder)); IndexReader reader = IndexReader.open(directory, true); IndexSearcher searcher = new IndexSearcher(reader); QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, fields, analyzer, getBoostedFields()); parser.setDefaultOperator(QueryParser.AND_OPERATOR); // execute the main query - we will use this to extract data to determine the facet searches // the search helper MUST BE SET TO FALSE if diacritic based searches are to work // putting a * following a diacritic does not work String executedQuery = SearchHelper.prepareMainSearchString(mainQueryString, false); Query mainQuery = parser.parse(executedQuery); if( log.isDebugEnabled() ) { log.debug("main query = " + executedQuery ); log.debug("main query parsed = " + mainQuery + " maxNumberOfMainQueryHits = " + maxNumberOfMainQueryHits ); } TopDocs topDocs = searcher.search(mainQuery, maxNumberOfMainQueryHits); // determine the set of data we should use to determine facets HashMap<String, HashMap<String, FacetResult>> possibleFacets = this.generateFacetSearches( topDocs, numberOfHitsToProcessForFacets, numberOfResultsToCollectForFacets, searcher); QueryWrapperFilter mainQueryWrapper = new QueryWrapperFilter(mainQuery); log.debug("executeSearchWithFacets 1 query = " + mainQuery); DocIdSet mainQueryBits = mainQueryWrapper.getDocIdSet(reader); OpenBitSetDISI mainQueryBitSet = new OpenBitSetDISI(mainQueryBits.iterator(), reader.maxDoc()); HashMap<String, Collection<FacetResult>> facetResults = new HashMap<String, Collection<FacetResult>>(); // process the data and determine the facets FacetSearchHelper helper = processPossibleFacets(possibleFacets, reader, mainQueryBitSet, facetResults, topDocs, numberOfIdsToCollect, idsToCollectStartPosition, numberOfFactsToShow, mainQueryString, searcher); helper.setExecutedQuery(executedQuery); searcher.close(); reader.close(); return helper; }
private void doChain(OpenBitSetDISI result, int logic, DocIdSet dis) throws IOException { if (dis instanceof OpenBitSet) { // optimized case for OpenBitSets switch (logic) { case OR: result.or((OpenBitSet) dis); break; case AND: result.and((OpenBitSet) dis); break; case ANDNOT: result.andNot((OpenBitSet) dis); break; case XOR: result.xor((OpenBitSet) dis); break; default: doChain(result, DEFAULT, dis); break; } } else { DocIdSetIterator disi; if (dis == null) { disi = DocIdSet.EMPTY_DOCIDSET.iterator(); } else { disi = dis.iterator(); if (disi == null) { disi = DocIdSet.EMPTY_DOCIDSET.iterator(); } } switch (logic) { case OR: result.inPlaceOr(disi); break; case AND: result.inPlaceAnd(disi); break; case ANDNOT: result.inPlaceNot(disi); break; case XOR: result.inPlaceXor(disi); break; default: doChain(result, DEFAULT, dis); break; } } }
private void doChain(OpenBitSetDISI result, int logic, DocIdSet dis) throws IOException { if (dis instanceof OpenBitSet) { // optimized case for OpenBitSets switch (logic) { case OR: result.or((OpenBitSet) dis); break; case AND: result.and((OpenBitSet) dis); break; case ANDNOT: result.andNot((OpenBitSet) dis); break; case XOR: result.xor((OpenBitSet) dis); break; default: doChain(result, DEFAULT, dis); break; } } else { DocIdSetIterator disi; if (dis == null) { disi = DocIdSetIterator.empty(); } else { disi = dis.iterator(); if (disi == null) { disi = DocIdSetIterator.empty(); } } switch (logic) { case OR: result.inPlaceOr(disi); break; case AND: result.inPlaceAnd(disi); break; case ANDNOT: result.inPlaceNot(disi); break; case XOR: result.inPlaceXor(disi); break; default: doChain(result, DEFAULT, dis); break; } } }