Skip to content

Commit 95309f5

Browse files
committed
fix(search): case user has not access to any table and ensure table filter also applies to count query
1 parent 9ff3824 commit 95309f5

File tree

4 files changed

+42
-29
lines changed

4 files changed

+42
-29
lines changed

opal-search/src/main/java/org/obiba/opal/search/service/OpalSearchService.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ public Search.QueryResultDto executeQuery(QuerySettings querySettings, String se
152152
return Search.QueryResultDto.newBuilder().setTotalHits(0).build();
153153
}
154154

155-
public Search.QueryCountDto executeCount(String query, String searchPath) throws SearchException {
155+
public Search.QueryCountDto executeCount(QuerySettings querySettings, String searchPath) throws SearchException {
156156
if (!isRunning()) return Search.QueryCountDto.newBuilder().setTotalHits(0).build();
157157
if (variablesIndexManager.getName().equals(searchPath)) {
158-
return variablesIndexManager.createQueryExecutor().count(query);
158+
return variablesIndexManager.createQueryExecutor().count(querySettings);
159159
} else if (tablesIndexManager.getName().equals(searchPath)) {
160-
return tablesIndexManager.createQueryExecutor().count(query);
160+
return tablesIndexManager.createQueryExecutor().count(querySettings);
161161
}
162162
// return getSearchServicePlugin().executeQuery(querySettings, searchPath, strategy);
163163
return Search.QueryCountDto.newBuilder().setTotalHits(0).build();

opal-search/src/main/java/org/obiba/opal/search/service/SearchQueryExecutor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ public interface SearchQueryExecutor {
2626
/**
2727
* Performs a count on the query.
2828
*
29-
* @param query
29+
* @param querySettings
3030
* @return
3131
* @throws SearchException
3232
*/
33-
Search.QueryCountDto count(String query) throws SearchException;
33+
Search.QueryCountDto count(QuerySettings querySettings) throws SearchException;
3434

3535
}

opal-search/src/main/java/org/obiba/opal/search/service/impl/ContentQueryExecutor.java

+36-23
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,13 @@ public Search.QueryResultDto execute(QuerySettings querySettings) throws SearchE
5454
// Create an IndexSearcher to perform the search
5555
IndexSearcher searcher = new IndexSearcher(reader);
5656

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();
7460
}
61+
62+
// Make query
63+
Query query = makeQuery(querySettings);
7564
String lastDoc = querySettings.getLastDoc();
7665
ScoreDoc lastScoreDoc = Strings.isNullOrEmpty(lastDoc) ? null : ScoreDocSerializer.deserialize(lastDoc);
7766

@@ -111,18 +100,20 @@ public Search.QueryResultDto execute(QuerySettings querySettings) throws SearchE
111100
}
112101

113102
@Override
114-
public Search.QueryCountDto count(String rawQuery) throws SearchException {
103+
public Search.QueryCountDto count(QuerySettings querySettings) throws SearchException {
115104
// Create an IndexReader to access the index
116105
try (IndexReader reader = DirectoryReader.open(directory);) {
117106
// Create an IndexSearcher to perform the search
118107
IndexSearcher searcher = new IndexSearcher(reader);
119108

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+
}
122113

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));
126117
return builder.build();
127118
} catch (IOException e) {
128119
throw new SearchException("Tables index access failure", e);
@@ -134,4 +125,26 @@ public Search.QueryCountDto count(String rawQuery) throws SearchException {
134125
return Search.QueryCountDto.newBuilder().setTotalHits(0).build();
135126
}
136127
}
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+
}
137150
}

opal-search/src/main/java/org/obiba/opal/web/search/DatasourcesVariablesSearchResource.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public Response count(@QueryParam("query") String query) {
7979

8080
try {
8181
if (!searchServiceAvailable()) return Response.status(Response.Status.SERVICE_UNAVAILABLE).build();
82-
Search.QueryCountDto queryCountDto = opalSearchService.executeCount(query, getSearchPath());
82+
Search.QueryCountDto queryCountDto = opalSearchService.executeCount(buildQuerySearch(query, null, 0, null, null, null), getSearchPath());
8383
return Response.ok().entity(queryCountDto).build();
8484
} catch (Exception e) {
8585
log.error("Unable to perform variables search", e);

0 commit comments

Comments
 (0)