|
| 1 | +.. toctree:: |
| 2 | + :maxdepth: 1 |
| 3 | + :hidden: |
| 4 | + |
| 5 | +Search |
| 6 | +------ |
| 7 | + |
| 8 | +Overview |
| 9 | +~~~~~~~~ |
| 10 | + |
| 11 | +Cloud Search allows you to quickly perform full-text and geospatial searches |
| 12 | +against your data without having to spin up your own instances |
| 13 | +and without the hassle of managing and maintaining a search service. |
| 14 | + |
| 15 | +Cloud Search provides a model for indexing your documents |
| 16 | +that contain structured data, |
| 17 | +with documents and indexes saved to a separate persistent store |
| 18 | +optimized for search operations. |
| 19 | +You can search an index, and organize and present your search results. |
| 20 | +The API supports full text matching on string fields |
| 21 | +and allows you to index any number of documents in any number of indexes. |
| 22 | + |
| 23 | +Indexes |
| 24 | +~~~~~~~ |
| 25 | + |
| 26 | +Here's an example of how you might deal with indexes:: |
| 27 | + |
| 28 | + >>> from gcloud import search |
| 29 | + >>> client = search.Client() |
| 30 | + |
| 31 | + >>> # List all indexes in your project |
| 32 | + >>> for index in client.list_indexes(): |
| 33 | + ... print index |
| 34 | + |
| 35 | + >>> # Create a new index |
| 36 | + >>> new_index = client.index('index-id-here') |
| 37 | + >>> new_index.name = 'My new index' |
| 38 | + >>> new_index.create() |
| 39 | + |
| 40 | + >>> # Update an existing index |
| 41 | + >>> index = client.get_index('existing-index-id') |
| 42 | + >>> print index |
| 43 | + <Index: Existing Index (existing-index-id)> |
| 44 | + >>> index.name = 'Modified name' |
| 45 | + >>> index.update() |
| 46 | + >>> print index |
| 47 | + <Index: Modified name (existing-index-id)> |
| 48 | + |
| 49 | + >>> # Delete an index |
| 50 | + >>> index = client.get_index('existing-index-id') |
| 51 | + >>> index.delete() |
| 52 | + |
| 53 | +Documents |
| 54 | +~~~~~~~~~ |
| 55 | + |
| 56 | +Documents are the things that you search for. |
| 57 | +The typical process is: |
| 58 | + |
| 59 | +#. Create a document |
| 60 | +#. Add fields to the document |
| 61 | +#. Add the document to an index to be searched for later |
| 62 | + |
| 63 | +Here's an example of how you might deal with documents:: |
| 64 | + |
| 65 | + >>> from gcloud import search |
| 66 | + >>> client = search.Client() |
| 67 | + |
| 68 | + >>> # Create a document |
| 69 | + >>> document = search.Document('document-id') |
| 70 | + |
| 71 | + >>> # Add a field to the document |
| 72 | + >>> field = search.Field('fieldname') |
| 73 | + >>> field.add_value('string') |
| 74 | + >>> document.add_field(field) |
| 75 | + |
| 76 | + >>> # Add the document to an index |
| 77 | + >>> index = client.get_index('existing-index-id') |
| 78 | + >>> index.add_document(document) |
| 79 | + |
| 80 | +Fields |
| 81 | +~~~~~~ |
| 82 | + |
| 83 | +Fields belong to documents and are the data that actually gets searched. |
| 84 | +Each field can have multiple values, |
| 85 | +and there are three different types of tokenization for string values: |
| 86 | + |
| 87 | +- **Atom** (``atom``) means "don't tokenize this string", treat it as one thing |
| 88 | + to compare against. |
| 89 | +- **Text** (``text``) means "treat this string as normal text" and split words |
| 90 | + apart to be compared against. |
| 91 | +- **HTML** (``html``) means "treat this string as HTML", understanding the |
| 92 | + tags, and treating the rest of the content like Text. |
| 93 | + |
| 94 | +You can set this using the ``tokenization`` paramater when adding a field |
| 95 | +value:: |
| 96 | + |
| 97 | + >>> from gcloud import search |
| 98 | + >>> document = search.Document('document-id') |
| 99 | + >>> document.add_field(search.Field('field-name', values=[ |
| 100 | + ... search.Value('britney spears', tokenization='atom'), |
| 101 | + ... search.Value('<h1>Britney Spears</h1>', tokenization='html'), |
| 102 | + ... ])) |
| 103 | + |
| 104 | +Searching |
| 105 | +~~~~~~~~~ |
| 106 | + |
| 107 | +Once you have indexes full of documents, you can search through them by |
| 108 | +issuing a search query. |
| 109 | + |
| 110 | +Here's a simple example of how you might start searching:: |
| 111 | + |
| 112 | + >>> from gcloud import search |
| 113 | + >>> client = search.Client() |
| 114 | + |
| 115 | + >>> index = client.get_index('existing-index-id') |
| 116 | + >>> query = search.Query('britney spears') |
| 117 | + >>> matching_documents = index.search(query) |
| 118 | + >>> for document in matching_documents: |
| 119 | + ... print document |
| 120 | + |
| 121 | +By default, all queries are sorted by the ``rank`` value you set |
| 122 | +when the documented was created. |
| 123 | +If you want to sort differently, use the ``order_by`` parameter:: |
| 124 | + |
| 125 | + >>> from gcloud import search |
| 126 | + >>> query = search.Query('britney spears', order_by=['field1', '-field2']) |
| 127 | + |
| 128 | +Note that the ``-`` character before ``field2`` means that |
| 129 | +this query will be sorted ascending by ``field1`` |
| 130 | +and then descending by ``field2``. |
| 131 | + |
| 132 | +If you want only want certain fields to be returned in the match, |
| 133 | +you can use the ``fields`` paramater:: |
| 134 | + |
| 135 | + >>> from gcloud import search |
| 136 | + >>> query = search.Query('britney spears', fields=['field1', 'field2']) |
| 137 | + |
0 commit comments