如何细分用户

时间:2016-09-19 21:46:31

标签: elasticsearch

如果我在弹性搜索中索引了用户属性,并且我想以某种方式向用户添加角色,例如:

roles: employee, manager, surpervisor

用户可以属于一个或多个角色。

然后我可以搜索属于角色"员工"或者"经理,主管"?

如果有500K或100万用户,这会是一个糟糕的设计吗?

1 个答案:

答案 0 :(得分:1)

是的,您可以向同一字段添加多个值。默认情况下,Elasticsearch中的每个字段都是“多值”的。这意味着你可以索引像这样的文件

文件1

<form>

文件2

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

文件3

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

现在假设您要搜索主管和经理的用户:

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

响应:

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

或者,如果您希望员工是主管和经理,或者是员工:

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