@@ -54,24 +54,13 @@ public Search.QueryResultDto execute(QuerySettings querySettings) throws SearchE
54
54
// Create an IndexSearcher to perform the search
55
55
IndexSearcher searcher = new IndexSearcher (reader );
56
56
57
- // Build a QueryParser
58
- QueryParser parser = new QueryParser ("content" , analyzer );
59
-
60
- // Parse a query
61
- Query query = parser .parse (querySettings .getQuery ());
62
- if (querySettings .hasFilterReferences ()) {
63
- // at least one table-ref must match
64
- BooleanQuery .Builder queryBuilder = new BooleanQuery .Builder ();
65
- for (String tableRef : querySettings .getFilterReferences ()) {
66
- queryBuilder .add (new TermQuery (new Term ("table-ref" , tableRef )), BooleanClause .Occur .SHOULD );
67
- }
68
- Query termsQuery = queryBuilder .build ();
69
-
70
- queryBuilder = new BooleanQuery .Builder ();
71
- queryBuilder .add (new BooleanClause (query , BooleanClause .Occur .MUST ));
72
- queryBuilder .add (new BooleanClause (termsQuery , BooleanClause .Occur .MUST ));
73
- query = queryBuilder .build ();
57
+ // Case no tables
58
+ if (!querySettings .hasFilterReferences ()) {
59
+ return Search .QueryResultDto .newBuilder ().setTotalHits (0 ).build ();
74
60
}
61
+
62
+ // Make query
63
+ Query query = makeQuery (querySettings );
75
64
String lastDoc = querySettings .getLastDoc ();
76
65
ScoreDoc lastScoreDoc = Strings .isNullOrEmpty (lastDoc ) ? null : ScoreDocSerializer .deserialize (lastDoc );
77
66
@@ -111,18 +100,20 @@ public Search.QueryResultDto execute(QuerySettings querySettings) throws SearchE
111
100
}
112
101
113
102
@ Override
114
- public Search .QueryCountDto count (String rawQuery ) throws SearchException {
103
+ public Search .QueryCountDto count (QuerySettings querySettings ) throws SearchException {
115
104
// Create an IndexReader to access the index
116
105
try (IndexReader reader = DirectoryReader .open (directory );) {
117
106
// Create an IndexSearcher to perform the search
118
107
IndexSearcher searcher = new IndexSearcher (reader );
119
108
120
- // Build a QueryParser
121
- QueryParser parser = new QueryParser ("content" , analyzer );
109
+ // Case no tables
110
+ if (!querySettings .hasFilterReferences ()) {
111
+ return Search .QueryCountDto .newBuilder ().setTotalHits (0 ).build ();
112
+ }
122
113
123
- // Parse a query (search for books with "Lucene" in the title)
124
- Query parsedQuery = parser . parse ( rawQuery );
125
- Search .QueryCountDto .Builder builder = Search .QueryCountDto .newBuilder ().setTotalHits (searcher .count (parsedQuery ));
114
+ // Make query
115
+ Query query = makeQuery ( querySettings );
116
+ Search .QueryCountDto .Builder builder = Search .QueryCountDto .newBuilder ().setTotalHits (searcher .count (query ));
126
117
return builder .build ();
127
118
} catch (IOException e ) {
128
119
throw new SearchException ("Tables index access failure" , e );
@@ -134,4 +125,26 @@ public Search.QueryCountDto count(String rawQuery) throws SearchException {
134
125
return Search .QueryCountDto .newBuilder ().setTotalHits (0 ).build ();
135
126
}
136
127
}
128
+
129
+ private Query makeQuery (QuerySettings querySettings ) throws ParseException {
130
+ // Build a QueryParser
131
+ QueryParser parser = new QueryParser ("content" , analyzer );
132
+
133
+ // Parse a query
134
+ Query query = parser .parse (querySettings .getQuery ());
135
+ if (querySettings .hasFilterReferences ()) {
136
+ // at least one table-ref must match
137
+ BooleanQuery .Builder queryBuilder = new BooleanQuery .Builder ();
138
+ for (String tableRef : querySettings .getFilterReferences ()) {
139
+ queryBuilder .add (new TermQuery (new Term ("table-ref" , tableRef )), BooleanClause .Occur .SHOULD );
140
+ }
141
+ Query termsQuery = queryBuilder .build ();
142
+
143
+ queryBuilder = new BooleanQuery .Builder ();
144
+ queryBuilder .add (new BooleanClause (query , BooleanClause .Occur .MUST ));
145
+ queryBuilder .add (new BooleanClause (termsQuery , BooleanClause .Occur .MUST ));
146
+ query = queryBuilder .build ();
147
+ }
148
+ return query ;
149
+ }
137
150
}
0 commit comments