Java Code Examples for org.apache.lucene.index.NumericDocValues#advance()

The following examples show how to use org.apache.lucene.index.NumericDocValues#advance() . 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: IntFieldWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public boolean write(SortDoc sortDoc, LeafReader reader, MapWriter.EntryWriter ew, int fieldIndex) throws IOException {
  int val;
  SortValue sortValue = sortDoc.getSortValue(this.field);
  if (sortValue != null) {
    if (sortValue.isPresent()) {
      val = (int) sortValue.getCurrentValue();
    } else { //empty-value
      return false;
    }
  } else {
    // field is not part of 'sort' param, but part of 'fl' param
    NumericDocValues vals = DocValues.getNumeric(reader, this.field);
    if (vals.advance(sortDoc.docId) == sortDoc.docId) {
      val = (int) vals.longValue();
    } else {
      return false;
    }
  }
  ew.put(this.field, val);
  return true;
}
 
Example 2
Source File: DateFieldWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public boolean write(SortDoc sortDoc, LeafReader reader, MapWriter.EntryWriter ew, int fieldIndex) throws IOException {
  Long val;
  SortValue sortValue = sortDoc.getSortValue(this.field);
  if (sortValue != null) {
    if (sortValue.isPresent()) {
      val = (long) sortValue.getCurrentValue();
    } else { //empty-value
      return false;
    }
  } else {
    // field is not part of 'sort' param, but part of 'fl' param
    NumericDocValues vals = DocValues.getNumeric(reader, this.field);
    if (vals.advance(sortDoc.docId) == sortDoc.docId) {
      val = vals.longValue();
    } else {
      return false;
    }
  }
  ew.put(this.field, new Date(val));
  return true;
}
 
Example 3
Source File: LongFieldWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public boolean write(SortDoc sortDoc, LeafReader reader, MapWriter.EntryWriter ew, int fieldIndex) throws IOException {
  long val;
  SortValue sortValue = sortDoc.getSortValue(this.field);
  if (sortValue != null) {
    if (sortValue.isPresent()) {
      val = (long) sortValue.getCurrentValue();
    } else { //empty-value
      return false;
    }
  } else {
    // field is not part of 'sort' param, but part of 'fl' param
    NumericDocValues vals = DocValues.getNumeric(reader, this.field);
    if (vals.advance(sortDoc.docId) == sortDoc.docId) {
      val = vals.longValue();
    } else {
      return false;
    }
  }
  ew.put(field, val);
  return true;
}
 
Example 4
Source File: TestParentChildrenBlockJoinQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testParentChildrenBlockJoinQuery() throws Exception {
  int numParentDocs = 8 + random().nextInt(8);
  int maxChildDocsPerParent = 8 + random().nextInt(8);

  Directory dir = newDirectory();
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
  for (int i = 0; i < numParentDocs; i++) {
    int numChildDocs = random().nextInt(maxChildDocsPerParent);
    List<Document> docs = new ArrayList<>(numChildDocs + 1);
    for (int j = 0; j < numChildDocs; j++) {
      Document childDoc = new Document();
      childDoc.add(new StringField("type", "child", Field.Store.NO));
      childDoc.add(new NumericDocValuesField("score", j + 1));
      docs.add(childDoc);
    }

    Document parenDoc = new Document();
    parenDoc.add(new StringField("type", "parent", Field.Store.NO));
    parenDoc.add(new NumericDocValuesField("num_child_docs", numChildDocs));
    docs.add(parenDoc);
    writer.addDocuments(docs);
  }

  IndexReader reader = writer.getReader();
  writer.close();

  IndexSearcher searcher = newSearcher(reader);
  BitSetProducer parentFilter = new QueryBitSetProducer(new TermQuery(new Term("type", "parent")));
  Query childQuery = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("type", "child")), BooleanClause.Occur.FILTER)
      .add(TestJoinUtil.numericDocValuesScoreQuery("score"), BooleanClause.Occur.SHOULD)
      .build();

  TopDocs parentDocs = searcher.search(new TermQuery(new Term("type", "parent")), numParentDocs);
  assertEquals(parentDocs.scoreDocs.length, numParentDocs);
  for (ScoreDoc parentScoreDoc : parentDocs.scoreDocs) {
    LeafReaderContext leafReader = reader.leaves().get(ReaderUtil.subIndex(parentScoreDoc.doc, reader.leaves()));
    NumericDocValues numericDocValuesField = leafReader.reader().getNumericDocValues("num_child_docs");
    numericDocValuesField.advance(parentScoreDoc.doc - leafReader.docBase);
    long expectedChildDocs = numericDocValuesField.longValue();

    ParentChildrenBlockJoinQuery parentChildrenBlockJoinQuery =
        new ParentChildrenBlockJoinQuery(parentFilter, childQuery, parentScoreDoc.doc);
    TopDocs topDocs = searcher.search(parentChildrenBlockJoinQuery, maxChildDocsPerParent);
    assertEquals(expectedChildDocs, topDocs.totalHits.value);
    if (expectedChildDocs > 0) {
      for (int i = 0; i < topDocs.scoreDocs.length; i++) {
        ScoreDoc childScoreDoc = topDocs.scoreDocs[i];
        assertEquals(expectedChildDocs - i, childScoreDoc.score, 0);
      }
    }

  }

  reader.close();
  dir.close();
}