Example usage for org.apache.lucene.facet Facets getAllDims

List of usage examples for org.apache.lucene.facet Facets getAllDims

Introduction

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

Prototype

public abstract List<FacetResult> getAllDims(int topN) throws IOException;

Source Link

Document

Returns topN labels for any dimension that had hits, sorted by the number of hits that dimension matched; this is used for "sparse" faceting, where many different dimensions were indexed, for example depending on the type of document.

Usage

From source file:org.efaps.admin.index.Searcher.java

License:Apache License

/**
 * Search.//ww w  .j a  v  a  2 s.  c o m
 *
 * @param _search the search
 * @return the search result
 * @throws EFapsException on error
 */
protected SearchResult executeSearch(final ISearch _search) throws EFapsException {
    final SearchResult ret = new SearchResult();
    try {
        LOG.debug("Starting search with: {}", _search.getQuery());
        final StandardQueryParser queryParser = new StandardQueryParser(Index.getAnalyzer());
        queryParser.setAllowLeadingWildcard(true);
        if (EFapsSystemConfiguration.get().containsAttributeValue(KernelSettings.INDEXDEFAULTOP)) {
            queryParser.setDefaultOperator(EnumUtils.getEnum(StandardQueryConfigHandler.Operator.class,
                    EFapsSystemConfiguration.get().getAttributeValue(KernelSettings.INDEXDEFAULTOP)));
        } else {
            queryParser.setDefaultOperator(StandardQueryConfigHandler.Operator.AND);
        }
        final Query query = queryParser.parse(_search.getQuery(), "ALL");

        final IndexReader reader = DirectoryReader.open(Index.getDirectory());
        Sort sort = _search.getSort();
        if (sort == null) {
            sort = new Sort(new SortField(Key.CREATED.name(), SortField.Type.LONG, true));
        }

        final FacetsConfig facetConfig = Index.getFacetsConfig();
        final DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(Index.getTaxonomyDirectory());

        final IndexSearcher searcher = new IndexSearcher(reader);
        final FacetsCollector fc = new FacetsCollector();

        final TopFieldDocs topFieldDocs = FacetsCollector.search(searcher, query, _search.getNumHits(), sort,
                fc);

        if (_search.getConfigs().contains(SearchConfig.ACTIVATE_DIMENSION)) {
            final Facets facets = new FastTaxonomyFacetCounts(taxoReader, facetConfig, fc);

            for (final FacetResult result : facets.getAllDims(1000)) {
                LOG.debug("FacetResult {}.", result);
                final DimConfig dimConfig = facetConfig.getDimConfig(result.dim);
                final Dimension retDim = new Dimension().setKey(result.dim);
                ret.getDimensions().add(retDim);
                for (final LabelAndValue labelValue : result.labelValues) {
                    final DimValue dimValue = new DimValue().setLabel(labelValue.label)
                            .setValue(labelValue.value.intValue());
                    dimValue.setPath(new String[] { retDim.getKey() });
                    retDim.getValues().add(dimValue);
                    if (dimConfig.hierarchical) {
                        addSubDimension(facets, dimValue, result.dim, labelValue.label);
                    }
                }
            }
        }
        ret.setHitCount(topFieldDocs.totalHits);
        if (ret.getHitCount() > 0) {
            final ScoreDoc[] hits = topFieldDocs.scoreDocs;

            LOG.debug("Found {} hits.", hits.length);
            for (int i = 0; i < hits.length; ++i) {
                final Document doc = searcher.doc(hits[i].doc);
                final String oid = doc.get(Key.OID.name());
                final String text = doc.get(Key.MSGPHRASE.name());
                LOG.debug("{}. {}\t {}", i + 1, oid, text);
                final Instance instance = Instance.get(oid);
                final List<Instance> list;
                if (this.typeMapping.containsKey(instance.getType())) {
                    list = this.typeMapping.get(instance.getType());
                } else {
                    list = new ArrayList<Instance>();
                    this.typeMapping.put(instance.getType(), list);
                }
                list.add(instance);
                final Element element = new Element().setOid(oid).setText(text);
                for (final Entry<String, Collection<String>> entry : _search.getResultFields().entrySet()) {
                    for (final String name : entry.getValue()) {
                        final String value = doc.get(name);
                        if (value != null) {
                            element.addField(name, value);
                        }
                    }
                }
                this.elements.put(instance, element);
            }
        }
        reader.close();
        checkAccess();
        ret.getElements().addAll(this.elements.values());
    } catch (final IOException | QueryNodeException e) {
        LOG.error("Catched Exception", e);
    }
    return ret;
}

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.//from   w  w  w  .  jav a  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();
}