|
1 | 1 | package org.jabref.logic.search.query;
|
2 | 2 |
|
| 3 | +import java.util.EnumSet; |
3 | 4 | import java.util.List;
|
| 5 | +import java.util.Locale; |
4 | 6 |
|
5 | 7 | import org.jabref.model.search.LinkedFilesConstants;
|
| 8 | +import org.jabref.model.search.SearchFlags; |
6 | 9 | import org.jabref.search.SearchBaseVisitor;
|
7 | 10 | import org.jabref.search.SearchParser;
|
8 | 11 |
|
9 |
| -import org.apache.lucene.index.Term; |
10 |
| -import org.apache.lucene.search.BooleanClause; |
11 |
| -import org.apache.lucene.search.BooleanQuery; |
12 |
| -import org.apache.lucene.search.MatchNoDocsQuery; |
13 |
| -import org.apache.lucene.search.Query; |
14 |
| -import org.apache.lucene.search.RegexpQuery; |
15 |
| -import org.apache.lucene.search.TermQuery; |
16 |
| -import org.apache.lucene.util.QueryBuilder; |
| 12 | +import org.apache.lucene.queryparser.classic.QueryParser; |
17 | 13 |
|
18 | 14 | /**
|
19 | 15 | * Tests are located in {@link org.jabref.logic.search.query.SearchQueryLuceneConversionTest}.
|
20 | 16 | */
|
21 |
| -public class SearchToLuceneVisitor extends SearchBaseVisitor<Query> { |
| 17 | +public class SearchToLuceneVisitor extends SearchBaseVisitor<String> { |
| 18 | + private final EnumSet<SearchFlags> searchFlags; |
22 | 19 |
|
23 |
| - private static final List<String> SEARCH_FIELDS = LinkedFilesConstants.PDF_FIELDS; |
24 |
| - |
25 |
| - private final QueryBuilder queryBuilder; |
26 |
| - |
27 |
| - public SearchToLuceneVisitor() { |
28 |
| - this.queryBuilder = new QueryBuilder(LinkedFilesConstants.LINKED_FILES_ANALYZER); |
| 20 | + public SearchToLuceneVisitor(EnumSet<SearchFlags> searchFlags) { |
| 21 | + this.searchFlags = searchFlags; |
29 | 22 | }
|
30 | 23 |
|
31 | 24 | @Override
|
32 |
| - public Query visitStart(SearchParser.StartContext ctx) { |
| 25 | + public String visitStart(SearchParser.StartContext ctx) { |
33 | 26 | return visit(ctx.andExpression());
|
34 | 27 | }
|
35 | 28 |
|
36 | 29 | @Override
|
37 |
| - public Query visitImplicitAndExpression(SearchParser.ImplicitAndExpressionContext ctx) { |
38 |
| - List<Query> children = ctx.expression().stream().map(this::visit).toList(); |
39 |
| - if (children.size() == 1) { |
40 |
| - return children.getFirst(); |
41 |
| - } |
42 |
| - BooleanQuery.Builder builder = new BooleanQuery.Builder(); |
43 |
| - for (Query child : children) { |
44 |
| - builder.add(child, BooleanClause.Occur.MUST); |
45 |
| - } |
46 |
| - return builder.build(); |
| 30 | + public String visitImplicitAndExpression(SearchParser.ImplicitAndExpressionContext ctx) { |
| 31 | + List<String> children = ctx.expression().stream().map(this::visit).toList(); |
| 32 | + return children.size() == 1 ? children.getFirst() : String.join(" ", children); |
47 | 33 | }
|
48 | 34 |
|
49 | 35 | @Override
|
50 |
| - public Query visitParenExpression(SearchParser.ParenExpressionContext ctx) { |
51 |
| - return visit(ctx.andExpression()); |
| 36 | + public String visitParenExpression(SearchParser.ParenExpressionContext ctx) { |
| 37 | + String expr = visit(ctx.andExpression()); |
| 38 | + return expr.isEmpty() ? "" : "(" + expr + ")"; |
52 | 39 | }
|
53 | 40 |
|
54 | 41 | @Override
|
55 |
| - public Query visitNegatedExpression(SearchParser.NegatedExpressionContext ctx) { |
56 |
| - Query innerQuery = visit(ctx.expression()); |
57 |
| - if (innerQuery instanceof MatchNoDocsQuery) { |
58 |
| - return innerQuery; |
59 |
| - } |
60 |
| - BooleanQuery.Builder builder = new BooleanQuery.Builder(); |
61 |
| - builder.add(innerQuery, BooleanClause.Occur.MUST_NOT); |
62 |
| - return builder.build(); |
| 42 | + public String visitNegatedExpression(SearchParser.NegatedExpressionContext ctx) { |
| 43 | + return "NOT (" + visit(ctx.expression()) + ")"; |
63 | 44 | }
|
64 | 45 |
|
65 | 46 | @Override
|
66 |
| - public Query visitBinaryExpression(SearchParser.BinaryExpressionContext ctx) { |
67 |
| - Query left = visit(ctx.left); |
68 |
| - Query right = visit(ctx.right); |
| 47 | + public String visitBinaryExpression(SearchParser.BinaryExpressionContext ctx) { |
| 48 | + String left = visit(ctx.left); |
| 49 | + String right = visit(ctx.right); |
69 | 50 |
|
70 |
| - if (left instanceof MatchNoDocsQuery) { |
| 51 | + if (left.isEmpty() && right.isEmpty()) { |
| 52 | + return ""; |
| 53 | + } |
| 54 | + if (left.isEmpty()) { |
71 | 55 | return right;
|
72 | 56 | }
|
73 |
| - if (right instanceof MatchNoDocsQuery) { |
| 57 | + if (right.isEmpty()) { |
74 | 58 | return left;
|
75 | 59 | }
|
76 | 60 |
|
77 |
| - BooleanQuery.Builder builder = new BooleanQuery.Builder(); |
| 61 | + String operator = ctx.bin_op.getType() == SearchParser.AND ? " AND " : " OR "; |
| 62 | + return left + operator + right; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public String visitComparison(SearchParser.ComparisonContext ctx) { |
| 67 | + String term = SearchQueryConversion.unescapeSearchValue(ctx.searchValue()); |
| 68 | + boolean isQuoted = ctx.searchValue().getStart().getType() == SearchParser.STRING_LITERAL; |
| 69 | + |
| 70 | + // unfielded expression |
| 71 | + if (ctx.FIELD() == null) { |
| 72 | + if (searchFlags.contains(SearchFlags.REGULAR_EXPRESSION)) { |
| 73 | + return "/" + term + "/"; |
| 74 | + } |
| 75 | + return isQuoted ? "\"" + escapeQuotes(term) + "\"" : QueryParser.escape(term); |
| 76 | + } |
78 | 77 |
|
79 |
| - if (ctx.bin_op.getType() == SearchParser.AND) { |
80 |
| - builder.add(left, BooleanClause.Occur.MUST); |
81 |
| - builder.add(right, BooleanClause.Occur.MUST); |
82 |
| - } else if (ctx.bin_op.getType() == SearchParser.OR) { |
83 |
| - builder.add(left, BooleanClause.Occur.SHOULD); |
84 |
| - builder.add(right, BooleanClause.Occur.SHOULD); |
| 78 | + String field = ctx.FIELD().getText().toLowerCase(Locale.ROOT); |
| 79 | + if (!isValidField(field)) { |
| 80 | + return ""; |
85 | 81 | }
|
86 | 82 |
|
87 |
| - return builder.build(); |
| 83 | + field = "any".equals(field) || "anyfield".equals(field) ? "" : field + ":"; |
| 84 | + int operator = ctx.operator().getStart().getType(); |
| 85 | + return buildFieldExpression(field, term, operator, isQuoted); |
88 | 86 | }
|
89 | 87 |
|
90 |
| - @Override |
91 |
| - public Query visitComparisonExpression(SearchParser.ComparisonExpressionContext ctx) { |
92 |
| - return visit(ctx.comparison()); |
| 88 | + private boolean isValidField(String field) { |
| 89 | + return "any".equals(field) || "anyfield".equals(field) || LinkedFilesConstants.PDF_FIELDS.contains(field); |
93 | 90 | }
|
94 | 91 |
|
95 |
| - @Override |
96 |
| - public Query visitComparison(SearchParser.ComparisonContext ctx) { |
97 |
| - String field = ctx.FIELD() == null ? null : ctx.FIELD().getText(); |
98 |
| - String term = SearchQueryConversion.unescapeSearchValue(ctx.searchValue()); |
| 92 | + private String buildFieldExpression(String field, String term, int operator, boolean isQuoted) { |
| 93 | + boolean isRegexOp = isRegexOperator(operator); |
| 94 | + boolean isNegationOp = isNegationOperator(operator); |
99 | 95 |
|
100 |
| - // unfielded expression |
101 |
| - if (field == null || "anyfield".equals(field) || "any".equals(field)) { |
102 |
| - return createMultiFieldQuery(term, ctx.operator()); |
103 |
| - } else if (SEARCH_FIELDS.contains(field)) { |
104 |
| - return createFieldQuery(field, term, ctx.operator()); |
| 96 | + if (isRegexOp) { |
| 97 | + String expression = field + "/" + term + "/"; |
| 98 | + return isNegationOp ? "NOT " + expression : expression; |
105 | 99 | } else {
|
106 |
| - return new MatchNoDocsQuery(); |
| 100 | + term = isQuoted ? "\"" + escapeQuotes(term) + "\"" : QueryParser.escape(term); |
| 101 | + String expression = field + term; |
| 102 | + return isNegationOp ? "NOT " + expression : expression; |
107 | 103 | }
|
108 | 104 | }
|
109 | 105 |
|
110 |
| - private Query createMultiFieldQuery(String value, SearchParser.OperatorContext operator) { |
111 |
| - BooleanQuery.Builder builder = new BooleanQuery.Builder(); |
112 |
| - for (String field : SEARCH_FIELDS) { |
113 |
| - builder.add(createFieldQuery(field, value, operator), BooleanClause.Occur.SHOULD); |
114 |
| - } |
115 |
| - return builder.build(); |
| 106 | + private static String escapeQuotes(String term) { |
| 107 | + return term.replace("\"", "\\\""); |
116 | 108 | }
|
117 | 109 |
|
118 |
| - private Query createFieldQuery(String field, String value, SearchParser.OperatorContext operator) { |
119 |
| - if (operator == null) { |
120 |
| - return createTermOrPhraseQuery(field, value); |
121 |
| - } |
122 |
| - |
123 |
| - return switch (operator.getStart().getType()) { |
124 |
| - case SearchParser.REQUAL, |
125 |
| - SearchParser.CREEQUAL -> |
126 |
| - new RegexpQuery(new Term(field, value)); |
| 110 | + private static boolean isNegationOperator(int operator) { |
| 111 | + return switch (operator) { |
127 | 112 | case SearchParser.NEQUAL,
|
128 | 113 | SearchParser.NCEQUAL,
|
129 | 114 | SearchParser.NEEQUAL,
|
130 |
| - SearchParser.NCEEQUAL -> |
131 |
| - createNegatedQuery(createTermOrPhraseQuery(field, value)); |
132 |
| - case SearchParser.NREQUAL, |
133 |
| - SearchParser.NCREEQUAL -> |
134 |
| - createNegatedQuery(new RegexpQuery(new Term(field, value))); |
135 |
| - default -> |
136 |
| - createTermOrPhraseQuery(field, value); |
| 115 | + SearchParser.NCEEQUAL, |
| 116 | + SearchParser.NREQUAL, |
| 117 | + SearchParser.NCREEQUAL -> true; |
| 118 | + default -> false; |
137 | 119 | };
|
138 | 120 | }
|
139 | 121 |
|
140 |
| - private Query createNegatedQuery(Query query) { |
141 |
| - BooleanQuery.Builder negatedQuery = new BooleanQuery.Builder(); |
142 |
| - negatedQuery.add(query, BooleanClause.Occur.MUST_NOT); |
143 |
| - return negatedQuery.build(); |
144 |
| - } |
145 |
| - |
146 |
| - private Query createTermOrPhraseQuery(String field, String value) { |
147 |
| - if (value.contains("*") || value.contains("?")) { |
148 |
| - return new TermQuery(new Term(field, value)); |
149 |
| - } |
150 |
| - return queryBuilder.createPhraseQuery(field, value); |
| 122 | + private static boolean isRegexOperator(int operator) { |
| 123 | + return switch (operator) { |
| 124 | + case SearchParser.REQUAL, |
| 125 | + SearchParser.CREEQUAL, |
| 126 | + SearchParser.NREQUAL, |
| 127 | + SearchParser.NCREEQUAL -> true; |
| 128 | + default -> false; |
| 129 | + }; |
151 | 130 | }
|
152 | 131 | }
|
0 commit comments