0
如果我有彈性的搜索索引的用戶的屬性,我想以某種方式角色添加到用戶,如:如何細分用戶
roles: employee, manager, surpervisor
用戶可以屬於1個或多個角色。
然後我可以搜索屬於角色「僱員」或「經理,主管」的用戶嗎?
如果有50萬或100萬的用戶,這會是一個糟糕的設計嗎?
如果我有彈性的搜索索引的用戶的屬性,我想以某種方式角色添加到用戶,如:如何細分用戶
roles: employee, manager, surpervisor
用戶可以屬於1個或多個角色。
然後我可以搜索屬於角色「僱員」或「經理,主管」的用戶嗎?
如果有50萬或100萬的用戶,這會是一個糟糕的設計嗎?
是的,你可以添加多個值到同一個字段。 Elasticsearch中的每個字段默認爲「多值」。這意味着你可以索引文件,這樣
文件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"] }'
文獻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"]}'
文獻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"] }'
現在讓我們說你要搜索誰是主管用戶AND經理:
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"
]
}
}
]
}}
或者,如果你希望員工誰是主管,經理或員工:
curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d '{
"query": {
"bool": {
"should": [{
"match": {
"roles": {
"query": "supervisor manager",
"operator" : "and"
}
}
},
{
"match": {
"roles": {
"query" : "employee"
}
}
}]
}
} }'