Return number of hits in field

Hi!

My mapping is:

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

Insert some data:

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:

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

The result is:

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

I'd do something like this:

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:

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

Thank you, this works!

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

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

But it's not working ...

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?

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