Back to index

plone3  3.1.7
Classes | Functions
plone.app.vocabularies.catalog Namespace Reference

Classes

class  SearchableTextSource
class  SearchableTextSourceBinder
class  QuerySearchableTextSourceView

Functions

def parse_query

Function Documentation

def plone.app.vocabularies.catalog.parse_query (   query,
  path_prefix = "" 
)
Parse the query string and turn it into a dictionary for querying the
    catalog.

    We want to find anything which starts with the given string, so we add
    a * at the end of words.

    >>> parse_query('foo')
    {'SearchableText': 'foo*'}

    If we have more than one word, each of them should have the * and
    they should be combined with the AND operator.

    >>> parse_query('foo bar')
    {'SearchableText': 'foo* AND bar*'}

    We also filter out some special characters. They are handled like
    spaces and seperate words from each other.

    >>> parse_query('foo +bar some-thing')
    {'SearchableText': 'foo* AND bar* AND some* AND thing*'}

    >>> parse_query('what? (spam) *ham')
    {'SearchableText': 'what* AND spam* AND ham*'}

    You can also limit searches to paths, if you only supply the path,
    then all contents of that folder will be searched. If you supply
    additional search words, then all subfolders are searched as well.

    >>> parse_query('path:/dummy')
    {'path': {'query': '/dummy', 'depth': 1}}

    >>> parse_query('bar path:/dummy')
    {'path': {'query': '/dummy'}, 'SearchableText': 'bar*'}

    >>> parse_query('path:/dummy foo')
    {'path': {'query': '/dummy'}, 'SearchableText': 'foo*'}

    If you supply more then one path, then only the last one is used.

    >>> parse_query('path:/dummy path:/spam')
    {'path': {'query': '/spam', 'depth': 1}}

    You can also provide a prefix for the path. This is useful for virtual
    hosting.

    >>> parse_query('path:/dummy', path_prefix='/portal')
    {'path': {'query': '/portal/dummy', 'depth': 1}}

Definition at line 14 of file catalog.py.

00014 
00015 def parse_query(query, path_prefix=""):
00016     """ Parse the query string and turn it into a dictionary for querying the
00017         catalog.
00018 
00019         We want to find anything which starts with the given string, so we add
00020         a * at the end of words.
00021 
00022         >>> parse_query('foo')
00023         {'SearchableText': 'foo*'}
00024 
00025         If we have more than one word, each of them should have the * and
00026         they should be combined with the AND operator.
00027 
00028         >>> parse_query('foo bar')
00029         {'SearchableText': 'foo* AND bar*'}
00030 
00031         We also filter out some special characters. They are handled like
00032         spaces and seperate words from each other.
00033 
00034         >>> parse_query('foo +bar some-thing')
00035         {'SearchableText': 'foo* AND bar* AND some* AND thing*'}
00036 
00037         >>> parse_query('what? (spam) *ham')
00038         {'SearchableText': 'what* AND spam* AND ham*'}
00039 
00040         You can also limit searches to paths, if you only supply the path,
00041         then all contents of that folder will be searched. If you supply
00042         additional search words, then all subfolders are searched as well.
00043 
00044         >>> parse_query('path:/dummy')
00045         {'path': {'query': '/dummy', 'depth': 1}}
00046 
00047         >>> parse_query('bar path:/dummy')
00048         {'path': {'query': '/dummy'}, 'SearchableText': 'bar*'}
00049 
00050         >>> parse_query('path:/dummy foo')
00051         {'path': {'query': '/dummy'}, 'SearchableText': 'foo*'}
00052 
00053         If you supply more then one path, then only the last one is used.
00054 
00055         >>> parse_query('path:/dummy path:/spam')
00056         {'path': {'query': '/spam', 'depth': 1}}
00057 
00058         You can also provide a prefix for the path. This is useful for virtual
00059         hosting.
00060 
00061         >>> parse_query('path:/dummy', path_prefix='/portal')
00062         {'path': {'query': '/portal/dummy', 'depth': 1}}
00063 
00064     """
00065     query_parts = query.split()
00066     query = {'SearchableText': []}
00067     for part in query_parts:
00068         if part.startswith('path:'):
00069             path = part[5:]
00070             query['path'] = {'query': path}
00071         else:
00072             query['SearchableText'].append(part)
00073     text = " ".join(query['SearchableText'])
00074     for char in '?-+*()':
00075         text = text.replace(char, ' ')
00076     query['SearchableText'] = " AND ".join(x+"*" for x in text.split())
00077     if query.has_key('path'):
00078         if query['SearchableText'] == '':
00079             del query['SearchableText']
00080             query["path"]["depth"] = 1
00081         query["path"]["query"] = path_prefix + query["path"]["query"]
00082     return query
00083 

Here is the caller graph for this function: