且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

具有可变属性的节点,如何建模,索引和查询?

更新时间:2023-01-12 12:33:44

对于用户,我猜最简单的方法是为可能用于查找用户的所有属性键配置自动索引. 为了将用户与事件相关联,我将在事件细节方面更加详细.例如.而不是一般的PUBLISH关系类型,请尝试使用例如

然后,您的查询将使用自动索引查找一个或多个用户,然后根据特定的关系类型与事件进行匹配.这样做会防止在遍历时检查性能,这对性能很有好处.

I have the following setup: user and event nodes, with (user)-[:PUBLISH]->(event). each user and event both have multiple properties. These properties vary significantly from user to user (and event to event) which makes it hard to create indexes for them, however I need to query both the users and event by these attributes in a fast manner.

The simplest form of query I need to perform: find all users with properties {x: 'a', ...} that have published at least one event with properties { y: 'b', ...} but haven't published any event with properties {z: 'c', ...}

So my questions are: how can I best model the data for this query? (and is neo4j suitable for this use case, for that matter?) and how do I make it work fast given that I MUST HAVE dynamic node attributes. AFAIK in neo4j one cannot have compound indexes, do I need to create an index for each property of user/event?

Any help/ideas will be most welcome. Thank you!

For users I guess the simplest approach would be to configure autoindexing for all property keys that are potentially used for looking up users. To associate users to events, I'd try to be more verbose on event specifics. E.g. instead of a generic PUBLISH relationship type, try to use e.g. PUBLISH_EVENT_TYPEA, PUBLISH_EVENT_TYPEB, PUBLISH_EVENT_TYPEC ....

YOur query would then look up one or more users using the autoindex and then match to the event based on the specific relationship types. Doing so prevents inspection of properties while traversing which is good for performance.