Java Code Examples for com.google.appengine.api.search.Document#newBuilder()

The following examples show how to use com.google.appengine.api.search.Document#newBuilder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SearchServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
public void addDocument(Index index, String docid) {
  Document.Builder builder = Document.newBuilder();
  builder.setId(docid);
  builder.setRank(docid.hashCode());

  Field.Builder field = Field.newBuilder();
  field.setName("title");
  field.setText(String.format("Title: title%%<%s>", docid));
  builder.addField(field);

  field = Field.newBuilder();
  field.setName("body");
  field.setHTML(String.format("<h3>body of %s, some string</h3>", docid));
  builder.addField(field);

  field = Field.newBuilder();
  field.setName("atom");
  field.setAtom(String.format("atom%% <%s>", docid));
  builder.addField(field);

  field = Field.newBuilder();
  field.setName("number");
  field.setNumber(docid.hashCode() % 4096);
  builder.addField(field);

  field = Field.newBuilder();
  field.setName("date");
  field.setDate(new Date(2011 - 1900, 11 - 1, (docid.hashCode() % 30) + 1));
  builder.addField(field);

  index.put(builder.build());
}
 
Example 2
Source File: SearchTestBase.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
protected void addDocs(Index index, int docCount) throws ParseException, InterruptedException {
    if (searchDocs(index, "", 0).getNumberFound() == 0) {
        List<Document> documents = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        DateFormat dfDate = new SimpleDateFormat("yyyy,M,d");
        for (int i = 0; i < docCount; i++) {
            Builder docBuilder = Document.newBuilder();
            // two text field with different locale
            docBuilder.addField(Field.newBuilder().setName("textfield").setText("text with num " + i));
            Field field = Field.newBuilder().setName("textfield").setText("C'est la vie " + i).setLocale(Locale.FRENCH).build();
            docBuilder.addField(field);
            docBuilder.addField(Field.newBuilder().setName("numfield").setNumber(i));
            String dateVal = "" + cal.get(Calendar.YEAR) + ",";
            dateVal += cal.get(Calendar.MONTH) + ",";
            int day = cal.get(Calendar.DATE) + i;
            dateVal += day;
            docBuilder.addField(Field.newBuilder().setName("datefield").setDate(dfDate.parse(dateVal)));
            docBuilder.addField(Field.newBuilder().setName("htmlfield").setHTML("<B>html</B> " + i));
            docBuilder.addField(Field.newBuilder().setName("atomfield").setAtom("atom" + i + ".com"));
            GeoPoint geoPoint = new GeoPoint((double) i, (double) (100 + i));
            docBuilder.addField(Field.newBuilder().setName("geofield").setGeoPoint(geoPoint));
            // two field in same name and with different field type
            docBuilder.addField(Field.newBuilder().setName("mixfield").setText("text and number mix field"));
            docBuilder.addField(Field.newBuilder().setName("mixfield").setNumber(987));
            docBuilder.setId("selfid" + i);
            // only doc(id="selfid0") has "cn" locale, others have "en" locale
            if (i == 0) {
                docBuilder.setLocale(new Locale("cn"));
            } else {
                docBuilder.setLocale(new Locale("en"));
            }
            documents.add(docBuilder.build());
        }
        index.put(documents);
        sync();
    }
}
 
Example 3
Source File: DocumentTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testMultipleNumField() {
    Builder docBuilder = Document.newBuilder();
    docBuilder.addField(Field.newBuilder().setName("numfield").setNumber(123));
    docBuilder.addField(Field.newBuilder().setName("numfield").setNumber(789));
}
 
Example 4
Source File: DocumentTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testMultipleDateField() {
    Builder docBuilder = Document.newBuilder();
    docBuilder.addField(Field.newBuilder().setName("datefield").setDate(new Date()));
    docBuilder.addField(Field.newBuilder().setName("datefield").setDate(new Date()));
}
 
Example 5
Source File: FieldTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testDocFields() throws Exception {
    String indexName = "test-doc-fields";
    Index index = searchService.getIndex(IndexSpec.newBuilder().setName(indexName));
    delDocs(index);

    Builder docBuilder = Document.newBuilder();
    Field field = Field.newBuilder().setName("textfield").setText("text field").build();
    docBuilder.addField(field);
    field = Field.newBuilder().setName("numberfield").setNumber(123).build();
    docBuilder.addField(field);
    Date now = new Date();
    field = Field.newBuilder().setName("datefield").setDate(now).build();
    docBuilder.addField(field);
    field = Field.newBuilder().setName("htmlfield").setHTML("<html>html field</html>").build();
    docBuilder.addField(field);
    User currentUser = new User("[email protected]", "appenginetest.com");
    field = Field.newBuilder().setName("atomfield").setAtom(currentUser.getAuthDomain()).build();
    docBuilder.addField(field);
    GeoPoint geoPoint = new GeoPoint((double) -10, 10.000001);
    field = Field.newBuilder().setName("geofield").setGeoPoint(geoPoint).build();
    docBuilder.addField(field);
    index.put(docBuilder);
    sync();

    Results<ScoredDocument> result = searchDocs(index, "", 0);
    assertEquals(1, result.getNumberReturned());
    ScoredDocument doc = result.iterator().next();
    Field retField = doc.getOnlyField("textfield");
    assertEquals(FieldType.TEXT, retField.getType());
    assertEquals("textfield", retField.getName());
    assertEquals("text field", retField.getText());

    retField = doc.getOnlyField("numberfield");
    assertEquals(FieldType.NUMBER, retField.getType());
    assertEquals(new Double("123"), retField.getNumber());

    retField = doc.getOnlyField("datefield");
    assertEquals(FieldType.DATE, retField.getType());
    assertEquals(now, retField.getDate());

    retField = doc.getOnlyField("htmlfield");
    assertEquals(FieldType.HTML, retField.getType());
    assertEquals("<html>html field</html>", retField.getHTML());

    retField = doc.getOnlyField("atomfield");
    assertEquals(FieldType.ATOM, retField.getType());
    assertEquals(currentUser.getAuthDomain(), retField.getAtom());

    retField = doc.getOnlyField("geofield");
    assertEquals(FieldType.GEO_POINT, retField.getType());
    assertEquals(-10, retField.getGeoPoint().getLatitude(), 0);
    assertEquals(10.000001, retField.getGeoPoint().getLongitude(), 0.000000);
}