Neo4j - 按相关性排序

时间:2017-05-29 22:06:15

标签: java neo4j cypher graph-databases columnsorting

我想在Neo4j中按相关性订购返回的数据。

出于我的目的,相关性可以简化为“我正在搜索的单词的索引”,其中较低的索引具有较高的相关性。

示例

我有这三个节点:

node : {
    Label: PROD
    properties : {
        name: "Bearing replacement Skateboard"
    }
}

node : {
    Label: PROD
    properties : {
        name: "Skateboard"
    }
}

node : {
    Label: PROD
    properties : {
        name: "L7 Skateboard"
    }
}

我希望他们以此订单退回:

node : {
    Label: PROD
    properties : {
        name: "Skateboard" // name.indexOf("Skateboard") = 0
    }
}

node : {
    Label: PROD
    properties : {
        name: "L7 Skateboard"  // name.indexOf("Skateboard") = 3
    }
}

node : {
    Label: PROD
    properties : {
        name: "Bearing replacement Skateboard"  // name.indexOf("Skateboard") = 19
    }
}

到目前为止我所拥有的:

String query = "MATCH (n:PROD) where LOWER(n.name) CONTAINS LOWER({textToSearch}) RETURN n ORDER BY LOWER(n.name) ASC LIMIT 15";

String textToSearch = "Skateboard";

Map<String, Object> queryParams = new HashMap<>();
queryParams.put("textToSearch", textToSearch);
try (
    Transaction ignored = database.beginTx();
    Result resultSet = database.execute(query, queryParams)
) {
    Iterator<Node> results = resultSet.columnAs("n");
    while (results.hasNext()) {
        Node node = results.next();
        /* data processing here */
    }
}

这只是按名称上升来命令结果。有没有办法告诉neo4j根据n.name.indexOf({textToFind})进行排序?

1 个答案:

答案 0 :(得分:4)

如何在Cypher中做这样的事情

node_t1 = pd.read_csv('5mins/nodes/N01')
node_t2 = pd.read_csv('10mins/nodes/N01')
node_t3 = pd.read_csv('15mins/nodes/N01')
...

有效利用上述内容......

在属性的小写版本

上创建索引
MATCH (n:PROD) 
WHERE n.name_lc CONTAINS toLower({textToSearch})
WITH n, SPLIT(n.name_lc, toLower({textToSearch})) as parts
RETURN n.name, SIZE(parts[0]) AS leading
ORDER BY leading

将常规名称复制为小写版本

CREATE INDEX ON :PROD(name_lc)