-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exposing lucene 6.x minhash filter. #20206
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
core/src/main/java/org/elasticsearch/index/analysis/MinHashTokenFilterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.index.analysis; | ||
|
||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.minhash.MinHashFilterFactory; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.env.Environment; | ||
import org.elasticsearch.index.IndexSettings; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* TokenFilterFactoryAdapter for {@link MinHashFilterFactory} | ||
* | ||
*/ | ||
public class MinHashTokenFilterFactory extends AbstractTokenFilterFactory { | ||
|
||
private final MinHashFilterFactory minHashFilterFactory; | ||
|
||
public MinHashTokenFilterFactory(IndexSettings indexSettings, Environment environment, String name, Settings settings) { | ||
super(indexSettings, name, settings); | ||
minHashFilterFactory = new MinHashFilterFactory(convertSettings(settings)); | ||
} | ||
|
||
@Override | ||
public TokenStream create(TokenStream tokenStream) { | ||
return minHashFilterFactory.create(tokenStream); | ||
} | ||
|
||
private Map<String, String> convertSettings(Settings settings) { | ||
Map<String, String> settingMap = new HashMap<>(); | ||
settingMap.put("hashCount", settings.get("hash_count")); | ||
settingMap.put("bucketCount", settings.get("bucket_count")); | ||
settingMap.put("hashSetSize", settings.get("hash_set_size")); | ||
settingMap.put("withRotation", settings.get("with_rotation")); | ||
return settingMap; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
core/src/test/java/org/elasticsearch/index/analysis/MinHashFilterFactoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.index.analysis; | ||
|
||
import org.apache.lucene.analysis.Tokenizer; | ||
import org.apache.lucene.analysis.core.WhitespaceTokenizer; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.env.Environment; | ||
import org.elasticsearch.test.ESTokenStreamTestCase; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
|
||
public class MinHashFilterFactoryTests extends ESTokenStreamTestCase { | ||
public void testDefault() throws IOException { | ||
int default_hash_count = 1; | ||
int default_bucket_size = 512; | ||
int default_hash_set_size = 1; | ||
Settings settings = Settings.builder() | ||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) | ||
.build(); | ||
AnalysisService analysisService = AnalysisTestsHelper.createAnalysisServiceFromSettings(settings); | ||
TokenFilterFactory tokenFilter = analysisService.tokenFilter("min_hash"); | ||
String source = "the quick brown fox"; | ||
Tokenizer tokenizer = new WhitespaceTokenizer(); | ||
tokenizer.setReader(new StringReader(source)); | ||
|
||
// with_rotation is true by default, and hash_set_size is 1, so even though the source doesn't | ||
// have enough tokens to fill all the buckets, we still expect 512 tokens. | ||
assertStreamHasNumberOfTokens(tokenFilter.create(tokenizer), | ||
default_hash_count * default_bucket_size * default_hash_set_size); | ||
} | ||
|
||
public void testSettings() throws IOException { | ||
Settings settings = Settings.builder() | ||
.put("index.analysis.filter.test_min_hash.type", "min_hash") | ||
.put("index.analysis.filter.test_min_hash.hash_count", "1") | ||
.put("index.analysis.filter.test_min_hash.bucket_count", "2") | ||
.put("index.analysis.filter.test_min_hash.hash_set_size", "1") | ||
.put("index.analysis.filter.test_min_hash.with_rotation", false) | ||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) | ||
.build(); | ||
AnalysisService analysisService = AnalysisTestsHelper.createAnalysisServiceFromSettings(settings); | ||
TokenFilterFactory tokenFilter = analysisService.tokenFilter("test_min_hash"); | ||
String source = "sushi"; | ||
Tokenizer tokenizer = new WhitespaceTokenizer(); | ||
tokenizer.setReader(new StringReader(source)); | ||
|
||
// despite the fact that bucket_count is 2 and hash_set_size is 1, | ||
// because with_rotation is false, we only expect 1 token here. | ||
assertStreamHasNumberOfTokens(tokenFilter.create(tokenizer), 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
docs/reference/analysis/tokenfilters/minhash-tokenfilter.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[[analysis-minhash-tokenfilter]] | ||
== Minhash Token Filter | ||
|
||
A token filter of type `minhash` hashes each token of the token stream and divides | ||
the resulting hashes into buckets, keeping the lowest-valued hashes per | ||
bucket. It then returns these hashes as tokens. | ||
|
||
The following are settings that can be set for a `minhash` token filter. | ||
|
||
[cols="<,<", options="header",] | ||
|======================================================================= | ||
|Setting |Description | ||
|`hash_count` |The number of hashes to hash the token stream with. Defaults to `1`. | ||
|
||
|`bucket_count` |The number of buckets to divide the minhashes into. Defaults to `512`. | ||
|
||
|`hash_set_size` |The number of minhashes to keep per bucket. Defaults to `1`. | ||
|
||
|`with_rotation` |Whether or not to fill empty buckets with the value of the first non-empty | ||
bucket to its circular right. Only takes effect if hash_set_size is equal to one. | ||
Defaults to `true` if bucket_count is greater than one, else `false`. | ||
|======================================================================= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/minhash/min_hash/