cool breeze
cool breeze

Reputation: 4811

How to segment users

If I have a user's properties indexed in elastic search, and I want to somehow add roles to a user like:

roles: employee, manager, surpervisor

A user can belong to 1 or more roles.

Could I then search for users who belong to role "employee" or "manager, supervisor"?

If there are 500K or 1 million users, will this be a bad design?

Upvotes: 0

Views: 71

Answers (1)

jay
jay

Reputation: 2077

Yes you can add multiple values to the same field. Every field in Elasticsearch is "multivalued" by default. Which means you can index documents like this

Document 1

curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch",
"roles": ["manager", "employee", "supervisor"] }'

Document 2

 curl -XPUT 'http://localhost:9200/twitter/tweet/2' -d '{
"user" : "cool breeze",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch",
"roles": ["manager", "supervisor"]}'

Document 3

curl -XPUT 'http://localhost:9200/twitter/tweet/3' -d '{
"user" : "jay",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch",
"roles": ["employee"] }'

Now lets say you want to search users who are supervisor AND managers:

curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d '{
    "query": {
        "bool": {
            "should": [{
                "match": {
                    "roles": {
                        "query": "supervisor manager",
                        "operator" : "and"
                    }
                }
            }]
        }
    }}'

Response:

    {
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.2712221,
    "hits": [
      {
        "_index": "twitter",
        "_type": "tweet",
        "_id": "2",
        "_score": 0.2712221,
        "_source": {
          "user": "cool breeze",
          "post_date": "2009-11-15T14:12:12",
          "message": "trying out Elasticsearch",
          "roles": [
            "manager",
            "supervisor"
          ]
        }
      },
      {
        "_index": "twitter",
        "_type": "tweet",
        "_id": "1",
        "_score": 0.2169777,
        "_source": {
          "user": "kimchy",
          "post_date": "2009-11-15T14:12:12",
          "message": "trying out Elasticsearch",
          "roles": [
            "manager",
            "employee",
            "supervisor"
          ]
        }
      }
    ]
  }}

Or if you want employees who are supervisor AND manager, OR employee:

curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d '{
    "query": {
        "bool": {
            "should": [{
                "match": {
                    "roles": {
                        "query": "supervisor manager",
                        "operator" : "and"
                    }
                }
            },
            {
              "match": {
                  "roles": {
                    "query" : "employee"
                  }
              }
          }]
        }
    } }'

Upvotes: 1

Related Questions