【发布时间】:2016-09-06 18:59:10
【问题描述】:
我有以下集成测试:
public class TestElasticIT extends ESIntegTestCase {
private static final String esIndex = "test";
private static final String esEntityType = "entity";
private static final String esDetailType = "details";
private Client client = null;
@Before
public void beforeTests() throws Exception {
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject(esEntityType).endObject().endObject();
XContentBuilder xContentBuilder2 = XContentFactory.jsonBuilder().startObject().startObject(esDetailType).startObject("_parent").field("type", esEntityType).endObject().endObject().endObject();
client = ESIntegTestCase.client();
createIndex(esIndex);
client.admin().indices().preparePutMapping(esIndex).setType(esDetailType).setSource(xContentBuilder2).get();
client.admin().indices().preparePutMapping(esIndex).setType(esEntityType).setSource(xContentBuilder).get();
}
@Test
public void testCreateAndRead() throws Exception {
ensureGreen(esIndex);
IndexResponse entityResponse = client.prepareIndex(esIndex, esEntityType).setSource(testJson).get(); //This could be any key/value json
IndexResponse detailResponse = client.prepareIndex(esIndex, esDetailType).setSource(testJsonDetail).setParent(entityResponse.getId()).get();
//I want to see both parent and child here
System.out.println(client.prepareSearch().execute().actionGet());
}
}
我遇到的问题是client.prepareSearch().execute().actionGet() 只会偶尔返回我的testJson。有时它会起作用,但大多数时候它什么也没返回。如何让我的集成测试每次都能通过?
编辑: 有时有效和无效的查询:
client.prepareGet().setIndex(esIndex).setId(detailResponse.getId()).get();
client.prepareGet().setIndex(esIndex).setType(esDetailType).setRouting(esEntityType).setId(detailResponse.getId()).get();
【问题讨论】:
-
新条目被解析和索引时会有延迟。我认为您的查询在该过程完成之前正在运行。
-
如果手动查询索引,数据会显示出来吗?
-
我不确定您要搜索什么。您应该提供搜索参数以便搜索。如果你想检查你的索引是否成功,你可以使用 isCreated 方法。如果要对数据库执行搜索操作,请提供要搜索的请求参数。
-
所以我看到的是 isCreated 方法将返回 true。即使我在 println 上设置一个断点并手动执行搜索,它也不会返回我的 json。
-
我认为这可能与 ES 提供的随机测试有关。或者我没有正确设置客户端。
标签: java elasticsearch junit integration-testing