1 # ====================================================================
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13 # ====================================================================
16 PythonQueryParser, PythonMultiFieldQueryParser, \
17 PhraseQuery, TermRangeQuery, SpanNearQuery, SpanTermQuery, \
18 Term, PhraseQuery, Version
20 from lia.extsearch.queryparser.NumberUtils import NumberUtils
23 # A QueryParser extension
26 class CustomQueryParser(PythonQueryParser):
28 def __init__(self, field, analyzer):
29 super(CustomQueryParser, self).__init__(Version.LUCENE_CURRENT, field, analyzer)
31 def getFuzzyQuery(self, field, termText, minSimilarity):
32 raise AssertionError, "Fuzzy queries not allowed"
34 def getWildcardQuery(self, field, termText):
35 raise AssertionError, "Wildcard queries not allowed"
38 # Special handling for the "id" field, pads each part
39 # to match how it was indexed.
41 def getRangeQuery(self, field, part1, part2, inclusive):
48 return TermRangeQuery(field,
49 NumberUtils.pad(num1),
50 NumberUtils.pad(num2),
53 if field == "special":
54 print part1, "->", part2
56 return TermRangeQuery("field", part1, part2, inclusive, True)
58 return super(CustomQueryParser,
59 self).getRangeQuery(field, part1, part2, inclusive)
62 def getFieldQuery_quoted(self, field, queryText, quoted):
64 return super(CustomQueryParser,
65 self).getFieldQuery_quoted_super(field, queryText, quoted)
68 # Replace PhraseQuery with SpanNearQuery to force in-order
69 # phrase matching rather than reverse.
71 def getFieldQuery_slop(self, field, queryText, slop):
73 orig = super(CustomQueryParser,
74 self).getFieldQuery_slop_super(field, queryText, slop)
76 if not PhraseQuery.instance_(orig):
79 pq = PhraseQuery.cast_(orig)
80 clauses = [SpanTermQuery(term) for term in pq.getTerms()]
82 return SpanNearQuery(clauses, slop, True);
86 class MultiFieldCustomQueryParser(PythonMultiFieldQueryParser):
88 def __init__(self, fields, analyzer):
89 super(MultiFieldCustomQueryParser, self).__init__(Version.LUCENE_CURRENT, fields, analyzer)
91 def getFuzzyQuery(self, super, field, termText, minSimilarity):
92 raise AssertionError, "Fuzzy queries not allowed"
94 def getWildcardQuery(self, super, field, termText):
95 raise AssertionError, "Wildcard queries not allowed"
98 # Special handling for the "id" field, pads each part
99 # to match how it was indexed.
101 def getRangeQuery(self, field, part1, part2, inclusive):
108 return TermRangeQuery(field,
109 NumberUtils.pad(num1),
110 NumberUtils.pad(num2),
113 if field == "special":
114 print part1, "->", part2
116 return TermRangeQuery("field", part1, part2, inclusive, True)
118 return super(CustomQueryParser,
119 self).getRangeQuery(field, part1, part2, inclusive)
121 def getFieldQuery_quoted(self, field, queryText, quoted):
123 return super(CustomQueryParser,
124 self).getFieldQuery_quoted_super(field, queryText, quoted)
127 # Replace PhraseQuery with SpanNearQuery to force in-order
128 # phrase matching rather than reverse.
130 def getFieldQuery_slop(self, field, queryText, slop):
132 # let QueryParser's implementation do the analysis
133 orig = super(CustomQueryParser,
134 self).getFieldQuery_slop_super(field, queryText, slop)
136 if not PhraseQuery.instance_(orig):
139 pq = PhraseQuery.cast_(orig)
140 clauses = [SpanTermQuery(term) for term in pq.getTerms()]
142 return SpanNearQuery(clauses, slop, True);