DependencyGraphIndex { public Set<Node> findNodes(String queryString) { try (DirectoryReader directoryReader = DirectoryReader.open(directory)) { IndexSearcher indexSearcher = new IndexSearcher(directoryReader); Query query = parseQuery(queryString); ScoreDoc[] hits = indexSearcher.search(query, MAX_RESULTS, Sort.INDEXORDER).scoreDocs; Set<Node> result = new HashSet<>(); for (ScoreDoc hit : hits) { Document hitDoc = indexSearcher.doc(hit.doc); String name = hitDoc.get("name"); result.add(graph.getNode(name)); } return result; } catch (IOException e) { throw new IllegalArgumentException("Finding nodes failed for query: " + queryString, e); } } DependencyGraphIndex(DependencyGraph graph, String... numericFields); Set<Node> findNodes(String queryString);  }
@Test public void findNodes() { Set<Node> nodes = index.findNodes("x:green"); assertThat(nodes).hasSize(1); assertThat(nodes).contains(v1); nodes = index.findNodes("x:yellow"); assertThat(nodes).hasSize(3); assertThat(nodes).contains(v3, v4, v5); }
@Test public void findNodesWithNumericFields() { Set<Node> nodes = index.findNodes("size:2"); assertThat(nodes).hasSize(1); assertThat(nodes).contains(v2); }
@Test public void findNodesWithNumericFieldMultipleHits() { Set<Node> nodes = index.findNodes("size:250"); assertThat(nodes).hasSize(2); assertThat(nodes).contains(v5, v6); }
@Test public void findNodesWithNumericRange() { Set<Node> nodes = index.findNodes("size:[80 TO 200]"); assertThat(nodes).hasSize(2); assertThat(nodes).contains(v3, v4); }
@Test public void invalidQuery() { try { index.findNodes("invalid:"); fail("IllegalArgumentException expected"); } catch(IllegalArgumentException e) { assertThat(e.getMessage()).startsWith("Parsing of query failed: invalid:"); } }