|
45 | 45 | * End-to-end example for querying Discovery. |
46 | 46 | */ |
47 | 47 | public class DiscoveryQueryExample { |
48 | | - private static final String DEFAULT_CONFIG_NAME = "Default Configuration"; |
49 | | - |
50 | | - public static void main(String[] args) { |
51 | | - Discovery discovery = new Discovery("2017-11-07"); |
52 | | - discovery.setEndPoint("https://gateway.watsonplatform.net/discovery/api"); |
53 | | - discovery.setUsernameAndPassword("<username>", "<password"); |
54 | | - String environmentId = null; |
55 | | - String configurationId = null; |
56 | | - String collectionId = null; |
57 | | - String documentId = null; |
58 | | - |
59 | | - //See if an environment already exists |
60 | | - System.out.println("Check if environment exists"); |
61 | | - ListEnvironmentsOptions listOptions = new ListEnvironmentsOptions.Builder().build(); |
62 | | - ListEnvironmentsResponse listResponse = discovery.listEnvironments(listOptions).execute(); |
63 | | - for (Environment environment : listResponse.getEnvironments()) { |
64 | | - //look for an existing environment that isn't read only |
65 | | - if (!environment.isReadOnly()) { |
66 | | - environmentId = environment.getEnvironmentId(); |
67 | | - System.out.println("Found existing environment ID: " + environmentId); |
68 | | - break; |
69 | | - } |
70 | | - } |
| 48 | + private static final String DEFAULT_CONFIG_NAME = "Default Configuration"; |
| 49 | + |
| 50 | + public static void main(String[] args) { |
| 51 | + Discovery discovery = new Discovery("2017-11-07"); |
| 52 | + discovery.setEndPoint("https://gateway.watsonplatform.net/discovery/api"); |
| 53 | + discovery.setUsernameAndPassword("<username>", "<password"); |
| 54 | + String environmentId = null; |
| 55 | + String configurationId = null; |
| 56 | + String collectionId = null; |
| 57 | + String documentId = null; |
| 58 | + |
| 59 | + //See if an environment already exists |
| 60 | + System.out.println("Check if environment exists"); |
| 61 | + ListEnvironmentsOptions listOptions = new ListEnvironmentsOptions.Builder().build(); |
| 62 | + ListEnvironmentsResponse listResponse = discovery.listEnvironments(listOptions).execute(); |
| 63 | + for (Environment environment : listResponse.getEnvironments()) { |
| 64 | + //look for an existing environment that isn't read only |
| 65 | + if (!environment.isReadOnly()) { |
| 66 | + environmentId = environment.getEnvironmentId(); |
| 67 | + System.out.println("Found existing environment ID: " + environmentId); |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
71 | 71 |
|
72 | | - if (environmentId == null) { |
73 | | - System.out.println("No environment found, creating new one..."); |
74 | | - //no environment found, create a new one (assuming we are a FREE plan) |
75 | | - String environmentName = "watson_developer_cloud_test_environment"; |
76 | | - CreateEnvironmentOptions createOptions = new CreateEnvironmentOptions.Builder() |
77 | | - .name(environmentName) |
78 | | - .size(0L) /* FREE */ |
79 | | - .build(); |
80 | | - Environment createResponse = discovery.createEnvironment(createOptions).execute(); |
81 | | - environmentId = createResponse.getEnvironmentId(); |
82 | | - System.out.println("Created new environment ID: " + environmentId); |
83 | | - |
84 | | - //wait for environment to be ready |
85 | | - System.out.println("Waiting for environment to be ready..."); |
86 | | - boolean environmentReady = false; |
87 | | - while (!environmentReady) { |
88 | | - GetEnvironmentOptions getEnvironmentOptions = new GetEnvironmentOptions.Builder(environmentId).build(); |
89 | | - Environment getEnvironmentResponse = discovery.getEnvironment(getEnvironmentOptions).execute(); |
90 | | - environmentReady = getEnvironmentResponse.getStatus().equals(Environment.Status.ACTIVE); |
91 | | - try { |
92 | | - if (!environmentReady) { |
93 | | - Thread.sleep(500); |
94 | | - } |
95 | | - } catch (InterruptedException e) { |
96 | | - throw new RuntimeException("Interrupted", e); |
97 | | - } |
98 | | - } |
99 | | - System.out.println("Environment Ready!"); |
| 72 | + if (environmentId == null) { |
| 73 | + System.out.println("No environment found, creating new one..."); |
| 74 | + //no environment found, create a new one (assuming we are a FREE plan) |
| 75 | + String environmentName = "watson_developer_cloud_test_environment"; |
| 76 | + CreateEnvironmentOptions createOptions = new CreateEnvironmentOptions.Builder() |
| 77 | + .name(environmentName) |
| 78 | + .size(0L) /* FREE */ |
| 79 | + .build(); |
| 80 | + Environment createResponse = discovery.createEnvironment(createOptions).execute(); |
| 81 | + environmentId = createResponse.getEnvironmentId(); |
| 82 | + System.out.println("Created new environment ID: " + environmentId); |
| 83 | + |
| 84 | + //wait for environment to be ready |
| 85 | + System.out.println("Waiting for environment to be ready..."); |
| 86 | + boolean environmentReady = false; |
| 87 | + while (!environmentReady) { |
| 88 | + GetEnvironmentOptions getEnvironmentOptions = new GetEnvironmentOptions.Builder(environmentId).build(); |
| 89 | + Environment getEnvironmentResponse = discovery.getEnvironment(getEnvironmentOptions).execute(); |
| 90 | + environmentReady = getEnvironmentResponse.getStatus().equals(Environment.Status.ACTIVE); |
| 91 | + try { |
| 92 | + if (!environmentReady) { |
| 93 | + Thread.sleep(500); |
| 94 | + } |
| 95 | + } catch (InterruptedException e) { |
| 96 | + throw new RuntimeException("Interrupted", e); |
100 | 97 | } |
| 98 | + } |
| 99 | + System.out.println("Environment Ready!"); |
| 100 | + } |
101 | 101 |
|
102 | | - //find the default configuration |
103 | | - System.out.println("Finding the default configuration"); |
104 | | - ListConfigurationsOptions listConfigsOptions = new ListConfigurationsOptions.Builder(environmentId).build(); |
105 | | - ListConfigurationsResponse listConfigsResponse = discovery.listConfigurations(listConfigsOptions).execute(); |
106 | | - for (Configuration configuration : listConfigsResponse.getConfigurations()) { |
107 | | - if (configuration.getName().equals(DEFAULT_CONFIG_NAME)) { |
108 | | - configurationId = configuration.getConfigurationId(); |
109 | | - System.out.println("Found default configuration ID: " + configurationId); |
110 | | - break; |
111 | | - } |
112 | | - } |
| 102 | + //find the default configuration |
| 103 | + System.out.println("Finding the default configuration"); |
| 104 | + ListConfigurationsOptions listConfigsOptions = new ListConfigurationsOptions.Builder(environmentId).build(); |
| 105 | + ListConfigurationsResponse listConfigsResponse = discovery.listConfigurations(listConfigsOptions).execute(); |
| 106 | + for (Configuration configuration : listConfigsResponse.getConfigurations()) { |
| 107 | + if (configuration.getName().equals(DEFAULT_CONFIG_NAME)) { |
| 108 | + configurationId = configuration.getConfigurationId(); |
| 109 | + System.out.println("Found default configuration ID: " + configurationId); |
| 110 | + break; |
| 111 | + } |
| 112 | + } |
113 | 113 |
|
114 | | - //create a new collection |
115 | | - System.out.println("Creating a new collection..."); |
116 | | - String collectionName = "my_watson_developer_cloud_collection"; |
117 | | - CreateCollectionOptions createCollectionOptions = |
118 | | - new CreateCollectionOptions.Builder(environmentId, collectionName) |
119 | | - .configurationId(configurationId) |
120 | | - .build(); |
121 | | - Collection collection = discovery.createCollection(createCollectionOptions).execute(); |
122 | | - collectionId = collection.getCollectionId(); |
123 | | - System.out.println("Created a collection ID: " + collectionId); |
124 | | - |
125 | | - //wait for the collection to be "available" |
126 | | - System.out.println("Waiting for collection to be ready..."); |
127 | | - boolean collectionReady = false; |
128 | | - while (!collectionReady) { |
129 | | - GetCollectionOptions getCollectionOptions = |
130 | | - new GetCollectionOptions.Builder(environmentId, collectionId).build(); |
131 | | - Collection getCollectionResponse = discovery.getCollection(getCollectionOptions).execute(); |
132 | | - collectionReady = getCollectionResponse.getStatus().equals(Collection.Status.ACTIVE); |
133 | | - try { |
134 | | - if (!collectionReady) { |
135 | | - Thread.sleep(500); |
136 | | - } |
137 | | - } catch (InterruptedException e) { |
138 | | - throw new RuntimeException("Interrupted", e); |
139 | | - } |
| 114 | + //create a new collection |
| 115 | + System.out.println("Creating a new collection..."); |
| 116 | + String collectionName = "my_watson_developer_cloud_collection"; |
| 117 | + CreateCollectionOptions createCollectionOptions = |
| 118 | + new CreateCollectionOptions.Builder(environmentId, collectionName) |
| 119 | + .configurationId(configurationId) |
| 120 | + .build(); |
| 121 | + Collection collection = discovery.createCollection(createCollectionOptions).execute(); |
| 122 | + collectionId = collection.getCollectionId(); |
| 123 | + System.out.println("Created a collection ID: " + collectionId); |
| 124 | + |
| 125 | + //wait for the collection to be "available" |
| 126 | + System.out.println("Waiting for collection to be ready..."); |
| 127 | + boolean collectionReady = false; |
| 128 | + while (!collectionReady) { |
| 129 | + GetCollectionOptions getCollectionOptions = |
| 130 | + new GetCollectionOptions.Builder(environmentId, collectionId).build(); |
| 131 | + Collection getCollectionResponse = discovery.getCollection(getCollectionOptions).execute(); |
| 132 | + collectionReady = getCollectionResponse.getStatus().equals(Collection.Status.ACTIVE); |
| 133 | + try { |
| 134 | + if (!collectionReady) { |
| 135 | + Thread.sleep(500); |
140 | 136 | } |
141 | | - System.out.println("Collection Ready!"); |
142 | | - |
143 | | - //add a document |
144 | | - System.out.println("Creating a new document..."); |
145 | | - String documentJson = "{\"field\":\"value\"}"; |
146 | | - InputStream documentStream = new ByteArrayInputStream(documentJson.getBytes()); |
147 | | - |
148 | | - AddDocumentOptions.Builder createDocumentBuilder = |
149 | | - new AddDocumentOptions.Builder(environmentId, collectionId); |
150 | | - createDocumentBuilder.file(documentStream).fileMediaType(HttpMediaType.APPLICATION_JSON); |
151 | | - DocumentAccepted createDocumentResponse = discovery.addDocument(createDocumentBuilder.build()).execute(); |
152 | | - documentId = createDocumentResponse.getDocumentId(); |
153 | | - System.out.println("Created a document ID: " + documentId); |
154 | | - |
155 | | - //wait for document to be ready |
156 | | - System.out.println("Waiting for document to be ready..."); |
157 | | - boolean documentReady = false; |
158 | | - while (!documentReady) { |
159 | | - GetDocumentStatusOptions getDocumentStatusOptions = |
160 | | - new GetDocumentStatusOptions.Builder(environmentId, collectionId, documentId).build(); |
161 | | - DocumentStatus getDocumentResponse = discovery.getDocument(getDocumentStatusOptions).execute(); |
162 | | - documentReady = !getDocumentResponse.getStatus().equals(DocumentStatus.Status.PROCESSING); |
163 | | - try { |
164 | | - if (!documentReady) { |
165 | | - Thread.sleep(500); |
166 | | - } |
167 | | - } catch (InterruptedException e) { |
168 | | - throw new RuntimeException("Interrupted"); |
169 | | - } |
| 137 | + } catch (InterruptedException e) { |
| 138 | + throw new RuntimeException("Interrupted", e); |
| 139 | + } |
| 140 | + } |
| 141 | + System.out.println("Collection Ready!"); |
| 142 | + |
| 143 | + //add a document |
| 144 | + System.out.println("Creating a new document..."); |
| 145 | + String documentJson = "{\"field\":\"value\"}"; |
| 146 | + InputStream documentStream = new ByteArrayInputStream(documentJson.getBytes()); |
| 147 | + |
| 148 | + AddDocumentOptions.Builder createDocumentBuilder = |
| 149 | + new AddDocumentOptions.Builder(environmentId, collectionId); |
| 150 | + createDocumentBuilder.file(documentStream).fileMediaType(HttpMediaType.APPLICATION_JSON); |
| 151 | + DocumentAccepted createDocumentResponse = discovery.addDocument(createDocumentBuilder.build()).execute(); |
| 152 | + documentId = createDocumentResponse.getDocumentId(); |
| 153 | + System.out.println("Created a document ID: " + documentId); |
| 154 | + |
| 155 | + //wait for document to be ready |
| 156 | + System.out.println("Waiting for document to be ready..."); |
| 157 | + boolean documentReady = false; |
| 158 | + while (!documentReady) { |
| 159 | + GetDocumentStatusOptions getDocumentStatusOptions = |
| 160 | + new GetDocumentStatusOptions.Builder(environmentId, collectionId, documentId).build(); |
| 161 | + DocumentStatus getDocumentResponse = discovery.getDocument(getDocumentStatusOptions).execute(); |
| 162 | + documentReady = !getDocumentResponse.getStatus().equals(DocumentStatus.Status.PROCESSING); |
| 163 | + try { |
| 164 | + if (!documentReady) { |
| 165 | + Thread.sleep(500); |
170 | 166 | } |
171 | | - System.out.println("Document Ready!"); |
172 | | - |
173 | | - //query document |
174 | | - System.out.println("Querying the collection..."); |
175 | | - QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId); |
176 | | - queryBuilder.query("field:value"); |
177 | | - QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute(); |
178 | | - |
179 | | - //print out the results |
180 | | - System.out.println("Query Results:"); |
181 | | - System.out.println(queryResponse); |
182 | | - |
183 | | - //cleanup the collection created |
184 | | - System.out.println("Deleting the collection..."); |
185 | | - DeleteCollectionOptions deleteOptions = |
186 | | - new DeleteCollectionOptions.Builder(environmentId, collectionId).build(); |
187 | | - discovery.deleteCollection(deleteOptions).execute(); |
188 | | - System.out.println("Collection deleted!"); |
189 | | - |
190 | | - System.out.println("Discovery example finished"); |
| 167 | + } catch (InterruptedException e) { |
| 168 | + throw new RuntimeException("Interrupted"); |
| 169 | + } |
191 | 170 | } |
| 171 | + System.out.println("Document Ready!"); |
| 172 | + |
| 173 | + //query document |
| 174 | + System.out.println("Querying the collection..."); |
| 175 | + QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId); |
| 176 | + queryBuilder.query("field:value"); |
| 177 | + QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute(); |
| 178 | + |
| 179 | + //print out the results |
| 180 | + System.out.println("Query Results:"); |
| 181 | + System.out.println(queryResponse); |
| 182 | + |
| 183 | + //cleanup the collection created |
| 184 | + System.out.println("Deleting the collection..."); |
| 185 | + DeleteCollectionOptions deleteOptions = |
| 186 | + new DeleteCollectionOptions.Builder(environmentId, collectionId).build(); |
| 187 | + discovery.deleteCollection(deleteOptions).execute(); |
| 188 | + System.out.println("Collection deleted!"); |
| 189 | + |
| 190 | + System.out.println("Discovery example finished"); |
| 191 | + } |
192 | 192 | } |
0 commit comments