Package org.apache.lucene.index

Examples of org.apache.lucene.index.DirectoryReader


      parentVal.setLongValue(random().nextInt(50));
      documents.add(parentDoc);
      writer.addDocuments(documents);
    }
    writer.forceMerge(1);
    final DirectoryReader indexReader = writer.getReader();
    writer.close();

    final AtomicReader reader = getOnlySegmentReader(indexReader);
    final Filter parentsFilter = new FixedBitSetCachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Term("parent", "true"))));
    final FixedBitSet parentBits = (FixedBitSet) parentsFilter.getDocIdSet(reader.getContext(), null);

    final NumericDocValues parentValues = reader.getNumericDocValues("parent_val");
    final Sorter.DocComparator parentComparator = new Sorter.DocComparator() {
      @Override
      public int compare(int docID1, int docID2) {
        assertTrue(parentBits.get(docID1));
        assertTrue(parentBits.get(docID2));
        final long v1 = parentValues.get(docID1);
        final long v2 = parentValues.get(docID2);
        return v1 < v2 ? -1 : v1 == v2 ? 0 : 1;
      }
    };

    final NumericDocValues childValues = reader.getNumericDocValues("child_val");
    final Sorter.DocComparator childComparator = new Sorter.DocComparator() {
      @Override
      public int compare(int docID1, int docID2) {
        assertFalse(parentBits.get(docID1));
        assertFalse(parentBits.get(docID2));
        final long v1 = childValues.get(docID1);
        final long v2 = childValues.get(docID2);
        return v1 < v2 ? -1 : v1 == v2 ? 0 : 1;
      }
    };

    final Sorter sorter = new BlockJoinSorter(parentsFilter) {
     
      @Override
      public String getID() {
        return "Dummy";
      }
     
      @Override
      protected DocComparator getParentComparator(AtomicReader r) {
        assertEquals(reader, r);
        return parentComparator;
      }

      @Override
      protected DocComparator getChildComparator(AtomicReader r) {
        assertEquals(reader, r);
        return childComparator;
      }

    };
    final Sorter.DocMap docMap = sorter.sort(reader);
    assertEquals(reader.maxDoc(), docMap.size());

    int[] children = new int[1];
    int numChildren = 0;
    int previousParent = -1;
    for (int i = 0; i < docMap.size(); ++i) {
      final int oldID = docMap.newToOld(i);
      if (parentBits.get(oldID)) {
        // check that we have the right children
        for (int j = 0; j < numChildren; ++j) {
          assertEquals(oldID, parentBits.nextSetBit(children[j]));
        }
        // check that children are sorted
        for (int j = 1; j < numChildren; ++j) {
          final int doc1 = children[j-1];
          final int doc2 = children[j];
          if (childValues.get(doc1) == childValues.get(doc2)) {
            assertTrue(doc1 < doc2); // sort is stable
          } else {
            assertTrue(childValues.get(doc1) < childValues.get(doc2));
          }
        }
        // check that parents are sorted
        if (previousParent != -1) {
          if (parentValues.get(previousParent) == parentValues.get(oldID)) {
            assertTrue(previousParent < oldID);
          } else {
            assertTrue(parentValues.get(previousParent) < parentValues.get(oldID));
          }
        }
        // reset
        previousParent = oldID;
        numChildren = 0;
      } else {
        children = ArrayUtil.grow(children, numChildren+1);
        children[numChildren++] = oldID;
      }
    }
    indexReader.close();
    writer.w.getDirectory().close();
  }
View Full Code Here


    RandomIndexWriter iw = new RandomIndexWriter(random(), dir,
        newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setCodec(cp));
    Document doc = new Document();
    doc.add(new TextField("foo", "a b b c c c d e f g g h i i j j k", Field.Store.NO));
    iw.addDocument(doc);
    DirectoryReader ir = iw.getReader();
    iw.close();
   
    AtomicReader segment = getOnlySegmentReader(ir);
    DocsEnum reuse = null;
    Map<DocsEnum,Boolean> allEnums = new IdentityHashMap<DocsEnum,Boolean>();
    TermsEnum te = segment.terms("foo").iterator(null);
    while (te.next() != null) {
      reuse = te.docs(null, reuse, DocsEnum.FLAG_NONE);
      allEnums.put(reuse, true);
    }
   
    assertEquals(2, allEnums.size());
   
    allEnums.clear();
    DocsAndPositionsEnum posReuse = null;
    te = segment.terms("foo").iterator(null);
    while (te.next() != null) {
      posReuse = te.docsAndPositions(null, posReuse);
      allEnums.put(posReuse, true);
    }
   
    assertEquals(2, allEnums.size());
   
    ir.close();
    dir.close();
  }
