Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't fail hard if broken analysis is used.
Today an analysis chain with broken tokenfilters or tokenizers like WordDelimiterFilter might produce somewhat broken term vectors that cause `StringIndexOutOfBoundsExceptions` if FastVectorHighlighter is used since the positions / offsets contract is violated and offsets of highlight tokens are not increasing but decreasing even if their positions are increasing. Yet, if we detect such a situation we can resort the tokens which might cause somewhat odd highlights but doesn't fail hard with a StringIndexOOBException. Closes #3006
- Loading branch information
Showing
5 changed files
with
225 additions
and
34 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
src/main/java/org/elasticsearch/search/highlight/vectorhighlight/FragmentBuilderHelper.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,102 @@ | ||
/* | ||
* Licensed to Elastic Search and Shay Banon under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. Elastic Search 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.search.highlight.vectorhighlight; | ||
|
||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
import org.apache.lucene.analysis.Analyzer; | ||
import org.apache.lucene.analysis.miscellaneous.WordDelimiterFilter; | ||
import org.apache.lucene.document.Field; | ||
import org.apache.lucene.search.vectorhighlight.FastVectorHighlighter; | ||
import org.apache.lucene.search.vectorhighlight.FieldFragList.WeightedFragInfo; | ||
import org.apache.lucene.search.vectorhighlight.FieldFragList.WeightedFragInfo.SubInfo; | ||
import org.apache.lucene.search.vectorhighlight.FragmentsBuilder; | ||
import org.elasticsearch.index.analysis.CustomAnalyzer; | ||
import org.elasticsearch.index.analysis.EdgeNGramTokenFilterFactory; | ||
import org.elasticsearch.index.analysis.EdgeNGramTokenizerFactory; | ||
import org.elasticsearch.index.analysis.NGramTokenFilterFactory; | ||
import org.elasticsearch.index.analysis.NGramTokenizerFactory; | ||
import org.elasticsearch.index.analysis.NamedAnalyzer; | ||
import org.elasticsearch.index.analysis.TokenFilterFactory; | ||
import org.elasticsearch.index.analysis.WordDelimiterTokenFilterFactory; | ||
import org.elasticsearch.index.mapper.FieldMapper; | ||
|
||
/** | ||
* Simple helper class for {@link FastVectorHighlighter} {@link FragmentsBuilder} implemenations. | ||
*/ | ||
public final class FragmentBuilderHelper { | ||
|
||
private FragmentBuilderHelper() { | ||
// no instance | ||
} | ||
|
||
/** | ||
* Fixes problems with broken analysis chains if positions and offsets are messed up that can lead to | ||
* {@link StringIndexOutOfBoundsException} in the {@link FastVectorHighlighter} | ||
*/ | ||
public static WeightedFragInfo fixWeightedFragInfo(FieldMapper<?> mapper, Field[] values, WeightedFragInfo fragInfo) { | ||
assert fragInfo != null : "FragInfo must not be null"; | ||
assert mapper.names().indexName().equals(values[0].name()) : "Expected FieldMapper for field " + values[0].name(); | ||
if (!fragInfo.getSubInfos().isEmpty() && (containsBrokenAnalysis(mapper.indexAnalyzer()))) { | ||
/* This is a special case where broken analysis like WDF is used for term-vector creation at index-time | ||
* which can potentially mess up the offsets. To prevent a SAIIOBException we need to resort | ||
* the fragments based on their offsets rather than using soley the positions as it is done in | ||
* the FastVectorHighlighter. Yet, this is really a lucene problem and should be fixed in lucene rather | ||
* than in this hack... aka. "we are are working on in!" */ | ||
final List<SubInfo> subInfos = fragInfo.getSubInfos(); | ||
Collections.sort(subInfos, new Comparator<SubInfo>() { | ||
@Override | ||
public int compare(SubInfo o1, SubInfo o2) { | ||
int startOffset = o1.getTermsOffsets().get(0).getStartOffset(); | ||
int startOffset2 = o2.getTermsOffsets().get(0).getStartOffset(); | ||
return Integer.compare(startOffset, startOffset2); | ||
} | ||
}); | ||
return new WeightedFragInfo(Math.min(fragInfo.getSubInfos().get(0).getTermsOffsets().get(0).getStartOffset(), | ||
fragInfo.getStartOffset()), fragInfo.getEndOffset(), subInfos, fragInfo.getTotalBoost()); | ||
} else { | ||
return fragInfo; | ||
} | ||
} | ||
|
||
private static boolean containsBrokenAnalysis(Analyzer analyzer) { | ||
// TODO maybe we need a getter on Namedanalyzer that tells if this uses broken Analysis | ||
if (analyzer instanceof NamedAnalyzer) { | ||
analyzer = ((NamedAnalyzer) analyzer).analyzer(); | ||
} | ||
if (analyzer instanceof CustomAnalyzer) { | ||
final CustomAnalyzer a = (CustomAnalyzer) analyzer; | ||
if (a.tokenizerFactory() instanceof EdgeNGramTokenizerFactory) { | ||
return true; | ||
} | ||
TokenFilterFactory[] tokenFilters = a.tokenFilters(); | ||
for (TokenFilterFactory tokenFilterFactory : tokenFilters) { | ||
if (tokenFilterFactory instanceof WordDelimiterTokenFilterFactory | ||
|| tokenFilterFactory instanceof EdgeNGramTokenFilterFactory) { | ||
return true; | ||
} | ||
} | ||
} | ||
return 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
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
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
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