Anonymous avatar Anonymous committed 4bcbb4d

add free text search feature

Comments (0)

Files changed (6)

     # default minimum number of results required to trigger a facet being included (integer)
     "facet_mincount" : 1,
     
-    "text_search_field" : "*",
+    "allow_text_search" : true,
+    
+    "text_search_field" : "text",
     
     "always_visible" : 5,
     
             initial_request = True
         
         # fold in the free text search options if relevant
-        if web.input().get("q") is not None:
-            if web.input().get("q") != "":
-                args["q"][config.text_search_field] = [web.input().get("q")]#.split(' ')
-            else:
-                if config.text_search_field in args["q"].keys():
-                    del args["q"][config.text_search_field]
+        text_search = web.input().get("q")
+        if text_search is not None and text_search != "":
+            initial_request = False
+            args["search"] = text_search
         
         implicit_facets = {}
         if path is not None:
             properties['results'] = ResultManager(s.initial(args), config, args)
         else:
             properties['results'] = ResultManager(s.search(args), config, args)
+        
+        if args.has_key("search"):
+            properties['q'] = args['search']
+        else:
+            properties['q'] = ""
+        
         return self.render(config.template, properties)
     
     def render(self, template_name, properties):
                 del myargs['q'][field]
         return myargs
     
+    def get_search_form_action(self):
+        return self.config.base_url
+    
+    def get_form_field_args(self):
+        myargs = deepcopy(self.base_args)
+        if myargs.has_key("search"):
+            del myargs['search']
+        j = json.dumps(myargs)
+        return urllib2.quote(j)
+    
     def get_add_url(self, field, value, upper=None):
         myargs = deepcopy(self.base_args)
         if myargs["q"].has_key(field):
         #         score=True, sort=None, sort_order="asc", **params):
         
         # do q separately
+        
+        # first do the actual 'q' arg
         qs = []
         for field in args['q'].keys():
             if field in self.config.facet_fields:
             elif field == "*":
                 for value in args['q'][field]:
                     qs.append(field + ":" + value)
+        
+        # now there's an optional "search" part which should be appended to the q
+        if args.has_key("search"):
+            search = args['search']
+            # if we detect a ":" in the search, we take it to be a lucene search
+            if ":" in search:
+                qs.append(search)
+            else:
+                # break the search by space and OR everything together
+                clauses = []
+                for term in search.split(" "):
+                    clauses.append(self.config.text_search_field + ":" + term)
+                ors = " OR ".join(clauses)
+                if ors != "":
+                    qs.append("(" + ors + ")")
+        
         q = " AND ".join(qs)
         
         if q == "": 
             q = "*:*"
         
+        print q
+        
         # start position (for paging)
         if args.has_key("start"):
             solr_args["start"] = args["start"]
         
-        # FIXME: add null checks
-        
         # set up the ranged search parameters
-        for f in args["facet_range"].keys():
-            if solr_args.has_key("facet.range"):
-                solr_args["facet.range"].append(f)
-            else:
-                solr_args["facet.range"] = [f]
-            
-            if args["facet_range"][f].has_key("mincount"):
-                solr_args["f." + f + ".facet.mincount"] = args["facet_range"][f]["mincount"]
-            
-            if args["facet_range"][f].has_key("min"):
-                solr_args["f." + f + ".facet.range.start"] = args["facet_range"][f]["min"]
+        if args.has_key("facet_range"):
+            for f in args["facet_range"].keys():
+                if solr_args.has_key("facet.range"):
+                    solr_args["facet.range"].append(f)
+                else:
+                    solr_args["facet.range"] = [f]
                 
-            if args["facet_range"][f].has_key("max"):
-                solr_args["f." + f + ".facet.range.end"] = args["facet_range"][f]["max"]
+                if args["facet_range"][f].has_key("mincount"):
+                    solr_args["f." + f + ".facet.mincount"] = args["facet_range"][f]["mincount"]
                 
-            if args["facet_range"][f].has_key("gap"):
-                solr_args["f." + f + ".facet.range.gap"] = args["facet_range"][f]["gap"]
+                if args["facet_range"][f].has_key("min"):
+                    solr_args["f." + f + ".facet.range.start"] = args["facet_range"][f]["min"]
+                    
+                if args["facet_range"][f].has_key("max"):
+                    solr_args["f." + f + ".facet.range.end"] = args["facet_range"][f]["max"]
+                    
+                if args["facet_range"][f].has_key("gap"):
+                    solr_args["f." + f + ".facet.range.gap"] = args["facet_range"][f]["gap"]
         
         # set up the date range parameters
