use of org.neo4j.index.lucene.QueryContext in project neo4j by neo4j.
the class TestLuceneIndex method shouldNotGetLatestTxModificationsWhenChoosingSpeedQueries.
@Test
public void shouldNotGetLatestTxModificationsWhenChoosingSpeedQueries() {
Index<Node> index = nodeIndex(LuceneIndexImplementation.EXACT_CONFIG);
Node node = graphDb.createNode();
index.add(node, "key", "value");
QueryContext queryContext = new QueryContext("value").tradeCorrectnessForSpeed();
assertThat(index.query("key", queryContext), emptyIterable());
assertThat(index.query("key", "value"), Contains.contains(node));
}
use of org.neo4j.index.lucene.QueryContext in project neo4j by neo4j.
the class DatabaseActions method getIndexedRelationshipsByQuery.
public ListRepresentation getIndexedRelationshipsByQuery(String indexName, String key, String query, String sort) {
if (!graphDb.index().existsForRelationships(indexName)) {
throw new NotFoundException();
}
if (query == null) {
return toListRelationshipRepresentation();
}
Index<Relationship> index = graphDb.index().forRelationships(indexName);
IndexResultOrder order = getOrdering(sort);
QueryContext queryCtx = order.updateQueryContext(new QueryContext(query));
return toListRelationshipRepresentation(index.query(key, queryCtx), order);
}
use of org.neo4j.index.lucene.QueryContext in project neo4j by neo4j.
the class TestLuceneIndex method testSortByRelevance.
@Test
public void testSortByRelevance() {
Index<Node> index = nodeIndex(LuceneIndexImplementation.EXACT_CONFIG);
Node node1 = graphDb.createNode();
Node node2 = graphDb.createNode();
Node node3 = graphDb.createNode();
index.add(node1, "name", "something");
index.add(node2, "name", "something");
index.add(node2, "foo", "yes");
index.add(node3, "name", "something");
index.add(node3, "foo", "yes");
index.add(node3, "bar", "yes");
restartTx();
IndexHits<Node> hits = index.query(new QueryContext("+name:something foo:yes bar:yes").sort(Sort.RELEVANCE));
assertEquals(node3, hits.next());
assertEquals(node2, hits.next());
assertEquals(node1, hits.next());
assertFalse(hits.hasNext());
index.delete();
node1.delete();
node2.delete();
node3.delete();
}
use of org.neo4j.index.lucene.QueryContext in project neo4j by neo4j.
the class TestLuceneIndex method testSorting.
@Test
public void testSorting() {
Index<Node> index = nodeIndex(LuceneIndexImplementation.EXACT_CONFIG);
String name = "name";
String title = "title";
String other = "other";
String sex = "sex";
Node adam = graphDb.createNode();
Node adam2 = graphDb.createNode();
Node jack = graphDb.createNode();
Node eva = graphDb.createNode();
index.add(adam, name, "Adam");
index.add(adam, title, "Software developer");
index.add(adam, sex, "male");
index.add(adam, other, "aaa");
index.add(adam2, name, "Adam");
index.add(adam2, title, "Blabla");
index.add(adam2, sex, "male");
index.add(adam2, other, "bbb");
index.add(jack, name, "Jack");
index.add(jack, title, "Apple sales guy");
index.add(jack, sex, "male");
index.add(jack, other, "ccc");
index.add(eva, name, "Eva");
index.add(eva, title, "Secretary");
index.add(eva, sex, "female");
index.add(eva, other, "ddd");
for (int i = 0; i < 2; i++) {
assertContainsInOrder(index.query(new QueryContext("name:*").sort(name, title)), adam2, adam, eva, jack);
assertContainsInOrder(index.query(new QueryContext("name:*").sort(name, other)), adam, adam2, eva, jack);
assertContainsInOrder(index.query(new QueryContext("name:*").sort(sex, title)), eva, jack, adam2, adam);
assertContainsInOrder(index.query(name, new QueryContext("*").sort(sex, title)), eva, jack, adam2, adam);
assertContainsInOrder(index.query(new QueryContext("name:*").sort(name, title).top(2)), adam2, adam);
restartTx();
}
}
use of org.neo4j.index.lucene.QueryContext in project neo4j by neo4j.
the class TestLuceneIndex method testScoring.
@Test
public void testScoring() {
Index<Node> index = nodeIndex(LuceneIndexImplementation.FULLTEXT_CONFIG);
Node node1 = graphDb.createNode();
Node node2 = graphDb.createNode();
String key = "text";
// Where the heck did I get this sentence from?
index.add(node1, key, "a time where no one was really awake");
index.add(node2, key, "once upon a time there was");
restartTx();
IndexHits<Node> hits = index.query(key, new QueryContext("once upon a time was").sort(Sort.RELEVANCE));
Node hit1 = hits.next();
float score1 = hits.currentScore();
Node hit2 = hits.next();
float score2 = hits.currentScore();
assertEquals(node2, hit1);
assertEquals(node1, hit2);
assertTrue("Score 1 (" + score1 + ") should have been higher than score 2 (" + score2 + ")", score1 > score2);
}
Aggregations