search is simple, functional, soon to be broken again
[mir.git] / source / mircoders / servlet / ServletModuleOpenIndy.java
index 6c7895c..d7a982a 100755 (executable)
@@ -74,6 +74,7 @@ import mircoders.producer.*;
 import mircoders.media.MediaRequest;
 import mircoders.global.*;
 import mircoders.localizer.*;
+import mircoders.search.*;
 
 /*
  *  ServletModuleOpenIndy -
@@ -82,7 +83,7 @@ import mircoders.localizer.*;
  *    open-postings to the newswire
  *
  * @author mir-coders group
- * @version $Id: ServletModuleOpenIndy.java,v 1.43 2002/11/21 23:51:07 john Exp $
+ * @version $Id: ServletModuleOpenIndy.java,v 1.44 2002/11/27 15:28:52 john Exp $
  *
  */
 
@@ -421,70 +422,164 @@ public class ServletModuleOpenIndy extends ServletModule
    */
   public void search(HttpServletRequest req, HttpServletResponse res)
     throws ServletModuleException, ServletModuleUserException {
-    String QUERYSTRING_REQUEST_PARAM = "query";
-    String queryString = req.getParameter(QUERYSTRING_REQUEST_PARAM);
+    
+    String queryString="";
+    
     SimpleHash mergeData = new SimpleHash();
-    //String indexPath=MirConfig.getProp("IndexPath");
-    String indexPath="/tmp/index";
+    
+    //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{
-      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!");
+      mergeData.put("topics", themenModule.getTopicsAsSimpleList());
     }
+    catch(ModuleException e) {
+      theLog.printDebugInfo("Can't get topics: " + e.toString());
+    }
+
+    //String indexPath=MirConfig.getProp("IndexPath");
+    String indexPath="/tmp/index";
+    
+    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();
       
-      // parse the query String.
-      Query query = null;
-      try {
-       query = QueryParser.parse(queryString, "content_and_description", new StandardAnalyzer());
-      } 
-      catch(Exception e) {
-       searcher.close();
-       theLog.printDebugInfo("Query don't parse: " + queryString);
-       throw new ServletModuleUserException("Problem with Query String!");
-      }
-      
-      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!");
-      }
+    AudioSearchTerm audioTerm = new AudioSearchTerm();
       
-      // iterate over the results
-      // the results are an array of document
-      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++) {
+    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();
-           h.put("id",hits.doc(i).get("id"));
-           h.put("where",hits.doc(i).get("where"));
-            h.put("creator",hits.doc(i).get("creator"));
-           h.put("title",hits.doc(i).get("title"));
-           h.put("webdb_create_formatted",hits.doc(i).get("webdb_create_formatted"));
+           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!");
        }
-       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!");
+      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);
   }