Commits

Anonymous committed 1eff182 Draft

Added methods to create Sunburnt query chains

Comments (0)

Files changed (6)

bloodhound_solr/bhsolr/admin.py

+r"""Administration commands for Bloodhound Solr Search."""
+from trac.core import Component, implements
+from trac.admin import IAdminCommandProvider
+from bhsolr.api import BloodhoundSolrApi
+
+class BloodhoundSolrSearchAdmin(Component):
+  """Bloodhound Solr Search administration component."""
+  implements(IAdminCommandProvider)
+
+  # IAdminCommandProvider methods
+  def get_admin_commands(self):
+    yield ('bhsolr populate_index', '', 'Populate Solr search index',
+      None, BloodhoundSolrApi(self.env).populate_index)
+    yield ('bhsolr optimize', '', 'Optimize Solr search index',
+      None, BloodhoundSearchApi(self.env).optimize)
+

bloodhound_solr/bhsolr/api.py

+from bhsolr.search_resources import TicketSearchModel
+from trac.core import Component, implements
+
+class BloodhoundSolrApi(Component):
+  def populate_index(self):
+    tickets = TicketSearchModel(self.env).get_entries_for_index()
+    print tickets

bloodhound_solr/bhsolr/solr_backend.py

 from bhsearch.search_resources.ticket_search import TicketIndexer
 from datetime import datetime
 from trac.util.datefmt import utc
+from bhsearch.api import ISearchBackend
+from bhsearch.query_parser import DefaultQueryParser
 
 UNIQUE_ID = "unique_id"
 
-class SolrModel(Component):
+class SolrBackend(Component):
   implements(ISearchBackend)
 
   def __init__(self):
     resource_filename = pkg_resources.resource_filename
     path = resource_filename(__name__, "schemadoc")
-    # self.solr_interface = Solr("http://localhost:8983/solr/", path + '/schema.xml').solr_interface
-    self.solr_interface = Solr("http://localhost:8983/solr/").solr_interface
+    self.solr_interface = Solr("http://localhost:8983/solr/").solr_interface # TODO: Refactor
+    self.field_boosts = DefaultQueryParser(self.env).field_boosts
 
 
   def add_doc(self, doc, operation_context=None):
     self.solr_interface.delete(unique_id)
 
 
-  def optimize():
+  def optimize(self):
     self.solr_interface.optimize()
 
+  def query(self, query, query_string, sort = None, fields = None, filter = None,
+            facets = None, pagenum = 1, pagelen = 20, highlight = False,
+            highlight_fields = None, context = None):
 
-  def query(self, query):
-    self.solr_instance.solr_interface.query(query).execute()
+    tokens = set([token.text for token in query.all_tokens()])
+    final_query_chain = None
+    for token in tokens:
+      token_query_chain = self._search_fields_for_token(token)
+      if final_query_chain == None:
+        final_query_chain = token_query_chain
+      else:
+        final_query_chain |= token_query_chain
 
+    print final_query_chain
+    return None
+
+  def is_index_outdated(self):
+    return False
+
+  def recreate_index(self):
+    return True
+
+  def start_operation(self):
+    return None
+
+  def _search_fields_for_token(self, token):
+    query_chain = None
+    for field, boost in self.field_boosts.iteritems():
+      field_token_dict = {field: token}
+      if query_chain == None:
+        query_chain = self.solr_interface.Q(**field_token_dict)**boost
+      else:
+        query_chain |= self.solr_interface.Q(**field_token_dict)**boost
+
+    return query_chain
 
   def _reformat_doc(self, doc):
     for key, value in doc.items():
     else:
       return u"%s:%s" % (doc_type, doc_id)
 
-if __name__ == '__main__':
-  env = trac.env.Environment("/Users/antonia/Documents/Code/bloodhound/installer/bloodhound/environments/main")
-  db_connection = env.get_db_cnx()
-  cursor = db_connection.cursor()
-  a = cursor.execute("select * from ticket")
-  ticket = a.fetchall()[0]
+  def getInstance(self):
+    return self.solr_interface
 
-  # for result in si.query(name="Ticket").execute():
-    # print result
+

bloodhound_solr/bhsolr/templates/bh_solr_test.html

-<!DOCTYPE html
-    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
-    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml"
-      xmlns:xi="http://www.w3.org/2001/XInclude"
-      xmlns:py="http://genshi.edgewall.org/"
-      xmlns:i18n="http://genshi.edgewall.org/i18n">
-      <link rel="stylesheet" href="${href.chrome('dashboard/css/bootstrap.css')}" type="text/css" />
-      <link rel="stylesheet" href="${href.chrome('dashboard/css/bootstrap-responsive.css')}" type="text/css" />
-
-  <head>
-    <title>Test</title>
-  </head>
-</html>

bloodhound_solr/bhsolr/web_ui.py

-import re
-import os
-import pkg_resources
-
-from trac.web.main import IRequestHandler
-from trac.core import Component, TracError, implements
-from trac.ticket.model import Ticket
-from trac.web.chrome import ITemplateProvider
-from bhsolr.search_resources.ticket_search import TicketSearchModel
-from bhsolr.search_resources.milestone_search import MilestoneSearchModel
-from bhsolr.search_resources.changeset_search import ChangesetSearchModel
-from bhsolr.search_resources.wiki_search import WikiSearchModel
-from bhsolr.solr_backend import SolrModel
-
-class BloodhoundSolrSearchModule(Component):
-    implements(IRequestHandler, ITemplateProvider)
-
-    def match_request(self, req):
-      if re.match(r'/solr$', req.path_info):
-        return True
-
-    def process_request(self, req):
-      # changeset_doc = next(ChangesetSearchModel(self.env).get_entries_for_index())
-      milestone_doc = next(MilestoneSearchModel(self.env).get_entries_for_index())
-      ticket_doc = next(TicketSearchModel(self.env).get_entries_for_index())
-      wiki_doc = next(WikiSearchModel(self.env).get_entries_for_index())
-
-      SolrModel(self.env).addDoc(ticket_doc)
-      SolrModel(self.env).addDoc(milestone_doc)
-      SolrModel(self.env).addDoc(wiki_doc)
-
-      data = {}
-      return 'bh_solr_test.html', data, None
-
-    def get_templates_dirs(self):
-        resource_filename = pkg_resources.resource_filename
-        return [resource_filename('bhsolr', 'templates')]
-
-    def get_htdocs_dirs(self):
-        resource_filename = pkg_resources.resource_filename
-        return [('solr', resource_filename('bhsolr', 'htdocs'))]
-

bloodhound_solr/setup.py

 from setuptools import setup, find_packages
 
-PKG_INFO = {'bhsolr': ['htdocs/*.*', 'templates/*', 'schemadoc/*.xml'],
+PKG_INFO = {'bhsolr': ['schemadoc/*.xml'],
             'bhsolr.search_resources' : [],
             }
 
 
 ENTRY_POINTS = {
           'trac.plugins': [
-          'bhsolr.web_ui = bhsolr.web_ui',
           'bhsolr.api = bhsolr.api',
           'bhsolr.solr = bhsolr.solr',
+          'bhsolr.admin = bhsolr.admin',
           'bhsolr.solr_backend = bhsolr.solr_backend',
           'bhsolr.search_resources.ticket_search = bhsolr.search_resources.ticket_search',
           'bhsolr.search_resources.milestone_search = bhsolr.search_resources.milestone_search',