Reputation: 337
I have the following problem: When I execute this command
curl -XGET "localhost:9200/customer/external/_search?pretty" -d @json.txt
Where json.txt looks like this:
{ "query":{ "match":{ "_id":"1" } } }
I get the following output (I shortened it):
{
"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"
}
}
} ]
}
Now I'm trying to do the same using the Java API but I just cannot manage to get it done (I tried like 8 different approches). I get always 0 hits. In the code I now set the source file as a string directly as a source but as you can see I also tried using the XContentBuilder
and WrapperQueryBuilder
versions but nothing worked. Here's my code:
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();
}
As a response I dont get any hits at all:
{
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
I hope anyone has an idea about this cause I'm already sick of trying to find a solution :/
Best regardes, Jan
Upvotes: 2
Views: 920
Reputation: 337
Sry for the new "answer" but I cannot edit my question. So I figured smth out but I don't know how to progress now. When I run in debug mode it works fine (I'm using it in a JUnit
test). So I thought maybe I'm closing the connection too early so I added a Thread.sleep(5000)
but it still fails. It only works in debug mode :/ If its important I'm using the TransportClient
.
It's a bit awkward but I hope someone might know the cause...
Cheers, Jan
the problem is solved: Since I was using this in unit tests and I emptied and reloaded the content of the DB everytime, the content wasn't ready yet when the request was executed. I'm waiting now for the status to be green and then start the tests.
Upvotes: 2
Reputation: 19253
You should use POST to search. Using GET will ignore the search body and gives you back all results.
curl -XPOST "localhost:9200/customer/external/_search?pretty" -d @json.txt
Upvotes: 0