Java Code Examples for org.apache.lucene.search.Hits#doc()

The following examples show how to use org.apache.lucene.search.Hits#doc() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndexManager.java    From spacewalk with GNU General Public License v2.0 6 votes vote down vote up
private void debugExplainResults(String indexName, Hits hits, IndexSearcher searcher,
        Query q, Set<Term> queryTerms)
    throws IOException {
    log.debug("Parsed Query is " + q.toString());
    log.debug("Looking at index:  " + indexName);
    for (int i = 0; i < hits.length(); i++) {
        if ((i < 10)) {
            Document doc = hits.doc(i);
            Float score = hits.score(i);
            Explanation ex = searcher.explain(q, hits.id(i));
            log.debug("Looking at hit<" + i + ", " + hits.id(i) + ", " + score +
                    ">: " + doc);
            log.debug("Explanation: " + ex);
            MatchingField match = new MatchingField(q.toString(), doc, queryTerms);
            String fieldName = match.getFieldName();
            String fieldValue = match.getFieldValue();
            log.debug("Guessing that matched fieldName is " + fieldName + " = " +
                    fieldValue);
        }
    }
}
 
Example 2
Source File: TestMixedDirectory.java    From RDFS with Apache License 2.0 6 votes vote down vote up
private void verify(Directory dir, int expectedHits) throws IOException {
  IndexSearcher searcher = new IndexSearcher(dir);
  Hits hits = searcher.search(new TermQuery(new Term("content", "apache")));
  int numHits = hits.length();

  assertEquals(expectedHits, numHits);

  int[] docs = new int[numHits];
  for (int i = 0; i < numHits; i++) {
    Document hit = hits.doc(i);
    docs[Integer.parseInt(hit.get("id"))]++;
  }
  for (int i = 0; i < numHits; i++) {
    assertEquals(1, docs[i]);
  }

  searcher.close();
}
 
Example 3
Source File: TestMixedDirectory.java    From hadoop-gpu with Apache License 2.0 6 votes vote down vote up
private void verify(Directory dir, int expectedHits) throws IOException {
  IndexSearcher searcher = new IndexSearcher(dir);
  Hits hits = searcher.search(new TermQuery(new Term("content", "apache")));
  int numHits = hits.length();

  assertEquals(expectedHits, numHits);

  int[] docs = new int[numHits];
  for (int i = 0; i < numHits; i++) {
    Document hit = hits.doc(i);
    docs[Integer.parseInt(hit.get("id"))]++;
  }
  for (int i = 0; i < numHits; i++) {
    assertEquals(1, docs[i]);
  }

  searcher.close();
}
 
Example 4
Source File: NGramTestSetup.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
protected void displayHits(Hits hits) throws IOException {
    for (int i = 0; i < hits.length(); i++) {
        Document doc = hits.doc(i);
        String name = doc.get("name");
        String description = doc.get("description");
        log.info("Hit<" + i + "> Score< " + hits.score(i) + ">  name = <" +
                name + "> description = <" + description + ">");
    }
}
 
Example 5
Source File: IndexManager.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
private void debugExplainResults(String indexName, Hits hits, IndexSearcher searcher,
        Query q, Set<Term> queryTerms)
    throws IOException {
    log.debug("Parsed Query is " + q.toString());
    log.debug("Looking at index:  " + indexName);
    for (int i = 0; i < hits.length(); i++) {
        if ((i < 10)) {
            Document doc = hits.doc(i);
            Float score = hits.score(i);
            Explanation ex = searcher.explain(q, hits.id(i));
            log.debug("Looking at hit<" + i + ", " + hits.id(i) + ", " + score +
                    ">: " + doc);
            log.debug("Explanation: " + ex);
            MatchingField match = new MatchingField(q.toString(), doc, queryTerms);
            String fieldName = match.getFieldName();
            String fieldValue = match.getFieldValue();
            log.debug("Guessing that matched fieldName is " + fieldName + " = " +
                    fieldValue);
        }
    }
}
 
