2

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

1
  • maybe I should add that the source string looks correct when I print it { "query":{ "match":{ "_id":"1" } } }. And I pass as index "customer" and types "external". Commented Mar 16, 2015 at 14:22

2 Answers 2

2

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

Edit:

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Edit: It also only works when I step through in debug (I have to place the breakpoint before processQuery() is called). Otherwise it fails. But I don't have to step into the method.
0

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

1 Comment

Well I'm only using java API, this was just for testing so it does not matter for me :) and what do you mean by "all results"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.