diff --git a/product_search_python/docs.py b/product_search_python/docs.py index 7e41a21..ad13e6d 100644 --- a/product_search_python/docs.py +++ b/product_search_python/docs.py @@ -140,6 +140,9 @@ class Store(BaseDocumentManager): STORE_LOCATION = 'store_location' STORE_ADDRESS = 'store_address' +class FakeQuery(): + """Protects against annoying failures""" + results = () class Product(BaseDocumentManager): """Provides helper methods to manage Product documents. All Product documents @@ -309,15 +312,20 @@ def generateRatingsBuckets(cls, query_string): try: sq = search.Query( query_string=query_string.strip()) - search_results = cls.getIndex().search(sq) + search_results = FakeQuery() + try: + search_results = cls.getIndex().search(sq) + except: + pass except search.Error: logging.exception('An error occurred on search.') return None ratings_buckets = collections.defaultdict(int) # populate the buckets - for res in search_results: - ratings_buckets[int((cls(res)).getAvgRating() or 0)] += 1 + if len(search_results.results): + for res in search_results: + ratings_buckets[int((cls(res)).getAvgRating() or 0)] += 1 return ratings_buckets @classmethod diff --git a/product_search_python/handlers.py b/product_search_python/handlers.py index ffb9951..f003226 100644 --- a/product_search_python/handlers.py +++ b/product_search_python/handlers.py @@ -211,6 +211,10 @@ def _tx(): return review return ndb.transaction(_tx) +class FakeQuery(): + """Protects against annoying failures""" + results = () + number_found = 0 class ProductSearchHandler(BaseHandler): """The handler for doing a product search.""" @@ -291,7 +295,11 @@ def doProductSearch(self, params): # build the query and perform the search search_query = self._buildQuery( query, sortq, sort_dict, doc_limit, offsetval) - search_results = docs.Product.getIndex().search(search_query) + search_results = FakeQuery() + try: + search_results = docs.Product.getIndex().search(search_query) + except: + pass returned_count = len(search_results.results) except search.Error: @@ -308,33 +316,34 @@ def doProductSearch(self, params): # cat_name = models.Category.getCategoryName(categoryq) psearch_response = [] # For each document returned from the search - for doc in search_results: - # logging.info("doc: %s ", doc) - pdoc = docs.Product(doc) - # use the description field as the default description snippet, since - # snippeting is not supported on the dev app server. - description_snippet = pdoc.getDescription() - price = pdoc.getPrice() - # on the dev app server, the doc.expressions property won't be populated. - for expr in doc.expressions: - if expr.name == docs.Product.DESCRIPTION: - description_snippet = expr.value - # uncomment to use 'adjusted price', which should be - # defined in returned_expressions in _buildQuery() below, as the - # displayed price. - # elif expr.name == 'adjusted_price': - # price = expr.value - - # get field information from the returned doc - pid = pdoc.getPID() - cat = catname = pdoc.getCategory() - pname = pdoc.getName() - avg_rating = pdoc.getAvgRating() - # for this result, generate a result array of selected doc fields, to - # pass to the template renderer - psearch_response.append( - [doc, urllib.quote_plus(pid), cat, - description_snippet, price, pname, catname, avg_rating]) + if len(search_results.results): + for doc in search_results: + # logging.info("doc: %s ", doc) + pdoc = docs.Product(doc) + # use the description field as the default description snippet, since + # snippeting is not supported on the dev app server. + description_snippet = pdoc.getDescription() + price = pdoc.getPrice() + # on the dev app server, the doc.expressions property won't be populated. + for expr in doc.expressions: + if expr.name == docs.Product.DESCRIPTION: + description_snippet = expr.value + # uncomment to use 'adjusted price', which should be + # defined in returned_expressions in _buildQuery() below, as the + # displayed price. + # elif expr.name == 'adjusted_price': + # price = expr.value + + # get field information from the returned doc + pid = pdoc.getPID() + cat = catname = pdoc.getCategory() + pname = pdoc.getName() + avg_rating = pdoc.getAvgRating() + # for this result, generate a result array of selected doc fields, to + # pass to the template renderer + psearch_response.append( + [doc, urllib.quote_plus(pid), cat, + description_snippet, price, pname, catname, avg_rating]) if not query: print_query = 'All' else: diff --git a/python/search_demo.py b/python/search_demo.py index f83a7e4..f577389 100644 --- a/python/search_demo.py +++ b/python/search_demo.py @@ -33,6 +33,9 @@ def jinja2(self): def render_template(self, filename, template_args): self.response.write(self.jinja2.render_template(filename, **template_args)) +class FakeRequest(): + """Protects against annoying failures""" + results = () class MainPage(BaseHandler): """Handles search requests for comments.""" @@ -56,7 +59,11 @@ def get(self): limit=3, sort_options=sort_opts) query_obj = search.Query(query_string=query, options=query_options) - results = search.Index(name=_INDEX_NAME).search(query=query_obj) + results = FakeRequest() + try: + results = search.Index(name=_INDEX_NAME).search(query=query_obj) + except Exception, e: + pass if users.get_current_user(): url = users.create_logout_url(self.request.uri) url_linktext = 'Logout' diff --git a/python/templates/index.html b/python/templates/index.html index 99920f2..7cd83d1 100644 --- a/python/templates/index.html +++ b/python/templates/index.html @@ -16,12 +16,14 @@ {{number_returned}} of {{results.number_found}} comments found
- {% for scored_document in results %} - {% for f in scored_document.fields %} - {{f.value}} + {% if (results > 0) %} + {% for scored_document in results %} + {% for f in scored_document.fields %} + {{f.value}} + {% endfor %} +
{% endfor %} -
- {% endfor %} + {% endif %} {{ url_linktext }}