Elasticsarch Java API doesn't return same as direct call


(Iurii Sergiichuk) #1

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:

{
  "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:

{
  "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:

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):

{
  "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:

{
  "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 ?


(Christoph) #2

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


(Iurii Sergiichuk) #3

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:

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

(David Pilato) #4

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.


(system) #5

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