Search in sources :

Example 11 with Document

use of com.google.appengine.api.search.Document in project java-docs-samples by GoogleCloudPlatform.

the class DocumentServlet method createDocument.

/**
 * Code snippet for creating a Document.
 * @return Document Created document.
 */
public Document createDocument() {
    // [START create_document]
    User currentUser = UserServiceFactory.getUserService().getCurrentUser();
    String userEmail = currentUser == null ? "" : currentUser.getEmail();
    String userDomain = currentUser == null ? "" : currentUser.getAuthDomain();
    String myDocId = "PA6-5000";
    Document doc = Document.newBuilder().setId(myDocId).addField(Field.newBuilder().setName("content").setText("the rain in spain")).addField(Field.newBuilder().setName("email").setText(userEmail)).addField(Field.newBuilder().setName("domain").setAtom(userDomain)).addField(Field.newBuilder().setName("published").setDate(new Date())).build();
    // [END create_document]
    return doc;
}
Also used : User(com.google.appengine.api.users.User) Document(com.google.appengine.api.search.Document) Date(java.util.Date)

Example 12 with Document

use of com.google.appengine.api.search.Document in project java-docs-samples by GoogleCloudPlatform.

the class SchemaServlet method doGet.

@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());
            }
        }
    }
// [END list_schema]
}
Also used : Schema(com.google.appengine.api.search.Schema) Index(com.google.appengine.api.search.Index) Document(com.google.appengine.api.search.Document) PrintWriter(java.io.PrintWriter) FieldType(com.google.appengine.api.search.Field.FieldType)

Example 13 with Document

use of com.google.appengine.api.search.Document in project java-docs-samples by GoogleCloudPlatform.

the class SearchServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    PrintWriter out = resp.getWriter();
    Document doc = Document.newBuilder().setId("theOnlyPiano").addField(Field.newBuilder().setName("product").setText("piano")).addField(Field.newBuilder().setName("maker").setText("Yamaha")).addField(Field.newBuilder().setName("price").setNumber(4000)).build();
    try {
        Utils.indexADocument(SEARCH_INDEX, doc);
    } catch (InterruptedException e) {
    // ignore
    }
    // [START search_document]
    final int maxRetry = 3;
    int attempts = 0;
    int delay = 2;
    while (true) {
        try {
            String queryString = "product = piano AND price < 5000";
            Results<ScoredDocument> results = getIndex().search(queryString);
            // Iterate over the documents in the results
            for (ScoredDocument document : results) {
                // handle results
                out.print("maker: " + document.getOnlyField("maker").getText());
                out.println(", price: " + document.getOnlyField("price").getNumber());
            }
        } catch (SearchException e) {
            if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode()) && ++attempts < maxRetry) {
                // retry
                try {
                    Thread.sleep(delay * 1000);
                } catch (InterruptedException e1) {
                // ignore
                }
                // easy exponential backoff
                delay *= 2;
                continue;
            } else {
                throw e;
            }
        }
        break;
    }
    // [END search_document]
    // We don't test the search result below, but we're fine if it runs without errors.
    out.println("Search performed");
    Index index = getIndex();
    // [START simple_search_1]
    index.search("rose water");
    // [END simple_search_1]
    // [START simple_search_2]
    index.search("1776-07-04");
    // [END simple_search_2]
    // [START simple_search_3]
    // search for documents with pianos that cost less than $5000
    index.search("product = piano AND price < 5000");
// [END simple_search_3]
}
Also used : ScoredDocument(com.google.appengine.api.search.ScoredDocument) SearchException(com.google.appengine.api.search.SearchException) Index(com.google.appengine.api.search.Index) Document(com.google.appengine.api.search.Document) ScoredDocument(com.google.appengine.api.search.ScoredDocument) PrintWriter(java.io.PrintWriter)

Example 14 with Document

use of com.google.appengine.api.search.Document in project java-docs-samples by GoogleCloudPlatform.

the class DocumentServletTest method createDocument_withSignedInUser.

@Test
public void createDocument_withSignedInUser() throws Exception {
    String email = "tmatsuo@example.com";
    String authDomain = "example.com";
    helper.setEnvEmail(email);
    helper.setEnvAuthDomain(authDomain);
    helper.setEnvIsLoggedIn(true);
    Document doc = servletUnderTest.createDocument();
    assertThat(doc.getOnlyField("content").getText()).named("content").contains("the rain in spain");
    assertThat(doc.getOnlyField("email").getText()).named("email").isEqualTo(email);
}
Also used : Document(com.google.appengine.api.search.Document) Test(org.junit.Test)

Example 15 with Document

use of com.google.appengine.api.search.Document in project blogwt by billy1380.

the class UserService method toDocument.

/* (non-Javadoc)
	 * 
	 * @see
	 * com.willshex.blogwt.server.service.search.IIndex#toDocument(java.lang.
	 * Object) */
@Override
public Document toDocument(User user) {
    Document document = null;
    if (user != null) {
        if (user.roleKeys != null) {
            user.roles = PersistenceHelper.batchLookupKeys(RoleServiceProvider.provide(), user.roleKeys);
        }
        if (user.permissionKeys != null) {
            user.permissions = PersistenceHelper.batchLookupKeys(PermissionServiceProvider.provide(), user.permissionKeys);
        }
        Document.Builder documentBuilder = Document.newBuilder();
        documentBuilder.setId(getName() + user.id.toString()).addField(Field.newBuilder().setName("username").setAtom(user.username)).addField(Field.newBuilder().setName("name").setText(UserHelper.name(user))).addField(Field.newBuilder().setName("forename").setText(user.forename)).addField(Field.newBuilder().setName("surname").setText(user.surname)).addField(Field.newBuilder().setName("email").setText(user.email)).addField(Field.newBuilder().setName("created").setDate(user.created)).addField(Field.newBuilder().setName("group").setText(user.group)).addField(Field.newBuilder().setName("summary").setText(user.summary));
        if (user.roles != null) {
            for (Role role : user.roles) {
                documentBuilder.addField(Field.newBuilder().setName("role").setText(role.name));
            }
        }
        if (user.permissions != null) {
            for (Permission permission : user.permissions) {
                documentBuilder.addField(Field.newBuilder().setName("permission").setText(permission.name));
            }
        }
        document = documentBuilder.build();
    }
    return document;
}
Also used : Role(com.willshex.blogwt.shared.api.datatype.Role) Permission(com.willshex.blogwt.shared.api.datatype.Permission) Document(com.google.appengine.api.search.Document) ScoredDocument(com.google.appengine.api.search.ScoredDocument)

Aggregations

Document (com.google.appengine.api.search.Document)19 ScoredDocument (com.google.appengine.api.search.ScoredDocument)8 PrintWriter (java.io.PrintWriter)6 Index (com.google.appengine.api.search.Index)5 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)3 IndexSpec (com.google.appengine.api.search.IndexSpec)2 Date (java.util.Date)2 LoopHelper (teammates.client.scripts.util.LoopHelper)2 Cursor (com.google.appengine.api.search.Cursor)1 FieldType (com.google.appengine.api.search.Field.FieldType)1 GetRequest (com.google.appengine.api.search.GetRequest)1 OperationResult (com.google.appengine.api.search.OperationResult)1 PutException (com.google.appengine.api.search.PutException)1 PutResponse (com.google.appengine.api.search.PutResponse)1 Schema (com.google.appengine.api.search.Schema)1 SearchException (com.google.appengine.api.search.SearchException)1 StatusCode (com.google.appengine.api.search.StatusCode)1 User (com.google.appengine.api.users.User)1 Permission (com.willshex.blogwt.shared.api.datatype.Permission)1