-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added few more sample code for Search API. (#173)
* Moved some checkstyle modules to java-repo-tools * Moved some lines in .gitignore to the top level
- Loading branch information
Showing
19 changed files
with
473 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,8 @@ service-account.json | |
# intellij | ||
.idea/ | ||
*.iml | ||
|
||
# Eclipse files | ||
.project | ||
.classpath | ||
.settings |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
appengine/search/src/main/java/com/example/appengine/search/SchemaServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.appengine.search; | ||
|
||
import com.google.appengine.api.search.Document; | ||
import com.google.appengine.api.search.Field; | ||
import com.google.appengine.api.search.SearchServiceFactory; | ||
|
||
// @formatter:off | ||
// CHECKSTYLE:OFF | ||
// [START schema_import] | ||
import com.google.appengine.api.search.Field.FieldType; | ||
import com.google.appengine.api.search.Index; | ||
import com.google.appengine.api.search.GetIndexesRequest; | ||
import com.google.appengine.api.search.GetResponse; | ||
import com.google.appengine.api.search.Schema; | ||
// [END schema_import] | ||
// @formatter:on | ||
// CHECKSTYLE:ON | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.util.List; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
|
||
@SuppressWarnings("serial") | ||
public class SchemaServlet extends HttpServlet { | ||
|
||
private static final String SEARCH_INDEX = "schemaIndex"; | ||
|
||
@Override | ||
public void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
throws IOException { | ||
PrintWriter out = resp.getWriter(); | ||
Document doc = Document.newBuilder() | ||
.setId("theOnlyCar") | ||
.addField(Field.newBuilder().setName("maker").setText("Toyota")) | ||
.addField(Field.newBuilder().setName("price").setNumber(300000)) | ||
.addField(Field.newBuilder().setName("color").setText("lightblue")) | ||
.addField(Field.newBuilder().setName("model").setText("Prius")) | ||
.build(); | ||
try { | ||
Utils.indexADocument(SEARCH_INDEX, doc); | ||
} catch (InterruptedException e) { | ||
// ignore | ||
} | ||
// [START list_schema] | ||
GetResponse<Index> response = SearchServiceFactory.getSearchService().getIndexes( | ||
GetIndexesRequest.newBuilder().setSchemaFetched(true).build()); | ||
|
||
// List out elements of each Schema | ||
for (Index index : response) { | ||
Schema schema = index.getSchema(); | ||
for (String fieldName : schema.getFieldNames()) { | ||
List<FieldType> typesForField = schema.getFieldTypes(fieldName); | ||
// Just printing out the field names and types | ||
for (FieldType type : typesForField) { | ||
out.println(index.getName() + ":" + fieldName + ":" + type.name()); | ||
} | ||
} | ||
} | ||
// [START list_schema] | ||
} | ||
} |
Oops, something went wrong.