Example usage for org.apache.lucene.util BytesRefHash BytesRefHash

List of usage examples for org.apache.lucene.util BytesRefHash BytesRefHash

Introduction

In this page you can find the example usage for org.apache.lucene.util BytesRefHash BytesRefHash.

Prototype

public BytesRefHash() 

Source Link

Document

Creates a new BytesRefHash with a ByteBlockPool using a DirectAllocator .

Usage

From source file:com.kmwllc.search.graph.GraphTermsCollector.java

GraphTermsCollector(String field, int maxDoc, Bits currentResult, DocSet leafNodes) {
    this.field = field;
    this.maxDoc = maxDoc;
    this.collectorTerms = new BytesRefHash();
    this.currentResult = currentResult;
    this.leafNodes = leafNodes;
    //this.base = base;
    // TODO: consider creating an edge cache for the compiled autn
    // for the query out of a given doc.
    // this.graphEdgeCache = GraphCache.getInstance();
    if (bits == null) {
        bits = new FixedBitSet(maxDoc);
    }//from   w w  w  .jav  a 2  s. co m
}

From source file:de.unihildesheim.iw.lucene.document.DocumentModel.java

License:Open Source License

/**
 * POJO serialization. Customized to handle de-serialization of the {@link
 * #terms terms list}./*  w  ww. j ava 2s.c o  m*/
 *
 * @param in Stream
 * @throws IOException Thrown on low-level i/o-errors
 * @throws ClassNotFoundException Thrown if de-serialization of the
 * term-frw-map failed.
 */
@SuppressWarnings({ "ObjectAllocationInLoop" })
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    this.terms = new BytesRefHash();

    final int size = in.readInt();
    final BytesRef spare = new BytesRef();

    for (int i = 0; i < size; i++) {
        final int bytesAvailable = in.readInt();
        spare.bytes = new byte[bytesAvailable];
        spare.length = spare.bytes.length;
        spare.offset = 0;
        final int bytesRead = in.read(spare.bytes);
        if (bytesRead < bytesAvailable) {
            throw new IllegalStateException("Not enough bytes to read. " + "Expected " + bytesAvailable
                    + " but got " + bytesRead + '.');
        }
        this.terms.add(spare);
    }

    this.termFrequency = Arrays.stream(this.freqs).sum();
}

From source file:de.unihildesheim.iw.lucene.index.FDRIndexDataProvider.java

License:Open Source License

@SuppressFBWarnings({ "EXS_EXCEPTION_SOFTENING_NO_CONSTRAINTS", "EXS_EXCEPTION_SOFTENING_NO_CHECKED" })
@Override//from   ww w .  j a v  a  2s  .  c om
public Stream<BytesRef> getDocumentTerms(final int docId, @NotNull final String... field) {
    Arrays.sort(field);
    final Fields fields;
    try {
        fields = this.index.reader.getTermVectors(docId);
    } catch (final IOException e) {
        throw new UncheckedIOException(e);
    }

    if (fields == null) {
        return Stream.empty();
    }

    final BytesRefHash terms = new BytesRefHash();
    StreamSupport.stream(fields.spliterator(), false)
            // filter for required fields
            .filter(fn -> Arrays.binarySearch(field, fn) >= 0).map(fn -> {
                try {
                    return fields.terms(fn);
                } catch (final IOException e) {
                    throw new UncheckedIOException(e);
                }
            }).filter(t -> t != null).forEach(t -> {
                try {
                    final TermsEnum te = t.iterator(null);
                    BytesRef term;
                    while ((term = te.next()) != null) {
                        terms.add(term);
                    }
                } catch (final IOException e) {
                    throw new UncheckedIOException(e);
                }
            });

    return StreamUtils.stream(terms);
}

From source file:de.unihildesheim.iw.lucene.index.FDRIndexDataProvider.java

License:Open Source License