-        for f in args["facet_date"].keys():
-            if solr_args.has_key("facet.date"):
-                solr_args["facet.date"].append(f)
-            else:
-                solr_args["facet.date"] = [f]
-            
-            if args["facet_date"][f].has_key("mincount"):
-                solr_args["f." + f + ".facet.mincount"] = args["facet_date"][f]["mincount"]
-            
-            if args["facet_date"][f].has_key("min"):
-                solr_args["f." + f + ".facet.date.start"] = args["facet_date"][f]["min"]
+        if args.has_key("facet_date"):
+            for f in args["facet_date"].keys():
+                if solr_args.has_key("facet.date"):
+                    solr_args["facet.date"].append(f)
+                else:
+                    solr_args["facet.date"] = [f]
                 
-            if args["facet_date"][f].has_key("max"):
-                solr_args["f." + f + ".facet.date.end"] = args["facet_date"][f]["max"]
-            else:
-                solr_args["f." + f + ".facet.date.end"] = datetime.today().strftime("%Y-%m-%dT%H:%M:%SZ")
+                if args["facet_date"][f].has_key("mincount"):
+                    solr_args["f." + f + ".facet.mincount"] = args["facet_date"][f]["mincount"]
                 
-            if args["facet_date"][f].has_key("gap"):
-                solr_args["f." + f + ".facet.date.gap"] = args["facet_date"][f]["gap"]
+                if args["facet_date"][f].has_key("min"):
+                    solr_args["f." + f + ".facet.date.start"] = args["facet_date"][f]["min"]
+                    
+                if args["facet_date"][f].has_key("max"):
+                    solr_args["f." + f + ".facet.date.end"] = args["facet_date"][f]["max"]
+                else:
+                    solr_args["f." + f + ".facet.date.end"] = datetime.today().strftime("%Y-%m-%dT%H:%M:%SZ")
+                    
+                if args["facet_date"][f].has_key("gap"):
+                    solr_args["f." + f + ".facet.date.gap"] = args["facet_date"][f]["gap"]
         
         # set up the facet queries
         if args.has_key('facet_query'):
             solr_args["rows"] = args["rows"]
             
         # plain facet fields
-        solr_args["facet.field"] = args["facet_field"]
+        if args.has_key("facet_field"):
+            solr_args["facet.field"] = args["facet_field"]
         
         # sort options
         if args.has_key("sort"):

static/styless.css

     float: right;
 }
 
+.search_constraints {
+    clear: both;
+}
+
 .search_constraint {
     float: left;
     border: 1px solid #33ff33;
     font-weight: bold;
     text-align: center;
     font-size: 140%;
+}
+
+.search_box {
+    margin: -5px;
+}
+
+.search_box form {
+    float: left;
+    padding: 3px;
+    margin-left: 0px;
+    margin-right: 5px;
+    margin-top: 10px;
+    margin-bottom: 0px;
+}
+
+.search_box form input[type="submit"] {
+    border: 1px solid #4444aa;
+    padding: 3px;
+    background: #9999ff;
+    margin-left: -5px;
+}
+
+.search_box form input[type="text"] {
+    border: 1px solid #4444aa;
+    padding: 3px;
+    background: #eeeeff;
+    width: 300;
 }

templates/noresults.mako

 <div class="no_results">
-There are currently no results to display.  Try selecting some facets
-from the left to start getting some results.  There are ${c['results'].numFound()} 
-records in the index.
+There are no results to display.
 </div>

templates/search-box.mako

+<div class="search_box">
+    <form method="get" action="${c['url_manager'].get_search_form_action()}">
+        <input type="text" name="q" value="${c['q']}"/>
+        <input type="hidden" name="a" value="${c['url_manager'].get_form_field_args()}"/>
+        <input type="submit" name="submit_search" value="Search"/>
+    </form>
+    % if c['q'] is not None and c['q'] != "":
+    <form method="get" action="${c['url_manager'].get_search_form_action()}">
+        <input type="hidden" name="q" value=""/>
+        <input type="hidden" name="a" value="${c['url_manager'].get_form_field_args()}"/>
+        <input type="submit" name="submit_clear" value="Clear Search" />
+    </form>
+    % endif
+</div>

templates/solreyes.mako

     
     <div id="panel">
     
+        <%include file="/implicit-title.mako"/>
+        % if c['config'].allow_text_search:
+            <%include file="/search-box.mako"/>
+        % endif
+        <%include file="/search-summary.mako"/>
+            
         % if c['results'].set_size() == 0:
             <%include file="/noresults.mako"/>
         % else:
-            <%include file="/implicit-title.mako"/>
-            <%include file="/search-summary.mako"/>
             <%include file="/sort-options.mako"/>
             <%include file="/paging.mako"/>
             <%include file="/resultsperpage.mako"/>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.