/** User runs a query and aggregates facets by summing their association values. */
  private List<FacetResult> sumAssociations() throws IOException {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    IndexSearcher searcher = new IndexSearcher(indexReader);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

    FacetsCollector fc = new FacetsCollector();

    // MatchAllDocsQuery is for "browsing" (counts facets
    // for all non-deleted docs in the index); normally
    // you'd use a "normal" query:
    FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);

    Facets tags = new TaxonomyFacetSumIntAssociations("$tags", taxoReader, config, fc);
    Facets genre = new TaxonomyFacetSumFloatAssociations("$genre", taxoReader, config, fc);

    // Retrieve results
    List<FacetResult> results = new ArrayList<>();
    results.add(tags.getTopChildren(10, "tags"));
    results.add(genre.getTopChildren(10, "genre"));

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

    return results;
  }
  private List<FacetResult> facetsOnly() throws IOException {

    FacetsCollector fc = new FacetsCollector();

    // MatchAllDocsQuery is for "browsing" (counts facets
    // for all non-deleted docs in the index); normally
    // you'd use a "normal" query:
    TopDocs docs = FacetsCollector.search(indexSearcher, new MatchAllDocsQuery(), 10, fc);

    //        indexSearcher.search(new MatchAllDocsQuery(), null /*Filter */, fc);

    // Retrieve results
    List<FacetResult> results = new ArrayList<FacetResult>();
    System.out.println("results size: " + results.size());

    // Count both "Publish Date" and "Author" dimensions
    Facets facets = new FastTaxonomyFacetCounts(taxoReader, taxoConfig, fc);

    results.add(facets.getTopChildren(20, "objects"));
    //        results.add(facets.getTopChildren(10, "Publish Date"));

    List<FacetResult> result = facets.getAllDims(10);

    //        indexReader.close();
    //        taxoReader.close();

    return result;
  }
  private TopDocsName facetsWithSearch(String query) throws IOException, ParseException {

    FacetsCollector fc = new FacetsCollector();

    ComplexPhraseQueryParser queryParser =
        new ComplexPhraseQueryParser("content", new StandardAnalyzer());

    Query luceneQuery = queryParser.parse(query);

    TopDocs docs = FacetsCollector.search(indexSearcher, luceneQuery, 400, fc);

    List<TopDocName> topDocNames = new ArrayList<TopDocName>();

    for (ScoreDoc scoreDoc : docs.scoreDocs) {
      String filename = indexSearcher.doc(scoreDoc.doc).get("filename");
      float score = scoreDoc.score;
      TopDocName docname = new TopDocName(score, filename.substring(0, filename.length()));
      topDocNames.add(docname);
    }

    List<FacetResult> facetResults = new ArrayList<FacetResult>();

    // Count both "Publish Date" and "Author" dimensions
    Facets facets = new FastTaxonomyFacetCounts(taxoReader, taxoConfig, fc);
    facetResults.add(facets.getTopChildren(11, "person"));
    facetResults.add(facets.getTopChildren(20, "objects"));
    TopDocsName results = new TopDocsName(1.0f, topDocNames, facetResults);

    return results;
  }
Exemplo n.º 4
0
 /**
  * TODO: Facets are based on DocValue fields and not on separate taxonomy index, this will be
  * slower but will take much less memory. Provide option to support hierarchical facets support
  * and add taxonomy index based faceting.
  *
  * @param user
  * @param req
  * @return
  * @throws ParseException
  * @throws IOException
  */
 public Map<Long, Integer> getTrend(User user, Request req) throws ParseException, IOException {
   user = userService.validate(user);
   Facets facets = runFacetedSearch(req, user);
   FacetResult result = facets.getTopChildren(10, req.trendField.name());
   Map<Long, Integer> countsByRes = new LinkedHashMap<>();
   for (LabelAndValue labelAndValue : result.labelValues) {
     countsByRes.put(Long.valueOf(labelAndValue.label), labelAndValue.value.intValue());
   }
   return countsByRes;
 }
