- public void search(HttpServletRequest req, HttpServletResponse res)
- throws ServletModuleException, ServletModuleUserException {
-
- String queryString="";
-
- SimpleHash mergeData = new SimpleHash();
-
- //make the query available to subsequent iterations
-
- for (Enumeration theParams = req.getParameterNames(); theParams.hasMoreElements() ;) {
- String pName=(String)theParams.nextElement();
- if (pName.startsWith("search_")){
- mergeData.put(pName,new SimpleScalar(req.getParameter(pName)));
- }
- }
-
- try{
- mergeData.put("topics", themenModule.getTopicsAsSimpleList());
- }
- catch(ModuleException e) {
- theLog.printDebugInfo("Can't get topics: " + e.toString());
- }
-
- String indexPath=MirConfig.getProp("IndexPath");
-
-
- KeywordSearchTerm dateTerm = new KeywordSearchTerm("date_formatted","search_date","webdb_create_formatted","webdbcreate_formatted","webdb_create_formatted");
-
- UnIndexedSearchTerm whereTerm = new UnIndexedSearchTerm("","","","where","where");
-
- TextSearchTerm creatorTerm = new TextSearchTerm("creator","search_creator","creator","creator","creator");
- TextSearchTerm titleTerm = new TextSearchTerm("title","search_content","title","title","title");
- TextSearchTerm descriptionTerm = new TextSearchTerm("description","search_content","description","description","description");
-
- ContentSearchTerm contentTerm = new ContentSearchTerm("content_data","search_content","content","","");
-
- TopicSearchTerm topicTerm = new TopicSearchTerm();
-
- ImagesSearchTerm imagesTerm = new ImagesSearchTerm();
-
- AudioSearchTerm audioTerm = new AudioSearchTerm();
-
- VideoSearchTerm videoTerm = new VideoSearchTerm();
-
- String creatorFragment = creatorTerm.makeTerm(req);
- if (creatorFragment != null){
- queryString = queryString + " +" + creatorFragment;
- }
-
- // search title, description, and content for something
- // the contentTerm uses "search_boolean" to combine its terms
- String contentFragment = contentTerm.makeTerm(req);
- if (contentFragment != null){
- theLog.printDebugInfo("contentFragment: " + contentFragment);
- queryString = queryString + " +" + contentFragment;
- }
-
- String topicFragment = topicTerm.makeTerm(req);
- if (topicFragment != null){
- queryString = queryString + " +" + topicFragment;
- }
-
- String imagesFragment = imagesTerm.makeTerm(req);
- if (imagesFragment != null){
- queryString = queryString + " +" + imagesFragment;
- }
-
- String audioFragment = audioTerm.makeTerm(req);
- if (audioFragment != null){
- queryString = queryString + " +" + audioFragment;
- }
-
- String videoFragment = videoTerm.makeTerm(req);
- if (videoFragment != null){
- queryString = queryString + " +" + videoFragment;
- }
-
- if (queryString == null || queryString == ""){
- queryString = "";
- }
- else{
-
- try{
- Searcher searcher = null;
- try {
- searcher = new IndexSearcher(indexPath);
- }
- catch(IOException e) {
- theLog.printDebugInfo("Can't open indexPath: " + indexPath);
- throw new ServletModuleUserException("Problem with Search Index!");
- }
-
- // parse the query String.
- Query query = null;
- try {
- query = QueryParser.parse(queryString, "content", new StandardAnalyzer());
- }
- catch(Exception e) {
- searcher.close();
- theLog.printDebugInfo("Query don't parse: " + queryString);
- throw new ServletModuleUserException("Problem with Query String! (was '"+queryString+"')");
- }
-
- Hits hits = null;
- try {
- hits = searcher.search(query);
- } catch(IOException e) {
- searcher.close();
- theLog.printDebugInfo("Can't get hits: " + e.toString());
- throw new ServletModuleUserException("Problem getting hits!");
- }
-
-
- // this is what needs to change in order to get sorting by date
- // iterate over the results
- // the results are an array of document
- // associate this with session so we don't need to reduplicate searches
- // make a map from date to position
- // make an list of dates
- // sort list by date
- // grab low+n entries from the list of dates,
- // iterate over these, using hash to get hit position,
- // and hit position to get hit
- // shove hit into SimpleList of SimpleHashes
-
-
-
- try {
- int start = 0;
- int end = hits.length();
- mergeData.put("numberOfHits", (new Integer(end)).toString());
- SimpleList theHits = new SimpleList();
- for(int i = start; i < end; i++) {
- SimpleHash h = new SimpleHash();
- Document theHit = hits.doc(i);
- whereTerm.returnMeta(h,theHit);
- creatorTerm.returnMeta(h,theHit);
- titleTerm.returnMeta(h,theHit);
- descriptionTerm.returnMeta(h,theHit);
- dateTerm.returnMeta(h,theHit);
- imagesTerm.returnMeta(h,theHit);
- audioTerm.returnMeta(h,theHit);
- videoTerm.returnMeta(h,theHit);
- theHits.add(h);
- }
- mergeData.put("hits",theHits);
- }
- catch (Exception e) {
- searcher.close();
- theLog.printDebugInfo("Can't iterate over hits: " + e.toString());
- throw new ServletModuleUserException("Problem getting hits!");
- }
- searcher.close();
- }
- catch (IOException e){
- theLog.printDebugInfo("Can't close searcher: " + e.toString());
- throw new ServletModuleUserException("Problem closing searcher!");
- }
- }
- mergeData.put("queryString",queryString);
- deliver(req,res,mergeData,searchResultsTemplate);
+
+ public void search(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
+ try {
+ final String[] search_variables = {
+ "search_content", "search_boolean", "search_creator",
+ "search_topic", "search_hasImages", "search_hasAudio", "search_hasVideo", "search_sort",
+ "search_submit", "search_back", "search_forward"};
+ HTTPRequestParser requestParser = new HTTPRequestParser(req);
+
+ int increment = 10;
+
+ HttpSession session = req.getSession(false);
+
+ String queryString = "";
+
+ Map mergeData = new HashMap();
+
+ KeywordSearchTerm dateTerm = new KeywordSearchTerm("date_formatted", "search_date", "webdb_create_formatted", "webdb_create_formatted", "webdb_create_formatted");
+ UnIndexedSearchTerm whereTerm = new UnIndexedSearchTerm("", "", "", "where", "where");
+ TextSearchTerm creatorTerm = new TextSearchTerm("creator", "search_creator", "creator", "creator", "creator");
+ TextSearchTerm titleTerm = new TextSearchTerm("title", "search_content", "title", "title", "title");
+ TextSearchTerm descriptionTerm = new TextSearchTerm("description", "search_content", "description", "description", "description");
+ ContentSearchTerm contentTerm = new ContentSearchTerm("content_data", "search_content", "content", "", "");
+ TopicSearchTerm topicTerm = new TopicSearchTerm();
+ TopicMatrixSearchTerm topicMatrixTerm = new TopicMatrixSearchTerm();
+ ImagesSearchTerm imagesTerm = new ImagesSearchTerm();
+ AudioSearchTerm audioTerm = new AudioSearchTerm();
+ VideoSearchTerm videoTerm = new VideoSearchTerm();
+
+ //make the query available to subsequent iterations
+
+ Iterator j = Arrays.asList(search_variables).iterator();
+ while (j.hasNext()) {
+ String variable = (String) j.next();
+
+ mergeData.put(variable, requestParser.getParameter(variable));
+ }
+
+ try {
+ mergeData.put("topics", DatabaseTopics.getInstance().getPopupData());
+ }
+ catch (Throwable e) {
+ logger.debug("Can't get topics: " + e.toString());
+ }
+
+ String searchBackValue = req.getParameter("search_back");
+ String searchForwardValue = req.getParameter("search_forward");
+
+ if (searchBackValue != null) {
+ int totalHits = ( (Integer) session.getAttribute("numberOfHits")).intValue();
+ int newPosition = ( (Integer) session.getAttribute("positionInResults")).intValue() - increment;
+ if (newPosition < 0)
+ newPosition = 0;
+ if (newPosition >= totalHits)
+ newPosition = totalHits - 1;
+ session.setAttribute("positionInResults", new Integer(newPosition));
+ }
+ else {
+ if (searchForwardValue != null) {
+ int totalHits = ( (Integer) session.getAttribute("numberOfHits")).intValue();
+ int newPosition = ( (Integer) session.getAttribute("positionInResults")).intValue() + increment;
+ if (newPosition < 0)
+ newPosition = 0;
+ if (newPosition >= totalHits)
+ newPosition = totalHits - 1;
+
+ session.setAttribute("positionInResults", new Integer(newPosition));
+ }
+ else {
+ String indexPath = configuration.getString("IndexPath");
+
+ String creatorFragment = creatorTerm.makeTerm(req);
+ if (creatorFragment != null) {
+ queryString = queryString + " +" + creatorFragment;
+ }
+
+ // search title, description, and content for something
+ // the contentTerm uses param "search_boolean" to combine its terms
+ String contentFragment = contentTerm.makeTerm(req);
+ if (contentFragment != null) {
+ logger.debug("contentFragment: " + contentFragment);
+ queryString = queryString + " +" + contentFragment;
+ }
+
+ String topicFragment = topicTerm.makeTerm(req);
+ if (topicFragment != null) {
+ queryString = queryString + " +" + topicFragment;
+ }
+
+ String topicMatrixFragment = topicMatrixTerm.makeTerm(req);
+ if (topicMatrixFragment != null) {
+ queryString = queryString + " +" + topicMatrixFragment;
+ }
+
+ String imagesFragment = imagesTerm.makeTerm(req);
+ if (imagesFragment != null) {
+ queryString = queryString + " +" + imagesFragment;
+ }
+
+ String audioFragment = audioTerm.makeTerm(req);
+ if (audioFragment != null) {
+ queryString = queryString + " +" + audioFragment;
+ }
+
+ String videoFragment = videoTerm.makeTerm(req);
+ if (videoFragment != null) {
+ queryString = queryString + " +" + videoFragment;
+ }
+
+ if (queryString == null || queryString == "") {
+ queryString = "";
+ }
+ else {
+ try {
+ Searcher searcher = null;
+ try {
+ searcher = new IndexSearcher(indexPath);
+ }
+ catch (IOException e) {
+ logger.debug("Can't open indexPath: " + indexPath);
+ throw new ServletModuleExc("Problem with Search Index! : " + e.toString());
+ }
+
+ Query query = null;
+ try {
+ query = QueryParser.parse(queryString, "content", new StandardAnalyzer());
+ }
+ catch (Exception e) {
+ searcher.close();
+ logger.debug("Query don't parse: " + queryString);
+ throw new ServletModuleExc("Problem with Query String! (was '" + queryString + "')");
+ }
+
+ Hits hits = null;
+ try {
+ hits = searcher.search(query);
+ }
+ catch (IOException e) {
+ searcher.close();
+ logger.debug("Can't get hits: " + e.toString());
+ throw new ServletModuleExc("Problem getting hits!");
+ }
+
+ int start = 0;
+ int end = hits.length();
+
+ String sortBy = req.getParameter("search_sort");
+ if (sortBy == null || sortBy.equals("")) {
+ throw new ServletModuleExc("Please let me sort by something!(missing search_sort)");
+ }
+
+ // here is where the documents will go for storage across sessions
+ ArrayList theDocumentsSorted = new ArrayList();
+
+ if (sortBy.equals("score")) {
+ for (int i = start; i < end; i++) {
+ theDocumentsSorted.add(hits.doc(i));
+ }
+ }
+ else {
+ // then we'll sort by date!
+ Map dateToPosition = new HashMap(end, 1.0F); //we know how big it will be
+ for (int i = start; i < end; i++) {
+ String creationDate = (hits.doc(i)).get("creationDate");
+ // do a little dance in case two contents created at the same second!
+ if (dateToPosition.containsKey(creationDate)) {
+ ( (ArrayList) (dateToPosition.get(creationDate))).add(new Integer(i));
+ }
+ else {
+ ArrayList thePositions = new ArrayList();
+ thePositions.add(new Integer(i));
+ dateToPosition.put(creationDate, thePositions);
+ }
+ }
+ Set keys = dateToPosition.keySet();
+ ArrayList keyList = new ArrayList(keys);
+ Collections.sort(keyList);
+ if (sortBy.equals("date_desc")) {
+ Collections.reverse(keyList);
+ }
+ else {
+ if (!sortBy.equals("date_asc")) {
+ throw new ServletModuleExc("don't know how to sort by: " + sortBy);
+ }
+ }
+ ListIterator keyTraverser = keyList.listIterator();
+ while (keyTraverser.hasNext()) {
+ ArrayList positions = (ArrayList) dateToPosition.get( (keyTraverser.next()));
+ ListIterator positionsTraverser = positions.listIterator();
+ while (positionsTraverser.hasNext()) {
+ theDocumentsSorted.add(hits.doc( ( (Integer) (positionsTraverser.next())).intValue()));
+ }
+ }
+ }
+
+ try {
+ searcher.close();
+ }
+ catch (IOException e) {
+ logger.debug("Can't close searcher: " + e.toString());
+ throw new ServletModuleFailure("Problem closing searcher(normal):" + e.getMessage(), e);
+ }
+
+ session.removeAttribute("numberOfHits");
+ session.removeAttribute("theDocumentsSorted");
+ session.removeAttribute("positionInResults");
+
+ session.setAttribute("numberOfHits", new Integer(end));
+ session.setAttribute("theDocumentsSorted", theDocumentsSorted);
+ session.setAttribute("positionInResults", new Integer(0));
+
+ }
+ catch (IOException e) {
+ logger.debug("Can't close searcher: " + e.toString());
+ throw new ServletModuleFailure("Problem closing searcher: " + e.getMessage(), e);
+ }
+ }
+ }
+ }
+
+ try {
+ ArrayList theDocs = (ArrayList) session.getAttribute("theDocumentsSorted");
+ if (theDocs != null) {
+
+ mergeData.put("numberOfHits", ( (Integer) session.getAttribute("numberOfHits")).toString());
+ List theHits = new Vector();
+ int pIR = ( (Integer) session.getAttribute("positionInResults")).intValue();
+ int terminus;
+ int numHits = ( (Integer) session.getAttribute("numberOfHits")).intValue();
+
+ if (! (pIR + increment >= numHits)) {
+ mergeData.put("hasNext", "y");
+ }
+ else {
+ mergeData.put("hasNext", null);
+ }
+ if (pIR > 0) {
+ mergeData.put("hasPrevious", "y");
+ }
+ else {
+ mergeData.put("hasPrevious", null);
+ }
+
+ if ( (pIR + increment) > numHits) {
+ terminus = numHits;
+ }
+ else {
+ terminus = pIR + increment;
+ }
+ for (int i = pIR; i < terminus; i++) {
+ Map h = new HashMap();
+ Document theHit = (Document) theDocs.get(i);
+ whereTerm.returnMeta(h, theHit);
+ creatorTerm.returnMeta(h, theHit);
+ titleTerm.returnMeta(h, theHit);
+ descriptionTerm.returnMeta(h, theHit);
+ dateTerm.returnMeta(h, theHit);
+ imagesTerm.returnMeta(h, theHit);
+ audioTerm.returnMeta(h, theHit);
+ videoTerm.returnMeta(h, theHit);
+ theHits.add(h);
+ }
+ mergeData.put("hits", theHits);
+ }
+ }
+ catch (Throwable e) {
+ logger.error("Can't iterate over hits: " + e.toString());
+
+ throw new ServletModuleFailure("Problem getting hits: " + e.getMessage(), e);
+ }
+
+ mergeData.put("queryString", queryString);
+
+ deliver(req, res, mergeData, null, searchResultsTemplate);
+ }
+ catch (NullPointerException n) {
+ throw new ServletModuleFailure("Null Pointer: " + n.toString(), n);
+ }