Search in sources :

Example 21 with QueryContext

use of org.neo4j.index.lucene.QueryContext in project graphdb by neo4j-attic.

the class TestLuceneIndex method testSortByRelevance.

@Test
public void testSortByRelevance() {
    Index<Node> index = nodeIndex("relevance", 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();
}
Also used : Node(org.neo4j.graphdb.Node) QueryContext(org.neo4j.index.lucene.QueryContext) Test(org.junit.Test)

Example 22 with QueryContext

use of org.neo4j.index.lucene.QueryContext in project graphdb by neo4j-attic.

the class TestLuceneIndex method testScoring.

@Test
public void testScoring() {
    Index<Node> index = nodeIndex("score-index", 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(score1 > score2);
}
Also used : Node(org.neo4j.graphdb.Node) QueryContext(org.neo4j.index.lucene.QueryContext) Test(org.junit.Test)

Aggregations

QueryContext (org.neo4j.index.lucene.QueryContext)22 Node (org.neo4j.graphdb.Node)18 Test (org.junit.Test)16 Term (org.apache.lucene.index.Term)3 TermQuery (org.apache.lucene.search.TermQuery)3 Relationship (org.neo4j.graphdb.Relationship)3 HashSet (java.util.HashSet)2 Sort (org.apache.lucene.search.Sort)2 WildcardQuery (org.apache.lucene.search.WildcardQuery)2 NotFoundException (org.neo4j.graphdb.NotFoundException)2 Transaction (org.neo4j.graphdb.Transaction)2 IndexManager (org.neo4j.graphdb.index.IndexManager)2 ValueContext (org.neo4j.index.lucene.ValueContext)2 EndNodeNotFoundException (org.neo4j.server.rest.domain.EndNodeNotFoundException)2 StartNodeNotFoundException (org.neo4j.server.rest.domain.StartNodeNotFoundException)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)1 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)1 JsonNode (org.codehaus.jackson.JsonNode)1