Class MinHashFilter
- java.lang.Object
-
- org.apache.lucene.util.AttributeSource
-
- org.apache.lucene.analysis.TokenStream
-
- org.apache.lucene.analysis.TokenFilter
-
- org.apache.lucene.analysis.minhash.MinHashFilter
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class MinHashFilter extends TokenFilter
Generate min hash tokens from an incoming stream of tokens. The incoming tokens would typically be 5 word shingles. 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 Classes Modifier and Type Class Description (package private) static class
MinHashFilter.FixedSizeTreeSet<E extends java.lang.Comparable<E>>
(package private) static class
MinHashFilter.LongPair
128 bits of state-
Nested classes/interfaces inherited from class org.apache.lucene.util.AttributeSource
AttributeSource.State
-
-
Field Summary
Fields Modifier and Type Field Description private int
bucketCount
private int
bucketPosition
private long
bucketSize
private static MinHashFilter.LongPair[]
cachedIntHashes
static int
DEFAULT_BUCKET_COUNT
static int
DEFAULT_HASH_COUNT
static int
DEFAULT_HASH_SET_SIZE
private int
endOffset
private AttributeSource.State
endState
private boolean
exhausted
private static int
HASH_CACHE_SIZE
private int
hashCount
private int
hashPosition
private int
hashSetSize
(package private) static java.lang.String
MIN_HASH_TYPE
private java.util.List<java.util.List<MinHashFilter.FixedSizeTreeSet<MinHashFilter.LongPair>>>
minHashSets
private OffsetAttribute
offsetAttribute
private PositionIncrementAttribute
posIncAttribute
private PositionLengthAttribute
posLenAttribute
private boolean
requiresInitialisation
private CharTermAttribute
termAttribute
private TypeAttribute
typeAttribute
private boolean
withRotation
-
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
Constructors Constructor Description MinHashFilter(TokenStream input, int hashCount, int bucketCount, int hashSetSize, boolean withRotation)
create a MinHash filter
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private 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.boolean
incrementToken()
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
-
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 Detail
-
HASH_CACHE_SIZE
private static final int HASH_CACHE_SIZE
- See Also:
- Constant Field Values
-
cachedIntHashes
private static final MinHashFilter.LongPair[] cachedIntHashes
-
DEFAULT_HASH_COUNT
public static final int DEFAULT_HASH_COUNT
- See Also:
- Constant Field Values
-
DEFAULT_HASH_SET_SIZE
public static final int DEFAULT_HASH_SET_SIZE
- See Also:
- Constant Field Values
-
DEFAULT_BUCKET_COUNT
public static final int DEFAULT_BUCKET_COUNT
- See Also:
- Constant Field Values
-
MIN_HASH_TYPE
static final java.lang.String MIN_HASH_TYPE
- See Also:
- Constant Field Values
-
minHashSets
private final java.util.List<java.util.List<MinHashFilter.FixedSizeTreeSet<MinHashFilter.LongPair>>> minHashSets
-
hashSetSize
private int hashSetSize
-
bucketCount
private int bucketCount
-
hashCount
private int hashCount
-
requiresInitialisation
private boolean requiresInitialisation
-
endState
private AttributeSource.State 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
private final CharTermAttribute termAttribute
-
offsetAttribute
private final OffsetAttribute offsetAttribute
-
typeAttribute
private final TypeAttribute typeAttribute
-
posIncAttribute
private final PositionIncrementAttribute posIncAttribute
-
posLenAttribute
private final PositionLengthAttribute posLenAttribute
-
-
Constructor Detail
-
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 Detail
-
getBytes
static byte[] getBytes(int i)
-
incrementToken
public final boolean incrementToken() throws java.io.IOException
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:
java.io.IOException
-
getIntHash
private static MinHashFilter.LongPair getIntHash(int i)
-
end
public void end() throws java.io.IOException
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:
java.io.IOException
- If an I/O error occurs
-
reset
public void reset() throws java.io.IOException
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:
java.io.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
private static MinHashFilter.LongPair combineOrdered(MinHashFilter.LongPair... hashCodes)
-
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)
-
-