我正在尝试将Jest中的几个示例用作ElasticSearch集成的POC。
现在,我正在尝试一个基本的GET。我创建了一个名为Document的POJO。在一些基本的制定者和吸气者是一些领域。我填充它,然后使用GSON生成JSON文本。
从这个生成的JSON中,我进入ElasticSearch Sense并执行以下操作:
PUT /reports/documents/3
{
// JSON code
}
这很好。然后我尝试使用Get
从Java中提取值,如下所示:
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();
}
字符串json
返回我期望的内容(显示_index,_type,_id,当然还有_source)。但是,doc
始终为NULL。我不确定为什么会这样。
为了看看这是否只是一个Get问题,我继续尝试搜索。我做了以下代码片段:
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();
}
查看result
时,会显示对象的JSON。但是,List<Document> results
表示为NULL。使用hits
时,匹配的大小是正确的,但是&#34;来源&#34;和&#34;前&#34;都是NULL。
关于我做错了什么的任何想法?
更新
在阅读了Cihat的评论之后,我继续进行了记录。事实证明我在尝试转换日期时遇到错误(因此它为什么总是以NULL返回)。
我收到以下错误消息:
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"
我尝试了所有不同的格式:
所有这些都失败了。我确定我尝试了其他一些格式,所以不确定日期应该是什么格式。我甚至尝试了DateFormat
JavaDocs的确切日期,但它仍然失败了。每次我进行搜索时,它都会在GsonBuilder中定义Dateformat,但是在Jest中我无法访问它。
答案 0 :(得分:0)
此测试用例演示了如何使用Jest索引文档,然后将相同的文档取回。不是一个完整的答案,但希望看到已知可行的东西是有用的。
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;
}
}
}