我正在将bulbflow(python)与Neo4j一起使用,并且我尝试仅在我的密钥子集上添加索引(现在,只需名为"name"的键用于可选的基于索引的查找)。
我不喜欢灯泡流模型(限制太多),而且由于"自动索引"是全局设置,因此我无法弄清楚如何在不更改代码的情况下进行选择性索引 - 我不明白如何根据键配置它。
有没有人做过这样的事情?
-安德鲁
将 g.config.autoindex
设置为 False
来禁用 Bulbs 自动索引。
请参阅 https://github.com/espeed/bulbs/blob/master/bulbs/config.py#L62
>>> from bulbs.neo4jserver import Graph
>>> g = Graph()
>>> g.config.autoindex = False
>>> g.vertices.create(name="James")
在上面的示例中,这将导致 name
属性不自动编制索引。
将autoindex
设置为 False
将切换到使用低级别客户端的 create_vertex()
方法,而不是 create_indexed_vertex()
方法:
见 https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/client.py#L422
create_indexed_vertex()
方法有一个keys
参数,可用于选择性索引:
见 https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/client.py#L424
这是 Bulbs 模型使用的低级client
方法。通常不需要显式调用低级别客户端方法,但如果这样做,则可以通过在键 arg 中包含属性名称来有选择地索引属性。
要有选择地索引模型中的属性,只需覆盖模型定义中的get_index_keys()
:
见 https://github.com/espeed/bulbs/blob/master/bulbs/model.py#L383
默认情况下,灯泡模型为所有属性编制索引。如果未提供键,则会为所有属性编制索引(如在 TinkerPop/Blueprints 中)。
请参阅模型 _create() 和 get_bundle() 方法:
-
_create()
https://github.com/espeed/bulbs/blob/master/bulbs/model.py#L583 -
get_bundle()
https://github.com/espeed/bulbs/blob/master/bulbs/model.py#L363 -
get_index_keys()
https://github.com/espeed/bulbs/blob/master/bulbs/model.py#L383
为了启用通用顶点和边的选择性索引,我更新了 Bulbs 通用顶点/边方法,以包含一个 _keys
arg,您可以在其中提供要索引的属性名称(键)列表。
见 https://github.com/espeed/bulbs/commit/4fe39d5a76675020286ec9aeaa8e71d58e3a432a
现在,要有选择地索引通用顶点/边上的属性,您可以提供要索引的属性名称列表:
>>> from bulbs.neo4jserver import Graph
>>> g = Graph()
>>> g.config.autoindex = False
>>> james = g.vertices.create(name="James", city="Dallas", _keys=["name"])
>>> julie = g.vertices.create(name="Julie", city="Dallas", _keys=["name"])
>>> g.edges.create(james, "knows", julie, timestamp=12345, someprop="somevalue", _keys=["someprop"])
在上面的示例中,将为每个顶点索引 name
属性,并为边编制索引someprop
属性。请注意,不会对city
和timestamp
编制索引,因为这些属性名称未显式包含在索引键列表中。
如果g.config.autoindex
True
且None
_keys
(默认值),则将为所有属性编制索引(就像以前一样)。
如果g.config.autoindex
False
并且_keys
None
,则不会对任何属性编制索引。
如果将_keys
显式设置为属性名称列表,则仅对这些属性编制索引,无论g.config.autoindex
是True
还是False
。
见 https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/client.py#L422
注意:如果您使用的是 Neo4j Server、Rexster 或 Titan Server,则自动索引的工作方式会有所不同,并且所有图形数据库服务器的索引体系结构在过去几个月中一直处于不断变化的状态。似乎所有人都在从手动索引系统转向自动索引系统。
对于直到最近才具有自动索引功能的图形数据库服务器(例如 Neo4j Server),Bulbs 通过使用数据库的低级手动索引方法的自定义 Gremlin 脚本启用了自动索引:
- https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/client.py#L1008
- https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/gremlin.groovy#L11
但是,手动索引在Neo4j Server,TinkerPop/Rexster和Titan Server中已被弃用,因此Bulbs 0.4索引架构将相应更改。通过预先声明索引键,仍然可以进行选择性索引,就像在 SQL create table
语句中一样。
BTW:你觉得模型有什么限制吗?灯泡模型(实际上是整个库)设计得很灵活,因此您可以根据需要对其进行修改。
有关如何自定义灯泡模型的信息,请参阅 Lightbulb 示例:在 neo4j 的灯泡框架中是否有等效的提交
如果您有任何问题,请告诉我。