Skip to content
This repository was archived by the owner on Sep 13, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions product_search_python/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
65 changes: 37 additions & 28 deletions product_search_python/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion python/search_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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'
Expand Down
12 changes: 7 additions & 5 deletions python/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
</form>

{{number_returned}} of {{results.number_found}} comments found <p>
{% for scored_document in results %}
{% for f in scored_document.fields %}
{{f.value}} &nbsp;
{% if (results > 0) %}
{% for scored_document in results %}
{% for f in scored_document.fields %}
{{f.value}} &nbsp;
{% endfor %}
<p>
{% endfor %}
<p>
{% endfor %}
{% endif %}

<a href="{{ url }}">{{ url_linktext }}</a>

Expand Down