#!/usr/bin/env python # coding: utf-8 # # Copyright 2007 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Full text indexing and search, implemented in pure python. Defines a SearchableModel subclass of db.Model that supports full text indexing and search, based on the datastore's existing indexes. Don't expect too much. First, there's no ranking, which is a killer drawback. There's also no exact phrase match, substring match, boolean operators, stemming, or other common full text search features. Finally, support for stop words (common words that are not indexed) is currently limited to English. To be indexed, entities must be created and saved as SearchableModel instances, e.g.: class Article(search.SearchableModel): text = db.TextProperty() ... article = Article(text=...) article.put() To search the full text index, use the SearchableModel.all() method to get an instance of SearchableModel.Query, which subclasses db.Query. Use its search() method to provide a search query, in addition to any other filters or sort orders, e.g.: query = article.all().search('a search query').filter(...).order(...) for result in query: ... The full text index is stored in a property named __searchable_text_index. If 300-8027402 you want to use search() in a query with an ancestor, filters, or sort orders, you'll need to create an index in index.yaml with the __searchable_text_index property. For example: - kind: Usuario properties: - name: __searchable_text_index - name: usuario_de_pais - name: nacimiento_f ... Note that using SearchableModel will noticeable increase the latency of save() operations, since it writes an index row for each indexable word. This also means that the latency of save() will increase roughly with the size of the properties in a given entity. Caveat hacker! """ import re import string import sys import logging from google.appengine.api import datastore from google.appengine.api import datastore_errors from google.appengine.api import datastore_types from google.appengine.ext import db from google.appengine.datastore import datastore_pb from google.appengine.ext.search import SearchableModel as SearchableModel2 class SearchableEntity(datastore.Entity): """A subclass of datastore.Entity that supports full text indexing. Automatically indexes all string and Text properties, using the datastore's built-in per-property indices. To search, use the SearchableQuery class and its Search() method. """ _FULL_TEXT_INDEX_PROPERTY = '__searchable_text_index' _FULL_TEXT_MIN_LENGTH = 2 _FULL_TEXT_MIN_LENGTH_IN = 4 _FULL_TEXT_STOP_WORDS = frozenset([]) _PUNCTUATION_REGEX = re.compile('[' + re.escape(string.punctuation) + ']') def __init__(self, kind_or_entity, *args, **kwargs): """Constructor. May be called as a copy constructor. If kind_or_entity is a datastore.Entity, copies it into this Entity. datastore.Get() and Query() returns instances of datastore.Entity, so this is useful for converting them back to SearchableEntity so that they'll be indexed when they're stored back in the datastore. Otherwise, passes through the positional and keyword args to the datastore.Entity constructor. Args: kind_or_entity: string or datastore.Entity """ if isinstance(kind_or_entity, datastore.Entity): self._Entity__key = kind_or_entity._Entity__key self.update(kind_or_entity) else: super(SearchableEntity, self).__init__(kind_or_entity, *args, **kwargs) @staticmethod def statico_ToPb(self, FULL_TEXT_MIN_LENGTH_IN=4): """ """ if SearchableEntity._FULL_TEXT_INDEX_PROPERTY in self: del self[SearchableEntity._FULL_TEXT_INDEX_PROPERTY] index = set() for (name, values) in self.items(): if name == 'nombre_con_espacios_t': if not isinstance(values, list): values = [values] if (isinstance(values[0], basestring) and not isinstance(values[0], datastore_types.Blob)): for value in values: index.update(SearchableEntity._FullTextIndex2(value, FULL_TEXT_MIN_LENGTH_IN)) index_list = list(index) if index_list: self[SearchableEntity._FULL_TEXT_INDEX_PROPERTY] = index_list def _ToPb(self): """Rebuilds the full text index, then delegates to the superclass. Returns: entity_pb.Entity """ if SearchableEntity._FULL_TEXT_INDEX_PROPERTY in self: del self[SearchableEntity._FULL_TEXT_INDEX_PROPERTY] index = set() for (name, values) in self.items(): if name == 'nombre_con_espacios_t': if not isinstance(values, list): values = [values] if (isinstance(values[0], basestring) and not isinstance(values[0], datastore_types.Blob)): for value in values: index.update(SearchableEntity._FullTextIndex2(value)) index_list = list(index) if index_list: self[SearchableEntity._FULL_TEXT_INDEX_PROPERTY] = index_list #logging.info(self) #logging.info("EDGAR"*30) #logging.info(index_list) return super(SearchableEntity, self)._ToPb() @classmethod def _FullTextIndex2(cls, text, FULL_TEXT_MIN_LENGTH_IN=None): """ It is working """ if text: done = False if FULL_TEXT_MIN_LENGTH_IN == None: tamano_minimo = cls._FULL_TEXT_MIN_LENGTH_IN else: tamano_minimo = FULL_TEXT_MIN_LENGTH_IN while not done: datastore_types.ValidateString(text, 'text', max_len=sys.maxint) text = cls._PUNCTUATION_REGEX.sub(' ', text) words = text.lower().split() words = set(words) words -= cls._FULL_TEXT_STOP_WORDS temp_words = list(words) for word in temp_words: if len(word) > tamano_minimo: for i in range(tamano_minimo, len(word)): pal = word[0:i] words.add(pal) for word in list(words): if len(word) < tamano_minimo: words.remove(word) if len(words) == 0: if tamano_minimo == 1: done = True else: tamano_minimo -= 1 else: done = True else: words = set() return words @classmethod def _FullTextIndex(cls, text): """Returns a set of keywords appropriate for full text indexing. See SearchableQuery.Search() for details. Args: text: string Returns: set of strings """ if text: datastore_types.ValidateString(text, 'text', max_len=sys.maxint) text = cls._PUNCTUATION_REGEX.sub(' ', text) words = text.lower().split() words = set(words) words -= cls._FULL_TEXT_STOP_WORDS for word in list(words): if len(word) < cls._FULL_TEXT_MIN_LENGTH: words.remove(word) else: words = set() return words class SearchableQuery(datastore.Query): """A subclass of datastore.Query that supports full text search. Only searches over entities that were created and stored using the SearchableEntity or SearchableModel classes. """ def Search(self, search_query): """Add a search query. This may be combined with filters. Note that keywords in the search query will be silently dropped if they are stop words or too short, ie if they wouldn't be indexed. Args: search_query: string Returns: # this query SearchableQuery """ datastore_types.ValidateString(search_query, 'search query') self._search_query = search_query return self def _ToPb(self, limit=None, offset=None): """Adds filters for the search query, then delegates to the superclass. Raises BadFilterError if a filter on the index property already exists. Args: # an upper bound on the number of results returned by the query. limit: int # number of results that match the query to skip. limit is applied # after the offset is fulfilled. offset: int Returns: datastore_pb.Query """ if SearchableEntity._FULL_TEXT_INDEX_PROPERTY in self: raise datastore_errors.BadFilterError( '%s is a reserved name.' % SearchableEntity._FULL_TEXT_INDEX_PROPERTY) pb = super(SearchableQuery, self)._ToPb(limit=limit, offset=offset) if hasattr(self, '_search_query'): keywords = SearchableEntity._FullTextIndex2(self._search_query) if len(keywords) == 0: raise Exception("I'm sorry, but I did not find another way to avoid query return all in case of no keywords") for keyword in keywords: filter = pb.add_filter() filter.set_op(datastore_pb.Query_Filter.EQUAL) prop = filter.add_property() prop.set_name(SearchableEntity._FULL_TEXT_INDEX_PROPERTY) prop.mutable_value().set_stringvalue(keyword) return pb class SearchableModel(db.Model): """A subclass of db.Model that supports full text search and indexing. Automatically indexes all string-based properties. To search, use the all() method to get a SearchableModel.Query, then use its search() method. """ class Query(SearchableModel2.Query): """A subclass of db.Query that supports full text search.""" def _populate_internal_entity(self, update_searchable=False, FULL_TEXT_MIN_LENGTH_IN=4): """Wraps db.Model._populate_internal_entity() and injects SearchableEntity.""" self._entity = self._populate_entity(_entity_class=SearchableEntity) if update_searchable == True: SearchableEntity.statico_ToPb(self._entity, FULL_TEXT_MIN_LENGTH_IN=FULL_TEXT_MIN_LENGTH_IN) if hasattr(self, '_key_name'): del self._key_name return self._entity def put(self, update_searchable=False, FULL_TEXT_MIN_LENGTH_IN=4): self._populate_internal_entity(update_searchable=update_searchable, FULL_TEXT_MIN_LENGTH_IN=FULL_TEXT_MIN_LENGTH_IN) return datastore.Put(self._entity) @classmethod def all(cls): """Returns a SearchableModel.Query for this kind.""" return SearchableModel.Query(cls)