Skip to content

Commit

Permalink
don't enter solrDownMode on an unparseable query #3632
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed May 7, 2014
1 parent 061b438 commit 18337ab
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions scripts/database/homebrew/rebuild-and-test
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ sleep 15
scripts/database/homebrew/run-post-create-post-deploy
scripts/search/tests/permissions
scripts/search/tests/delete-dataverse
scripts/search/tests/query-unparseable
11 changes: 11 additions & 0 deletions scripts/search/tests/expected/query-unparseable
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

{
"q":":",
"fq_provided":"[]",
"fq_actual":"[]",
"total_count":0,
"start":0,
"count_in_response":0,
"items":"[]",
"error":"Trouble parsing query? org.apache.solr.search.SyntaxError: Cannot parse ':': Encountered \" \":\" \": \"\" at line 1, column 0.\nWas expecting one of:\n <NOT> ...\n \"+\" ...\n \"-\" ...\n <BAREOPER> ...\n \"(\" ...\n \"*\" ...\n <QUOTED> ...\n <TERM> ...\n <PREFIXTERM> ...\n <WILDTERM> ...\n <REGEXPTERM> ...\n \"[\" ...\n \"{\" ...\n <LPARAMS> ...\n <NUMBER> ...\n <TERM> ...\n \"*\" ...\n "
}
11 changes: 11 additions & 0 deletions scripts/search/tests/expected/solr-down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

{
"q":"*",
"fq_provided":"[]",
"fq_actual":"[]",
"total_count":0,
"start":0,
"count_in_response":0,
"items":"[]",
"error":"Internal error: Server refused connection at: http://localhost:8983/solr"
}
2 changes: 2 additions & 0 deletions scripts/search/tests/query-unparseable
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
diff <(curl -s 'http://localhost:8080/api/search?q=:') scripts/search/tests/expected/query-unparseable
2 changes: 2 additions & 0 deletions scripts/search/tests/solr-down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
diff <(curl -s 'http://localhost:8080/api/search?q=*') scripts/search/tests/expected/solr-down
28 changes: 26 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/SearchServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer.RemoteSolrException;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.RangeFacet;
Expand Down Expand Up @@ -238,8 +239,31 @@ public SolrQueryResponse search(DataverseUser dataverseUser, Dataverse dataverse
QueryResponse queryResponse;
try {
queryResponse = solrServer.query(solrQuery);
} catch (SolrServerException ex) {
throw new RuntimeException("Is the Solr server down?");
} catch (SolrServerException | RemoteSolrException ex) {
String error = "Bloops!";
if (ex instanceof SolrServerException) {
error = "Internal error: " + ex.getLocalizedMessage();
} else if (ex instanceof RemoteSolrException) {
error = "Trouble parsing query? " + ex.getLocalizedMessage();
}

logger.info(error + " " + ex.getLocalizedMessage());
SolrQueryResponse exceptionSolrQueryResponse = new SolrQueryResponse();
exceptionSolrQueryResponse.setError(error);

long zeroNumResultsFound = 0;
long zeroGetResultsStart = 0;
List<SolrSearchResult> emptySolrSearchResults = new ArrayList<>();
List<FacetCategory> exceptionFacetCategoryList = new ArrayList<>();
Map<String, List<String>> emptySpellingSuggestion = new HashMap<>();
exceptionSolrQueryResponse.setNumResultsFound(zeroNumResultsFound);
exceptionSolrQueryResponse.setResultsStart(zeroGetResultsStart);
exceptionSolrQueryResponse.setSolrSearchResults(emptySolrSearchResults);
exceptionSolrQueryResponse.setFacetCategoryList(exceptionFacetCategoryList);
exceptionSolrQueryResponse.setTypeFacetCategories(exceptionFacetCategoryList);
exceptionSolrQueryResponse.setSpellingSuggestionsByToken(emptySpellingSuggestion);

return exceptionSolrQueryResponse;
}
SolrDocumentList docs = queryResponse.getResults();
Iterator<SolrDocument> iter = docs.iterator();
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/SolrQueryResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class SolrQueryResponse {
Map<String, String> datasetfieldFriendlyNamesBySolrField = new HashMap<>();
private Map<String, String> staticSolrFieldFriendlyNamesBySolrField;
private List<String> filterQueriesActual = new ArrayList<String>();
private String error;

public List<SolrSearchResult> getSolrSearchResults() {
return solrSearchResults;
Expand Down Expand Up @@ -92,4 +93,12 @@ public void setFilterQueriesActual(List<String> filterQueriesActual) {
this.filterQueriesActual = filterQueriesActual;
}

public String getError() {
return error;
}

public void setError(String error) {
this.error = error;
}

}
5 changes: 5 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/api/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer.RemoteSolrException;

@Path("search")
public class Search extends AbstractApiBean {
Expand Down Expand Up @@ -134,6 +136,9 @@ public String search(@QueryParam("key") String apiKey,
value.add("spelling_alternatives", spelling_alternatives);
value.add("facets", facets);
}
if (solrQueryResponse.getError() != null) {
value.add("error", solrQueryResponse.getError());
}
return Util.jsonObject2prettyString(value.build());
} else {
/**
Expand Down

0 comments on commit 18337ab

Please sign in to comment.