Example 6
Source File: NGramTestSetup.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
protected void displayHits(Hits hits) throws IOException {
    for (int i = 0; i < hits.length(); i++) {
        Document doc = hits.doc(i);
        String name = doc.get("name");
        String description = doc.get("description");
        log.info("Hit<" + i + "> Score< " + hits.score(i) + ">  name = <" +
                name + "> description = <" + description + ">");
    }
}
 
Example 7
Source File: TestDistributionPolicy.java    From RDFS with Apache License 2.0 5 votes vote down vote up
private void verify(Shard[] shards) throws IOException {
  // verify the index
  IndexReader[] readers = new IndexReader[shards.length];
  for (int i = 0; i < shards.length; i++) {
    Directory dir =
        new FileSystemDirectory(fs, new Path(shards[i].getDirectory()),
            false, conf);
    readers[i] = IndexReader.open(dir);
  }

  IndexReader reader = new MultiReader(readers);
  IndexSearcher searcher = new IndexSearcher(reader);
  Hits hits = searcher.search(new TermQuery(new Term("content", "apache")));
  assertEquals(0, hits.length());

  hits = searcher.search(new TermQuery(new Term("content", "hadoop")));
  assertEquals(numDocsPerRun / 2, hits.length());

  int[] counts = new int[numDocsPerRun];
  for (int i = 0; i < hits.length(); i++) {
    Document doc = hits.doc(i);
    counts[Integer.parseInt(doc.get("id"))]++;
  }

  for (int i = 0; i < numDocsPerRun; i++) {
    if (i % 2 == 0) {
      assertEquals(0, counts[i]);
    } else {
      assertEquals(1, counts[i]);
    }
  }

  searcher.close();
  reader.close();
}
 
Example 8
Source File: TestDistributionPolicy.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
private void verify(Shard[] shards) throws IOException {
  // verify the index
  IndexReader[] readers = new IndexReader[shards.length];
  for (int i = 0; i < shards.length; i++) {
    Directory dir =
        new FileSystemDirectory(fs, new Path(shards[i].getDirectory()),
            false, conf);
    readers[i] = IndexReader.open(dir);
  }

  IndexReader reader = new MultiReader(readers);
  IndexSearcher searcher = new IndexSearcher(reader);
  Hits hits = searcher.search(new TermQuery(new Term("content", "apache")));
  assertEquals(0, hits.length());

  hits = searcher.search(new TermQuery(new Term("content", "hadoop")));
  assertEquals(numDocsPerRun / 2, hits.length());

  int[] counts = new int[numDocsPerRun];
  for (int i = 0; i < hits.length(); i++) {
    Document doc = hits.doc(i);
    counts[Integer.parseInt(doc.get("id"))]++;
  }

  for (int i = 0; i < numDocsPerRun; i++) {
    if (i % 2 == 0) {
      assertEquals(0, counts[i]);
    } else {
      assertEquals(1, counts[i]);
    }
  }

  searcher.close();
  reader.close();
}
 