View Full Code Here

    doc.add(new TextField("foo", "a b b c c c d e f g g g h i i j j k l l m m m", Field.Store.NO));
    // note: the reuse is imperfect, here we would have 4 enums (lost reuse when we get an enum for 'm')
    // this is because we only track the 'last' enum we reused (not all).
    // but this seems 'good enough' for now.
    iw.addDocument(doc);
    DirectoryReader ir = iw.getReader();
    iw.close();
   
    AtomicReader segment = getOnlySegmentReader(ir);
    DocsEnum reuse = null;
    Map<DocsEnum,Boolean> allEnums = new IdentityHashMap<DocsEnum,Boolean>();
    TermsEnum te = segment.terms("foo").iterator(null);
    while (te.next() != null) {
      reuse = te.docs(null, reuse, DocsEnum.FLAG_NONE);
      allEnums.put(reuse, true);
    }
   
    assertEquals(4, allEnums.size());
   
    allEnums.clear();
    DocsAndPositionsEnum posReuse = null;
    te = segment.terms("foo").iterator(null);
    while (te.next() != null) {
      posReuse = te.docsAndPositions(null, posReuse);
      allEnums.put(posReuse, true);
    }
   
    assertEquals(4, allEnums.size());
   
    ir.close();
    dir.close();
  }
View Full Code Here

        iw.commit();
      }
    }
    iw.close();

    DirectoryReader rd = DirectoryReader.open(d);
    for (AtomicReaderContext leave : rd.leaves()) {
      final FunctionValues ids = new LongFieldSource("id").getValues(null, leave);
      final ValueSource vs;
      switch (type) {
        case BINARY:
        case SORTED:
          vs = new BytesRefFieldSource("dv");
          break;
        case NUMERIC:
          vs = new LongFieldSource("dv");
          break;
        default:
          throw new AssertionError();
      }
      final FunctionValues values = vs.getValues(null, leave);
      BytesRef bytes = new BytesRef();
      for (int i = 0; i < leave.reader().maxDoc(); ++i) {
        assertTrue(values.exists(i));
        if (vs instanceof BytesRefFieldSource) {
          assertTrue(values.objectVal(i) instanceof String);
        } else if (vs instanceof LongFieldSource) {
          assertTrue(values.objectVal(i) instanceof Long);
          assertTrue(values.bytesVal(i, bytes));
        } else {
          throw new AssertionError();
        }
       
        Object expected = vals[ids.intVal(i)];
        switch (type) {
          case SORTED:
            values.ordVal(i); // no exception
            assertTrue(values.numOrd() >= 1);
          case BINARY:
            assertEquals(expected, values.objectVal(i));
            assertEquals(expected, values.strVal(i));
            assertEquals(expected, values.objectVal(i));
            assertEquals(expected, values.strVal(i));
            assertTrue(values.bytesVal(i, bytes));
            assertEquals(new BytesRef((String) expected), bytes);
            break;
          case NUMERIC:
            assertEquals(((Number) expected).longValue(), values.longVal(i));
            break;
        }
      }
    }
    rd.close();
    d.close();
  }
View Full Code Here

   
    // index 100 docs, each with one category: ["root", docnum/10, docnum]
    // e.g. root/8/87
    index100Docs(indexDir, taxoDir, fip);
   
    DirectoryReader r = DirectoryReader.open(indexDir);
    TaxonomyReader tr = new DirectoryTaxonomyReader(taxoDir);
   
    CountFacetRequest facetRequest = new CountFacetRequest(new CategoryPath("root"), 10);
    // Setting the depth to '2', should potentially get all categories
    facetRequest.setDepth(2);
