# MultiSearchResponse.Item is not giving expected result

**URL:** https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627
**Category:** Elasticsearch
**Created:** [March 16, 2019, 7:41am UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627 "2019-03-16T07:41:37Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![srikanth\_muddu](https://avatars.discourse-cdn.com/v4/letter/s/9e8a1a/32.png) [@srikanth\_muddu](https://discuss.elastic.co/u/srikanth_muddu)
#### Post date: [March 16, 2019, 7:41am UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/1 "2019-03-16T07:41:37Z")

</div>

Hi,  
I have a total of 10000 docs in my index A. I am trying to access response hits in my custom plugin class to modify my output for a use case.  
When I am using the below for loop statement, response hits are only 9945.  
My expected hits should be 10000.  
`for(MultiSearchResponse.Item response : items)`  
Attaching a

 ![HitsSize](https://us1.discourse-cdn.com/elastic/original/3X/9/b/9b70d4bc41189ed4d067af276452ef45d46327fd.png) screenshot of response.  
Please help.

---

<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: [March 16, 2019, 11:22am UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/2 "2019-03-16T11:22:47Z")

</div>

Just a guess because you're not telling a lot about the context.

Are you running this in an integration test?  
And you indexed 10k docs?  
And just after run the search?  
But you did not call refresh before searching?

If I'm wrong then share what the query is.

---

<div class="post-metadata">

### Author: ![srikanth\_muddu](https://avatars.discourse-cdn.com/v4/letter/s/9e8a1a/32.png) [@srikanth\_muddu](https://discuss.elastic.co/u/srikanth_muddu)
#### Post date: [March 18, 2019, 5:54am UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/3 "2019-03-18T05:54:05Z")

</div>

I am using MultiSearchResponse in my custom plugin class to perform/modify some output where ever certain query is matched.  
I have 10000 docs. Static index.  
Ex:  
`{ "query": { "bool" : { "must" : { "term" : { "pname" : "apple" } } } } }`

---

<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: [March 18, 2019, 8:27am UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/4 "2019-03-18T08:27:40Z")

</div>

That doesn't answer my questions.

Anyway what is the output of

```
GET _search?size=0

```

In Kibana console?

---

<div class="post-metadata">

### Author: ![srikanth\_muddu](https://avatars.discourse-cdn.com/v4/letter/s/9e8a1a/32.png) [@srikanth\_muddu](https://discuss.elastic.co/u/srikanth_muddu)
#### Post date: [March 18, 2019, 9:43am UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/5 "2019-03-18T09:43:30Z")

</div>

I executed the query

```
     curl -XGET localhost:9208/querytest/_search?size=0

```

Result:

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

While debug also I face the same issue with totalHits and InternalSearchHit.  
Attaching snapshot.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/4/241d6346ab733029baf505c94000dab814379d0e.png)

Is this info relate for the question? please correct me if I'm wrong

---

<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: [March 18, 2019, 10:38am UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/6 "2019-03-18T10:38:35Z")

</div>

Here is the original image you posted:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/0/5037abf70d9f3f83184d6ad26758dde150c37455.jpeg)

The totalHits is `9945`, not `10000`.

I don't understand why you think there is an issue.

---

<div class="post-metadata">

### Author: ![srikanth\_muddu](https://avatars.discourse-cdn.com/v4/letter/s/9e8a1a/32.png) [@srikanth\_muddu](https://discuss.elastic.co/u/srikanth_muddu)
#### Post date: [March 18, 2019, 10:49am UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/7 "2019-03-18T10:49:43Z")

</div>

It's showing as 9945 but the total hits when I run through curl which I posted earlier  
gives totalhits as 10000

 ![image%20(1)](https://us1.discourse-cdn.com/elastic/original/3X/c/8/c8c70713d86c72f66e5b88dbb5c295803bcc7401.png)

In the below code :

```
SearchHit[] searchHits = response.getResponse().getHits().getHits();
    if(searchHits.length > 0){
      for(SearchHit hit : searchHits){
        Map<String, Object> sourceMap = hit.sourceAsMap();

```

I am left with 9945 hits instead of 10000 hits.

---

<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: [March 18, 2019, 10:53am UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/8 "2019-03-18T10:53:44Z")

</div>

At the moment you initially run the request, you had `9945` hits that matches your query. That's all what I can say.  
There is no bug here.

As I said, I believe that you did not refresh the index before running your first search.

But if you think there is an issue, please share the full code which can help to reproduce the problem.

---

<div class="post-metadata">

### Author: ![srikanth\_muddu](https://avatars.discourse-cdn.com/v4/letter/s/9e8a1a/32.png) [@srikanth\_muddu](https://discuss.elastic.co/u/srikanth_muddu)
#### Post date: [March 18, 2019, 11:05am UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/9 "2019-03-18T11:05:14Z")

</div>

```
curl -XGET localhost:9208/querytest/_search?size=0 -d '{"query": {
"bool" : {
  "must" : {
    "term" : { "nde" : "scs" }
  }
} }}'

```

Result:

```
{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":10000,"max_score":0.0,"hits":[]}}

```

> [@dadoonet](#):
>
> As I said, I believe that you did not refresh the index before running your first search.

I have created a new index, To confirm I have refreshed index with \_refresh with curl.  
Still i face same issue

---

<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: [March 18, 2019, 11:34am UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/10 "2019-03-18T11:34:38Z")

</div>

Sorry but what is the issue? I can't see it here. Could you explain again?

---

<div class="post-metadata">

### Author: ![srikanth\_muddu](https://avatars.discourse-cdn.com/v4/letter/s/9e8a1a/32.png) [@srikanth\_muddu](https://discuss.elastic.co/u/srikanth_muddu)
#### Post date: [March 18, 2019, 12:03pm UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/11 "2019-03-18T12:03:37Z")

</div>

I have an Index containing 10k docs, In every document the field `nde:scs` exists.  
When I search this query in kibana/curl, I get the output as expected which is 10000 hits.  
But when I try to access the hits through MultiSearchResponse Class in the code I am getting hits as only 9945 while debugging.

Kibana snapshot

 ![image%20(2)](https://us1.discourse-cdn.com/elastic/original/3X/7/a/7a3a995ada4600d686c5e15d03cea4bb27b78477.png)

Debug snapshot

 ![image%20(1)](https://us1.discourse-cdn.com/elastic/original/3X/c/8/c8c70713d86c72f66e5b88dbb5c295803bcc7401.png)

---

<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: [March 18, 2019, 1:03pm UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/12 "2019-03-18T13:03:30Z")

</div>

I see. Let me check.

---

<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: [March 18, 2019, 1:15pm UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/13 "2019-03-18T13:15:52Z")

</div>

So I created an index with 10000 records. Then I ran the following code:

```auto
SearchRequest searchRequest = new SearchRequest("test");
searchRequest.source(new SearchSourceBuilder().size(10000));
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

System.out.println("response size = " + searchResponse.getHits().getHits().length);

MultiSearchRequest request = new MultiSearchRequest().add(searchRequest);
MultiSearchResponse response = client.msearch(request, RequestOptions.DEFAULT);

System.out.println("response size = " + response.getResponses()[0].getResponse().getHits().getHits().length);

```

This is giving:

```auto
response size = 10000
response size = 10000

```

So I'm always getting the right number of records.

What is your version? I'm using 6.6.2.

---

<div class="post-metadata">

### Author: ![srikanth\_muddu](https://avatars.discourse-cdn.com/v4/letter/s/9e8a1a/32.png) [@srikanth\_muddu](https://discuss.elastic.co/u/srikanth_muddu)
#### Post date: [March 18, 2019, 2:14pm UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/14 "2019-03-18T14:14:55Z")

</div>

I am getting searchHits.length as 9945 but totalHits as 10000 can u help me understand.

 ![image%20(3)](https://us1.discourse-cdn.com/elastic/original/3X/2/2/2238fddbfd645a7cd7b33bf641de6336b6468ba4.png)

Code block which I'm using for response

```
   for(MultiSearchResponse.Item response : items) {
        if (response.getResponse() == null) {
            break;
        }
        SearchHit[] searchHits = response.getResponse().getHits().getHits();
        if(searchHits.length > 0){
            for(SearchHit hit : searchHits){
                sourceSecondMap = hit.sourceAsMap();
```

---

<div class="post-metadata">

### Author: ![srikanth\_muddu](https://avatars.discourse-cdn.com/v4/letter/s/9e8a1a/32.png) [@srikanth\_muddu](https://discuss.elastic.co/u/srikanth_muddu)
#### Post date: [March 18, 2019, 2:15pm UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/15 "2019-03-18T14:15:24Z")

</div>

> [@dadoonet](#):
>
> What is your version? I'm using 6.6.2.

Version 5.0.1

---

<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: [March 18, 2019, 3:02pm UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/16 "2019-03-18T15:02:02Z")

</div>

Then upgrade may be. That version is sooooo old.

> [@srikanth\_muddu](#):
>
> I am getting searchHits.length as 9945 but totalHits as 10000 can u help me understand.

I can if you share a full example as I did.  
If you don't share more, it's hard to help.

---

<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: [April 15, 2019, 3:06pm UTC](https://discuss.elastic.co/t/multisearchresponse-item-is-not-giving-expected-result/172627/17 "2019-04-15T15:06:17Z")

</div>

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