我遇到以下问题:当我执行此命令时
curl -XGET "localhost:9200/customer/external/_search?pretty" -d @json.txt
json.txt 看起来像这样:
{ "query":{ "match":{ "_id":"1" } } }
我得到以下输出(我缩短了它):
{
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [ {
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_score" : 1.0,
"_source":{
"name":"Jan",
"age":99,
"address":{
"city":"KKKKKKK",
"zip":"xxxx"
}
}
} ]
}
现在我尝试使用 Java API 做同样的事情,但我就是无法完成它(我尝试了 8 种不同的方法)。我的点击率总是 0。在代码中,我现在将源文件直接设置为字符串作为源,但正如您所看到的,我也尝试使用 XContentBuilder
和 WrapperQueryBuilder
版本,但没有任何效果。这是我的代码:
public void processQuery(String filePath, String index, String... types) {
String source = convertFileToString(filePath);
//XContentBuilder query = null;
//try {
// JSONObject json = new JSONObject(source.trim());
// query = convertJsonToXContentBuilder(json);
//} catch (...) {...}
//WrapperQueryBuilder query = QueryBuilders.wrapperQuery(source);
SearchResponse response = client.prepareSearch(index)
.setSource(source)
// .setQuery(query)
.setTypes(types)
.get();
}
作为回应,我根本没有得到任何点击:
{
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
我希望任何人都知道这个问题,因为我已经厌倦了寻找解决方案:/
最诚挚的问候, 一月
最佳答案
对新的“答案”感到抱歉,但我无法编辑我的问题。所以我想通了,但我现在不知道如何进展。当我在 Debug模式下运行时,它工作正常(我在 JUnit
测试中使用它)。所以我想也许我太早关闭了连接,所以我添加了 Thread.sleep(5000) ,但它仍然失败。它仅在 Debug模式下工作:/如果它很重要,我将使用 TransportClient
。
有点尴尬,但我希望有人知道原因......
干杯, 一月
编辑:
问题解决了:由于我在单元测试中使用它,并且每次都清空并重新加载数据库的内容,所以在执行请求时内容尚未准备好。我现在正在等待状态变为绿色,然后开始测试。
关于Elasticsearch:匹配查询返回错误结果(Java API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29079000/