Get properties when search text


(Tung Do) #1

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]


(Abdon Pijpelink) #2

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


(Tung Do) #3

No, i want list all attribute


(Abdon Pijpelink) #4

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"
              }
            }
          }
        ]
      }
    }
  }
}

(Tung Do) #5

ok, thanks :smile:


(system) #7

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