Class MinHashFilter
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Unwrappable<TokenStream>
The number of hashes used and the number of minimum values for each hash can be set. You could have 1 hash and keep the 100 lowest values or 100 hashes and keep the lowest one for each. Hashes can also be bucketed in ranges over the 128-bit hash space,
A 128-bit hash is used internally. 5 word shingles from 10e5 words generate 10e25 combinations So a 64 bit hash would have collisions (1.8e19)
When using different hashes 32 bits are used for the hash position leaving scope for 8e28 unique hashes. A single hash will use all 128 bits.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static class
MinHashFilter.FixedSizeTreeSet<E extends Comparable<E>>
(package private) static final class
128 bits of stateNested classes/interfaces inherited from class org.apache.lucene.util.AttributeSource
AttributeSource.State
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
private int
private long
private static final MinHashFilter.LongPair[]
static final int
static final int
static final int
private int
private AttributeSource.State
private boolean
private static final int
private int
private int
private int
(package private) static final String
private final List<List<MinHashFilter.FixedSizeTreeSet<MinHashFilter.LongPair>>>
private final OffsetAttribute
private final PositionIncrementAttribute
private final PositionLengthAttribute
private boolean
private final CharTermAttribute
private final TypeAttribute
private final boolean
Fields inherited from class org.apache.lucene.analysis.TokenFilter
input
Fields inherited from class org.apache.lucene.analysis.TokenStream
DEFAULT_TOKEN_ATTRIBUTE_FACTORY
-
Constructor Summary
ConstructorsConstructorDescriptionMinHashFilter
(TokenStream input, int hashCount, int bucketCount, int hashSetSize, boolean withRotation) create a MinHash filter -
Method Summary
Modifier and TypeMethodDescriptionprivate static MinHashFilter.LongPair
combineOrdered
(MinHashFilter.LongPair... hashCodes) private void
doRest()
void
end()
This method is called by the consumer after the last token has been consumed, afterTokenStream.incrementToken()
returnedfalse
(using the newTokenStream
API).private static long
fmix64
(long k) (package private) static byte[]
getBytes
(int i) private static MinHashFilter.LongPair
getIntHash
(int i) private static long
getLongLittleEndian
(byte[] buf, int offset) Gets a long from a byte buffer in little endian byte order.final boolean
Consumers (i.e.,IndexWriter
) use this method to advance the stream to the next token.private static char
int0
(int x) private static char
int1
(int x) (package private) static boolean
isLessThanUnsigned
(long n1, long n2) private static char
long0
(long x) private static char
long1
(long x) private static char
long2
(long x) private static char
long3
(long x) (package private) static void
murmurhash3_x64_128
(byte[] key, int offset, int len, int seed, MinHashFilter.LongPair out) Returns the MurmurHash3_x64_128 hash, placing the result in "out".void
reset()
This method is called by a consumer before it begins consumption usingTokenStream.incrementToken()
.Methods inherited from class org.apache.lucene.analysis.TokenFilter
close, unwrap
Methods inherited from class org.apache.lucene.util.AttributeSource
addAttribute, addAttributeImpl, captureState, clearAttributes, cloneAttributes, copyTo, endAttributes, equals, getAttribute, getAttributeClassesIterator, getAttributeFactory, getAttributeImplsIterator, hasAttribute, hasAttributes, hashCode, reflectAsString, reflectWith, removeAllAttributes, restoreState, toString
-
Field Details
-
HASH_CACHE_SIZE
private static final int HASH_CACHE_SIZE- See Also:
-
cachedIntHashes
-
DEFAULT_HASH_COUNT
public static final int DEFAULT_HASH_COUNT- See Also:
-
DEFAULT_HASH_SET_SIZE
public static final int DEFAULT_HASH_SET_SIZE- See Also:
-
DEFAULT_BUCKET_COUNT
public static final int DEFAULT_BUCKET_COUNT- See Also:
-
MIN_HASH_TYPE
- See Also:
-
minHashSets
-
hashSetSize
private int hashSetSize -
bucketCount
private int bucketCount -
hashCount
private int hashCount -
requiresInitialisation
private boolean requiresInitialisation -
endState
-
hashPosition
private int hashPosition -
bucketPosition
private int bucketPosition -
bucketSize
private long bucketSize -
withRotation
private final boolean withRotation -
endOffset
private int endOffset -
exhausted
private boolean exhausted -
termAttribute
-
offsetAttribute
-
typeAttribute
-
posIncAttribute
-
posLenAttribute
-
-
Constructor Details
-
MinHashFilter
public MinHashFilter(TokenStream input, int hashCount, int bucketCount, int hashSetSize, boolean withRotation) create a MinHash filter- Parameters:
input
- the token streamhashCount
- the no. of hashesbucketCount
- the no. of buckets for hashinghashSetSize
- the no. of min hashes to keepwithRotation
- whether rotate or not hashes while incrementing tokens
-
-
Method Details
-
getBytes
static byte[] getBytes(int i) -
incrementToken
Description copied from class:TokenStream
Consumers (i.e.,IndexWriter
) use this method to advance the stream to the next token. Implementing classes must implement this method and update the appropriateAttributeImpl
s with the attributes of the next token.The producer must make no assumptions about the attributes after the method has been returned: the caller may arbitrarily change it. If the producer needs to preserve the state for subsequent calls, it can use
AttributeSource.captureState()
to create a copy of the current attribute state.This method is called for every token of a document, so an efficient implementation is crucial for good performance. To avoid calls to
AttributeSource.addAttribute(Class)
andAttributeSource.getAttribute(Class)
, references to allAttributeImpl
s that this stream uses should be retrieved during instantiation.To ensure that filters and consumers know which attributes are available, the attributes must be added during instantiation. Filters and consumers are not required to check for availability of attributes in
TokenStream.incrementToken()
.- Specified by:
incrementToken
in classTokenStream
- Returns:
- false for end of stream; true otherwise
- Throws:
IOException
-
getIntHash
-
end
Description copied from class:TokenFilter
This method is called by the consumer after the last token has been consumed, afterTokenStream.incrementToken()
returnedfalse
(using the newTokenStream
API). Streams implementing the old API should upgrade to use this feature.This method can be used to perform any end-of-stream operations, such as setting the final offset of a stream. The final offset of a stream might differ from the offset of the last token eg in case one or more whitespaces followed after the last token, but a WhitespaceTokenizer was used.
Additionally any skipped positions (such as those removed by a stopfilter) can be applied to the position increment, or any adjustment of other attributes where the end-of-stream value may be important.
If you override this method, always call
super.end()
.NOTE: The default implementation chains the call to the input TokenStream, so be sure to call
super.end()
first when overriding this method.- Overrides:
end
in classTokenFilter
- Throws:
IOException
- If an I/O error occurs
-
reset
Description copied from class:TokenFilter
This method is called by a consumer before it begins consumption usingTokenStream.incrementToken()
.Resets this stream to a clean state. Stateful implementations must implement this method so that they can be reused, just as if they had been created fresh.
If you override this method, always call
super.reset()
, otherwise some internal state will not be correctly reset (e.g.,Tokenizer
will throwIllegalStateException
on further usage).NOTE: The default implementation chains the call to the input TokenStream, so be sure to call
super.reset()
when overriding this method.- Overrides:
reset
in classTokenFilter
- Throws:
IOException
-
doRest
private void doRest() -
long0
private static char long0(long x) -
long1
private static char long1(long x) -
long2
private static char long2(long x) -
long3
private static char long3(long x) -
int0
private static char int0(int x) -
int1
private static char int1(int x) -
isLessThanUnsigned
static boolean isLessThanUnsigned(long n1, long n2) -
combineOrdered
-
getLongLittleEndian
private static long getLongLittleEndian(byte[] buf, int offset) Gets a long from a byte buffer in little endian byte order. -
murmurhash3_x64_128
static void murmurhash3_x64_128(byte[] key, int offset, int len, int seed, MinHashFilter.LongPair out) Returns the MurmurHash3_x64_128 hash, placing the result in "out". -
fmix64
private static long fmix64(long k)
-