Pre-configured shingle filter should disable graph analysis (#25853)
This change disables the graph analysis on default `shingle` filter. The pre-configured shingle filter produces shingles of different size. Graph analysis on such token stream is useless and dangerous as it may create too many paths. Fixes #25555
This commit is contained in:
parent
4a9995145c
commit
ab3b5c695a
|
@ -46,6 +46,7 @@ import org.apache.lucene.analysis.fr.FrenchAnalyzer;
|
||||||
import org.apache.lucene.analysis.hi.HindiNormalizationFilter;
|
import org.apache.lucene.analysis.hi.HindiNormalizationFilter;
|
||||||
import org.apache.lucene.analysis.in.IndicNormalizationFilter;
|
import org.apache.lucene.analysis.in.IndicNormalizationFilter;
|
||||||
import org.apache.lucene.analysis.miscellaneous.ASCIIFoldingFilter;
|
import org.apache.lucene.analysis.miscellaneous.ASCIIFoldingFilter;
|
||||||
|
import org.apache.lucene.analysis.miscellaneous.DisableGraphAttribute;
|
||||||
import org.apache.lucene.analysis.miscellaneous.KeywordRepeatFilter;
|
import org.apache.lucene.analysis.miscellaneous.KeywordRepeatFilter;
|
||||||
import org.apache.lucene.analysis.miscellaneous.LengthFilter;
|
import org.apache.lucene.analysis.miscellaneous.LengthFilter;
|
||||||
import org.apache.lucene.analysis.miscellaneous.LimitTokenCountFilter;
|
import org.apache.lucene.analysis.miscellaneous.LimitTokenCountFilter;
|
||||||
|
@ -207,7 +208,17 @@ public class CommonAnalysisPlugin extends Plugin implements AnalysisPlugin {
|
||||||
filters.add(PreConfiguredTokenFilter.singleton("russian_stem", false, input -> new SnowballFilter(input, "Russian")));
|
filters.add(PreConfiguredTokenFilter.singleton("russian_stem", false, input -> new SnowballFilter(input, "Russian")));
|
||||||
filters.add(PreConfiguredTokenFilter.singleton("scandinavian_folding", true, ScandinavianFoldingFilter::new));
|
filters.add(PreConfiguredTokenFilter.singleton("scandinavian_folding", true, ScandinavianFoldingFilter::new));
|
||||||
filters.add(PreConfiguredTokenFilter.singleton("scandinavian_normalization", true, ScandinavianNormalizationFilter::new));
|
filters.add(PreConfiguredTokenFilter.singleton("scandinavian_normalization", true, ScandinavianNormalizationFilter::new));
|
||||||
filters.add(PreConfiguredTokenFilter.singleton("shingle", false, ShingleFilter::new));
|
filters.add(PreConfiguredTokenFilter.singleton("shingle", false, input -> {
|
||||||
|
TokenStream ts = new ShingleFilter(input);
|
||||||
|
/**
|
||||||
|
* We disable the graph analysis on this token stream
|
||||||
|
* because it produces shingles of different size.
|
||||||
|
* Graph analysis on such token stream is useless and dangerous as it may create too many paths
|
||||||
|
* since shingles of different size are not aligned in terms of positions.
|
||||||
|
*/
|
||||||
|
ts.addAttribute(DisableGraphAttribute.class);
|
||||||
|
return ts;
|
||||||
|
}));
|
||||||
filters.add(PreConfiguredTokenFilter.singleton("snowball", false, input -> new SnowballFilter(input, "English")));
|
filters.add(PreConfiguredTokenFilter.singleton("snowball", false, input -> new SnowballFilter(input, "English")));
|
||||||
filters.add(PreConfiguredTokenFilter.singleton("sorani_normalization", true, SoraniNormalizationFilter::new));
|
filters.add(PreConfiguredTokenFilter.singleton("sorani_normalization", true, SoraniNormalizationFilter::new));
|
||||||
filters.add(PreConfiguredTokenFilter.singleton("stemmer", false, PorterStemFilter::new));
|
filters.add(PreConfiguredTokenFilter.singleton("stemmer", false, PorterStemFilter::new));
|
||||||
|
|
|
@ -0,0 +1,66 @@
|
||||||
|
/*
|
||||||
|
* 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.analysis.common;
|
||||||
|
|
||||||
|
import org.apache.lucene.analysis.TokenStream;
|
||||||
|
import org.apache.lucene.analysis.Tokenizer;
|
||||||
|
import org.apache.lucene.analysis.core.WhitespaceTokenizer;
|
||||||
|
import org.apache.lucene.analysis.miscellaneous.DisableGraphAttribute;
|
||||||
|
import org.elasticsearch.Version;
|
||||||
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
|
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||||
|
import org.elasticsearch.common.settings.Settings;
|
||||||
|
import org.elasticsearch.env.Environment;
|
||||||
|
import org.elasticsearch.index.IndexSettings;
|
||||||
|
import org.elasticsearch.index.analysis.AnalysisTestsHelper;
|
||||||
|
import org.elasticsearch.index.analysis.IndexAnalyzers;
|
||||||
|
import org.elasticsearch.index.analysis.NamedAnalyzer;
|
||||||
|
import org.elasticsearch.index.analysis.TokenFilterFactory;
|
||||||
|
import org.elasticsearch.index.query.Operator;
|
||||||
|
import org.elasticsearch.plugins.Plugin;
|
||||||
|
import org.elasticsearch.test.ESIntegTestCase;
|
||||||
|
import org.elasticsearch.test.ESTestCase;
|
||||||
|
import org.elasticsearch.test.ESTokenStreamTestCase;
|
||||||
|
import org.elasticsearch.test.IndexSettingsModule;
|
||||||
|
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;
|
||||||
|
import static org.elasticsearch.test.ESTestCase.createTestAnalysis;
|
||||||
|
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
||||||
|
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
|
||||||
|
|
||||||
|
public class ShingleTokenFilterTests extends ESTokenStreamTestCase {
|
||||||
|
public void testPreConfiguredShingleFilterDisableGraphAttribute() throws Exception {
|
||||||
|
ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(
|
||||||
|
Settings.builder()
|
||||||
|
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
|
||||||
|
.put("index.analysis.filter.my_ascii_folding.type", "asciifolding")
|
||||||
|
.build(),
|
||||||
|
new CommonAnalysisPlugin());
|
||||||
|
TokenFilterFactory tokenFilter = analysis.tokenFilter.get("shingle");
|
||||||
|
Tokenizer tokenizer = new WhitespaceTokenizer();
|
||||||
|
tokenizer.setReader(new StringReader("this is a test"));
|
||||||
|
TokenStream tokenStream = tokenFilter.create(tokenizer);
|
||||||
|
assertTrue(tokenStream.hasAttribute(DisableGraphAttribute.class));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue