From c8fdff40737ff4b7303147a4463c99c40517933e Mon Sep 17 00:00:00 2001
From: Kazuhiko Shiozaki <kazuhiko@nexedi.com>
Date: Wed, 25 Aug 2010 08:10:31 +0000
Subject: [PATCH] * implicit OR search for sphinx. * somehow working "phrase
 search". * use extended2 mode instead of extended mode.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@38008 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ZSQLCatalog/Operator/ComparisonOperator.py   | 10 ++--------
 product/ZSQLCatalog/SearchKey/SphinxSEFullTextKey.py | 10 +++++++++-
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/product/ZSQLCatalog/Operator/ComparisonOperator.py b/product/ZSQLCatalog/Operator/ComparisonOperator.py
index 0658ce3440..688173160f 100644
--- a/product/ZSQLCatalog/Operator/ComparisonOperator.py
+++ b/product/ZSQLCatalog/Operator/ComparisonOperator.py
@@ -138,19 +138,13 @@ class SphinxSEComparisonOperator(MonovaluedComparisonOperator):
 
   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
+    * add ';mode=extended2' to invoke extended search
     """
     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'
+    value_list = '%s;mode=extended2' % value_list
     return self._renderValue(value_list)
 
   @profiler_decorator
diff --git a/product/ZSQLCatalog/SearchKey/SphinxSEFullTextKey.py b/product/ZSQLCatalog/SearchKey/SphinxSEFullTextKey.py
index b4a097d0b4..ff493c0e64 100644
--- a/product/ZSQLCatalog/SearchKey/SphinxSEFullTextKey.py
+++ b/product/ZSQLCatalog/SearchKey/SphinxSEFullTextKey.py
@@ -48,14 +48,22 @@ class SphinxSEFullTextKey(SearchKey):
       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.
+
+      Example:
+      'aaa bbb' : '"aaa" | "bbb"'
+      '"aaa bbb"' : '"aaa" | "bbb"' XXX no way to differentiate with the
+                                        above for now
+      '"aaa bbb" ccc' : '"aaa bbb" | "ccc"'
     """
     column = self.getColumn()
     query_list = []
     append = query_list.append
     for comparison_operator, value_list in operator_value_dict.iteritems():
+      if len(value_list) == 1:
+        value_list = value_list[0].split()
       append(SimpleQuery(search_key=self,
                          comparison_operator=comparison_operator,
-                         group=group, **{column: ' '.join(value_list)}))
+                         group=group, **{column:'"%s"' % '" | "'.join(value_list)}))
     return query_list
 
 verifyClass(ISearchKey, SphinxSEFullTextKey)
-- 
2.30.9