Get properties when search text

example: {"name":"john", "attribute": [1,2,3]}, {"name":"Dr john", "attibute":[2,3,4]},{"name":"david john", "attibute":[4,6,7]}.
How to search "john" return array attribute [1,2,3,4,6,7]

You can use the terms aggregation for that. For example:

GET my_index/_search
{
  "query": {
    "match": {
      "name": "john"
    }
  },
  "aggs": {
    "common_attributes": {
      "terms": {
        "field": "attribute",
        "size": 10,
        "order": {
          "_key": "asc"
        }
      }
    }
  }
}

By default, it returns the 10 most common values, but you can add an order clause (as in the example above) to sort on for example the key (which represents the values of the attribute field).

No, i want list all attribute

You mean all instead of the top 10 most common values? In that case, you can use the composite aggregation:

GET my_index/_search
{
  "size": 0,
  "query": {
    "match": {
      "name": "john"
    }
  },
  "aggs": {
    "all_attributes": {
      "composite": {
        "sources": [
          {
            "attribute": {
              "terms": {
                "field": "attribute"
              }
            }
          }
        ]
      }
    }
  }
}
2 Likes

ok, thanks :smile:

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