View Full Code Here

      } catch (InterruptedException e) {
        throw new ThreadInterruptedException(e);
      }

      try {
        DirectoryReader reader = DirectoryReader.open(dir);
        try {
          int handlerID = Integer.parseInt(reader.getIndexCommit().getUserData().get(VERSION_ID), 16);
          if (expectedID == handlerID) {
            return;
          } else if (VERBOSE) {
            System.out.println("expectedID=" + expectedID + " actual=" + handlerID + " generation=" + reader.getIndexCommit().getGeneration());
          }
        } finally {
          reader.close();
        }
      } catch (Exception e) {
        // we can hit IndexNotFoundException or e.g. EOFException (on
        // segments_N) because it is being copied at the same time it is read by
        // DirectoryReader.open().
View Full Code Here

            System.out.println("hit exception during update: " + t);
            t.printStackTrace(System.out);
          }
          try {
            // test that the index can be read and also some basic statistics
            DirectoryReader reader = DirectoryReader.open(handlerDir.getDelegate());
            try {
              int numDocs = reader.numDocs();
              int version = Integer.parseInt(reader.getIndexCommit().getUserData().get(VERSION_ID), 16);
              assertEquals(numDocs, version);
            } finally {
              reader.close();
            }
            // verify index consistency
            _TestUtil.checkIndex(handlerDir.getDelegate());
          } catch (IOException e) {
            // exceptions here are bad, don't ignore them
View Full Code Here

    // Commit Changes
    indexWriter.commit();
    taxoWriter.commit();

    // Open readers
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

    // As this is the first time we have invoked the TotalFacetCountsManager,
    // we should expect to compute and not read from disk.
    TotalFacetCounts totalCounts = TFC.getTotalCounts(indexReader, taxoReader, iParams);
    int prevGen = assertRecomputed(totalCounts, 0, "after first attempt to get it!");

    // Repeating same operation should pull from the cache - not recomputed.
    assertTrue("Should be obtained from cache at 2nd attempt",totalCounts ==
      TFC.getTotalCounts(indexReader, taxoReader, iParams));

    // Repeat the same operation as above. but clear first - now should recompute again
    initCache();
    totalCounts = TFC.getTotalCounts(indexReader, taxoReader, iParams);
    prevGen = assertRecomputed(totalCounts, prevGen, "after cache clear, 3rd attempt to get it!");
   
    //store to file
    File outputFile = _TestUtil.createTempFile("test", "tmp", TEMP_DIR);
    initCache();
    TFC.store(outputFile, indexReader, taxoReader, iParams);
    totalCounts = TFC.getTotalCounts(indexReader, taxoReader, iParams);
    prevGen = assertRecomputed(totalCounts, prevGen, "after cache clear, 4th attempt to get it!");

    //clear and load
    initCache();
    TFC.load(outputFile, indexReader, taxoReader, iParams);
    totalCounts = TFC.getTotalCounts(indexReader, taxoReader, iParams);
    prevGen = assertReadFromDisc(totalCounts, prevGen, "after 5th attempt to get it!");

    // Add a new facet to the index, commit and refresh readers
    addFacets(iParams, indexWriter, taxoWriter, "c", "d");
    IOUtils.close(indexWriter, taxoWriter);

    TaxonomyReader newTaxoReader = TaxonomyReader.openIfChanged(taxoReader);
    assertNotNull(newTaxoReader);
    assertTrue("should have received more cagtegories in updated taxonomy", newTaxoReader.getSize() > taxoReader.getSize());
    taxoReader.close();
    taxoReader = newTaxoReader;
   
    DirectoryReader r2 = DirectoryReader.openIfChanged(indexReader);
    assertNotNull(r2);
    indexReader.close();
    indexReader = r2;

    // now use the new reader - should recompute
View Full Code Here

    addFacets(iParams, indexWriter, taxoWriter, "a", "b");
    // Commit Changes
    indexWriter.commit();
    taxoWriter.commit();

    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

    // Create TFC and write cache to disk
    File outputFile = _TestUtil.createTempFile("test", "tmp", TEMP_DIR);
    TFC.store(outputFile, indexReader, taxoReader, iParams);
View Full Code Here

    indexWriter2.commit();
    taxoWriter1.commit();
    taxoWriter2.commit();

    // Open two readers
    DirectoryReader indexReader1 = DirectoryReader.open(indexDir1);
    DirectoryReader indexReader2 = DirectoryReader.open(indexDir2);
    TaxonomyReader taxoReader1 = new DirectoryTaxonomyReader(taxoDir1);
    TaxonomyReader taxoReader2 = new DirectoryTaxonomyReader(taxoDir2);

    // As this is the first time we have invoked the TotalFacetCountsManager, we
    // should expect to compute.
View Full Code Here

TOP

Related Classes of org.apache.lucene.index.DirectoryReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.