Search in sources :

Example 1 with Document

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

the class DeleteServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // Put one document to avoid an error
    Document document = Document.newBuilder().addField(Field.newBuilder().setName("f").setText("v")).build();
    try {
        Utils.indexADocument(SEARCH_INDEX, document);
    } catch (InterruptedException e) {
    // ignore
    }
    // [START delete_documents]
    try {
        // looping because getRange by default returns up to 100 documents at a time
        while (true) {
            List<String> docIds = new ArrayList<>();
            // Return a set of doc_ids.
            GetRequest request = GetRequest.newBuilder().setReturningIdsOnly(true).build();
            GetResponse<Document> response = getIndex().getRange(request);
            if (response.getResults().isEmpty()) {
                break;
            }
            for (Document doc : response) {
                docIds.add(doc.getId());
            }
            getIndex().delete(docIds);
        }
    } catch (RuntimeException e) {
        LOG.log(Level.SEVERE, "Failed to delete documents", e);
    }
    // [END delete_documents]
    PrintWriter out = resp.getWriter();
    out.println("Deleted documents.");
}
Also used : GetRequest(com.google.appengine.api.search.GetRequest) ArrayList(java.util.ArrayList) Document(com.google.appengine.api.search.Document) PrintWriter(java.io.PrintWriter)

Example 2 with Document

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

the class IndexServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    PrintWriter out = resp.getWriter();
    Document document = Document.newBuilder().setId("AZ125").addField(Field.newBuilder().setName("myField").setText("myValue")).build();
    try {
        Utils.indexADocument(INDEX, document);
    } catch (InterruptedException e) {
        out.println("Interrupted");
        return;
    }
    out.println("Indexed a new document.");
    // [START get_document]
    IndexSpec indexSpec = IndexSpec.newBuilder().setName(INDEX).build();
    Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
    // Fetch a single document by its  doc_id
    Document doc = index.get("AZ125");
    // Fetch a range of documents by their doc_ids
    GetResponse<Document> docs = index.getRange(GetRequest.newBuilder().setStartId("AZ125").setLimit(100).build());
    // [END get_document]
    out.println("myField: " + docs.getResults().get(0).getOnlyField("myField").getText());
}
Also used : IndexSpec(com.google.appengine.api.search.IndexSpec) Index(com.google.appengine.api.search.Index) Document(com.google.appengine.api.search.Document) PrintWriter(java.io.PrintWriter)

Example 3 with Document

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

the class SearchOptionServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // Put one document to avoid an error
    Document document = Document.newBuilder().setId("theOnlyCoffeeRoaster").addField(Field.newBuilder().setName("price").setNumber(200)).addField(Field.newBuilder().setName("model").setText("TZ4000")).addField(Field.newBuilder().setName("brand").setText("MyBrand")).addField(Field.newBuilder().setName("product").setText("coffee roaster")).addField(Field.newBuilder().setName("description").setText("A coffee bean roaster at home")).build();
    try {
        Utils.indexADocument(SEARCH_INDEX, document);
    } catch (InterruptedException e) {
    // ignore
    }
    PrintWriter out = resp.getWriter();
    Results<ScoredDocument> result = doSearch();
    for (ScoredDocument doc : result.getResults()) {
        out.println(doc.toString());
    }
}
Also used : ScoredDocument(com.google.appengine.api.search.ScoredDocument) Document(com.google.appengine.api.search.Document) ScoredDocument(com.google.appengine.api.search.ScoredDocument) PrintWriter(java.io.PrintWriter)

Example 4 with Document

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

the class DocumentServletTest method createDocument_withoutSignedIn.

@Test
public void createDocument_withoutSignedIn() throws Exception {
    helper.setEnvIsLoggedIn(false);
    Document doc = servletUnderTest.createDocument();
    assertThat(doc.getOnlyField("content").getText()).named("content").contains("the rain in spain");
    assertThat(doc.getOnlyField("email").getText()).named("email").isEmpty();
}
Also used : Document(com.google.appengine.api.search.Document) Test(org.junit.Test)

Example 5 with Document

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

the class UtilsTest method indexADocument_successfullyInvoked.

@Test
public void indexADocument_successfullyInvoked() throws Exception {
    String id = "test";
    Document doc = Document.newBuilder().setId(id).addField(Field.newBuilder().setName("f").setText("v")).build();
    Utils.indexADocument(INDEX, doc);
    // get the document by id
    IndexSpec indexSpec = IndexSpec.newBuilder().setName(INDEX).build();
    Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
    Document fetched = index.get(id);
    assertThat(fetched.getOnlyField("f").getText()).named("A value of the fetched document").isEqualTo("v");
}
Also used : IndexSpec(com.google.appengine.api.search.IndexSpec) Index(com.google.appengine.api.search.Index) Document(com.google.appengine.api.search.Document) Test(org.junit.Test)

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