Example 9
Source File: IndexManager.java    From uyuni with GNU General Public License v2.0 4 votes vote down vote up
private List<Result> processHits(String indexName, Hits hits, Set<Term> queryTerms,
        String query, String lang)
    throws IOException {
    List<Result> retval = new ArrayList<Result>();
    for (int x = 0; x < hits.length(); x++) {
        Document doc = hits.doc(x);
        Result pr = null;
        if (!isScoreAcceptable(indexName, hits, x, query)) {
            break;
        }
        if (indexName.compareTo(BuilderFactory.DOCS_TYPE) == 0) {
            pr = new DocResult(x, hits.score(x), doc);
            String summary = lookupDocSummary(doc, query, lang);
            if (summary != null) {
                ((DocResult)pr).setSummary(summary);
            }
        }
        else if (indexName.compareTo(BuilderFactory.HARDWARE_DEVICE_TYPE) == 0) {
            pr = new HardwareDeviceResult(x, hits.score(x), doc);
        }
        else if (indexName.compareTo(BuilderFactory.SNAPSHOT_TAG_TYPE)  == 0) {
            pr = new SnapshotTagResult(x, hits.score(x), doc);
        }
        else if (indexName.compareTo(BuilderFactory.SERVER_CUSTOM_INFO_TYPE) == 0) {
            pr = new ServerCustomInfoResult(x, hits.score(x), doc);
        }
        else if (indexName.compareTo(BuilderFactory.XCCDF_IDENT_TYPE) == 0) {
            pr = new Result(x,
                    doc.getField("id").stringValue(),
                    doc.getField("identifier").stringValue(),
                    hits.score(x));
        }
        else {
            pr = new Result(x,
                    doc.getField("id").stringValue(),
                    doc.getField("name").stringValue(),
                    hits.score(x));
        }
        if (log.isDebugEnabled()) {
            log.debug("Hit[" + x + "] Score = " + hits.score(x) + ", Result = " + pr);
        }
        /**
         * matchingField will help the webUI to understand what field was responsible
         * for this match.  Later implementation should use "Explanation" to determine
         * field, for now we will simply grab one term and return it's field.
         */
        try {
            MatchingField match = new MatchingField(query, doc, queryTerms);
            pr.setMatchingField(match.getFieldName());
            pr.setMatchingFieldValue(match.getFieldValue());
            log.info("hit[" + x + "] matchingField is being set to: <" +
                pr.getMatchingField() + "> based on passed in query field.  " +
                "matchingFieldValue = " + pr.getMatchingFieldValue());
        }
        catch (Exception e) {
            log.error("Caught exception: ", e);
        }
        if (pr != null) {
            retval.add(pr);
        }
        if (maxHits > 0 && x == maxHits) {
            break;
        }
    }
    return retval;
}
 
Example 10
Source File: IndexManager.java    From spacewalk with GNU General Public License v2.0 4 votes vote down vote up
private List<Result> processHits(String indexName, Hits hits, Set<Term> queryTerms,
        String query, String lang)
    throws IOException {
    List<Result> retval = new ArrayList<Result>();
    for (int x = 0; x < hits.length(); x++) {
        Document doc = hits.doc(x);
        Result pr = null;
        if (!isScoreAcceptable(indexName, hits, x, query)) {
            break;
        }
        if (indexName.compareTo(BuilderFactory.DOCS_TYPE) == 0) {
            pr = new DocResult(x, hits.score(x), doc);
            String summary = lookupDocSummary(doc, query, lang);
            if (summary != null) {
                ((DocResult)pr).setSummary(summary);
            }
        }
        else if (indexName.compareTo(BuilderFactory.HARDWARE_DEVICE_TYPE) == 0) {
            pr = new HardwareDeviceResult(x, hits.score(x), doc);
        }
        else if (indexName.compareTo(BuilderFactory.SNAPSHOT_TAG_TYPE)  == 0) {
            pr = new SnapshotTagResult(x, hits.score(x), doc);
        }
        else if (indexName.compareTo(BuilderFactory.SERVER_CUSTOM_INFO_TYPE) == 0) {
            pr = new ServerCustomInfoResult(x, hits.score(x), doc);
        }
        else if (indexName.compareTo(BuilderFactory.XCCDF_IDENT_TYPE) == 0) {
            pr = new Result(x,
                    doc.getField("id").stringValue(),
                    doc.getField("identifier").stringValue(),
                    hits.score(x));
        }
        else {
            pr = new Result(x,
                    doc.getField("id").stringValue(),
                    doc.getField("name").stringValue(),
                    hits.score(x));
        }
        if (log.isDebugEnabled()) {
            log.debug("Hit[" + x + "] Score = " + hits.score(x) + ", Result = " + pr);
        }
        /**
         * matchingField will help the webUI to understand what field was responsible
         * for this match.  Later implementation should use "Explanation" to determine
         * field, for now we will simply grab one term and return it's field.
         */
        try {
            MatchingField match = new MatchingField(query, doc, queryTerms);
            pr.setMatchingField(match.getFieldName());
            pr.setMatchingFieldValue(match.getFieldValue());
            log.info("hit[" + x + "] matchingField is being set to: <" +
                pr.getMatchingField() + "> based on passed in query field.  " +
                "matchingFieldValue = " + pr.getMatchingFieldValue());
        }
        catch (Exception e) {
            log.error("Caught exception: ", e);
        }
        if (pr != null) {
            retval.add(pr);
        }
        if (maxHits > 0 && x == maxHits) {
            break;
        }
    }
    return retval;
}