1
아래처럼 index.query가있는 lucene 인덱스에서 임의의 노드를 가져 오는 옵션이 있습니까?neo4j 인덱스에서 임의의 노드를 얻으십시오
Index<Node> index = graphDb.index().forNodes("actors");
Node rand = index.query("foo:bar").getRandom();
감사
아래처럼 index.query가있는 lucene 인덱스에서 임의의 노드를 가져 오는 옵션이 있습니까?neo4j 인덱스에서 임의의 노드를 얻으십시오
Index<Node> index = graphDb.index().forNodes("actors");
Node rand = index.query("foo:bar").getRandom();
감사
내 문제가 점차 노드의 목록을 작업했다 요른하지만, 임의의 순서로한다.
특정 속성 (사용되지 않고 foo = bar)이있는 노드 만 저장되는 임시 솔루션으로 "id 캐시"가 끝날 때까지 약간 놀았습니다.
새 노드를 캐시에 추가하고 캐시에서도 삭제하면 캐시를 더 오래 사용할 수 있습니다.
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));
}