您可以通过设置禁用灯泡自动索引g.config.autoindex
to False
.
See https://github.com/espeed/bulbs/blob/master/bulbs/config.py#L62 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
属性不会被自动索引。
Setting autoindex
to False
将切换到使用低级客户端create_vertex()
方法而不是create_indexed_vertex()
method:
See https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/client.py#L422 https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/client.py#L422
The create_indexed_vertex()
方法有一个keys
arg,可用于选择性索引:
See https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/client.py#L424 https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/client.py#L424
这是低级的client
灯泡模型使用的方法。您通常不需要显式调用低级客户端方法,但如果这样做,您可以通过在键参数中包含属性名称来选择性地索引属性。
要选择性地索引模型中的属性,只需覆盖get_index_keys()
在您的模型定义中:
See https://github.com/espeed/bulbs/blob/master/bulbs/model.py#L383 https://github.com/espeed/bulbs/blob/master/bulbs/model.py#L383
默认情况下,灯泡模型索引所有属性。如果没有提供键,则所有属性都会被索引(就像在 TinkerPop/Blueprints 中一样)。
请参阅 Model _create() 和 get_bundle() 方法:
-
_create()
https://github.com/espeed/bulbs/blob/master/bulbs/model.py#L583 https://github.com/espeed/bulbs/blob/master/bulbs/model.py#L583
-
get_bundle()
https://github.com/espeed/bulbs/blob/master/bulbs/model.py#L363 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 https://github.com/espeed/bulbs/blob/master/bulbs/model.py#L383
为了启用通用顶点和边的选择性索引,我更新了 Bulbs 通用顶点/边方法以包含_keys
arg,您可以在其中提供要索引的属性名称(键)列表。
See https://github.com/espeed/bulbs/commit/4fe39d5a76675020286ec9aeaa8e71d58e3a432a 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
and timestamp
不会被索引,因为这些属性名称未明确包含在索引键列表中。
If g.config.autoindex
is True
and _keys
is None
(默认),所有属性都将被索引(就像以前一样)。
If g.config.autoindex
is False
and _keys
is None
,不会对任何属性建立索引。
If _keys
显式设置为属性名称列表,只有这些属性才会被索引,无论是否g.config.autoindex
is True
or False
.
See https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/client.py#L422 https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/client.py#L422
NOTE:如果您使用 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/client.py#L1008
- https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/gremlin.groovy#L11 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:您觉得模特受到什么限制吗?灯泡模型(实际上是整个库)的设计非常灵活,因此您可以将其修改为您需要的任何内容。
有关如何自定义灯泡模型的信息,请参阅灯泡示例:Neo4j 的灯泡框架中是否有相当于 commit 的功能 https://stackoverflow.com/questions/16759606/is-there-a-equivalent-to-commit-in-bulbs-framework-for-neo4j/16764036#16764036
如果您有任何疑问,请告诉我。