1
package org.apache.lucene.queryParser.standard.processors;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
import java.util.List;
22
import org.apache.lucene.queryParser.core.QueryNodeException;
23
import org.apache.lucene.queryParser.core.nodes.FieldQueryNode;
24
import org.apache.lucene.queryParser.core.nodes.FuzzyQueryNode;
25
import org.apache.lucene.queryParser.core.nodes.ParametricQueryNode;
26
import org.apache.lucene.queryParser.core.nodes.QueryNode;
27
import org.apache.lucene.queryParser.core.nodes.QuotedFieldQueryNode;
28
import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorImpl;
29
import org.apache.lucene.queryParser.core.util.UnescapedCharSequence;
30
import org.apache.lucene.queryParser.standard.nodes.PrefixWildcardQueryNode;
31
import org.apache.lucene.queryParser.standard.nodes.WildcardQueryNode;
32
import org.apache.lucene.queryParser.standard.parser.StandardSyntaxParser;
33
import org.apache.lucene.search.PrefixQuery;
36
* The {@link StandardSyntaxParser} creates {@link PrefixWildcardQueryNode} nodes which
37
* have values containing the prefixed wildcard. However, Lucene
38
* {@link PrefixQuery} cannot contain the prefixed wildcard. So, this processor
39
* basically removed the prefixed wildcard from the
40
* {@link PrefixWildcardQueryNode} value. <br/>
43
* @see PrefixWildcardQueryNode
45
public class WildcardQueryNodeProcessor extends QueryNodeProcessorImpl {
47
public WildcardQueryNodeProcessor() {
52
protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
54
// the old Lucene Parser ignores FuzzyQueryNode that are also PrefixWildcardQueryNode or WildcardQueryNode
55
// we do the same here, also ignore empty terms
56
if (node instanceof FieldQueryNode || node instanceof FuzzyQueryNode) {
57
FieldQueryNode fqn = (FieldQueryNode) node;
58
CharSequence text = fqn.getText();
60
// do not process wildcards for ParametricQueryNode and
61
// QuotedFieldQueryNode to reproduce the old parser behavior
62
if (fqn instanceof ParametricQueryNode
63
|| fqn instanceof QuotedFieldQueryNode
64
|| text.length() <= 0){
69
// Code below simulates the old lucene parser behavior for wildcards
71
if (isPrefixWildcard(text)) {
72
PrefixWildcardQueryNode prefixWildcardQN = new PrefixWildcardQueryNode(fqn);
73
return prefixWildcardQN;
75
} else if (isWildcard(text)){
76
WildcardQueryNode wildcardQN = new WildcardQueryNode(fqn);
86
private boolean isWildcard(CharSequence text) {
87
if (text ==null || text.length() <= 0) return false;
89
// If a un-escaped '*' or '?' if found return true
90
// start at the end since it's more common to put wildcards at the end
91
for(int i=text.length()-1; i>=0; i--){
92
if ((text.charAt(i) == '*' || text.charAt(i) == '?') && !UnescapedCharSequence.wasEscaped(text, i)){
100
private boolean isPrefixWildcard(CharSequence text) {
101
if (text == null || text.length() <= 0 || !isWildcard(text)) return false;
103
// Validate last character is a '*' and was not escaped
104
// If single '*' is is a wildcard not prefix to simulate old queryparser
105
if (text.charAt(text.length()-1) != '*') return false;
106
if (UnescapedCharSequence.wasEscaped(text, text.length()-1)) return false;
107
if (text.length() == 1) return false;
109
// Only make a prefix if there is only one single star at the end and no '?' or '*' characters
110
// If single wildcard return false to mimic old queryparser
111
for(int i=0; i<text.length(); i++){
112
if (text.charAt(i) == '?') return false;
113
if (text.charAt(i) == '*' && !UnescapedCharSequence.wasEscaped(text, i)){
114
if (i == text.length()-1)
125
protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
132
protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
133
throws QueryNodeException {