[Erp5-report] r37943 kazuhiko - in /erp5/trunk/products/ZSQLCatalog: Operator/ SearchKey/

nobody at svn.erp5.org nobody at svn.erp5.org
Mon Aug 23 10:58:15 CEST 2010


Author: kazuhiko
Date: Mon Aug 23 10:58:09 2010
New Revision: 37943

URL: http://svn.erp5.org?rev=37943&view=rev
Log:
support Sphinx Search Engine.

Added:
    erp5/trunk/products/ZSQLCatalog/SearchKey/SphinxSEFullTextKey.py
Modified:
    erp5/trunk/products/ZSQLCatalog/Operator/ComparisonOperator.py

Modified: erp5/trunk/products/ZSQLCatalog/Operator/ComparisonOperator.py
URL: http://svn.erp5.org/erp5/trunk/products/ZSQLCatalog/Operator/ComparisonOperator.py?rev=37943&r1=37942&r2=37943&view=diff
==============================================================================
--- erp5/trunk/products/ZSQLCatalog/Operator/ComparisonOperator.py [utf8] (original)
+++ erp5/trunk/products/ZSQLCatalog/Operator/ComparisonOperator.py [utf8] Mon Aug 23 10:58:09 2010
@@ -131,6 +131,46 @@ class MatchComparisonOperator(Monovalued
 
 verifyClass(IOperator, MatchComparisonOperator)
 
+class SphinxSEComparisonOperator(MonovaluedComparisonOperator):
+  def __init__(self, operator, mode=''):
+    MonovaluedComparisonOperator.__init__(self, operator, '')
+    self.where_expression_format_string = '%(column)s=%(value_list)s'
+
+  def renderValue(self, value_list):
+    """
+    * quote each query word explicitly to invoke phrase search for
+      n-gram characters.
+    * add ';mode=extended' to invoke extended search
+
+    TODO:
+    * escape double quote in query word
+    * respect existing double quotes in user's input
+    """
+    if isinstance(value_list, (tuple, list)):
+      if len(value_list) > 1:
+        raise ValueError, '%r: value_list must not contain more than one item. Got %r' % (self, value_list)
+      value_list = value_list[0]
+    value_list = '"'+'" "'.join(value_list.split())+'";mode=extended'
+    return self._renderValue(value_list)
+
+  @profiler_decorator
+  def asSQLExpression(self, column, value_list, only_group_columns):
+    """
+      This operator can emit a select expression, so it overrides
+      asSQLExpression inseatd of just defining a render method.
+    """
+    match_string = self.where_expression_format_string % {
+      'column': column,
+      'value_list': self.renderValue(value_list)
+    }
+    return SQLExpression(
+      self,
+      where_expression=match_string,
+      can_merge_select_dict=True,
+    )
+
+verifyClass(IOperator, SphinxSEComparisonOperator)
+
 operator_dict = {
   '=': MonovaluedComparisonOperator('='),
   '!=': MonovaluedComparisonOperator('!='),
@@ -143,6 +183,7 @@ operator_dict = {
   'match': MatchComparisonOperator('match'),
   'match_boolean': MatchComparisonOperator('match_boolean', mode=' IN BOOLEAN MODE'),
   'match_expansion': MatchComparisonOperator('match_expansion', mode=' WITH QUERY EXPANSION'),
+  'sphinxse': SphinxSEComparisonOperator('sphinxse'),
   'in': MultivaluedComparisonOperator('in'),
   'is': MonovaluedComparisonOperator('is'),
 }

Added: erp5/trunk/products/ZSQLCatalog/SearchKey/SphinxSEFullTextKey.py
URL: http://svn.erp5.org/erp5/trunk/products/ZSQLCatalog/SearchKey/SphinxSEFullTextKey.py?rev=37943&view=auto
==============================================================================
--- erp5/trunk/products/ZSQLCatalog/SearchKey/SphinxSEFullTextKey.py (added)
+++ erp5/trunk/products/ZSQLCatalog/SearchKey/SphinxSEFullTextKey.py [utf8] Mon Aug 23 10:58:09 2010
@@ -0,0 +1,61 @@
+##############################################################################
+#
+# Copyright (c) 2010 Nexedi SA and Contributors. All Rights Reserved.
+#
+# WARNING: This program as such is intended to be used by professional
+# programmers who take the whole responsibility of assessing all potential
+# consequences resulting from its eventual inadequacies and bugs
+# End users who are looking for a ready-to-use solution with commercial
+# guarantees and support are strongly adviced to contract a Free Software
+# Service Company
+#
+# This program is Free Software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+##############################################################################
+
+from SearchKey import SearchKey
+from Products.ZSQLCatalog.Query.SimpleQuery import SimpleQuery
+from Products.ZSQLCatalog.SearchText import parse
+from Products.ZSQLCatalog.interfaces.search_key import ISearchKey
+from zope.interface.verify import verifyClass
+from Products.ZSQLCatalog.SQLCatalog import profiler_decorator
+
+class SphinxSEFullTextKey(SearchKey):
+  """
+    This SearchKey generates SQL fulltext comparisons.
+  """
+  default_comparison_operator = 'sphinxse'
+  get_operator_from_value = False
+
+  def parseSearchText(self, value, is_column):
+    return parse(value, is_column)
+
+  @profiler_decorator
+  def _buildQuery(self, operator_value_dict, logical_operator, parsed, group):
+    """
+      Special Query builder for FullText queries: merge all values having the
+      same operator into just one query, to save SQL server from the burden to
+      do multiple fulltext lookups when one would suit the purpose.
+    """
+    column = self.getColumn()
+    query_list = []
+    append = query_list.append
+    for comparison_operator, value_list in operator_value_dict.iteritems():
+      append(SimpleQuery(search_key=self,
+                         comparison_operator=comparison_operator,
+                         group=group, **{column: ' '.join(value_list)}))
+    return query_list
+
+verifyClass(ISearchKey, SphinxSEFullTextKey)




More information about the Erp5-report mailing list