Neo4j and Lucene multikey queries (or: should I us

2019-08-26 10:28发布

问题:

I have a graph in Neo4j in which nodes represent random points in the plane, each node having the coordinates stored as properties (x and y, the value type is double).
I create the nodes and index them:

    IndexManager index = graph.index();
    Index<Node> nodesIndex = index.forNodes("points");

    for (int i = 0; i < points.length; i++) {
        Point p = points[i]; 
        Node n;

        Transaction tx = graph.beginTx();
        try {
            n = graph.createNode();
            n.setProperty("x", p.getX());
            n.setProperty("y", p.getY());

            nodesIndex.add(n, "x", new ValueContext(p.getX()).indexNumeric());
            nodesIndex.add(n, "y", new ValueContext(p.getY()).indexNumeric());

            tx.success();
        } finally {
            tx.finish();
        }
    }

Now, what I need to do, is query for the nodes which are in a square area. So for example I made this query:

http://localhost:7474/db/data/index/node/points?query=x:[0.0 TO 3.0] AND y:[0.0 TO 3.0]

And this is the response:

    Node
    Properties
    y   1.0
    x   14.0
    Node info
    self    /db/data/node/10

    Node
    Properties
    y   1.0
    x   2.0
    Node info
    self    /db/data/node/7

    Node
    Properties
    y   1.0
    x   6.0
    Node info
    self    /db/data/node/8

    Node
    Properties
    y   1.0
    x   7.0
    Node info
    self    /db/data/node/9

[Etc...]

As you see it is not working. And I don't understand why (maybe I need to configure the index?).
Note that I haven't got to use Lucene. If there is a way to gather that information with Cypher (starting from a node centered in the square area) would be actually better, for I also need the relationships between the nodes found.

Additional informations If that matters, the graph represents a Delaunay triangolation on a random set of points on the plane. In more abstract terms, I need to "extract" the entire subgraph that lays in a given area.

Any help is really appreciated!

回答1:

I am afraid you can't do this via Cypher. There is no way Cypher can infer that you want to use a numeric Value context for this query (could probably be in some indexing meta info in future releases), and you need that to be able to query Lucene the way you want to. Easiest way over REST is probably to use Groovy, see custom sorting (same issue) at http://docs.neo4j.org/chunked/snapshot/gremlin-plugin.html#rest-api-send-an-arbitrary-groovy-script---lucene-sorting