Lucene neo4j从索引中获取一个随机节点

Lucene neo4j从索引中获取一个随机节点,lucene,indexing,neo4j,Lucene,Indexing,Neo4j,是否有任何选项可以从lucene索引中使用index.query(如下所示)获取随机节点 Index<Node> index = graphDb.index().forNodes("actors"); Node rand = index.query("foo:bar").getRandom(); Index Index=graphDb.Index().forNodes(“参与者”); 节点rand=index.query(“foo:bar”).getRandom(); 谢谢 jö

是否有任何选项可以从lucene索引中使用index.query(如下所示)获取随机节点

Index<Node> index = graphDb.index().forNodes("actors");
Node rand = index.query("foo:bar").getRandom();
Index Index=graphDb.Index().forNodes(“参与者”);
节点rand=index.query(“foo:bar”).getRandom();
谢谢
jörn

我的问题是逐渐地通过节点列表工作,但顺序是随机的

我玩了一些游戏,最后得到一个“id缓存”作为临时解决方案,其中只存储具有特定属性(未使用和foo=bar)的节点

如果同时将新节点添加到缓存并将其从缓存中删除,则可以延长缓存的使用时间

private ArrayList<Long> myIndexIDs = new ArrayList<Long>();
private int minCacheSize = 100;
private int maxCacheSize = 5000;

public Node getRandomNode() {
    boolean found  = false;
    Node n = null;

    int index = getMyNodeIndex();
    long id = myIndexIDs.get(index);

    System.out.println(String.format("found id %d at index: %d", id, index));
    ExecutionResult result = search.execute("START n=node(" + id + ") RETURN n");

    for (Map<String, Object> row : result) {
        n = (Node) row.get("n");
        found = true;
        break;
    }

    if (found) {
        myIndexIDs.remove(index);
        myIndexIDs.trimToSize();
    }

    return n;
}

// fill the arraylist with node ids
private void createMyNodeIDs() {
    System.out.println("create node cache");
    IndexHits<Node> result = this.myIndex.query("used:false");
    int count = 0;

    while (result.hasNext() && count <= this.maxCacheSize) {
        Node n = result.next();
        if (!(n.hasProperty("foo") && "bar" == (String) n.getProperty("foo"))) {
            myIndexIDs.add(n.getId());
            count++;
        }
    }

    result.close();
}

// returns a random index from the cache
private int getMyIndexNodeIndex() {
    // create a new index if you're feeling that it became too small
    if (this.myIndexIDs.size() < this.minCacheSize) {
        createMyNodeIDs();
    }
    // the current size of the cache
    System.out.println(this.myIndexIDs.size());

    // http://stackoverflow.com/a/363732/520544
    return (int) (Math.random() * ((this.myIndexIDs.size() - 1) + 1));
}
private ArrayList myIndexIDs=new ArrayList();
私有int minCacheSize=100;
私有int maxCacheSize=5000;
公共节点getRandomNode(){
布尔值=false;
节点n=null;
int index=getMyNodeIndex();
long id=myIndexIDs.get(索引);
System.out.println(String.format(“在索引%d处找到id%d”,id,索引));
ExecutionResult=search.execute(“开始n=node(“+id+”)返回n”);
用于(地图行:结果){
n=(节点)row.get(“n”);
发现=真;
打破
}
如果(找到){
myIndexIDs.去除(索引);
myIndexIDs.trimToSize();
}
返回n;
}
//用节点ID填充arraylist
私有void createMyNodeIDs(){
System.out.println(“创建节点缓存”);
IndexHits result=this.myIndex.query(“used:false”);
整数计数=0;

虽然(result.hasNext()&&count我的问题是逐渐地处理节点列表,但顺序是随机的

我玩了一些游戏,最后得到一个“id缓存”作为临时解决方案,其中只存储具有特定属性(未使用和foo=bar)的节点

如果同时将新节点添加到缓存并将其从缓存中删除,则可以延长缓存的使用时间

private ArrayList<Long> myIndexIDs = new ArrayList<Long>();
private int minCacheSize = 100;
private int maxCacheSize = 5000;

public Node getRandomNode() {
    boolean found  = false;
    Node n = null;

    int index = getMyNodeIndex();
    long id = myIndexIDs.get(index);

    System.out.println(String.format("found id %d at index: %d", id, index));
    ExecutionResult result = search.execute("START n=node(" + id + ") RETURN n");

    for (Map<String, Object> row : result) {
        n = (Node) row.get("n");
        found = true;
        break;
    }

    if (found) {
        myIndexIDs.remove(index);
        myIndexIDs.trimToSize();
    }

    return n;
}

// fill the arraylist with node ids
private void createMyNodeIDs() {
    System.out.println("create node cache");
    IndexHits<Node> result = this.myIndex.query("used:false");
    int count = 0;

    while (result.hasNext() && count <= this.maxCacheSize) {
        Node n = result.next();
        if (!(n.hasProperty("foo") && "bar" == (String) n.getProperty("foo"))) {
            myIndexIDs.add(n.getId());
            count++;
        }
    }

    result.close();
}

// returns a random index from the cache
private int getMyIndexNodeIndex() {
    // create a new index if you're feeling that it became too small
    if (this.myIndexIDs.size() < this.minCacheSize) {
        createMyNodeIDs();
    }
    // the current size of the cache
    System.out.println(this.myIndexIDs.size());

    // http://stackoverflow.com/a/363732/520544
    return (int) (Math.random() * ((this.myIndexIDs.size() - 1) + 1));
}
private ArrayList myIndexIDs=new ArrayList();
私有int minCacheSize=100;
私有int maxCacheSize=5000;
公共节点getRandomNode(){
布尔值=false;
节点n=null;
int index=getMyNodeIndex();
long id=myIndexIDs.get(索引);
System.out.println(String.format(“在索引%d处找到id%d”,id,索引));
ExecutionResult=search.execute(“开始n=node(“+id+”)返回n”);
用于(地图行:结果){
n=(节点)row.get(“n”);
发现=真;
打破
}
如果(找到){
myIndexIDs.去除(索引);
myIndexIDs.trimToSize();
}
返回n;
}
//用节点ID填充arraylist
私有void createMyNodeIDs(){
System.out.println(“创建节点缓存”);
IndexHits result=this.myIndex.query(“used:false”);
整数计数=0;
while(result.hasNext()&&count)