|
| 1 | +# Copyright 2016 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from datetime import datetime |
| 16 | + |
| 17 | +from google.appengine.api import search |
| 18 | + |
| 19 | + |
| 20 | +def simple_search(index): |
| 21 | + index.search('rose water') |
| 22 | + |
| 23 | + |
| 24 | +def search_date(index): |
| 25 | + index.search('1776-07-04') |
| 26 | + |
| 27 | + |
| 28 | +def search_terms(index): |
| 29 | + # search for documents with pianos that cost less than $5000 |
| 30 | + index.search("product = piano AND price < 5000") |
| 31 | + |
| 32 | + |
| 33 | +def create_document(): |
| 34 | + document = search.Document( |
| 35 | + # Setting the doc_id is optional. If omitted, the search service will |
| 36 | + # create an identifier. |
| 37 | + doc_id='PA6-5000', |
| 38 | + fields=[ |
| 39 | + search.TextField(name='customer', value='Joe Jackson'), |
| 40 | + search.HtmlField( |
| 41 | + name='comment', value='this is <em>marked up</em> text'), |
| 42 | + search.NumberField(name='number_of_visits', value=7), |
| 43 | + search.DateField(name='last_visit', value=datetime.now()), |
| 44 | + search.DateField( |
| 45 | + name='birthday', value=datetime(year=1960, month=6, day=19)), |
| 46 | + search.GeoField( |
| 47 | + name='home_location', value=search.GeoPoint(37.619, -122.37)) |
| 48 | + ]) |
| 49 | + return document |
| 50 | + |
| 51 | + |
| 52 | +def add_document_to_index(document): |
| 53 | + index = search.Index('products') |
| 54 | + index.put(document) |
| 55 | + |
| 56 | + |
| 57 | +def add_document_and_get_doc_id(documents): |
| 58 | + index = search.Index('products') |
| 59 | + results = index.put(documents) |
| 60 | + document_ids = [document.id for document in results] |
| 61 | + return document_ids |
| 62 | + |
| 63 | + |
| 64 | +def get_document_by_id(): |
| 65 | + index = search.Index('products') |
| 66 | + |
| 67 | + # Get a single document by ID. |
| 68 | + document = index.get("AZ125") |
| 69 | + |
| 70 | + # Get a range of documents starting with a given ID. |
| 71 | + documents = index.get_range(start_id="AZ125", limit=100) |
| 72 | + |
| 73 | + return document, documents |
| 74 | + |
| 75 | + |
| 76 | +def query_index(): |
| 77 | + index = search.Index('products') |
| 78 | + query_string = 'product: piano AND price < 5000' |
| 79 | + |
| 80 | + results = index.search(query_string) |
| 81 | + |
| 82 | + for scored_document in results: |
| 83 | + print(scored_document) |
| 84 | + |
| 85 | + |
| 86 | +def delete_all_in_index(index): |
| 87 | + # index.get_range by returns up to 100 documents at a time, so we must |
| 88 | + # loop until we've deleted all items. |
| 89 | + while True: |
| 90 | + # Use ids_only to get the list of document IDs in the index without |
| 91 | + # the overhead of getting the entire document. |
| 92 | + document_ids = [ |
| 93 | + document.doc_id |
| 94 | + for document |
| 95 | + in index.get_range(ids_only=True)] |
| 96 | + |
| 97 | + # If no IDs were returned, we've deleted everything. |
| 98 | + if not document_ids: |
| 99 | + break |
| 100 | + |
| 101 | + # Delete the documents for the given IDs |
| 102 | + index.delete(document_ids) |
| 103 | + |
| 104 | + |
| 105 | +def async_query(index): |
| 106 | + futures = [index.search_async('foo'), index.search_async('bar')] |
| 107 | + results = [future.get_result() for future in futures] |
| 108 | + return results |
| 109 | + |
| 110 | + |
| 111 | +def query_options(): |
| 112 | + index = search.Index('products') |
| 113 | + query_string = "product: piano AND price < 5000" |
| 114 | + |
| 115 | + # Create sort options to sory on price and brand. |
| 116 | + sort_price = search.SortExpression( |
| 117 | + expression='price', |
| 118 | + direction=search.SortExpression.DESCENDING, |
| 119 | + default_value=0) |
| 120 | + sort_brand = search.SortExpression( |
| 121 | + expression='brand', |
| 122 | + direction=search.SortExpression.DESCENDING, |
| 123 | + default_value="") |
| 124 | + sort_options = search.SortOptions(expressions=[sort_price, sort_brand]) |
| 125 | + |
| 126 | + # Create field expressions to add new fields to the scored documents. |
| 127 | + price_per_note_expression = search.FieldExpression( |
| 128 | + name='price_per_note', expression='price/88') |
| 129 | + ivory_expression = search.FieldExpression( |
| 130 | + name='ivory', expression='snippet("ivory", summary, 120)') |
| 131 | + |
| 132 | + # Create query options using the sort options and expressions created |
| 133 | + # above. |
| 134 | + query_options = search.QueryOptions( |
| 135 | + limit=25, |
| 136 | + returned_fields=['model', 'price', 'description'], |
| 137 | + returned_expressions=[price_per_note_expression, ivory_expression], |
| 138 | + sort_options=sort_options) |
| 139 | + |
| 140 | + # Build the Query and run the search |
| 141 | + query = search.Query(query_string=query_string, options=query_options) |
| 142 | + results = index.search(query) |
| 143 | + for scored_document in results: |
| 144 | + print(scored_document) |
| 145 | + |
| 146 | + |
| 147 | +def query_results(index, query_string): |
| 148 | + result = index.search(query_string) |
| 149 | + total_matches = result.number_found |
| 150 | + list_of_docs = result.results |
| 151 | + number_of_docs_returned = len(list_of_docs) |
| 152 | + return total_matches, list_of_docs, number_of_docs_returned |
| 153 | + |
| 154 | + |
| 155 | +def query_offset(index, query_string): |
| 156 | + offset = 0 |
| 157 | + |
| 158 | + while True: |
| 159 | + # Build the query using the current offset. |
| 160 | + options = search.QueryOptions(offset=offset) |
| 161 | + query = search.Query(query_string=query_string, options=options) |
| 162 | + |
| 163 | + # Get the results |
| 164 | + results = index.search(query) |
| 165 | + |
| 166 | + number_retrieved = len(results.results) |
| 167 | + if number_retrieved == 0: |
| 168 | + break |
| 169 | + |
| 170 | + # Add the number of documents found to the offset, so that the next |
| 171 | + # iteration will grab the next page of documents. |
| 172 | + offset += number_retrieved |
| 173 | + |
| 174 | + # Process the matched documents |
| 175 | + for document in results: |
| 176 | + print(document) |
| 177 | + |
| 178 | + |
| 179 | +def query_cursor(index, query_string): |
| 180 | + cursor = search.Cursor() |
| 181 | + |
| 182 | + while cursor: |
| 183 | + # Build the query using the cursor. |
| 184 | + options = search.QueryOptions(cursor=cursor) |
| 185 | + query = search.Query(query_string=query_string, options=options) |
| 186 | + |
| 187 | + # Get the results and the next cursor |
| 188 | + results = index.search(query) |
| 189 | + cursor = results.cursor |
| 190 | + |
| 191 | + for document in results: |
| 192 | + print(document) |
| 193 | + |
| 194 | + |
| 195 | +def query_per_document_cursor(index, query_string): |
| 196 | + cursor = search.Cursor(per_result=True) |
| 197 | + |
| 198 | + # Build the query using the cursor. |
| 199 | + options = search.QueryOptions(cursor=cursor) |
| 200 | + query = search.Query(query_string=query_string, options=options) |
| 201 | + |
| 202 | + # Get the results. |
| 203 | + results = index.search(query) |
| 204 | + |
| 205 | + document_cursor = None |
| 206 | + for document in results: |
| 207 | + # discover some document of interest and grab its cursor, for this |
| 208 | + # sample we'll just use the first document. |
| 209 | + document_cursor = document.cursor |
| 210 | + break |
| 211 | + |
| 212 | + # Start the next search from the document of interest. |
| 213 | + if document_cursor is None: |
| 214 | + return |
| 215 | + |
| 216 | + options = search.QueryOptions(cursor=document_cursor) |
| 217 | + query = search.Query(query_string=query_string, options=options) |
| 218 | + results = index.search(query) |
| 219 | + |
| 220 | + for document in results: |
| 221 | + print(document) |
| 222 | + |
| 223 | + |
| 224 | +def saving_and_restoring_cursor(cursor): |
| 225 | + # Convert the cursor to a web-safe string. |
| 226 | + cursor_string = cursor.web_safe_string |
| 227 | + # Restore the cursor from a web-safe string. |
| 228 | + cursor = search.Cursor(web_safe_string=cursor_string) |
| 229 | + |
| 230 | + |
| 231 | +def add_faceted_document(index): |
| 232 | + document = search.Document( |
| 233 | + doc_id='doc1', |
| 234 | + fields=[ |
| 235 | + search.AtomField(name='name', value='x86')], |
| 236 | + facets=[ |
| 237 | + search.AtomFacet(name='type', value='computer'), |
| 238 | + search.NumberFacet(name='ram_size_gb', value=8)]) |
| 239 | + |
| 240 | + index.put(document) |
| 241 | + |
| 242 | + |
| 243 | +def facet_discovery(index): |
| 244 | + # Create the query and enable facet discovery. |
| 245 | + query = search.Query('name:x86', enable_facet_discovery=True) |
| 246 | + results = index.search(query) |
| 247 | + |
| 248 | + for facet in results.facets: |
| 249 | + print('facet {}.'.format(facet.name)) |
| 250 | + for value in facet.values: |
| 251 | + print('{}: count={}, refinement_token={}'.format( |
| 252 | + value.label, value.count, value.refinement_token)) |
| 253 | + |
| 254 | + |
| 255 | +def facet_by_name(index): |
| 256 | + # Create the query and specify to only return the "type" and "ram_size_gb" |
| 257 | + # facets. |
| 258 | + query = search.Query('name:x86', return_facets=['type', 'ram_size_gb']) |
| 259 | + results = index.search(query) |
| 260 | + |
| 261 | + for facet in results.facets: |
| 262 | + print('facet {}'.format(facet.name)) |
| 263 | + for value in facet.values: |
| 264 | + print('{}: count={}, refinement_token={}'.format( |
| 265 | + value.label, value.count, value.refinement_token)) |
| 266 | + |
| 267 | + |
| 268 | +def facet_by_name_and_value(index): |
| 269 | + # Create the query and specify to return the "type" facet with values |
| 270 | + # "computer" and "printer" and the "ram_size_gb" facet with value in the |
| 271 | + # ranges [0,4), [4, 8), and [8, max]. |
| 272 | + query = search.Query( |
| 273 | + 'name:x86', |
| 274 | + return_facets=[ |
| 275 | + search.FacetRequest('type', values=['computer', 'printer']), |
| 276 | + search.FacetRequest('ram_size_gb', ranges=[ |
| 277 | + search.FacetRange(end=4), |
| 278 | + search.FacetRange(start=4, end=8), |
| 279 | + search.FacetRange(start=8)]) |
| 280 | + ]) |
| 281 | + |
| 282 | + results = index.search(query) |
| 283 | + for facet in results.facets: |
| 284 | + print('facet {}'.format(facet.name)) |
| 285 | + for value in facet.values: |
| 286 | + print('{}: count={}, refinement_token={}'.format( |
| 287 | + value.label, value.count, value.refinement_token)) |
0 commit comments