# Return number of hits in field

**URL:** <https://discuss.elastic.co/t/return-number-of-hits-in-field/286101>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [October 7, 2021, 7:35am UTC](https://discuss.elastic.co/t/return-number-of-hits-in-field/286101 "2021-10-07T07:35:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LaySoft](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/laysoft/32/65555_2.png) [@LaySoft](https://discuss.elastic.co/u/LaySoft)\
**Post date:** [October 7, 2021, 7:35am UTC](https://discuss.elastic.co/t/return-number-of-hits-in-field/286101/1 "2021-10-07T07:35:54Z")

</div>

Hi!

My mapping is:

```auto
PUT /test
{
    "mappings": {
        "properties": {
            "lufi": {
                "properties": {
                    "duma": {
                        "type": "keyword"
                    }
                }
            }
        }
    }
}

```

Insert some data:

```auto
POST /test/_doc/777
{
    "lufi": [
        {
            "duma": "aaa"
        },
        {
            "duma": "aaa bbb"
        },
        {
            "duma": "ccc"
        }
    ]
}

```

Search for documents which 'duma' fields contains 'aaa', and I'd like to return the number of hits in the 'duma' fields:

```auto
POST /test/_search
{
    "query": {
        "match": {
            "lufi.duma": "aaa"
        }
    },
    "script_fields": {
        "cnt": {
            "script": {
                "lang": "painless",
                "source": "doc['lufi.duma'].size()"
            }
        }
    }
}

```

The result is:

```auto
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.39556286,
        "hits": [
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "777",
                "_score": 0.39556286,
                "_source": [],
                "fields": {
                    "cnt": [
                        3
                    ]
                }
            }
        ]
    }
}

```

My problem is, the 'cnt' field contains 3, number of all 'duma' fields, but I need only the hits number, which is 2, because two duma fields ('aaa' and 'aaa bbb') values contains 'aaa', 'ccc' not. How can I solve this?

Thank you!

---

<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:** [October 20, 2021, 4:38pm UTC](https://discuss.elastic.co/t/return-number-of-hits-in-field/286101/2 "2021-10-20T16:38:15Z")

</div>

I'd do something like this:

```auto
DELETE /test
PUT /test
{
  "mappings": {
    "properties": {
      "lufi": {
        "type": "nested", 
        "properties": {
          "duma": {
            "type": "text"
          }
        }
      }
    }
  }
}
POST /test/_doc
{
  "lufi": [
    {
      "duma": "aaa"
    },
    {
      "duma": "aaa bbb"
    },
    {
      "duma": "ccc"
    }
  ]
}
GET /test/_search
{
  "query": {
    "nested": {
      "path": "lufi",
      "query": {
        "match": {
          "lufi.duma": "aaa"
        }
      },
      "inner_hits": {}
    }
  }
}

```

This gives:

```auto
{
  "took" : 26,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.45687002,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "4PSQnnwBoSW7dtUfnJcb",
        "_score" : 0.45687002,
        "_source" : {
          "lufi" : [
            {
              "duma" : "aaa"
            },
            {
              "duma" : "aaa bbb"
            },
            {
              "duma" : "ccc"
            }
          ]
        },
        "inner_hits" : {
          "lufi" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : 0.52354836,
              "hits" : [
                {
                  "_index" : "test",
                  "_type" : "_doc",
                  "_id" : "4PSQnnwBoSW7dtUfnJcb",
                  "_nested" : {
                    "field" : "lufi",
                    "offset" : 0
                  },
                  "_score" : 0.52354836,
                  "_source" : {
                    "duma" : "aaa"
                  }
                },
                {
                  "_index" : "test",
                  "_type" : "_doc",
                  "_id" : "4PSQnnwBoSW7dtUfnJcb",
                  "_nested" : {
                    "field" : "lufi",
                    "offset" : 1
                  },
                  "_score" : 0.39019167,
                  "_source" : {
                    "duma" : "aaa bbb"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

```

But that's may be not what you are after...

Another easier solution would be to index individual documents instead of an array of nested documents.

---

<div class="post-metadata">

**Author:** ![LaySoft](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/laysoft/32/65555_2.png) [@LaySoft](https://discuss.elastic.co/u/LaySoft)\
**Post date:** [October 21, 2021, 9:23am UTC](https://discuss.elastic.co/t/return-number-of-hits-in-field/286101/3 "2021-10-21T09:23:25Z")

</div>

Thank you, this works!

And how can i sort by this number? I tried something like this:

```auto
   "_script": {
        "script": "inner_hits.lufi.hits.total.value",
        "type": "number",
        "order": "desc"
    }

```

But it's not working ...

---

<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:** [October 21, 2021, 1:40pm UTC](https://discuss.elastic.co/t/return-number-of-hits-in-field/286101/4 "2021-10-21T13:40:28Z")

</div>

What do you want to sort? The full resultset? Or the inner hits?

If the former, I don't know if it's doable TBH.  
But may be there is another way to solve your use case. What is the use case?

---

<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:** [November 18, 2021, 1:41pm UTC](https://discuss.elastic.co/t/return-number-of-hits-in-field/286101/5 "2021-11-18T13:41:15Z")

</div>

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