我想根据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})进行排序?

最佳答案

在Cypher中做这样的事情怎么样

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)


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

MATCH (n:PPOD)
SET n.name_lc = toLower(n.name)

关于java - Neo4j-按相关性排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44250810/

10-08 23:07