Search in sources :

Example 1 with QueryContext

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));
}
Also used : Node(org.neo4j.graphdb.Node) QueryContext(org.neo4j.index.lucene.QueryContext) Test(org.junit.Test)

Example 2 with QueryContext

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);
}
Also used : Relationship(org.neo4j.graphdb.Relationship) StartNodeNotFoundException(org.neo4j.server.rest.domain.StartNodeNotFoundException) NotFoundException(org.neo4j.graphdb.NotFoundException) EndNodeNotFoundException(org.neo4j.server.rest.domain.EndNodeNotFoundException) QueryContext(org.neo4j.index.lucene.QueryContext)

Example 3 with QueryContext

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();
}
Also used : Node(org.neo4j.graphdb.Node) QueryContext(org.neo4j.index.lucene.QueryContext) Test(org.junit.Test)

Example 4 with QueryContext

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();
    }
}
Also used : Node(org.neo4j.graphdb.Node) QueryContext(org.neo4j.index.lucene.QueryContext) Test(org.junit.Test)

Example 5 with QueryContext

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);
}
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