Exemplo n.º 5
0
  /** User runs a query and counts facets. */
  public FacetResult search() throws IOException {

    FacetsCollector fc = new FacetsCollector();

    searcher.search(new MatchAllDocsQuery(), fc);

    Facets facets =
        new DoubleRangeFacetCounts(
            "field",
            getDistanceValueSource(),
            fc,
            getBoundingBoxQuery(ORIGIN_LATITUDE, ORIGIN_LONGITUDE, 10.0),
            ONE_KM,
            TWO_KM,
            FIVE_KM,
            TEN_KM);

    return facets.getTopChildren(10, "field");
  }
  @Test
  public void testAllCounts() throws Exception {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
    IndexSearcher searcher = newSearcher(indexReader);

    FacetsCollector sfc = new FacetsCollector();
    searcher.search(new MatchAllDocsQuery(), sfc);

    Facets facets = getTaxonomyFacetCounts(taxoReader, getConfig(), sfc);

    FacetResult result = facets.getTopChildren(NUM_CHILDREN_CP_A, CP_A);
    assertEquals(-1, result.value.intValue());
    int prevValue = Integer.MAX_VALUE;
    for (LabelAndValue labelValue : result.labelValues) {
      assertEquals(allExpectedCounts.get(CP_A + "/" + labelValue.label), labelValue.value);
      assertTrue(
          "wrong sort order of sub results: labelValue.value="
              + labelValue.value
              + " prevValue="
              + prevValue,
          labelValue.value.intValue() <= prevValue);
      prevValue = labelValue.value.intValue();
    }

    result = facets.getTopChildren(NUM_CHILDREN_CP_B, CP_B);
    assertEquals(allExpectedCounts.get(CP_B), result.value);
    prevValue = Integer.MAX_VALUE;
    for (LabelAndValue labelValue : result.labelValues) {
      assertEquals(allExpectedCounts.get(CP_B + "/" + labelValue.label), labelValue.value);
      assertTrue(
          "wrong sort order of sub results: labelValue.value="
              + labelValue.value
              + " prevValue="
              + prevValue,
          labelValue.value.intValue() <= prevValue);
      prevValue = labelValue.value.intValue();
    }

    IOUtils.close(indexReader, taxoReader);
  }
  /** User drills down on 'tags/solr'. */
  private FacetResult drillDown() throws IOException {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    IndexSearcher searcher = new IndexSearcher(indexReader);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

    // Passing no baseQuery means we drill down on all
    // documents ("browse only"):
    DrillDownQuery q = new DrillDownQuery(config);

    // Now user drills down on Publish Date/2010:
    q.add("tags", "solr");
    FacetsCollector fc = new FacetsCollector();
    FacetsCollector.search(searcher, q, 10, fc);

    // Retrieve results
    Facets facets = new TaxonomyFacetSumFloatAssociations("$genre", taxoReader, config, fc);
    FacetResult result = facets.getTopChildren(10, "genre");

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

    return result;
  }
  private TopDocsName drillDown(Map<String, String> facets_value) throws IOException {

    // Passing no baseQuery means we drill down on all
    // documents ("browse only"):

    DrillDownQuery q = new DrillDownQuery(taxoConfig);

    for (Map.Entry<String, String> entry : facets_value.entrySet()) {
      q.add(entry.getKey(), entry.getValue());
    }

    FacetsCollector fc = new FacetsCollector();
    TopDocs docs = FacetsCollector.search(indexSearcher, q, 100000, fc);

    List<TopDocName> topDocNames = new ArrayList<TopDocName>();

    for (ScoreDoc scoreDoc : docs.scoreDocs) {
      String filename = indexSearcher.doc(scoreDoc.doc).get("filename");
      String videoID = indexSearcher.doc(scoreDoc.doc).get("videoID");
      String frameNumber = indexSearcher.doc(scoreDoc.doc).get("frameNumber");
      float score = scoreDoc.score;
      TopDocName docname = new TopDocName(score, filename, videoID, frameNumber);
      topDocNames.add(docname);
    }

    List<FacetResult> facetResults = new ArrayList<FacetResult>();

    // Count both "Publish Date" and "Author" dimensions
    Facets facets = new FastTaxonomyFacetCounts(taxoReader, taxoConfig, fc);
    facetResults.add(facets.getTopChildren(11, "person"));
    facetResults.add(facets.getTopChildren(20, "objects"));
    facetResults.add(facets.getTopChildren(200, "background"));
    TopDocsName results = new TopDocsName(1.0f, topDocNames, facetResults);

    return results;
  }
  @Test
  public void testNoParents() throws Exception {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
    IndexSearcher searcher = newSearcher(indexReader);

    FacetsCollector sfc = new FacetsCollector();
    searcher.search(new MatchAllDocsQuery(), sfc);

    Facets facets = getTaxonomyFacetCounts(taxoReader, getConfig(), sfc);

    FacetResult result = facets.getTopChildren(NUM_CHILDREN_CP_C, CP_C);
    assertEquals(allExpectedCounts.get(CP_C), result.value);
    for (LabelAndValue labelValue : result.labelValues) {
      assertEquals(allExpectedCounts.get(CP_C + "/" + labelValue.label), labelValue.value);
    }
    result = facets.getTopChildren(NUM_CHILDREN_CP_D, CP_D);
    assertEquals(allExpectedCounts.get(CP_C), result.value);
    for (LabelAndValue labelValue : result.labelValues) {
      assertEquals(allExpectedCounts.get(CP_D + "/" + labelValue.label), labelValue.value);
    }

    IOUtils.close(indexReader, taxoReader);
  }
  @Test
  public void testBigNumResults() throws Exception {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
    IndexSearcher searcher = newSearcher(indexReader);

    FacetsCollector sfc = new FacetsCollector();
    searcher.search(new MatchAllDocsQuery(), sfc);

    Facets facets = getTaxonomyFacetCounts(taxoReader, getConfig(), sfc);

    FacetResult result = facets.getTopChildren(Integer.MAX_VALUE, CP_A);
    assertEquals(-1, result.value.intValue());
    for (LabelAndValue labelValue : result.labelValues) {
      assertEquals(allExpectedCounts.get(CP_A + "/" + labelValue.label), labelValue.value);
    }
    result = facets.getTopChildren(Integer.MAX_VALUE, CP_B);
    assertEquals(allExpectedCounts.get(CP_B), result.value);
    for (LabelAndValue labelValue : result.labelValues) {
      assertEquals(allExpectedCounts.get(CP_B + "/" + labelValue.label), labelValue.value);
    }

    IOUtils.close(indexReader, taxoReader);
  }
  @Test
  public void testDifferentNumResults() throws Exception {
    // test the collector w/ FacetRequests and different numResults
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
    IndexSearcher searcher = newSearcher(indexReader);

    FacetsCollector sfc = new FacetsCollector();
    TermQuery q = new TermQuery(A);
    searcher.search(q, sfc);
    Facets facets = getTaxonomyFacetCounts(taxoReader, getConfig(), sfc);
    FacetResult result = facets.getTopChildren(NUM_CHILDREN_CP_A, CP_A);
    assertEquals(-1, result.value.intValue());
    for (LabelAndValue labelValue : result.labelValues) {
      assertEquals(termExpectedCounts.get(CP_A + "/" + labelValue.label), labelValue.value);
    }
    result = facets.getTopChildren(NUM_CHILDREN_CP_B, CP_B);
    assertEquals(termExpectedCounts.get(CP_B), result.value);
    for (LabelAndValue labelValue : result.labelValues) {
      assertEquals(termExpectedCounts.get(CP_B + "/" + labelValue.label), labelValue.value);
    }

    IOUtils.close(indexReader, taxoReader);
  }