# Elasticsarch Java API doesn't return same as direct call

**URL:** <https://discuss.elastic.co/t/elasticsarch-java-api-doesnt-return-same-as-direct-call/65799>\
**Category:** Elasticsearch\
**Created:** [November 11, 2016, 12:09pm UTC](https://discuss.elastic.co/t/elasticsarch-java-api-doesnt-return-same-as-direct-call/65799 "2016-11-11T12:09:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![IuriiSergiichuk](https://avatars.discourse-cdn.com/v4/letter/i/e68b1a/32.png) [@IuriiSergiichuk](https://discuss.elastic.co/u/IuriiSergiichuk)\
**Post date:** [November 11, 2016, 12:09pm UTC](https://discuss.elastic.co/t/elasticsarch-java-api-doesnt-return-same-as-direct-call/65799/1 "2016-11-11T12:09:48Z")

</div>

Hi all, I'm trying to explore elasticsearch and it's Java API (I'm using 5.0 version) and right now have following problem.  
I've written query, that should filter docs by action and range:

```auto
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "analyze_wildcard": true,
            "query": "action:LOGIN"
          }
        },
        {
          "range": {
            "@timestamp": {
              "gte": 1478815200000,
              "lte": 1478901599999,
              "format": "epoch_millis"
            }
          }
        }
      ]
    }
  }
}

```

It's execution result with GET /\_search:

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1027,
    "max_score": 0,
    "hits": []
  }
}

```

I've re-written it with Java:

```auto
Client client = ElasticsearchUtils.client();
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(Constants.EVENT_INDEX)
        .setTypes(Constants.EVENT_TYPE)
        .setSearchType(SearchType.QUERY_AND_FETCH)
        .setSize(0)
        .setQuery(
                QueryBuilders.boolQuery()
                        .must(
                                QueryBuilders.queryStringQuery("action:LOGIN")
                        ).must(
                        QueryBuilders.rangeQuery("@timestamp")
                                .format("epoch_millis")
                                .gte(1478815200000L)
                                .lte(1478901599999L)
                )
        );
SearchRequest request = searchRequestBuilder.request();
System.out.println(request.toString());
SearchResponse sr = searchRequestBuilder.execute().actionGet();

```

But it says, that there's no hits for such query.  
As you can see, I've tried to print its request and get following request query (as I understand - it's what actually will be executed):

```auto
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "action:LOGIN",
            "fields": [],
            "use_dis_max": true,
            "tie_breaker": 0,
            "default_operator": "or",
            "auto_generate_phrase_queries": false,
            "max_determined_states": 10000,
            "lowercase_expanded_terms": true,
            "enable_position_increment": true,
            "fuzziness": "AUTO",
            "fuzzy_prefix_length": 0,
            "fuzzy_max_expansions": 50,
            "phrase_slop": 0,
            "locale": "und",
            "escape": false,
            "boost": 1
          }
        },
        {
          "range": {
            "@timestamp": {
              "from": 1478815200000,
              "to": 1478901599999,
              "include_lower": true,
              "include_upper": true,
              "format": "epoch_millis",
              "boost": 1
            }
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

```

And in Kibana console it do return same result as my own query:

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1027,
    "max_score": 0,
    "hits": []
  }
}

```

Could anyone help me with that ?

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 14, 2016, 9:16pm UTC](https://discuss.elastic.co/t/elasticsarch-java-api-doesnt-return-same-as-direct-call/65799/2 "2016-11-14T21:16:41Z")

</div>

> [@IuriiSergiichuk](#):
>
> But it says, that there's no hits for such query.

How do you check that there is no hits in your SearchResponse?

---

<div class="post-metadata">

**Author:** ![IuriiSergiichuk](https://avatars.discourse-cdn.com/v4/letter/i/e68b1a/32.png) [@IuriiSergiichuk](https://discuss.elastic.co/u/IuriiSergiichuk)\
**Post date:** [November 15, 2016, 7:41am UTC](https://discuss.elastic.co/t/elasticsarch-java-api-doesnt-return-same-as-direct-call/65799/3 "2016-11-15T07:41:16Z")

</div>

Hi, I've already tried to remove this topic but it was restored by someone from elastic.

I've already found the answer and it was pretty obvious - misprint, there was a misprint in index name, so I haven't received any results.

And if anyone will help it, I've checked hits amount with:

```auto
.......
SearchResponse sr = searchRequestBuilder.execute().actionGet();
sr.getHits().getTotalHits();

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 15, 2016, 8:03am UTC](https://discuss.elastic.co/t/elasticsarch-java-api-doesnt-return-same-as-direct-call/65799/4 "2016-11-15T08:03:10Z")

</div>

> [@IuriiSergiichuk](#):
>
> Hi, I've already tried to remove this topic but it was restored by someone from elastic.

Yes. That was me.

I think it's better to answer your own thread with your findings and mark it as resolved (as I just did) so any future reader can fix its similar concern using your solution.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 13, 2016, 8:03am UTC](https://discuss.elastic.co/t/elasticsarch-java-api-doesnt-return-same-as-direct-call/65799/5 "2016-12-13T08:03:13Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
