43
43
import static org .opensearch .index .mapper .NumberFieldMapper .NumberType .SHORT ;
44
44
import static org .opensearch .index .mapper .NumberFieldMapper .NumberType .hasDecimalPart ;
45
45
import static org .opensearch .index .mapper .NumberFieldMapper .NumberType .signum ;
46
+ import static org .opensearch .search .startree .filter .DimensionFilter .MatchType .GTE ;
46
47
47
48
public interface DimensionFilterMapper {
48
49
DimensionFilter getExactMatchFilter (MappedFieldType mappedFieldType , List <Object > rawValues );
@@ -345,20 +346,36 @@ public Optional<Long> getMatchingOrdinal(
345
346
} else {
346
347
TermsEnum termsEnum = sortedSetIterator .termsEnum ();
347
348
TermsEnum .SeekStatus seekStatus = termsEnum .seekCeil ((BytesRef ) value );
348
- if (matchType == DimensionFilter .MatchType .GT || matchType == DimensionFilter .MatchType .GTE ) {
349
- // We reached the end and couldn't match anything, else we found a term which matches.
350
- return (seekStatus == TermsEnum .SeekStatus .END ) ? Optional .empty () : Optional .of (termsEnum .ord ());
351
- } else { // LT || LTE
352
- // If we found a term just greater, then return ordinal of the term just before it.
353
- if (seekStatus == TermsEnum .SeekStatus .NOT_FOUND ) {
354
- long ordGreaterThanValue = termsEnum .ord ();
355
- // Checking if we are in bounds for satisfying LT
356
- return ((ordGreaterThanValue - 1 ) < sortedSetIterator .getValueCount ())
357
- ? Optional .of (ordGreaterThanValue - 1 )
358
- : Optional .empty ();
359
- } else {
360
- return Optional .of (termsEnum .ord ());
361
- }
349
+ // We reached the end and couldn't match anything, else we found a term which matches.
350
+ // LT || LTE
351
+ // If we found a term just greater, then return ordinal of the term just before it.
352
+ // Checking if we are in bounds for satisfying LT
353
+ // Checking if we are in bounds for satisfying LT
354
+ switch (matchType ) {
355
+ case GTE :
356
+ return seekStatus == TermsEnum .SeekStatus .END ? Optional .empty () : Optional .of (termsEnum .ord ());
357
+ case GT :
358
+ return switch (seekStatus ) {
359
+ case END -> Optional .empty ();
360
+ case FOUND -> ((termsEnum .ord () + 1 ) < sortedSetIterator .getValueCount ())
361
+ ? Optional .of (termsEnum .ord () + 1 )
362
+ : Optional .empty ();
363
+ case NOT_FOUND -> Optional .of (termsEnum .ord ());
364
+ };
365
+ case LTE :
366
+ if (seekStatus == TermsEnum .SeekStatus .NOT_FOUND ) {
367
+ return ((termsEnum .ord () - 1 ) >= 0 ) ? Optional .of (termsEnum .ord () - 1 ) : Optional .empty ();
368
+ } else {
369
+ return Optional .of (termsEnum .ord ());
370
+ }
371
+ case LT :
372
+ if (seekStatus == TermsEnum .SeekStatus .END ) {
373
+ return Optional .of (termsEnum .ord ());
374
+ } else {
375
+ return ((termsEnum .ord () - 1 ) >= 0 ) ? Optional .of (termsEnum .ord () - 1 ) : Optional .empty ();
376
+ }
377
+ default :
378
+ throw new IllegalStateException ("unexpected matchType " + matchType );
362
379
}
363
380
}
364
381
} catch (IOException e ) {
@@ -371,14 +388,16 @@ public Optional<Long> getMatchingOrdinal(
371
388
372
389
// TODO : Think around making TermBasedFT#indexedValueForSearch() accessor public for reuse here.
373
390
private Object parseRawKeyword (String field , Object rawValue , KeywordFieldType keywordFieldType ) {
374
- Object parsedValue ;
375
- if (keywordFieldType .getTextSearchInfo ().getSearchAnalyzer () == Lucene .KEYWORD_ANALYZER ) {
376
- parsedValue = BytesRefs .toBytesRef (rawValue );
377
- } else {
378
- if (rawValue instanceof BytesRef ) {
379
- rawValue = ((BytesRef ) rawValue ).utf8ToString ();
391
+ Object parsedValue = null ;
392
+ if (rawValue != null ) {
393
+ if (keywordFieldType .getTextSearchInfo ().getSearchAnalyzer () == Lucene .KEYWORD_ANALYZER ) {
394
+ parsedValue = BytesRefs .toBytesRef (rawValue );
395
+ } else {
396
+ if (rawValue instanceof BytesRef ) {
397
+ rawValue = ((BytesRef ) rawValue ).utf8ToString ();
398
+ }
399
+ parsedValue = keywordFieldType .getTextSearchInfo ().getSearchAnalyzer ().normalize (field , rawValue .toString ());
380
400
}
381
- parsedValue = keywordFieldType .getTextSearchInfo ().getSearchAnalyzer ().normalize (field , rawValue .toString ());
382
401
}
383
402
return parsedValue ;
384
403
}
0 commit comments