Ascalonian
Ascalonian

Reputation: 15203

Jest getSourceAsObject always returns NULL

I am trying several examples from Jest to use as a POC for ElasticSearch integration.

Right now, I am trying just a basic GET. I created a POJO called Document. In there are some basic setters and getters are some fields. I populate it and then use GSON to generate the JSON text.

From this generated JSON, I go into ElasticSearch Sense and do the following:

PUT /reports/documents/3
{
    // JSON code
}

This generates just fine. I then try using Get to pull the values out from Java, like so:

JestClientFactory factory = new JestClientFactory();

factory.setHttpClientConfig(new HttpClientConfig
                            .Builder("http://localhost:9200")
                            .multiThreaded(true)
                            .build());

client = factory.getObject();

Get get = new Get.Builder("reports", "3").type("documents").build();

try {
    JestResult result = client.execute(get);
    String json = result.getJsonString();
    System.out.println(json);

    Document doc = null; 
    doc = result.getSourceAsObject(Document.class);
    System.out.println("is doc null? " + doc == null);
}catch (Exception e) {
    System.err.println("Error getting document");
    e.printStackTrace();
}

The String json returns what I would expect (showing _index, _type, _id and of course _source). However, doc always comes out as NULL. I am not sure why that is happening.

Just to see if this was just a Get problem, I proceeded to try to Search.I did the following code snippet:

try {
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchQuery("reportNumber", "101221895CRT-004"));

    Search search = new Search.Builder(searchSourceBuilder.toString())
                                        // multiple index or types can be added.
                                        .addIndex("reports")
                                        .addType("documents")
                                        .build();

    SearchResult result = client.execute(search);

    //List<Document> results = result.getSourceAsObjectList(Document.class);

    List<SearchResult.Hit<Document, Void>> hits = result.getHits(Document.class);

    for (SearchResult.Hit hit : hits) {
        Document source = (Document) hit.source;
        Void ex = (Void) hit.explanation;

        System.out.println();
    }

    System.out.println("Result size: " + hits.size());
}catch (Exception e) {
    System.err.println("Error searching");
    e.printStackTrace();
}

When looking at result, the JSON of the object is shown. However, the List<Document> results comes out as NULL. When using hits, the size of hits is correct, but the "source" and "ex" are both NULL.

Any ideas on what I am doing wrong with this?

UPDATE
After reading Cihat's comment, I went ahead and added in logging. It turns out I am getting an error when trying to convert a date (hence why it's always coming back as NULL).

I get the following error message:

Unhandled exception occurred while converting source to the object .com.someCompanyName.data.Document
com.google.gson.JsonSyntaxException: java.text.ParseException: Unparseable date: "Nov 6, 2014 8:29:00 AM"

I have tried all different formats:

All of those failed. I am sure I tried some other formats, so not sure what format the date should be in. I even tried the exact date from DateFormat JavaDocs and it still failed. Every time I do a search, it says to define the Dateformat in the GsonBuilder, but in Jest I do not have access to that.

Upvotes: 4

Views: 2909

Answers (1)

Christian Trimble
Christian Trimble

Reputation: 2136

This test case demonstrates indexing a document with Jest and then getting the same document back out. Not a complete answer, but hopefully it is useful to see something that is known to work.

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Get;
import io.searchbox.core.Index;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
import org.junit.Test;

public class JestRoundtripIT {

  public static final String INDEX = "reports";
  public static final String TYPE = "documents";
  public static final String ID = "3";

  @Test
  public void documentRoundTrip() throws Exception {
    JestClientFactory factory = new JestClientFactory();

    factory.setHttpClientConfig(new HttpClientConfig
                                .Builder("http://localhost:9200")
                                .multiThreaded(true)
                                .build());

    JestClient client = factory.getObject();

    Document original = new Document()
      .withAuthor("Shay Banon")
      .withContent("You know, for search...");

    JestResult indexResult = client.execute(
      new Index.Builder(original)
        .index(INDEX)
        .type(TYPE)
        .id(ID)
        .build());
    assertThat(indexResult.isSucceeded(), equalTo(true));

    JestResult getResult = client.execute(
      new Get.Builder(INDEX, ID)
      .type(TYPE)
      .build());
    assertThat(getResult.isSucceeded(), equalTo(true));

    Document fromEs = getResult.getSourceAsObject(Document.class);

    assertThat(fromEs, notNullValue());
    assertThat(fromEs.getAuthor(), equalTo(original.getAuthor()));
    assertThat(fromEs.getContent(), equalTo(original.getContent()));
  }

  public static class Document {
    protected String author;
    protected String content;
    public Document withAuthor( String author ) {
      this.author = author;
      return this;
    }

    public Document withContent( String content ) {
      this.content = content;
      return this;
    }

    public String getAuthor() {
      return author;
    }

    public void setAuthor( String author ) {
      this.author = author;
    }

    public String getContent() {
      return content;
    }

    public void setContent( String content ) {
      this.content = content;
    }
  }
}

Upvotes: 2

Related Questions