Skip to content

Commit

Permalink
Fix/node type search (Pometry#1524)
Browse files Browse the repository at this point in the history
* Added node_type to indexed search

* fmt
  • Loading branch information
miratepuffin authored Mar 1, 2024
1 parent 57d744d commit df232df
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 178 deletions.
178 changes: 0 additions & 178 deletions python/tests/test_graphdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1985,184 +1985,6 @@ def test_leading_zeroes_ids():
# assert g.node(g.node(1).name) is not None


def test_search_in_python():
g = Graph()
g.add_node(
1, "hamza", properties={"value": 60, "value_f": 31.3, "value_str": "abc123"}
)
g.add_node(
2,
"ben",
properties={"value": 59, "value_f": 11.4, "value_str": "test test test"},
)
g.add_node(
3,
"haaroon",
properties={
"value": 199,
"value_f": 52.6,
"value_str": "I wanna rock right now",
},
)

g.add_edge(
2,
"haaroon",
"hamza",
properties={"value": 60, "value_f": 31.3, "value_str": "abc123"},
)
g.add_edge(
1,
"ben",
"hamza",
properties={"value": 59, "value_f": 11.4, "value_str": "test test test"},
)
g.add_edge(
3,
"ben",
"haaroon",
properties={
"value": 199,
"value_f": 52.6,
"value_str": "I wanna rock right now",
},
)

index = g.index()

# Name tests
assert len(index.search_nodes("name:ben")) == 1
assert len(index.search_nodes("name:ben OR name:hamza")) == 2
assert len(index.search_nodes("name:ben AND name:hamza")) == 0
assert len(index.search_nodes("name: IN [ben, hamza]")) == 2

# Property tests
assert len(index.search_nodes("value:<120 OR value_f:>30")) == 3
assert len(index.search_nodes("value:[0 TO 60]")) == 2
assert len(index.search_nodes("value:[0 TO 60}")) == 1 # } == exclusive
assert len(index.search_nodes("value:>59 AND value_str:abc123")) == 1

# edge tests
assert len(index.search_edges("from:ben")) == 2
assert len(index.search_edges("from:ben OR from:haaroon")) == 3
assert len(index.search_edges("to:haaroon AND from:ben")) == 1
assert len(index.search_edges("to: IN [ben, hamza]")) == 2

# edge prop tests
assert len(index.search_edges("value:<120 OR value_f:>30")) == 3
assert len(index.search_edges("value:[0 TO 60]")) == 2
assert len(index.search_edges("value:[0 TO 60}")) == 1 # } == exclusive
assert len(index.search_edges("value:>59 AND value_str:abc123")) == 1

# Multiple history points test
g = Graph()
g.add_node(
1, "hamza", properties={"value": 60, "value_f": 31.3, "value_str": "abc123"}
)
g.add_node(
2, "hamza", properties={"value": 70, "value_f": 21.3, "value_str": "avc125"}
)
g.add_node(
3, "hamza", properties={"value": 80, "value_f": 11.3, "value_str": "dsc2312"}
)

index = g.index()

# The semantics here are that the expressions independently need to evaluate at ANY point in the lifetime of the node - hence hamza is returned even though at no point does he have both these values at the same time
assert len(index.search_nodes("value:<70 AND value_f:<19.2")) == 1

g.add_node(
4, "hamza", properties={"value": 100, "value_f": 11.3, "value_str": "dsc2312"}
)
# the graph isn't currently reindexed so this will not return hamza even though he now has a value which fits the bill
assert len(index.search_nodes("value:>99")) == 0


def test_search_with_windows():
# Window test
g = Graph()
g.add_node(
1, "hamza", properties={"value": 60, "value_f": 31.3, "value_str": "abc123"}
)
g.add_node(
2, "hamza", properties={"value": 70, "value_f": 21.3, "value_str": "avc125"}
)
g.add_node(
3, "hamza", properties={"value": 80, "value_f": 11.3, "value_str": "dsc2312"}
)

g.add_edge(
1,
"haaroon",
"hamza",
properties={"value": 50, "value_f": 31.3, "value_str": "abc123"},
)
g.add_edge(
2,
"haaroon",
"hamza",
properties={"value": 60, "value_f": 21.3, "value_str": "abddasc1223"},
)
g.add_edge(
3,
"haaroon",
"hamza",
properties={"value": 70, "value_f": 11.3, "value_str": "abdsda2c123"},
)
g.add_edge(
4,
"ben",
"naomi",
properties={"value": 100, "value_f": 22.3, "value_str": "ddddd"},
)

w_g = g.window(1, 3)

w_index = w_g.index()

# Testing if windowing works - ben shouldn't be included and Hamza should only have max value of 70
assert len(w_index.search_nodes("name:ben")) == 0
assert len(w_index.search_nodes("value:70")) == 1
assert len(w_index.search_nodes("value:>80")) == 0

assert len(w_index.search_edges("from:ben")) == 0
assert len(w_index.search_edges("from:haaroon AND value:>70")) == 0
assert len(w_index.search_edges("from:haaroon AND to:hamza")) == 1


def test_search_with_subgraphs():
g = Graph()
g.add_edge(
2,
"haaroon",
"hamza",
properties={"value": 60, "value_f": 31.3, "value_str": "abc123"},
)
g.add_edge(
1,
"ben",
"hamza",
properties={"value": 59, "value_f": 11.4, "value_str": "test test test"},
)
g.add_edge(
3,
"ben",
"haaroon",
properties={
"value": 199,
"value_f": 52.6,
"value_str": "I wanna rock right now",
},
)
g.add_edge(4, "hamza", "naomi")

index = g.index()
assert len(index.search_edges("from:hamza OR to:hamza")) == 3

subgraph = g.subgraph([g.node("ben"), g.node("hamza"), g.node("haaroon")])
index = subgraph.index()

assert len(index.search_edges("from:hamza OR to:hamza")) == 2


def test_node_types():
Expand Down
188 changes: 188 additions & 0 deletions python/tests/test_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
from raphtory import Graph

def test_search_in_python():
g = Graph()
g.add_node(
1, "hamza", properties={"value": 60, "value_f": 31.3, "value_str": "abc123"}
)
g.add_node(
2,
"ben",
properties={"value": 59, "value_f": 11.4, "value_str": "test test test"},
)
g.add_node(
3,
"haaroon",
properties={
"value": 199,
"value_f": 52.6,
"value_str": "I wanna rock right now",
},
)

g.add_edge(
2,
"haaroon",
"hamza",
properties={"value": 60, "value_f": 31.3, "value_str": "abc123"},
)
g.add_edge(
1,
"ben",
"hamza",
properties={"value": 59, "value_f": 11.4, "value_str": "test test test"},
)
g.add_edge(
3,
"ben",
"haaroon",
properties={
"value": 199,
"value_f": 52.6,
"value_str": "I wanna rock right now",
},
)

index = g.index()

# Name tests
assert len(index.search_nodes("name:ben")) == 1
assert len(index.search_nodes("name:ben OR name:hamza")) == 2
assert len(index.search_nodes("name:ben AND name:hamza")) == 0
assert len(index.search_nodes("name: IN [ben, hamza]")) == 2

# Property tests
assert len(index.search_nodes("value:<120 OR value_f:>30")) == 3
assert len(index.search_nodes("value:[0 TO 60]")) == 2
assert len(index.search_nodes("value:[0 TO 60}")) == 1 # } == exclusive
assert len(index.search_nodes("value:>59 AND value_str:abc123")) == 1

# edge tests
assert len(index.search_edges("from:ben")) == 2
assert len(index.search_edges("from:ben OR from:haaroon")) == 3
assert len(index.search_edges("to:haaroon AND from:ben")) == 1
assert len(index.search_edges("to: IN [ben, hamza]")) == 2

# edge prop tests
assert len(index.search_edges("value:<120 OR value_f:>30")) == 3
assert len(index.search_edges("value:[0 TO 60]")) == 2
assert len(index.search_edges("value:[0 TO 60}")) == 1 # } == exclusive
assert len(index.search_edges("value:>59 AND value_str:abc123")) == 1

# Multiple history points test
g = Graph()
g.add_node(
1, "hamza", properties={"value": 60, "value_f": 31.3, "value_str": "abc123"}
)
g.add_node(
2, "hamza", properties={"value": 70, "value_f": 21.3, "value_str": "avc125"}
)
g.add_node(
3, "hamza", properties={"value": 80, "value_f": 11.3, "value_str": "dsc2312"}
)

index = g.index()

# The semantics here are that the expressions independently need to evaluate at ANY point in the lifetime of the node - hence hamza is returned even though at no point does he have both these values at the same time
assert len(index.search_nodes("value:<70 AND value_f:<19.2")) == 1

g.add_node(
4, "hamza", properties={"value": 100, "value_f": 11.3, "value_str": "dsc2312"}
)
# the graph isn't currently reindexed so this will not return hamza even though he now has a value which fits the bill
assert len(index.search_nodes("value:>99")) == 0


def test_type_search():
g = Graph()
ben = g.add_node(1, "ben", node_type="type_1")
hamza = g.add_node(2, "hamza", node_type="type_2")
indexed = g.index()
assert indexed.search_nodes("node_type:type_1") == [ben]
assert set(indexed.search_nodes("node_type:type_1 OR node_type:type_2")) == {hamza, ben}

def test_search_with_windows():
# Window test
g = Graph()
g.add_node(
1, "hamza", properties={"value": 60, "value_f": 31.3, "value_str": "abc123"}
)
g.add_node(
2, "hamza", properties={"value": 70, "value_f": 21.3, "value_str": "avc125"}
)
g.add_node(
3, "hamza", properties={"value": 80, "value_f": 11.3, "value_str": "dsc2312"}
)

g.add_edge(
1,
"haaroon",
"hamza",
properties={"value": 50, "value_f": 31.3, "value_str": "abc123"},
)
g.add_edge(
2,
"haaroon",
"hamza",
properties={"value": 60, "value_f": 21.3, "value_str": "abddasc1223"},
)
g.add_edge(
3,
"haaroon",
"hamza",
properties={"value": 70, "value_f": 11.3, "value_str": "abdsda2c123"},
)
g.add_edge(
4,
"ben",
"naomi",
properties={"value": 100, "value_f": 22.3, "value_str": "ddddd"},
)

w_g = g.window(1, 3)

w_index = w_g.index()

# Testing if windowing works - ben shouldn't be included and Hamza should only have max value of 70
assert len(w_index.search_nodes("name:ben")) == 0
assert len(w_index.search_nodes("value:70")) == 1
assert len(w_index.search_nodes("value:>80")) == 0

assert len(w_index.search_edges("from:ben")) == 0
assert len(w_index.search_edges("from:haaroon AND value:>70")) == 0
assert len(w_index.search_edges("from:haaroon AND to:hamza")) == 1


def test_search_with_subgraphs():
g = Graph()
g.add_edge(
2,
"haaroon",
"hamza",
properties={"value": 60, "value_f": 31.3, "value_str": "abc123"},
)
g.add_edge(
1,
"ben",
"hamza",
properties={"value": 59, "value_f": 11.4, "value_str": "test test test"},
)
g.add_edge(
3,
"ben",
"haaroon",
properties={
"value": 199,
"value_f": 52.6,
"value_str": "I wanna rock right now",
},
)
g.add_edge(4, "hamza", "naomi")

index = g.index()
assert len(index.search_edges("from:hamza OR to:hamza")) == 3

subgraph = g.subgraph([g.node("ben"), g.node("hamza"), g.node("haaroon")])
index = subgraph.index()

assert len(index.search_edges("from:hamza OR to:hamza")) == 2
5 changes: 5 additions & 0 deletions raphtory/src/search/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,11 @@ impl<'graph, G: GraphViewOps<'graph>> IndexedGraph<G> {
Self::index_prop_value(&mut document, prop_field, prop_value);
}

match node.node_type() {
None => {}
Some(str) => document.add_text(schema.get_field("node_type")?, (*str).to_string()),
}

writer.add_document(document)?;
Ok(())
}
Expand Down

0 comments on commit df232df

Please sign in to comment.