Improved resilience of query object when index is lacking necessary information, improve efficiency of search results by not using handles if the resource ID is available

git-svn-id: http://scm.dspace.org/svn/repo/branches/dspace-1_5_x@2521 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Graham Triggs
2008-01-13 19:19:19 +00:00
parent 654462dbe0
commit a2f335d697
2 changed files with 83 additions and 55 deletions

View File

@@ -165,14 +165,24 @@ public class DSQuery
Query myquery = qp.parse(querystring); Query myquery = qp.parse(querystring);
Hits hits = null; Hits hits = null;
if (args.getSortOption() == null) try
{ {
hits = searcher.search(myquery, new Sort(new SortField[] { new SortField("type"), SortField.FIELD_SCORE })); if (args.getSortOption() == null)
{
hits = searcher.search(myquery, new Sort(new SortField[] { new SortField("type"), SortField.FIELD_SCORE }));
}
else
{
SortField[] sortFields = new SortField[] { new SortField("type"), new SortField("sort_" + args.getSortOption().getName(), SortOption.DESCENDING.equals(args.getSortOrder())), SortField.FIELD_SCORE };
hits = searcher.search(myquery, new Sort(sortFields));
}
} }
else catch (Exception e)
{ {
SortField[] sortFields = new SortField[] { new SortField("type"), new SortField("sort_" + args.getSortOption().getName(), SortOption.DESCENDING.equals(args.getSortOrder())), SortField.FIELD_SCORE }; // Lucene can throw an exception if it is unable to determine a sort time from the specified field
hits = searcher.search(myquery, new Sort(sortFields)); // Provide a fall back that just works on relevancy.
log.error("Unable to use speficied sort option: " + (args.getSortOption() == null ? "type/relevance": args.getSortOption().getName()));
hits = searcher.search(myquery, new Sort(SortField.FIELD_SCORE));
} }
// set total number of hits // set total number of hits

View File

@@ -109,9 +109,9 @@ public class SimpleSearchServlet extends DSpaceServlet
start = 0; start = 0;
} }
List itemHandles = new ArrayList(); int collCount = 0;
List collectionHandles = new ArrayList(); int commCount = 0;
List communityHandles = new ArrayList(); int itemCount = 0;
Item[] resultsItems; Item[] resultsItems;
Collection[] resultsCollections; Collection[] resultsCollections;
@@ -228,82 +228,100 @@ public class SimpleSearchServlet extends DSpaceServlet
} }
// now instantiate the results and put them in their buckets // now instantiate the results and put them in their buckets
for (int i = 0; i < qResults.getHitHandles().size(); i++) for (int i = 0; i < qResults.getHitTypes().size(); i++)
{ {
String myHandle = (String) qResults.getHitHandles().get(i);
Integer myType = (Integer) qResults.getHitTypes().get(i); Integer myType = (Integer) qResults.getHitTypes().get(i);
// add the handle to the appropriate lists // add the handle to the appropriate lists
switch (myType.intValue()) switch (myType.intValue())
{ {
case Constants.ITEM: case Constants.ITEM:
itemHandles.add(myHandle); itemCount++;
break; break;
case Constants.COLLECTION: case Constants.COLLECTION:
collectionHandles.add(myHandle); collCount++;
break; break;
case Constants.COMMUNITY: case Constants.COMMUNITY:
communityHandles.add(myHandle); commCount++;
break; break;
} }
} }
int numCommunities = communityHandles.size();
int numCollections = collectionHandles.size();
int numItems = itemHandles.size();
// Make objects from the handles - make arrays, fill them out // Make objects from the handles - make arrays, fill them out
resultsCommunities = new Community[numCommunities]; resultsCommunities = new Community[commCount];
resultsCollections = new Collection[numCollections]; resultsCollections = new Collection[collCount];
resultsItems = new Item[numItems]; resultsItems = new Item[itemCount];
for (int i = 0; i < numItems; i++) collCount = 0;
commCount = 0;
itemCount = 0;
for (int i = 0; i < qResults.getHitTypes().size(); i++)
{ {
String myhandle = (String) itemHandles.get(i); Integer myId = (Integer) qResults.getHitIds().get(i);
String myHandle = (String) qResults.getHitHandles().get(i);
Integer myType = (Integer) qResults.getHitTypes().get(i);
Object o = HandleManager.resolveToObject(context, myhandle); // add the handle to the appropriate lists
switch (myType.intValue())
resultsItems[i] = (Item) o;
if (resultsItems[i] == null)
{ {
throw new SQLException("Query \"" + query case Constants.ITEM:
+ "\" returned unresolvable handle: " + myhandle); if (myId != null)
} {
} resultsItems[itemCount] = Item.find(context, myId);
}
else
{
resultsItems[itemCount] = (Item)HandleManager.resolveToObject(context, myHandle);
}
for (int i = 0; i < collectionHandles.size(); i++) if (resultsItems[itemCount] == null)
{ {
String myhandle = (String) collectionHandles.get(i); throw new SQLException("Query \"" + query
+ "\" returned unresolvable item");
}
itemCount++;
break;
Object o = HandleManager.resolveToObject(context, myhandle); case Constants.COLLECTION:
if (myId != null)
{
resultsCollections[collCount] = Collection.find(context, myId);
}
else
{
resultsCollections[collCount] = (Collection)HandleManager.resolveToObject(context, myHandle);
}
resultsCollections[i] = (Collection) o; if (resultsCollections[collCount] == null)
{
throw new SQLException("Query \"" + query
+ "\" returned unresolvable collection");
}
if (resultsCollections[i] == null) collCount++;
{ break;
throw new SQLException("Query \"" + query
+ "\" returned unresolvable handle: " + myhandle);
}
}
for (int i = 0; i < communityHandles.size(); i++) case Constants.COMMUNITY:
{ if (myId != null)
String myhandle = (String) communityHandles.get(i); {
resultsCommunities[commCount] = Community.find(context, myId);
}
else
{
resultsCommunities[commCount] = (Community)HandleManager.resolveToObject(context, myHandle);
}
Object o = HandleManager.resolveToObject(context, myhandle); if (resultsCommunities[commCount] == null)
{
throw new SQLException("Query \"" + query
+ "\" returned unresolvable community");
}
resultsCommunities[i] = (Community) o; commCount++;
break;
if (resultsCommunities[i] == null)
{
throw new SQLException("Query \"" + query
+ "\" returned unresolvable handle: " + myhandle);
} }
} }