Jest getSourceAsObject always returns NULL

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

Right now, I'm trying to just create a GET. I created a POJO called Document. Some basic setters and getters have multiple fields. I fill it in and then use GSON to generate JSON text.

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

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

      

It's just great. Then I try to use Get

to pull values ​​from Java like:

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 expect (showing _index, _type, _id, and of course the source). However, it doc

always appears as NULL. I don't know why this is happening.

To see if this is just a Get problem, I tried searching. I made the following piece of code:

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 viewed result

, the JSON of the object is displayed. However, it List<Document> results

is outputted as NULL. When used, hits

the call size is correct, but "source" and "ex" are NULL.

Any ideas on what I am doing wrong with this?

UPDATE
After reading Chiat's comment, I went ahead and added to the log. It turns out I am getting an error when trying to convert a date (hence why it always returns as NULL).

The following error message appears:

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've tried all different formats:

  • 11/06/2014 8:29:00 AM (and no time and only 14 year)
  • 06-NOV-2014 8:29:00 AM (and no time and only 14 year)
  • 2014-11-06 8:29:00 AM (same with time and year changes)
  • 2014-NOV-06 8:29:00 AM (same with time and year changes)
  • 06/11/2014 8:29:00 AM (the same)

All these failures. I'm sure I've tried some other formats, so I'm 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 don't have access to that.

+3


source to share


1 answer


This test case demonstrates indexing a document using Jest and then returning the same document. Not a complete answer, but hopefully helpful 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;
    }
  }
}

      

0


source







All Articles