@SuppressFBWarnings("EXS_EXCEPTION_SOFTENING_NO_CONSTRAINTS")
@Override//from  w ww . j  a v  a2 s  .c o m
@NotNull
public Stream<BytesRef> getDocumentsTerms(@NotNull final DocIdSet docIds) {
    try {
        return StreamUtils.stream(docIds).mapToObj(docId -> {
            try {
                return this.index.reader.getTermVectors(docId);
            } catch (final IOException e) {
                throw new UncheckedIOException(e);
            }
        }).filter(f -> f != null).map(f -> {
            final BytesRefHash terms = new BytesRefHash();
            StreamSupport.stream(f.spliterator(), false).map(fn -> {
                try {
                    return f.terms(fn);
                } catch (final IOException e) {
                    throw new UncheckedIOException(e);
                }
            }).filter(t -> t != null).forEach(t -> {
                try {
                    final TermsEnum te = t.iterator(null);
                    BytesRef term;
                    while ((term = te.next()) != null) {
                        terms.add(term);
                    }
                } catch (final IOException e) {
                    throw new UncheckedIOException(e);
                }
            });
            return terms;
        }).collect(MergingBytesRefHash::new, MergingBytesRefHash::addAll, MergingBytesRefHash::addAll).stream();
    } catch (final IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:de.unihildesheim.iw.lucene.util.BytesRefUtilsTest.java

License:Open Source License

@SuppressWarnings("ImplicitNumericConversion")
@Test/*www  .  ja v  a2 s  .c o m*/
public void testHashToArray() throws Exception {
    final Collection<String> data = new HashSet<>(3);
    data.add("foo");
    data.add("bar");
    data.add("baz");

    final BytesRefHash brh = new BytesRefHash();
    data.stream().map(BytesRef::new).forEach(brh::add);

    final BytesRefArray bra = BytesRefUtils.hashToArray(brh);

    Assert.assertEquals("Not all terms found.", data.size(), bra.size());

    final BytesRefIterator bri = bra.iterator();
    BytesRef br;
    while ((br = bri.next()) != null) {
        Assert.assertNotSame("BytesRef not found.", -1, brh.find(br));
    }
}

From source file:de.unihildesheim.iw.lucene.util.BytesRefUtilsTest.java

License:Open Source License

@SuppressWarnings("ImplicitNumericConversion")
@Test// w  ww.j a va  2  s  .c  om
public void testHashToArray_empty() throws Exception {
    final BytesRefHash brh = new BytesRefHash();
    final BytesRefArray bra = BytesRefUtils.hashToArray(brh);
    Assert.assertEquals("Expected an empty BytesRefArray.", 0, bra.size());
}

From source file:de.unihildesheim.iw.lucene.util.BytesRefUtilsTest.java

License:Open Source License

@SuppressWarnings("ImplicitNumericConversion")
@Test/*from w w w.j a va2  s  .  co m*/
public void testHashToSet() throws Exception {
    final Collection<String> data = new HashSet<>(3);
    data.add("foo");
    data.add("bar");
    data.add("baz");

    final BytesRefHash brh = new BytesRefHash();
    data.stream().map(BytesRef::new).forEach(brh::add);

    final Set<String> result = BytesRefUtils.hashToSet(brh);

    Assert.assertNotNull("Result was null.", result);
    Assert.assertEquals("Not all terms returned.", data.size(), result.size());

    Assert.assertEquals("Not all BytesRefs found.", data.size(),
            data.stream().filter(result::contains).count());
}

From source file:de.unihildesheim.iw.lucene.util.BytesRefUtilsTest.java

License:Open Source License

@Test
public void testHashToSet_empty() throws Exception {
    final BytesRefHash brh = new BytesRefHash();
    final Set<String> result = BytesRefUtils.hashToSet(brh);
    Assert.assertNotNull("Result was null.", result);
    Assert.assertTrue("Expected an empty Set.", result.isEmpty());
}

From source file:de.unihildesheim.iw.lucene.util.StreamUtilsTest.java

License:Open Source License

@Test
public void testStream_bytesRefHash() throws Exception {
    final BytesRefHash brh = new BytesRefHash();
    brh.add(new BytesRef("foo"));
    brh.add(new BytesRef("bar"));
    brh.add(new BytesRef("baz"));

    Assert.assertEquals("Not all terms streamed.", 3L, StreamUtils.stream(brh).count());

    Assert.assertEquals("Term not found.", 1L,
            StreamUtils.stream(brh).filter(br -> br.bytesEquals(new BytesRef("foo"))).count());
    Assert.assertEquals("Term not found.", 1L,
            StreamUtils.stream(brh).filter(br -> br.bytesEquals(new BytesRef("bar"))).count());
    Assert.assertEquals("Term not found.", 1L,
            StreamUtils.stream(brh).filter(br -> br.bytesEquals(new BytesRef("baz"))).count());

    Assert.assertEquals("Unknown term found.", 0L,
            StreamUtils//from w w  w .j  ava 2  s. co m
                    .stream(brh).filter(t -> !t.bytesEquals(new BytesRef("foo"))
                            && !t.bytesEquals(new BytesRef("bar")) && !t.bytesEquals(new BytesRef("baz")))
                    .count());
}

From source file:de.unihildesheim.iw.lucene.util.StreamUtilsTest.java

License:Open Source License

@Test
public void testStream_bytesRefHash_empty() throws Exception {
    final BytesRefHash brh = new BytesRefHash();
    Assert.assertEquals("Too much document ids streamed.", 0L, StreamUtils.stream(brh).count());
}