为什么我的 Elasticsearch 集成测试有时只能工作
Why do my Elasticsearch integration tests only work sometimes
我有以下集成测试:
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()
偶尔只会 return 我的 testJson
。有时它会起作用,但大多数时候它只是 return 什么都没有。我怎样才能让我的集成测试每次都通过?
编辑:
有时有效和无效的查询:
client.prepareGet().setIndex(esIndex).setId(detailResponse.getId()).get();
client.prepareGet().setIndex(esIndex).setType(esDetailType).setRouting(esEntityType).setId(detailResponse.getId()).get();
这是一个竞争条件,可能是因为您没有在创建索引后调用 refresh(),而是依赖 ES 在 运行 搜索请求之前进行更新。
因为你的 class 扩展了 ESIntegTestCase 你应该能够做类似的事情
this.refresh(esIndex);
有关测试中此方法的更多信息,请参阅这些链接 API:
https://www.elastic.co/guide/en/elasticsearch/reference/current/integration-tests.html
https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java#L1186
这在主要(非测试)中也可用 Java API 像这样:
client.admin().indices().prepareRefresh(esIndex).get();
有关 Java API 可用性的更多信息,请参见此处:
https://static.javadoc.io/org.elasticsearch/elasticsearch/2.3.0/org/elasticsearch/client/IndicesAdminClient.html#refresh(org.elasticsearch.action.admin.indices.refresh.RefreshRequest)
编辑:
我上面提供的第一个片段仅适用于 Elasticsearch 5.0 Alpha。在当前版本中,刷新方法不接受参数:
https://github.com/elastic/elasticsearch/blob/v2.4.0/core/src/test/java/org/elasticsearch/test/ESIntegTestCase.java
我有以下集成测试:
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()
偶尔只会 return 我的 testJson
。有时它会起作用,但大多数时候它只是 return 什么都没有。我怎样才能让我的集成测试每次都通过?
编辑: 有时有效和无效的查询:
client.prepareGet().setIndex(esIndex).setId(detailResponse.getId()).get();
client.prepareGet().setIndex(esIndex).setType(esDetailType).setRouting(esEntityType).setId(detailResponse.getId()).get();
这是一个竞争条件,可能是因为您没有在创建索引后调用 refresh(),而是依赖 ES 在 运行 搜索请求之前进行更新。
因为你的 class 扩展了 ESIntegTestCase 你应该能够做类似的事情
this.refresh(esIndex);
有关测试中此方法的更多信息,请参阅这些链接 API: https://www.elastic.co/guide/en/elasticsearch/reference/current/integration-tests.html https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java#L1186
这在主要(非测试)中也可用 Java API 像这样:
client.admin().indices().prepareRefresh(esIndex).get();
有关 Java API 可用性的更多信息,请参见此处: https://static.javadoc.io/org.elasticsearch/elasticsearch/2.3.0/org/elasticsearch/client/IndicesAdminClient.html#refresh(org.elasticsearch.action.admin.indices.refresh.RefreshRequest)
编辑:
我上面提供的第一个片段仅适用于 Elasticsearch 5.0 Alpha。在当前版本中,刷新方法不接受参数: https://github.com/elastic/elasticsearch/blob/v2.4.0/core/src/test/java/org/elasticsearch/test/ESIntegTestCase.java