Skip to content

Commit

Permalink
_analyzer api inconsistency without index name (elastic#115930)
Browse files Browse the repository at this point in the history
Set positionIncrementGap to default (100) when no index is provided for the _analyze API
  • Loading branch information
drempapis authored Nov 18, 2024
1 parent c32b300 commit 3253c2a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/115930.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 115930
summary: Inconsistency in the _analyzer api when the index is not included
area: Search
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ public Analyzer getAnalyzer(String analyzer) throws IOException {
}
});
}
return analyzerProvider.get(environment, analyzer).get();

return overridePositionIncrementGap(
(NamedAnalyzer) analyzerProvider.get(environment, analyzer).get(),
TextFieldMapper.Defaults.POSITION_INCREMENT_GAP
);
}

@Override
Expand Down Expand Up @@ -720,20 +724,22 @@ private static NamedAnalyzer produceAnalyzer(
throw new IllegalArgumentException("analyzer [" + analyzerFactory.name() + "] created null analyzer");
}
NamedAnalyzer analyzer;
if (analyzerF instanceof NamedAnalyzer) {
// if we got a named analyzer back, use it...
analyzer = (NamedAnalyzer) analyzerF;
if (overridePositionIncrementGap >= 0 && analyzer.getPositionIncrementGap(analyzer.name()) != overridePositionIncrementGap) {
// unless the positionIncrementGap needs to be overridden
analyzer = new NamedAnalyzer(analyzer, overridePositionIncrementGap);
}
if (analyzerF instanceof NamedAnalyzer namedAnalyzer) {
analyzer = overridePositionIncrementGap(namedAnalyzer, overridePositionIncrementGap);
} else {
analyzer = new NamedAnalyzer(name, analyzerFactory.scope(), analyzerF, overridePositionIncrementGap);
}
checkVersions(analyzer);
return analyzer;
}

private static NamedAnalyzer overridePositionIncrementGap(NamedAnalyzer analyzer, int overridePositionIncrementGap) {
if (overridePositionIncrementGap >= 0 && analyzer.getPositionIncrementGap(analyzer.name()) != overridePositionIncrementGap) {
analyzer = new NamedAnalyzer(analyzer, overridePositionIncrementGap);
}
return analyzer;
}

private static void processNormalizerFactory(
String name,
AnalyzerProvider<?> normalizerFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import java.io.IOException;
import java.io.Reader;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -250,6 +251,32 @@ public void testFillsAttributes() throws IOException {
assertEquals("<ALPHANUM>", tokens.get(3).getType());
}

public void testAnalyzerWithTwoTextsAndNoIndexName() throws IOException {
AnalyzeAction.Request request = new AnalyzeAction.Request();

for (String analyzer : Arrays.asList("standard", "simple", "stop", "keyword", "whitespace", "classic")) {
request.analyzer(analyzer);
request.text("a a", "b b");

AnalyzeAction.Response analyzeIndex = TransportAnalyzeAction.analyze(request, registry, mockIndexService(), maxTokenCount);
List<AnalyzeAction.AnalyzeToken> tokensIndex = analyzeIndex.getTokens();

AnalyzeAction.Response analyzeNoIndex = TransportAnalyzeAction.analyze(request, registry, null, maxTokenCount);
List<AnalyzeAction.AnalyzeToken> tokensNoIndex = analyzeNoIndex.getTokens();

assertEquals(tokensIndex.size(), tokensNoIndex.size());
for (int i = 0; i < tokensIndex.size(); i++) {
AnalyzeAction.AnalyzeToken withIndex = tokensIndex.get(i);
AnalyzeAction.AnalyzeToken withNoIndex = tokensNoIndex.get(i);

assertEquals(withIndex.getStartOffset(), withNoIndex.getStartOffset());
assertEquals(withIndex.getEndOffset(), withNoIndex.getEndOffset());
assertEquals(withIndex.getPosition(), withNoIndex.getPosition());
assertEquals(withIndex.getType(), withNoIndex.getType());
}
}
}

public void testWithIndexAnalyzers() throws IOException {
AnalyzeAction.Request request = new AnalyzeAction.Request();
request.text("the quick brown fox");
Expand Down

0 comments on commit 3253c2a

Please sign in to comment.