Full-Text Search Demo App: Product Search
16 | 17 |Introduction
18 | 19 |This Python App Engine application illustrates the use of the Full-Text Search 20 | API in a “Product 21 | Search” domain with two categories of sample products: books and 22 | hd televisions. This README assumes that you are already familiar with how to 23 | configure and deploy an App Engine app. If not, first see the App Engine 24 | documentation 25 | and Getting Started guide.
26 | 27 |This demo app allows users to search product information using full-text search, 28 | and to add reviews with ratings to the products. Search results can be sorted 29 | according to several criteria. In conjunction with listed search results, a 30 | sidebar allows the user to further filter the search results on product rating. 31 | (A product’s rating is the average of its reviews, so if a product has no 32 | reviews yet, its rating will be 0).
33 | 34 |A user does not need to be logged in to search the products, or to add reviews. 35 | A user must be logged in as an admin of the app to add or modify product 36 | data. The sidebar admin links are not displayed for non-admin users.
37 | 38 |Configuration
39 | 40 |Before you deploy the application, edit app.yaml
to specify your own app id and version.
In templates/product.html
, the Google Maps API is accessed. It does not require an API key, but you are encouraged to use one to monitor your maps usage. In the
src="https://maps.googleapis.com/maps/api/js?sensor=false"
45 |
46 |
47 | and replace it with something like the following, where replaceWithYourAPIKey
is your own API key:
src="https://maps.googleapis.com/maps/api/js?sensor=false&key=replaceWithYourAPIKey"
50 |
51 |
52 | as described here.
53 | 54 |Information About Running the App Locally
55 | 56 |Log in as an app admin to add and modify the app’s product data.
57 | 58 |The app uses XG (cross-group) transactions, which requires the dev_appserver to
59 | be run with the --high_replication
flag. E.g., to start up the dev_appserver
60 | from the command line in the project directory (this directory), assuming the
61 | GAE SDK is in your path, do:
dev_appserver.py --high_replication .
64 |
65 |
66 | The app is configured to use Python 2.7. On some platforms, it may also be 67 | necessary to have Python 2.7 installed locally when running the dev_appserver. 68 | The app’s unit tests also require Python 2.7.
69 | 70 |When running the app locally, not all features of the search API are supported. 71 | So, not all search queries may give the same results during local testing as 72 | when run with the deployed app. 73 | Be sure to test on a deployed version of your app as well as locally.
74 | 75 |Administering the deployed app
76 | 77 |You will need to be logged in as an administrator of the app to add and modify
78 | product data, though not to search products or add reviews. If you want to
79 | remove this restriction, you can edit the login: admin
specification in
80 | app.yaml
, and remove the @BaseHandler.admin
decorators in
81 | admin_handlers.py
.
Loading Sample Data
84 | 85 |When you first start up your app, you will want to add sample data to it. 86 |
87 | 88 |Sample product data can be added in two ways. First, sample product data in CSV 89 | format can be added in batch via a link on the app’s admin page. Batch indexing 90 | of documents is more efficient than adding the documents one at a time. For consistency, 91 | the batch addition of sample data first removes all 92 | existing index and datastore product data.
93 | 94 |The second way to add sample data is via the admin’s “Create new product” link 95 | in the sidebar, which lets an admin add sample products (either “books” or 96 | “hd televisions”) one at a time.
97 | 98 |Updating product documents with a new average rating
99 | 100 |When a user creates a new review, the average rating for that product is
101 | updated in the datastore. The app may be configured to update the associated
102 | product search.Document
at the same time (the default), or do this at a
103 | later time in batch (which is more efficient). See cron.yaml
for an example
104 | of how to do this update periodically in batch.
Searches
107 | 108 |Any valid queries can be typed into the search box. This includes simple word 109 | and phrase queries, but you may also submit queries that include references to 110 | specific document fields and use numeric comparators on numeric fields. See the 111 | Search API’s 112 | documentation for 113 | a description of the query syntax.
114 | 115 |Thus, for explanatory purposes, the “product details” show all actual 116 | field names of the given product document; you can use this information to 117 | construct queries against those fields. In the same spirit, the raw 118 | query string used for the query is displayed with the search results.
119 | 120 |Only product information is searched; product review text is not included in the 121 | search.
122 | 123 |Some example searches
124 | 125 |Below are some example product queries, which assume the sample data has been loaded. 126 | As discussed above, not all of these queries are supported by the dev_appserver.
127 | 128 |stories price < 10
129 | price > 10 price < 15
130 | publisher:Vintage
131 | Mega TVs
132 | name:tv1
133 | size > 30
Geosearch
136 | 137 |This application includes an example of using the Search API to perform
138 | location-based queries. Sample store location data is defined in stores.py
,
139 | and is loaded along with the product data. The product details page for a
140 | product allows a search for stores within a given radius of the user’s current
141 | location. The user’s location is obtained from the browser.