42
42
import org .elasticsearch .index .IndexService ;
43
43
import org .elasticsearch .index .IndexSettings ;
44
44
import org .elasticsearch .index .analysis .AnalysisRegistry ;
45
+ import org .elasticsearch .index .analysis .AnalyzerComponents ;
46
+ import org .elasticsearch .index .analysis .AnalyzerComponentsProvider ;
45
47
import org .elasticsearch .index .analysis .CharFilterFactory ;
46
- import org .elasticsearch .index .analysis .CustomAnalyzer ;
47
48
import org .elasticsearch .index .analysis .NameOrDefinition ;
48
49
import org .elasticsearch .index .analysis .NamedAnalyzer ;
49
50
import org .elasticsearch .index .analysis .TokenFilterFactory ;
@@ -261,18 +262,23 @@ private static AnalyzeAction.DetailAnalyzeResponse detailAnalyze(AnalyzeAction.R
261
262
}
262
263
}
263
264
264
- CustomAnalyzer customAnalyzer = null ;
265
- if (analyzer instanceof CustomAnalyzer ) {
266
- customAnalyzer = (CustomAnalyzer ) analyzer ;
267
- } else if (analyzer instanceof NamedAnalyzer && ((NamedAnalyzer ) analyzer ).analyzer () instanceof CustomAnalyzer ) {
268
- customAnalyzer = (CustomAnalyzer ) ((NamedAnalyzer ) analyzer ).analyzer ();
265
+ // maybe unwrap analyzer from NamedAnalyzer
266
+ Analyzer potentialCustomAnalyzer = analyzer ;
267
+ if (analyzer instanceof NamedAnalyzer ) {
268
+ potentialCustomAnalyzer = ((NamedAnalyzer ) analyzer ).analyzer ();
269
269
}
270
270
271
- if (customAnalyzer != null ) {
272
- // customAnalyzer = divide charfilter, tokenizer tokenfilters
273
- CharFilterFactory [] charFilterFactories = customAnalyzer .charFilters ();
274
- TokenizerFactory tokenizerFactory = customAnalyzer .tokenizerFactory ();
275
- TokenFilterFactory [] tokenFilterFactories = customAnalyzer .tokenFilters ();
271
+ if (potentialCustomAnalyzer instanceof AnalyzerComponentsProvider ) {
272
+ AnalyzerComponentsProvider customAnalyzer = (AnalyzerComponentsProvider ) potentialCustomAnalyzer ;
273
+ // note: this is not field-name dependent in our cases so we can leave out the argument
274
+ int positionIncrementGap = potentialCustomAnalyzer .getPositionIncrementGap ("" );
275
+ int offsetGap = potentialCustomAnalyzer .getOffsetGap ("" );
276
+ AnalyzerComponents components = customAnalyzer .getComponents ();
277
+ // divide charfilter, tokenizer tokenfilters
278
+ CharFilterFactory [] charFilterFactories = components .getCharFilters ();
279
+ TokenizerFactory tokenizerFactory = components .getTokenizerFactory ();
280
+ TokenFilterFactory [] tokenFilterFactories = components .getTokenFilters ();
281
+ String tokenizerName = components .getTokenizerName ();
276
282
277
283
String [][] charFiltersTexts = new String [charFilterFactories != null ? charFilterFactories .length : 0 ][request .text ().length ];
278
284
TokenListCreator [] tokenFiltersTokenListCreator = new TokenListCreator [tokenFilterFactories != null ?
@@ -298,7 +304,7 @@ private static AnalyzeAction.DetailAnalyzeResponse detailAnalyze(AnalyzeAction.R
298
304
// analyzing only tokenizer
299
305
Tokenizer tokenizer = tokenizerFactory .create ();
300
306
tokenizer .setReader (reader );
301
- tokenizerTokenListCreator .analyze (tokenizer , customAnalyzer , includeAttributes );
307
+ tokenizerTokenListCreator .analyze (tokenizer , includeAttributes , positionIncrementGap , offsetGap );
302
308
303
309
// analyzing each tokenfilter
304
310
if (tokenFilterFactories != null ) {
@@ -308,7 +314,7 @@ private static AnalyzeAction.DetailAnalyzeResponse detailAnalyze(AnalyzeAction.R
308
314
}
309
315
TokenStream stream = createStackedTokenStream (request .text ()[textIndex ],
310
316
charFilterFactories , tokenizerFactory , tokenFilterFactories , tokenFilterIndex + 1 );
311
- tokenFiltersTokenListCreator [tokenFilterIndex ].analyze (stream , customAnalyzer , includeAttributes );
317
+ tokenFiltersTokenListCreator [tokenFilterIndex ].analyze (stream , includeAttributes , positionIncrementGap , offsetGap );
312
318
}
313
319
}
314
320
}
@@ -331,8 +337,8 @@ private static AnalyzeAction.DetailAnalyzeResponse detailAnalyze(AnalyzeAction.R
331
337
tokenFilterFactories [tokenFilterIndex ].name (), tokenFiltersTokenListCreator [tokenFilterIndex ].getArrayTokens ());
332
338
}
333
339
}
334
- detailResponse = new AnalyzeAction .DetailAnalyzeResponse (charFilteredLists , new AnalyzeAction . AnalyzeTokenList (
335
- customAnalyzer . getTokenizerName () , tokenizerTokenListCreator .getArrayTokens ()), tokenFilterLists );
340
+ detailResponse = new AnalyzeAction .DetailAnalyzeResponse (charFilteredLists ,
341
+ new AnalyzeAction . AnalyzeTokenList ( tokenizerName , tokenizerTokenListCreator .getArrayTokens ()), tokenFilterLists );
336
342
} else {
337
343
String name ;
338
344
if (analyzer instanceof NamedAnalyzer ) {
@@ -343,8 +349,8 @@ private static AnalyzeAction.DetailAnalyzeResponse detailAnalyze(AnalyzeAction.R
343
349
344
350
TokenListCreator tokenListCreator = new TokenListCreator (maxTokenCount );
345
351
for (String text : request .text ()) {
346
- tokenListCreator .analyze (analyzer .tokenStream ("" , text ), analyzer ,
347
- includeAttributes );
352
+ tokenListCreator .analyze (analyzer .tokenStream ("" , text ), includeAttributes , analyzer . getPositionIncrementGap ( "" ) ,
353
+ analyzer . getOffsetGap ( "" ) );
348
354
}
349
355
detailResponse
350
356
= new AnalyzeAction .DetailAnalyzeResponse (new AnalyzeAction .AnalyzeTokenList (name , tokenListCreator .getArrayTokens ()));
@@ -414,7 +420,7 @@ private static class TokenListCreator {
414
420
tc = new TokenCounter (maxTokenCount );
415
421
}
416
422
417
- private void analyze (TokenStream stream , Analyzer analyzer , Set <String > includeAttributes ) {
423
+ private void analyze (TokenStream stream , Set <String > includeAttributes , int positionIncrementGap , int offsetGap ) {
418
424
try {
419
425
stream .reset ();
420
426
CharTermAttribute term = stream .addAttribute (CharTermAttribute .class );
@@ -437,8 +443,8 @@ private void analyze(TokenStream stream, Analyzer analyzer, Set<String> includeA
437
443
lastOffset += offset .endOffset ();
438
444
lastPosition += posIncr .getPositionIncrement ();
439
445
440
- lastPosition += analyzer . getPositionIncrementGap ( "" ) ;
441
- lastOffset += analyzer . getOffsetGap ( "" ) ;
446
+ lastPosition += positionIncrementGap ;
447
+ lastOffset += offsetGap ;
442
448
443
449
} catch (IOException e ) {
444
450
throw new ElasticsearchException ("failed to analyze" , e );
0 commit comments