MroongaFullTextKey.py 4.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2014-2006 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.
#
##############################################################################

29
from DefaultKey import DefaultKey
30 31 32
from FullTextKey import FullTextKey
from Products.ZSQLCatalog.Query.SimpleQuery import SimpleQuery
from Products.ZSQLCatalog.interfaces.search_key import ISearchKey
33
from SearchKey import SearchKey
34 35 36 37 38 39 40 41
from zope.interface.verify import verifyClass
import re
 
class MroongaFullTextKey(FullTextKey):
  """
    This SearchKey generates SQL fulltext comparisons for Mroonga.
  """
  default_comparison_operator = 'match'
42
  fulltext_boolean_splitter = re.compile(r'(\s|\(.+?\)|".+?")')
43
  fulltext_boolean_detector = re.compile(r'(^[+-]|^.+\*$|^["(].+[")]$)')
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

  def _processSearchValue(self, search_value, logical_operator,
                          comparison_operator):
    """
      Special SearchValue processor for MroongaFullText queries:
      if a searched token from 'match' operator group contains an
      operator recognised in boolean mode, make the operator for
      that value be 'match_boolean'.
    """
    operator_value_dict, logical_operator, parsed = \
      SearchKey._processSearchValue(self, search_value, logical_operator,
                                    comparison_operator)
    new_value_list = []
    append = new_value_list.append
    for value in operator_value_dict.pop('match', []):
      if isinstance(value, basestring):
        for token in self.fulltext_boolean_splitter.split(value):
          token = token.strip()
          if not token:
            continue
          elif self.fulltext_boolean_detector.match(token):
            operator_value_dict.setdefault('match_boolean', []).append(token)
          else:
            append(token)
      else:
        append(value)
    operator_value_dict['match'] = new_value_list
    return operator_value_dict, logical_operator, parsed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

  def _buildQuery(self, operator_value_dict, logical_operator, parsed, group):
    """
      Special Query builder for MroongaFullText queries:
      * by default 'AND' search by using '*D+' pragma.
      * similarity search for non-boolean queries by using '*S"..."' operator.
    """
    column = self.getColumn()
    query_list = []
    append = query_list.append
    match_query = operator_value_dict.pop('match', [])
    match_boolean_query = operator_value_dict.pop('match_boolean', [])
    fulltext_query = '*D+'
    if match_query:
      fulltext_query += ' *S"%s"' % ' '.join(x.replace('"', '\\"') for x in match_query)
    if match_boolean_query:
      fulltext_query += ' %s' % ' '.join(match_boolean_query)
89 90 91 92
    if match_query or match_boolean_query:
      append(SimpleQuery(search_key=self,
                         comparison_operator='match_boolean',
                         group=group, **{column: fulltext_query}))
93 94 95 96
    # other comparison operators are handled by DefaultKey.
    if operator_value_dict:
      query_list += DefaultKey._buildQuery(
        self, operator_value_dict, logical_operator, parsed, group)
97 98 99
    return query_list

verifyClass(ISearchKey, MroongaFullTextKey)