Example usage for org.apache.lucene.facet DrillDownQuery DrillDownQuery

List of usage examples for org.apache.lucene.facet DrillDownQuery DrillDownQuery

Introduction

In this page you can find the example usage for org.apache.lucene.facet DrillDownQuery DrillDownQuery.

Prototype

public DrillDownQuery(FacetsConfig config, Query baseQuery) 

Source Link

Document

Creates a new DrillDownQuery over the given base query.

Usage

From source file:com.orientechnologies.lucene.collections.LuceneResultSet.java

License:Apache License

private void fetchFacet() {
    if (queryContext.facet) {
        FacetsCollector facetsCollector = new FacetsCollector(true);

        try {/*from w w w  . j a  v a2 s . c  om*/

            String[] pathFacet = null;
            if (queryContext.isDrillDown()) {
                DrillDownQuery drillDownQuery = new DrillDownQuery(queryContext.getFacetConfig(), query);
                String[] path = queryContext.getDrillDownQuery().split(":");
                pathFacet = path[1].split("/");
                drillDownQuery.add(path[0], pathFacet);
                FacetsCollector.search(queryContext.searcher, drillDownQuery, PAGE_SIZE, facetsCollector);
            } else {
                FacetsCollector.search(queryContext.searcher, query, PAGE_SIZE, facetsCollector);
            }

            Facets facets = new FastTaxonomyFacetCounts(queryContext.reader, queryContext.getFacetConfig(),
                    facetsCollector);

            FacetResult facetResult = null;
            if (pathFacet != null) {
                facetResult = facets.getTopChildren(PAGE_SIZE, queryContext.getFacetField(), pathFacet);
            } else {
                facetResult = facets.getTopChildren(PAGE_SIZE, queryContext.getFacetField());
            }

            if (facetResult != null) {
                List<ODocument> documents = new ArrayList<ODocument>();
                // for (FacetResult facetResult : res) {

                ODocument doc = new ODocument();

                doc.field("childCount", facetResult.childCount);
                doc.field("value", facetResult.value);
                doc.field("dim", facetResult.dim);
                List<ODocument> labelsAndValue = new ArrayList<ODocument>();
                for (LabelAndValue labelValue : facetResult.labelValues) {
                    ODocument doc1 = new ODocument();
                    doc1.field("label", labelValue.label);
                    doc1.field("value", labelValue.value);
                    labelsAndValue.add(doc1);

                }
                doc.field("labelsValue", labelsAndValue);
                documents.add(doc);
                queryContext.context.setVariable("$facet", documents);
            }
            // }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.xiaomi.linden.core.search.LindenCoreImpl.java

License:Apache License

public LindenResult search(LindenSearchRequest request) throws IOException {
    SearcherTaxonomyManager.SearcherAndTaxonomy searcherAndTaxonomy = lindenNRTSearcherManager.acquire();
    try {// w ww.j a  v a2 s  .  com
        IndexSearcher indexSearcher = searcherAndTaxonomy.searcher;
        Filter filter = FilterConstructor.constructFilter(request.getFilter(), config);
        Sort sort = SortConstructor.constructSort(request, indexSearcher, config);
        indexSearcher.setSimilarity(config.getSearchSimilarityInstance());

        Query query = QueryConstructor.constructQuery(request.getQuery(), config);
        if (filter != null) {
            query = new FilteredQuery(query, filter);
        }

        int from = request.getOffset();
        int size = request.getLength();
        LindenResultParser resultParser = new LindenResultParser(config, request, indexSearcher,
                snippetGenerator, query, filter, sort);
        // very common search, no group, no facet, no early termination, no search time limit
        if (!request.isSetGroupParam() && !request.isSetFacet() && !request.isSetEarlyParam()
                && config.getSearchTimeLimit() <= 0) {
            TopDocs docs;
            if (sort != null) {
                docs = indexSearcher.search(query, from + size, sort);
            } else {
                docs = indexSearcher.search(query, from + size);
            }
            return resultParser.parse(docs, null, null, null);
        }

        // group param will suppress facet, group, early termination and search time limit parameters
        if (request.isSetGroupParam()) {
            String groupField = request.getGroupParam().getGroupField();
            GroupingSearch groupingSearch = new GroupingSearch(groupField);
            groupingSearch.setGroupDocsLimit(request.getGroupParam().getGroupInnerLimit());
            if (sort != null) {
                groupingSearch.setGroupSort(sort);
                groupingSearch.setSortWithinGroup(sort);
                groupingSearch.setFillSortFields(true);
            }
            groupingSearch.setCachingInMB(8.0, true);
            groupingSearch.setAllGroups(true);
            TopGroups<TopDocs> topGroupedDocs = groupingSearch.search(indexSearcher, query, 0, from + size);
            return resultParser.parse(null, topGroupedDocs, null, null);
        }

        TopDocsCollector topDocsCollector;
        if (sort != null) {
            topDocsCollector = TopFieldCollector.create(sort, from + size, null, true, false, false, false);
        } else {
            topDocsCollector = TopScoreDocCollector.create(from + size, false);
        }

        LindenDocsCollector lindenDocsCollector;
        if (request.isSetEarlyParam()) {
            MergePolicy mergePolicy = indexWriter.getConfig().getMergePolicy();
            Sort mergePolicySort = null;
            if (mergePolicy instanceof SortingMergePolicyDecorator) {
                mergePolicySort = ((SortingMergePolicyDecorator) mergePolicy).getSort();
            }
            EarlyTerminationCollector earlyTerminationCollector = new EarlyTerminationCollector(
                    topDocsCollector, mergePolicySort, request.getEarlyParam().getMaxNum());
            lindenDocsCollector = new LindenDocsCollector(earlyTerminationCollector);
        } else {
            lindenDocsCollector = new LindenDocsCollector(topDocsCollector);
        }

        Collector collector = lindenDocsCollector;
        if (config.getSearchTimeLimit() > 0) {
            collector = new TimeLimitingCollector(lindenDocsCollector, TimeLimitingCollector.getGlobalCounter(),
                    config.getSearchTimeLimit());
        }

        // no facet param
        if (!request.isSetFacet()) {
            indexSearcher.search(query, collector);
            return resultParser.parse(lindenDocsCollector.topDocs(), null, null, null);
        }

        // facet search
        LindenFacet facetRequest = request.getFacet();
        FacetsCollector facetsCollector = new FacetsCollector();
        lindenDocsCollector.wrap(facetsCollector);

        Facets facets = null;
        if (facetRequest.isSetDrillDownDimAndPaths()) {
            // drillDown or drillSideways
            DrillDownQuery drillDownQuery = new DrillDownQuery(facetsConfig, query);
            List<LindenFacetDimAndPath> drillDownDimAndPaths = facetRequest.getDrillDownDimAndPaths();
            for (int i = 0; i < drillDownDimAndPaths.size(); ++i) {
                String fieldName = drillDownDimAndPaths.get(i).dim;
                if (drillDownDimAndPaths.get(i).path != null) {
                    drillDownQuery.add(fieldName, drillDownDimAndPaths.get(i).path.split("/"));
                } else {
                    drillDownQuery.add(fieldName);
                }
            }

            // drillSideways
            if (facetRequest.getFacetDrillingType() == FacetDrillingType.DRILLSIDEWAYS) {
                DrillSideways dillSideways = new DrillSideways(indexSearcher, facetsConfig,
                        searcherAndTaxonomy.taxonomyReader);
                DrillSideways.DrillSidewaysResult drillSidewaysResult = dillSideways.search(drillDownQuery,
                        collector);
                facets = drillSidewaysResult.facets;
            } else {
                // drillDown
                indexSearcher.search(drillDownQuery, collector);
                facets = new FastTaxonomyFacetCounts(searcherAndTaxonomy.taxonomyReader, facetsConfig,
                        facetsCollector);
            }
        } else {
            indexSearcher.search(query, collector);
            // Simple facet browsing
            if (facetRequest.isSetFacetParams()) {
                facets = new FastTaxonomyFacetCounts(searcherAndTaxonomy.taxonomyReader, facetsConfig,
                        facetsCollector);
            }
        }
        return resultParser.parse(lindenDocsCollector.topDocs(), null, facets, facetsCollector);
    } catch (Exception e) {
        throw new IOException(Throwables.getStackTraceAsString(e));
    } finally {
        lindenNRTSearcherManager.release(searcherAndTaxonomy);
    }
}

From source file:de.mirkosertic.desktopsearch.LuceneIndexHandler.java

License:Open Source License

public QueryResult performQuery(String aQueryString, String aBacklink, String aBasePath,
        Configuration aConfiguration, Map<String, String> aDrilldownFields) throws IOException {

    searcherManager.maybeRefreshBlocking();
    IndexSearcher theSearcher = searcherManager.acquire();
    SortedSetDocValuesReaderState theSortedSetState = new DefaultSortedSetDocValuesReaderState(
            theSearcher.getIndexReader());

    List<QueryResultDocument> theResultDocuments = new ArrayList<>();

    long theStartTime = System.currentTimeMillis();

    LOGGER.info("Querying for " + aQueryString);

    DateFormat theDateFormat = new SimpleDateFormat("dd.MMMM.yyyy", Locale.ENGLISH);

    try {/*from   w  w  w  . j  av a2 s .  c o m*/

        List<FacetDimension> theDimensions = new ArrayList<>();

        // Search only if a search query is given
        if (!StringUtils.isEmpty(aQueryString)) {

            Query theQuery = computeBooleanQueryFor(aQueryString);

            LOGGER.info(" query is " + theQuery);

            theQuery = theQuery.rewrite(theSearcher.getIndexReader());

            LOGGER.info(" rewritten query is " + theQuery);

            DrillDownQuery theDrilldownQuery = new DrillDownQuery(facetsConfig, theQuery);
            aDrilldownFields.entrySet().stream().forEach(aEntry -> {
                LOGGER.info(" with Drilldown " + aEntry.getKey() + " for " + aEntry.getValue());
                theDrilldownQuery.add(aEntry.getKey(), aEntry.getValue());
            });

            FacetsCollector theFacetCollector = new FacetsCollector();

            TopDocs theDocs = FacetsCollector.search(theSearcher, theDrilldownQuery, null,
                    aConfiguration.getNumberOfSearchResults(), theFacetCollector);
            SortedSetDocValuesFacetCounts theFacetCounts = new SortedSetDocValuesFacetCounts(theSortedSetState,
                    theFacetCollector);

            List<Facet> theAuthorFacets = new ArrayList<>();
            List<Facet> theFileTypesFacets = new ArrayList<>();
            List<Facet> theLastModifiedYearFacet = new ArrayList<>();
            List<Facet> theLanguageFacet = new ArrayList<>();

            LOGGER.info("Found " + theDocs.scoreDocs.length + " documents");

            // We need this cache to detect duplicate documents while searching for similarities
            Set<Integer> theUniqueDocumentsFound = new HashSet<>();

            Map<String, QueryResultDocument> theDocumentsByHash = new HashMap<>();

            for (int i = 0; i < theDocs.scoreDocs.length; i++) {
                int theDocumentID = theDocs.scoreDocs[i].doc;
                theUniqueDocumentsFound.add(theDocumentID);
                Document theDocument = theSearcher.doc(theDocumentID);

                String theUniqueID = theDocument.getField(IndexFields.UNIQUEID).stringValue();
                String theFoundFileName = theDocument.getField(IndexFields.FILENAME).stringValue();
                String theHash = theDocument.getField(IndexFields.CONTENTMD5).stringValue();
                QueryResultDocument theExistingDocument = theDocumentsByHash.get(theHash);
                if (theExistingDocument != null) {
                    theExistingDocument.addFileName(theFoundFileName);
                } else {
                    Date theLastModified = new Date(
                            theDocument.getField(IndexFields.LASTMODIFIED).numericValue().longValue());
                    SupportedLanguage theLanguage = SupportedLanguage
                            .valueOf(theDocument.getField(IndexFields.LANGUAGESTORED).stringValue());
                    String theFieldName;
                    if (analyzerCache.supportsLanguage(theLanguage)) {
                        theFieldName = analyzerCache.getFieldNameFor(theLanguage);
                    } else {
                        theFieldName = IndexFields.CONTENT;
                    }

                    String theOriginalContent = theDocument.getField(theFieldName).stringValue();

                    final Query theFinalQuery = theQuery;

                    ForkJoinTask<String> theHighligherResult = executorPool.submit(() -> {
                        StringBuilder theResult = new StringBuilder(theDateFormat.format(theLastModified));
                        theResult.append("&nbsp;-&nbsp;");
                        Highlighter theHighlighter = new Highlighter(new SimpleHTMLFormatter(),
                                new QueryScorer(theFinalQuery));
                        for (String theFragment : theHighlighter.getBestFragments(analyzer, theFieldName,
                                theOriginalContent, NUMBER_OF_FRAGMENTS)) {
                            if (theResult.length() > 0) {
                                theResult = theResult.append("...");
                            }
                            theResult = theResult.append(theFragment);
                        }
                        return theResult.toString();
                    });

                    int theNormalizedScore = (int) (theDocs.scoreDocs[i].score / theDocs.getMaxScore() * 5);

                    File theFileOnDisk = new File(theFoundFileName);
                    if (theFileOnDisk.exists()) {

                        boolean thePreviewAvailable = previewProcessor.previewAvailableFor(theFileOnDisk);

                        theExistingDocument = new QueryResultDocument(theDocumentID, theFoundFileName,
                                theHighligherResult,
                                Long.parseLong(theDocument.getField(IndexFields.LASTMODIFIED).stringValue()),
                                theNormalizedScore, theUniqueID, thePreviewAvailable);
                        theDocumentsByHash.put(theHash, theExistingDocument);
                        theResultDocuments.add(theExistingDocument);
                    }
                }
            }

            if (aConfiguration.isShowSimilarDocuments()) {

                MoreLikeThis theMoreLikeThis = new MoreLikeThis(theSearcher.getIndexReader());
                theMoreLikeThis.setAnalyzer(analyzer);
                theMoreLikeThis.setMinTermFreq(1);
                theMoreLikeThis.setMinDocFreq(1);
                theMoreLikeThis.setFieldNames(analyzerCache.getAllFieldNames());

                for (QueryResultDocument theDocument : theResultDocuments) {
                    Query theMoreLikeThisQuery = theMoreLikeThis.like(theDocument.getDocumentID());
                    TopDocs theMoreLikeThisTopDocs = theSearcher.search(theMoreLikeThisQuery, 5);
                    for (ScoreDoc theMoreLikeThisScoreDoc : theMoreLikeThisTopDocs.scoreDocs) {
                        int theSimilarDocument = theMoreLikeThisScoreDoc.doc;
                        if (theUniqueDocumentsFound.add(theSimilarDocument)) {
                            Document theMoreLikeThisDocument = theSearcher.doc(theSimilarDocument);
                            String theFilename = theMoreLikeThisDocument.getField(IndexFields.FILENAME)
                                    .stringValue();
                            theDocument.addSimilarFile(theFilename);
                        }
                    }
                }
            }

            LOGGER.info("Got Dimensions");
            for (FacetResult theResult : theFacetCounts.getAllDims(20000)) {
                String theDimension = theResult.dim;
                if ("author".equals(theDimension)) {
                    for (LabelAndValue theLabelAndValue : theResult.labelValues) {
                        if (!StringUtils.isEmpty(theLabelAndValue.label)) {
                            theAuthorFacets.add(new Facet(theLabelAndValue.label,
                                    theLabelAndValue.value.intValue(), aBasePath + "/" + encode(
                                            FacetSearchUtils.encode(theDimension, theLabelAndValue.label))));
                        }
                    }
                }
                if ("extension".equals(theDimension)) {
                    for (LabelAndValue theLabelAndValue : theResult.labelValues) {
                        if (!StringUtils.isEmpty(theLabelAndValue.label)) {
                            theFileTypesFacets.add(new Facet(theLabelAndValue.label,
                                    theLabelAndValue.value.intValue(), aBasePath + "/" + encode(
                                            FacetSearchUtils.encode(theDimension, theLabelAndValue.label))));
                        }
                    }
                }
                if ("last-modified-year".equals(theDimension)) {
                    for (LabelAndValue theLabelAndValue : theResult.labelValues) {
                        if (!StringUtils.isEmpty(theLabelAndValue.label)) {
                            theLastModifiedYearFacet.add(new Facet(theLabelAndValue.label,
                                    theLabelAndValue.value.intValue(), aBasePath + "/" + encode(
                                            FacetSearchUtils.encode(theDimension, theLabelAndValue.label))));
                        }
                    }
                }
                if (IndexFields.LANGUAGEFACET.equals(theDimension)) {
                    for (LabelAndValue theLabelAndValue : theResult.labelValues) {
                        if (!StringUtils.isEmpty(theLabelAndValue.label)) {
                            Locale theLocale = new Locale(theLabelAndValue.label);
                            theLanguageFacet.add(new Facet(theLocale.getDisplayLanguage(Locale.ENGLISH),
                                    theLabelAndValue.value.intValue(), aBasePath + "/" + encode(
                                            FacetSearchUtils.encode(theDimension, theLabelAndValue.label))));
                        }
                    }
                }

                LOGGER.info(" " + theDimension);
            }

            if (!theAuthorFacets.isEmpty()) {
                theDimensions.add(new FacetDimension("Author", theAuthorFacets));
            }
            if (!theLastModifiedYearFacet.isEmpty()) {
                theDimensions.add(new FacetDimension("Last modified", theLastModifiedYearFacet));
            }
            if (!theFileTypesFacets.isEmpty()) {
                theDimensions.add(new FacetDimension("File types", theFileTypesFacets));
            }
            if (!theLanguageFacet.isEmpty()) {
                theDimensions.add(new FacetDimension("Language", theLanguageFacet));
            }

            // Wait for all Tasks to complete for the search result highlighter
            ForkJoinTask.helpQuiesce();
        }

        long theDuration = System.currentTimeMillis() - theStartTime;

        LOGGER.info("Total amount of time : " + theDuration + "ms");

        return new QueryResult(System.currentTimeMillis() - theStartTime, theResultDocuments, theDimensions,
                theSearcher.getIndexReader().numDocs(), aBacklink);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        searcherManager.release(theSearcher);
    }
}

From source file:org.wso2.carbon.analytics.dataservice.core.indexing.AnalyticsDataIndexer.java

License:Open Source License

private CategoryDrillDownResponse drilldowncategories(int tenantId, IndexReader indexReader,
        TaxonomyReader taxonomyReader, CategoryDrillDownRequest drillDownRequest)
        throws AnalyticsIndexException {
    List<CategorySearchResultEntry> searchResults = new ArrayList<>();
    try {//w  ww  .j  a  va2s  . c  o m
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        FacetsCollector facetsCollector = new FacetsCollector(true);
        Map<String, ColumnDefinition> indices = this.lookupIndices(tenantId, drillDownRequest.getTableName());
        FacetsConfig config = this.getFacetsConfigurations(indices);
        DrillSideways drillSideways = new DrillSideways(indexSearcher, config, taxonomyReader);
        Query queryObj = new MatchAllDocsQuery();
        if (drillDownRequest.getQuery() != null && !drillDownRequest.getQuery().isEmpty()) {
            Analyzer analyzer = getPerFieldAnalyzerWrapper(indices);
            queryObj = (new AnalyticsQueryParser(analyzer, indices)).parse(drillDownRequest.getQuery());
        }
        DrillDownQuery drillDownQuery = new DrillDownQuery(config, queryObj);
        String[] path = drillDownRequest.getPath();
        if (path == null) {
            path = new String[] {};
        }
        drillDownQuery.add(drillDownRequest.getFieldName(), path);
        drillSideways.search(drillDownQuery, facetsCollector);
        ValueSource valueSource = this.getCompiledScoreFunction(drillDownRequest.getScoreFunction(), indices);
        Facets facets = new TaxonomyFacetSumValueSource(taxonomyReader, config, facetsCollector, valueSource);
        return getCategoryDrillDownResponse(drillDownRequest, searchResults, path, facets);
    } catch (IndexNotFoundException ignore) {
        return new CategoryDrillDownResponse(new ArrayList<CategorySearchResultEntry>(0));
    } catch (IOException e) {
        throw new AnalyticsIndexException("Error while performing drilldownCategories: " + e.getMessage(), e);
    } catch (org.apache.lucene.queryparser.classic.ParseException e) {
        throw new AnalyticsIndexException("Error while parsing query " + e.getMessage(), e);
    } finally {
        this.closeTaxonomyIndexReaders(indexReader, taxonomyReader);
    }
}

From source file:org.wso2.carbon.analytics.dataservice.core.indexing.AnalyticsDataIndexer.java

License:Open Source License

private DrillDownQuery createDrillDownQuery(AnalyticsDrillDownRequest drillDownRequest,
        Map<String, ColumnDefinition> indices, FacetsConfig config, String rangeField,
        AnalyticsDrillDownRange range) throws AnalyticsIndexException {
    Query languageQuery = new MatchAllDocsQuery();
    try {/*from www  .  j a  v a2s . co m*/
        if (drillDownRequest.getQuery() != null && !drillDownRequest.getQuery().isEmpty()) {
            Analyzer analyzer = getPerFieldAnalyzerWrapper(indices);
            languageQuery = new AnalyticsQueryParser(analyzer, indices).parse(drillDownRequest.getQuery());
        }
        DrillDownQuery drillDownQuery = new DrillDownQuery(config, languageQuery);
        if (range != null && rangeField != null) {
            drillDownQuery.add(rangeField,
                    NumericRangeQuery.newDoubleRange(rangeField, range.getFrom(), range.getTo(), true, false));
        }
        if (drillDownRequest.getCategoryPaths() != null && !drillDownRequest.getCategoryPaths().isEmpty()) {
            for (Map.Entry<String, List<String>> entry : drillDownRequest.getCategoryPaths().entrySet()) {
                List<String> path = entry.getValue();
                String[] pathAsArray;
                if (path == null || path.isEmpty()) {
                    pathAsArray = new String[] {};
                } else {
                    pathAsArray = path.toArray(new String[path.size()]);
                }
                drillDownQuery.add(entry.getKey(), pathAsArray);
            }
        }
        return drillDownQuery;
    } catch (org.apache.lucene.queryparser.classic.ParseException e) {
        throw new AnalyticsIndexException(
                "Error while parsing lucene query '" + languageQuery + "': " + e.getMessage(), e.getCause());
    }
}

From source file:perf.TaskParser.java

License:Apache License

public Task parseOneTask(String line) throws ParseException {

    final int spot = line.indexOf(':');
    if (spot == -1) {
        throw new RuntimeException("task line is malformed: " + line);
    }/*from w w w .ja v  a 2 s  . c  om*/
    final String category = line.substring(0, spot);

    int spot2 = line.indexOf(" #");
    if (spot2 == -1) {
        spot2 = line.length();
    }

    String text = line.substring(spot + 1, spot2).trim();
    String origText = text;

    final Task task;
    if (category.equals("Respell")) {
        task = new RespellTask(new Term(fieldName, text));
    } else {
        if (text.length() == 0) {
            throw new RuntimeException("null query line");
        }

        // Check for filter (eg: " +filter=0.5%")
        final Matcher m = filterPattern.matcher(text);
        Query filter;
        if (m.find()) {
            final double filterPct = Double.parseDouble(m.group(1));
            // Splice out the filter string:
            text = (text.substring(0, m.start(0)) + text.substring(m.end(0), text.length())).trim();
            filter = new RandomQuery(filterPct);
        } else {
            filter = null;
        }

        final Matcher m2 = minShouldMatchPattern.matcher(text);
        final int minShouldMatch;
        if (m2.find()) {
            minShouldMatch = Integer.parseInt(m2.group(1));
            // Splice out the minShouldMatch string:
            text = (text.substring(0, m2.start(0)) + text.substring(m2.end(0), text.length())).trim();
        } else {
            minShouldMatch = 0;
        }

        final List<String> facets = new ArrayList<String>();
        while (true) {
            int i = text.indexOf(" +facets:");
            if (i == -1) {
                break;
            }
            int j = text.indexOf(" ", i + 1);
            if (j == -1) {
                j = text.length();
            }
            facets.add(text.substring(i + 9, j));
            text = text.substring(0, i) + text.substring(j);
        }

        final List<String> drillDowns = new ArrayList<String>();

        // Eg: +drillDown:Date=2001,2004
        while (true) {
            int i = text.indexOf("+drillDown:");
            if (i == -1) {
                break;
            }
            int j = text.indexOf(" ", i);
            if (j == -1) {
                j = text.length();
            }

            String s = text.substring(i + 11, j);
            text = text.substring(0, i) + text.substring(j);

            drillDowns.add(s);
        }

        boolean doDrillSideways;
        if (text.indexOf("+drillSideways") != -1) {
            text = text.replace("+drillSideways", "");
            doDrillSideways = true;
            if (drillDowns.size() == 0) {
                throw new RuntimeException("cannot +drillSideways unless at least one +drillDown is defined");
            }
        } else {
            doDrillSideways = false;
        }

        final Sort sort;
        Query query;
        final String group;
        final boolean doHilite;

        boolean doStoredLoads = this.doStoredLoads;

        if (text.startsWith("hilite//")) {
            doHilite = true;
            text = text.substring(8);

            // Highlighting does its own loading
            doStoredLoads = false;
        } else {
            doHilite = false;
        }

        if (text.startsWith("near//")) {
            final int spot3 = text.indexOf(' ');
            if (spot3 == -1) {
                throw new RuntimeException("failed to parse query=" + text);
            }
            query = new SpanNearQuery(
                    new SpanQuery[] { new SpanTermQuery(new Term(fieldName, text.substring(6, spot3))),
                            new SpanTermQuery(new Term(fieldName, text.substring(spot3 + 1).trim())) },
                    10, true);
            sort = null;
            group = null;
        } else if (text.startsWith("multiPhrase//(")) {
            int colon = text.indexOf(':');
            if (colon == -1) {
                throw new RuntimeException("failed to parse query=" + text);
            }
            String field = text.substring("//multiPhrase(".length(), colon);
            MultiPhraseQuery.Builder b = new MultiPhraseQuery.Builder();
            int endParen = text.indexOf(')');
            if (endParen == -1) {
                throw new RuntimeException("failed to parse query=" + text);
            }
            String queryText = text.substring(colon + 1, endParen);
            String elements[] = queryText.split("\\s+");
            for (int i = 0; i < elements.length; i++) {
                String words[] = elements[i].split("\\|");
                Term terms[] = new Term[words.length];
                for (int j = 0; j < words.length; j++) {
                    terms[j] = new Term(field, words[j]);
                }
                b.add(terms);
            }
            query = b.build();
            sort = null;
            group = null;
        } else if (text.startsWith("disjunctionMax//")) {
            final int spot3 = text.indexOf(' ');
            if (spot3 == -1) {
                throw new RuntimeException("failed to parse query=" + text);
            }
            List<Query> clauses = new ArrayList<Query>();
            clauses.add(new TermQuery(new Term(fieldName, text.substring(16, spot3))));
            clauses.add(new TermQuery(new Term(fieldName, text.substring(spot3 + 1).trim())));
            DisjunctionMaxQuery dismax = new DisjunctionMaxQuery(clauses, 1f);
            query = dismax;
            sort = null;
            group = null;
        } else if (text.startsWith("nrq//")) {
            // field start end
            final int spot3 = text.indexOf(' ');
            if (spot3 == -1) {
                throw new RuntimeException("failed to parse query=" + text);
            }
            final int spot4 = text.indexOf(' ', spot3 + 1);
            if (spot4 == -1) {
                throw new RuntimeException("failed to parse query=" + text);
            }
            final String nrqFieldName = text.substring(5, spot3);
            final int start = Integer.parseInt(text.substring(1 + spot3, spot4));
            final int end = Integer.parseInt(text.substring(1 + spot4));
            query = IntPoint.newRangeQuery(nrqFieldName, start, end);
            sort = null;
            group = null;
        } else if (text.startsWith("datetimesort//")) {
            throw new IllegalArgumentException("use lastmodndvsort instead");
        } else if (text.startsWith("titlesort//")) {
            throw new IllegalArgumentException("use titledvsort instead");
        } else if (text.startsWith("titledvsort//")) {
            sort = titleDVSort;
            query = queryParser.parse(text.substring(13, text.length()));
            group = null;
        } else if (text.startsWith("lastmodndvsort//")) {
            sort = lastModNDVSort;
            query = queryParser.parse(text.substring(16, text.length()));
            group = null;
        } else if (text.startsWith("group100//")) {
            group = "group100";
            query = queryParser.parse(text.substring(10, text.length()));
            sort = null;
        } else if (text.startsWith("group10K//")) {
            group = "group10K";
            query = queryParser.parse(text.substring(10, text.length()));
            sort = null;
        } else if (text.startsWith("group100K//")) {
            group = "group100K";
            query = queryParser.parse(text.substring(11, text.length()));
            sort = null;
        } else if (text.startsWith("group1M//")) {
            group = "group1M";
            query = queryParser.parse(text.substring(9, text.length()));
            sort = null;
        } else if (text.startsWith("groupblock1pass//")) {
            group = "groupblock1pass";
            query = queryParser.parse(text.substring(17, text.length()));
            sort = null;
        } else if (text.startsWith("groupblock//")) {
            group = "groupblock";
            query = queryParser.parse(text.substring(12, text.length()));
            sort = null;
        } else {
            group = null;
            query = queryParser.parse(text);
            sort = null;
        }

        if (query.toString().equals("")) {
            throw new RuntimeException("query text \"" + text + "\" parsed to empty query");
        }

        if (minShouldMatch != 0) {
            if (!(query instanceof BooleanQuery)) {
                throw new RuntimeException(
                        "minShouldMatch can only be used with BooleanQuery: query=" + origText);
            }
            Builder b = new BooleanQuery.Builder();
            b.setMinimumNumberShouldMatch(minShouldMatch);
            for (BooleanClause clause : ((BooleanQuery) query)) {
                b.add(clause);
            }
            query = b.build();
        }

        Query query2;

        if (!drillDowns.isEmpty()) {
            DrillDownQuery q = new DrillDownQuery(state.facetsConfig, query);
            for (String s : drillDowns) {
                int i = s.indexOf('=');
                if (i == -1) {
                    throw new IllegalArgumentException("drilldown is missing =");
                }
                String dim = s.substring(0, i);
                String values = s.substring(i + 1);

                while (true) {
                    i = values.indexOf(',');
                    if (i == -1) {
                        q.add(dim, values);
                        break;
                    }
                    q.add(dim, values.substring(0, i));
                    values = values.substring(i + 1);
                }
            }
            query2 = q;
        } else {
            query2 = query;
        }

        if (filter != null) {
            query2 = new BooleanQuery.Builder().add(query2, Occur.MUST).add(filter, Occur.FILTER).build();
        }

        /*
          if (category.startsWith("Or")) {
          for(BooleanClause clause : ((BooleanQuery) query).clauses()) {
          ((TermQuery) clause.getQuery()).setNoSkip();
          }
          }
        */

        task = new SearchTask(category, query2, sort, group, topN, doHilite, doStoredLoads, facets,
                doDrillSideways);
    }

    return task;
}

From source file:test.lucene.SimpleFacetsExample.java

License:Apache License

private void facetsWithSearch() throws IOException {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    IndexSearcher searcher = new IndexSearcher(indexReader);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

    FacetsCollector fc = new FacetsCollector();
    //1./* w  w w  .j  a va  2  s . c o  m*/
    System.out.println("----------");
    TermQuery query = new TermQuery(new Term("device", ""));
    FacetsCollector.search(searcher, query, 10, fc);
    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
    List<FacetResult> results = facets.getAllDims(10);
    //3,??2?1;4G 2?4G 1
    for (FacetResult tmp : results) {
        System.out.println(tmp);
    }
    //2.drill down??
    System.out.println("-----?-----");
    DrillDownQuery drillDownQuery = new DrillDownQuery(config, query);
    drillDownQuery.add("brand", "?");
    FacetsCollector fc1 = new FacetsCollector();//?newcollector?
    FacetsCollector.search(searcher, drillDownQuery, 10, fc1);
    facets = new FastTaxonomyFacetCounts(taxoReader, config, fc1);
    results = facets.getAllDims(10);
    //?24G 1?4G 1
    for (FacetResult tmp : results) {
        System.out.println(tmp);
    }

    //3.drill down?4G
    System.out.println("-----4G?-----");
    drillDownQuery.add("network", "4G");
    FacetsCollector fc2 = new FacetsCollector();
    FacetsCollector.search(searcher, drillDownQuery, 10, fc2);
    facets = new FastTaxonomyFacetCounts(taxoReader, config, fc2);
    results = facets.getAllDims(10);
    for (FacetResult tmp : results) {
        System.out.println(tmp);
    }

    //4.drill sideways??
    //???(?)sideways
    System.out.println("-----?drill sideways-----");
    DrillSideways ds = new DrillSideways(searcher, config, taxoReader);
    DrillDownQuery drillDownQuery1 = new DrillDownQuery(config, query);
    drillDownQuery1.add("brand", "?");
    DrillSidewaysResult result = ds.search(drillDownQuery1, 10);
    results = result.facets.getAllDims(10);
    for (FacetResult tmp : results) {
        System.out.println(tmp);
    }

    indexReader.close();
    taxoReader.close();
}