|
23 | 23 | import org.apache.lucene.search.DocIdSetIterator;
|
24 | 24 | import org.apache.lucene.search.IndexSearcher;
|
25 | 25 | import org.apache.lucene.search.MatchAllDocsQuery;
|
| 26 | +import org.apache.lucene.search.MatchNoDocsQuery; |
26 | 27 | import org.apache.lucene.search.MultiTermQuery;
|
27 | 28 | import org.apache.lucene.search.Query;
|
28 | 29 | import org.apache.lucene.search.QueryVisitor;
|
@@ -448,14 +449,20 @@ public Query wildcardQuery(String value, MultiTermQuery.RewriteMethod method, bo
|
448 | 449 | };
|
449 | 450 | }
|
450 | 451 |
|
451 |
| - return new WildcardMatchingQuery( |
452 |
| - name(), |
453 |
| - matchAllTermsQuery(name(), getRequiredNGrams(finalValue)), |
454 |
| - matchPredicate, |
455 |
| - value, |
456 |
| - context, |
457 |
| - this |
458 |
| - ); |
| 452 | + Set<String> requiredNGrams = getRequiredNGrams(finalValue); |
| 453 | + Query approximation; |
| 454 | + if (requiredNGrams.isEmpty()) { |
| 455 | + // This only happens when all characters are wildcard characters (* or ?), |
| 456 | + // or it's the empty string. |
| 457 | + if (value.length() == 0 || value.contains("?")) { |
| 458 | + approximation = this.existsQuery(context); |
| 459 | + } else { |
| 460 | + return existsQuery(context); |
| 461 | + } |
| 462 | + } else { |
| 463 | + approximation = matchAllTermsQuery(name(), requiredNGrams); |
| 464 | + } |
| 465 | + return new WildcardMatchingQuery(name(), approximation, matchPredicate, value, context, this); |
459 | 466 | }
|
460 | 467 |
|
461 | 468 | // Package-private for testing
|
@@ -540,10 +547,23 @@ public Query regexpQuery(
|
540 | 547 | Automaton automaton = regExp.toAutomaton(maxDeterminizedStates);
|
541 | 548 | CompiledAutomaton compiledAutomaton = new CompiledAutomaton(automaton);
|
542 | 549 |
|
543 |
| - return new WildcardMatchingQuery(name(), regexpToQuery(name(), regExp), s -> { |
544 |
| - BytesRef valueBytes = BytesRefs.toBytesRef(s); |
545 |
| - return compiledAutomaton.runAutomaton.run(valueBytes.bytes, valueBytes.offset, valueBytes.length); |
546 |
| - }, "/" + value + "/", context, this); |
| 550 | + Predicate<String> regexpPredicate; |
| 551 | + if (compiledAutomaton.type == CompiledAutomaton.AUTOMATON_TYPE.ALL) { |
| 552 | + return existsQuery(context); |
| 553 | + } else if (compiledAutomaton.type == CompiledAutomaton.AUTOMATON_TYPE.NONE) { |
| 554 | + return new MatchNoDocsQuery("Regular expression matches nothing"); |
| 555 | + } else { |
| 556 | + regexpPredicate = s -> { |
| 557 | + BytesRef valueBytes = BytesRefs.toBytesRef(s); |
| 558 | + return compiledAutomaton.runAutomaton.run(valueBytes.bytes, valueBytes.offset, valueBytes.length); |
| 559 | + }; |
| 560 | + } |
| 561 | + |
| 562 | + Query approximation = regexpToQuery(name(), regExp); |
| 563 | + if (approximation instanceof MatchAllDocsQuery) { |
| 564 | + approximation = existsQuery(context); |
| 565 | + } |
| 566 | + return new WildcardMatchingQuery(name(), approximation, regexpPredicate, "/" + value + "/", context, this); |
547 | 567 | }
|
548 | 568 |
|
549 | 569 | /**
|
@@ -602,6 +622,8 @@ private static Query regexpToQuery(String fieldName, RegExp regExp) {
|
602 | 622 | }
|
603 | 623 | if (query.clauses().size() == 1) {
|
604 | 624 | return query.iterator().next().getQuery();
|
| 625 | + } else if (query.clauses().size() == 0) { |
| 626 | + return new MatchAllDocsQuery(); |
605 | 627 | }
|
606 | 628 | return query;
|
607 | 629 | }
|
@@ -704,7 +726,7 @@ private WildcardMatchingQuery(
|
704 | 726 |
|
705 | 727 | @Override
|
706 | 728 | public String toString(String s) {
|
707 |
| - return "WildcardMatchingQuery(" + fieldName + "\"" + patternString + "\")"; |
| 729 | + return "WildcardMatchingQuery(" + fieldName + ":\"" + patternString + "\")"; |
708 | 730 | }
|
709 | 731 |
|
710 | 732 | @Override
|
|
0 commit comments