Commits

Raza Ali committed 4f5f42f

Implementation of basic search

  • Participants
  • Parent commits ee62062

Comments (0)

Files changed (7)

File grails-app/conf/Config.groovy

     jummp.vcs.workingDirectory = jummpConfig.jummp.vcs.workingDirectory
 }
 // search plugin
-if (jummpConfig.jummp.plugins.search.index) {
-    jummp.plugins.search.index = jummpConfig.jummp.plugins.search.index
+if (jummpConfig.jummp.search.index) {
+    jummp.search.index = jummpConfig.jummp.search.index
 }
 
 // registration settings

File grails-app/conf/UrlMappings.groovy

         "500"(controller: "errors", action: "error500")
         "500"(controller: "errors", action: "error403", exception: org.springframework.security.access.AccessDeniedException)
         "/models"(controller: "search", action: "list")
+	"/search"(controller: "search", action: "search")
 	"/models/model"(controller:"model", action="model")
 	"/feedback"(controller:"jummp", action="feedback")
 	"/"(view:"/index")

File grails-app/services/net/biomodels/jummp/core/ModelService.groovy

 import org.springframework.security.acls.domain.PrincipalSid
 import org.springframework.security.acls.model.Acl
 import org.springframework.security.core.userdetails.UserDetails
-
+import org.apache.lucene.document.Document
 /**
  * @short Service class for managing Models
  *
     static transactional = true
 
     
+    public Set<ModelTransportCommand> searchModels(String query) {
+    	    System.out.println("SEARCHING FOR: "+query)
+    	    def searchEngine=grailsApplication.mainContext.getBean("searchEngine")
+    	    Set<Document> results=searchEngine.performSearch("name", query)
+    	    results.addAll(searchEngine.performSearch("description", query))
+    	    results.addAll(searchEngine.performSearch("content", query))
+    	    Set<ModelTransportCommand> returnVals=new HashSet<ModelTransportCommand>()
+    	    results.each {
+    	    	    try
+    	    	    {
+    	    	    	    Model returned=getModel(Integer.parseInt(it.get("model_id")))
+    	    	    	    if (returned) {
+    	    	    	    	    returnVals.add(returned.toCommandObject())
+    	    	    	    }
+    	    	    }
+    	    	    catch(Exception ignore) {
+    	    	    }
+    	    }
+    	    return returnVals
+    }
+    
     
     /**
     * Returns list of Models the user has access to.

File jummp-plugins/jummp-plugin-web-application/grails-app/controllers/net/biomodels/jummp/webapp/SearchController.groovy

     	   [query:params.search_block_form]
     }
 
+        /**
+     * Action returning the DataTable content as JSON
+     */
+    def executeSearch = {
+        System.out.println("SEARCH QUERY FOR: "+params.id)
+    	int start = 0
+        int length = 10
+        if (params.iDisplayStart) {
+            start = params.iDisplayStart as int
+        }
+        if (params.iDisplayLength) {
+            length = Math.min(100, params.iDisplayLength as int)
+        }
+        def dataToRender = [:]
+        dataToRender.sEcho = params.sEcho
+        dataToRender.aaData = []
+        dataToRender.modelIDs= []
+
+        Set models = modelService.searchModels(params.id)
+        
+        switch (params.iSortCol_0 as int) {
+        case 0:
+            models = models.sort{ m1, m2 -> m1.name.compareTo(m2.name)  }
+            break
+        case 1:
+            models = models.sort{ m1, m2 -> m1.format.name.compareTo(m2.format.name)  }
+            break
+        case 2:
+            models = models.sort{ m1, m2 -> m1.submitter.compareTo(m2.submitter)  }
+            break
+        case 3:
+            models = models.sort{ m1, m2 -> m1.submissionDate.getTime() - m2.submissionDate.getTime()  }
+            break
+        case 4:
+            models = models.sort{ m1, m2 -> m1.lastModifiedDate.getTime() - m2.lastModifiedDate.getTime()  }
+            break
+        default:
+            models = models.sort{ m1, m2 -> m1.id - m2.id  }
+            break
+        }
+        
+        dataToRender.iTotalRecords = models.size()
+        dataToRender.iTotalDisplayRecords = dataToRender.iTotalRecords
+        dataToRender.offset = start
+        dataToRender.iSortCol_0 = params.iSortCol_0
+        dataToRender.sSortDir_0 = params.sSortDir_0
+        
+        models.each { modelTC ->
+            dataToRender.modelIDs << [ modelTC.id ]
+            dataToRender.aaData << [
+                modelTC.name,
+                modelTC.format.name,
+                modelTC.submitter,
+                modelTC.submissionDate.getTime(),
+                modelTC.lastModifiedDate.getTime()
+            ]
+        }
+        render dataToRender as JSON
+    }
+
+    
+    
+    
     /**
      * Action returning the DataTable content as JSON
      */

File jummp-plugins/jummp-plugin-web-application/grails-app/views/search/search.gsp

          <g:javascript src="jquery/jquery-ui-v1.10.3.js"/>
         <g:javascript>
         	$(document).ready(function() {
-        		 $.jummp.showModels.loadModelList();
+        		 $.jummp.showModels.searchModels('${query}');
         	} );
         </g:javascript>
         <g:javascript contextPath="" src="showmodels.js"/>

File src/groovy/net/biomodels/jummp/search/SearchProvider.groovy

 import org.apache.lucene.search.SearcherManager
 import org.apache.lucene.search.TopDocs
 import org.apache.lucene.util.Version
+import org.apache.lucene.document.Document
 /**
  * @short Listener for new revisions and models for indexing
  * 
 	
 	SearcherManager mgr
 	
-	public void performSearch(String field, String query) {
+	public Set<Document> performSearch(String field, String query) {
+		Set<Document> docs=new HashSet<Document>()
 		if (!mgr) {
 			mgr=grailsApplication.mainContext.getBean("indexingEventListener").getSearcherManager()
 		}
 		System.out.println("Got search query: "+query)
 		IndexSearcher indexSearcher = mgr.acquire();
-		System.out.println("index size: "+indexSearcher.getIndexReader().numDocs())
-		System.out.println("isSearcherCurrent: "+mgr.isSearcherCurrent())
 		try {
 			QueryParser queryParser = new QueryParser(Version.LUCENE_44,field,new StandardAnalyzer(Version.LUCENE_44));
 			Query termQuery = queryParser.parse(query);
 			TopDocs topDocs = indexSearcher.search(termQuery,10);
-			System.out.println(topDocs.scoreDocs.getProperties())
-			System.out.println("GOT: ${topDocs.totalHits}")
 			for (int i=0; i<topDocs.totalHits; i++) {
-				System.out.println(indexSearcher.doc(topDocs.scoreDocs[i].doc).inspect())
+				docs.add(indexSearcher.doc(topDocs.scoreDocs[i].doc))
 			}
 			
 		} finally {
 			mgr.release(indexSearcher);
 			indexSearcher = null;
 		}
+		return docs
 	}
 	
 	public void refreshIndex() {

File web-app/js/showmodels.js

     });
 };
 
+$.jummp.showModels.searchModels = function (query) {
+    "use strict";
+    $('#modelTable').dataTable({
+        bFilter: false,
+        bProcessing: true,
+        bServerSide: true,
+        bSort: true,
+        bJQueryUI: true,
+        bAutoWidth: false,
+        sSource: 'search',
+        sPaginationType: "full_numbers",
+        iDisplayLength: 10,
+        bLengthChange: false,
+        bScrollInfinite: false,
+        bScrollCollapse: true,
+        bDeferRender: true,
+        "fnServerData": function (sSource, aoData, fnCallback) {
+            $.ajax({
+                "dataType": 'json',
+                "type": "POST",
+                "url": $.jummp.createLink("search", "executeSearch", query),
+                "data": aoData,
+                "error": function () {
+                    // clear the table
+                    fnCallback({aaData: [], iTotalRecords: 0, iTotalDisplayRecords: 0});
+                },
+                "success": function (json) {
+                    var i, rowData, id;
+                    for (i = 0; i < json.aaData.length; i += 1) {
+                    	rowData = json.aaData[i];
+                        id = json.modelIDs[i]
+                        rowData[0] = "<a href=\"" + $.jummp.createLink("model", "show", id) + "\">" + (rowData[0] ? rowData[0].replace(/_/g, " ") : "-") + "</a>";
+                        var date=new Date(rowData[3]);
+                        rowData[3]=date.toUTCString();
+                        date=new Date(rowData[4]);
+                        rowData[4]=date.toUTCString();
+                    }
+                    fnCallback(json);
+                }
+            });
+        }
+    });
+};
+
+
 $.jummp.showModels.loadModelList = function () {
     "use strict";
     $('#modelTable').dataTable({
                         id = json.modelIDs[i]
                         rowData[0] = "<a href=\"" + $.jummp.createLink("model", "show", id) + "\">" + (rowData[0] ? rowData[0].replace(/_/g, " ") : "-") + "</a>";
                         var date=new Date(rowData[3]);
-                        rowData[3]=date.toDateString();
+                        rowData[3]=date.toUTCString();
                         date=new Date(rowData[4]);
-                        rowData[4]=date.toDateString();
+                        rowData[4]=date.toUTCString();
                     }
                     fnCallback(json);
                 }
             });
         }
-        
-        
     });
 };