public void runExample() {

    LoadExampleData.load();

    List<Customer> list2 = Customer.find.query().select("name").fetch("contacts").findList();

    PathProperties pathProperties =
        PathProperties.parse("(id,version,name,contacts(id,email,version))");

    Query<Customer> query = Customer.find.query();
    pathProperties.apply(query);

    List<Customer> list = query.findList();

    JsonContext jsonContext = Customer.find.db().json();

    String jsonString = jsonContext.toJson(list);
    System.out.println(jsonString);

    for (Customer customer : list2) {
      customer.getName();
      List<Contact> contacts = customer.getContacts();
      if (contacts != null) {
        for (Contact contact : contacts) {
          System.out.println("contact: " + contact.getFirstName() + " " + contact.getLastName());
        }
      }
    }
  }
Example #2
0
  @Ignore
  @Test
  public void testBulkUpdate() throws IOException {

    ResetBasicData.reset();

    EbeanServer server = Ebean.getServer(null);
    JsonContext jsonContext = server.json();

    StringWriter writer = new StringWriter();
    JsonGenerator generator = jsonContext.createGenerator(writer);

    List<Customer> customers = Ebean.find(Customer.class).findList();

    for (Customer customer : customers) {

      customer.setName(customer.getName() + "esMod");
      PathProperties updateProps = PathProperties.parse("name");

      generator.writeStartObject();
      generator.writeFieldName("update");
      generator.writeStartObject();
      generator.writeStringField("_id", customer.getId().toString());
      generator.writeStringField("_type", "customer");
      generator.writeStringField("_index", "customer");
      generator.writeEndObject();
      generator.writeEndObject();
      generator.writeRaw("\n");

      generator.writeStartObject();
      generator.writeFieldName("doc");
      jsonContext.toJson(customer, generator, updateProps);
      generator.writeEndObject();
      generator.writeRaw("\n");
    }

    generator.close();
    String json = writer.toString();

    post("http://localhost:9200/_bulk", json);

    // curl -s -XPOST localhost:9200/_bulk

    //    { "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1"} }
    //    { "doc" : {"field2" : "value2"} }

  }
Example #3
0
  @Ignore
  @Test
  public void test() throws IOException {

    ResetBasicData.reset();

    EbeanServer server = Ebean.getServer(null);
    JsonContext jsonContext = server.json();

    List<Customer> customers = Ebean.find(Customer.class).findList();

    PathProperties paths = PathProperties.parse("name, status, anniversary");
    for (Customer customer : customers) {

      String json = jsonContext.toJson(customer, paths);
      put("http://localhost:9200/customer/customer/" + customer.getId(), json);
    }
  }