|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| package org.apache.xindice.core.query.ftsearch; |
|
21 |
| |
|
22 |
| import org.apache.lucene.analysis.Analyzer; |
|
23 |
| import org.apache.lucene.queryParser.CharStream; |
|
24 |
| import org.apache.lucene.queryParser.QueryParser; |
|
25 |
| import org.apache.lucene.queryParser.QueryParserTokenManager; |
|
26 |
| import org.apache.lucene.search.BooleanClause; |
|
27 |
| import org.apache.lucene.search.Query; |
|
28 |
| |
|
29 |
| import java.util.List; |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| public class SpecialQueryParser extends QueryParser { |
|
39 |
| private static final int CONJ_AND = 1; |
|
40 |
| private static final int CONJ_OR = 2; |
|
41 |
| |
|
42 |
| private static final int MOD_NOT = 10; |
|
43 |
| private static final int MOD_REQ = 11; |
|
44 |
| |
|
45 |
9
| public SpecialQueryParser(String f, Analyzer a) {
|
|
46 |
9
| super(f, a);
|
|
47 |
| } |
|
48 |
| |
|
49 |
0
| public SpecialQueryParser(CharStream stream) {
|
|
50 |
0
| super(stream);
|
|
51 |
| } |
|
52 |
| |
|
53 |
0
| public SpecialQueryParser(QueryParserTokenManager tm) {
|
|
54 |
0
| super(tm);
|
|
55 |
| } |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
9
| protected void addClause(List clauses, int conj, int mods, Query q) {
|
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
9
| if (clauses.size() > 0 && conj == CONJ_AND) {
|
|
67 |
0
| BooleanClause c = (BooleanClause) clauses.get(clauses.size()-1);
|
|
68 |
0
| if (!c.isProhibited())
|
|
69 |
0
| c.setOccur(BooleanClause.Occur.MUST);
|
|
70 |
| } |
|
71 |
| |
|
72 |
9
| if (clauses.size() > 0 && getDefaultOperator() == AND_OPERATOR && conj == CONJ_OR) {
|
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
0
| BooleanClause c = (BooleanClause) clauses.get(clauses.size()-1);
|
|
78 |
0
| if (!c.isProhibited())
|
|
79 |
0
| c.setOccur(BooleanClause.Occur.SHOULD);
|
|
80 |
| } |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
9
| if (q == null)
|
|
85 |
0
| return;
|
|
86 |
| |
|
87 |
9
| boolean required, prohibited;
|
|
88 |
9
| if (getDefaultOperator() == OR_OPERATOR) {
|
|
89 |
| |
|
90 |
| |
|
91 |
9
| prohibited = (mods == MOD_NOT);
|
|
92 |
9
| required = (mods == MOD_REQ);
|
|
93 |
9
| if (conj == CONJ_AND && !prohibited) {
|
|
94 |
0
| required = true;
|
|
95 |
| } |
|
96 |
| } else { |
|
97 |
| |
|
98 |
| |
|
99 |
0
| prohibited = (mods == MOD_NOT);
|
|
100 |
0
| required = (!prohibited && conj != CONJ_OR);
|
|
101 |
| } |
|
102 |
9
| if (required && !prohibited) {
|
|
103 |
0
| clauses.add(new BooleanClause(q, BooleanClause.Occur.MUST));
|
|
104 |
9
| } else if (!required && !prohibited) {
|
|
105 |
9
| clauses.add(new BooleanClause(q, BooleanClause.Occur.SHOULD));
|
|
106 |
0
| } else if (required && prohibited) {
|
|
107 |
0
| throw new RuntimeException("Clause cannot be both required and prohibited");
|
|
108 |
| } |
|
109 |
| } |
|
110 |
| } |