Skip to content

Commit 25cadca

Browse files
drempapisalexey-ivanov-es
authored andcommitted
_analyzer api inconsistency without index name (elastic#115930)
Set positionIncrementGap to default (100) when no index is provided for the _analyze API
1 parent 906de15 commit 25cadca

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

docs/changelog/115930.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 115930
2+
summary: Inconsistency in the _analyzer api when the index is not included
3+
area: Search
4+
type: bug
5+
issues: []

server/src/main/java/org/elasticsearch/index/analysis/AnalysisRegistry.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,11 @@ public Analyzer getAnalyzer(String analyzer) throws IOException {
189189
}
190190
});
191191
}
192-
return analyzerProvider.get(environment, analyzer).get();
192+
193+
return overridePositionIncrementGap(
194+
(NamedAnalyzer) analyzerProvider.get(environment, analyzer).get(),
195+
TextFieldMapper.Defaults.POSITION_INCREMENT_GAP
196+
);
193197
}
194198

195199
@Override
@@ -720,20 +724,22 @@ private static NamedAnalyzer produceAnalyzer(
720724
throw new IllegalArgumentException("analyzer [" + analyzerFactory.name() + "] created null analyzer");
721725
}
722726
NamedAnalyzer analyzer;
723-
if (analyzerF instanceof NamedAnalyzer) {
724-
// if we got a named analyzer back, use it...
725-
analyzer = (NamedAnalyzer) analyzerF;
726-
if (overridePositionIncrementGap >= 0 && analyzer.getPositionIncrementGap(analyzer.name()) != overridePositionIncrementGap) {
727-
// unless the positionIncrementGap needs to be overridden
728-
analyzer = new NamedAnalyzer(analyzer, overridePositionIncrementGap);
729-
}
727+
if (analyzerF instanceof NamedAnalyzer namedAnalyzer) {
728+
analyzer = overridePositionIncrementGap(namedAnalyzer, overridePositionIncrementGap);
730729
} else {
731730
analyzer = new NamedAnalyzer(name, analyzerFactory.scope(), analyzerF, overridePositionIncrementGap);
732731
}
733732
checkVersions(analyzer);
734733
return analyzer;
735734
}
736735

736+
private static NamedAnalyzer overridePositionIncrementGap(NamedAnalyzer analyzer, int overridePositionIncrementGap) {
737+
if (overridePositionIncrementGap >= 0 && analyzer.getPositionIncrementGap(analyzer.name()) != overridePositionIncrementGap) {
738+
analyzer = new NamedAnalyzer(analyzer, overridePositionIncrementGap);
739+
}
740+
return analyzer;
741+
}
742+
737743
private static void processNormalizerFactory(
738744
String name,
739745
AnalyzerProvider<?> normalizerFactory,

server/src/test/java/org/elasticsearch/action/admin/indices/TransportAnalyzeActionTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import java.io.IOException;
4444
import java.io.Reader;
45+
import java.util.Arrays;
4546
import java.util.List;
4647
import java.util.Map;
4748

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

254+
public void testAnalyzerWithTwoTextsAndNoIndexName() throws IOException {
255+
AnalyzeAction.Request request = new AnalyzeAction.Request();
256+
257+
for (String analyzer : Arrays.asList("standard", "simple", "stop", "keyword", "whitespace", "classic")) {
258+
request.analyzer(analyzer);
259+
request.text("a a", "b b");
260+
261+
AnalyzeAction.Response analyzeIndex = TransportAnalyzeAction.analyze(request, registry, mockIndexService(), maxTokenCount);
262+
List<AnalyzeAction.AnalyzeToken> tokensIndex = analyzeIndex.getTokens();
263+
264+
AnalyzeAction.Response analyzeNoIndex = TransportAnalyzeAction.analyze(request, registry, null, maxTokenCount);
265+
List<AnalyzeAction.AnalyzeToken> tokensNoIndex = analyzeNoIndex.getTokens();
266+
267+
assertEquals(tokensIndex.size(), tokensNoIndex.size());
268+
for (int i = 0; i < tokensIndex.size(); i++) {
269+
AnalyzeAction.AnalyzeToken withIndex = tokensIndex.get(i);
270+
AnalyzeAction.AnalyzeToken withNoIndex = tokensNoIndex.get(i);
271+
272+
assertEquals(withIndex.getStartOffset(), withNoIndex.getStartOffset());
273+
assertEquals(withIndex.getEndOffset(), withNoIndex.getEndOffset());
274+
assertEquals(withIndex.getPosition(), withNoIndex.getPosition());
275+
assertEquals(withIndex.getType(), withNoIndex.getType());
276+
}
277+
}
278+
}
279+
253280
public void testWithIndexAnalyzers() throws IOException {
254281
AnalyzeAction.Request request = new AnalyzeAction.Request();
255282
request.text("the quick brown fox");

0 commit comments

Comments
 (0)