Search in sources :

Example 16 with QueryContext

use of org.neo4j.index.lucene.QueryContext in project neo4j by neo4j.

the class TestLuceneIndex method makeSureCompositeQueriesCanBeAsked.

@Test
public void makeSureCompositeQueriesCanBeAsked() {
    Index<Node> index = nodeIndex(LuceneIndexImplementation.EXACT_CONFIG);
    Node neo = graphDb.createNode();
    Node trinity = graphDb.createNode();
    index.add(neo, "username", "neo@matrix");
    index.add(neo, "sex", "male");
    index.add(trinity, "username", "trinity@matrix");
    index.add(trinity, "sex", "female");
    for (int i = 0; i < 2; i++) {
        assertThat(index.query("username:*@matrix AND sex:male"), Contains.contains(neo));
        assertThat(index.query(new QueryContext("username:*@matrix sex:male").defaultOperator(Operator.AND)), Contains.contains(neo));
        assertThat(index.query("username:*@matrix OR sex:male"), Contains.contains(neo, trinity));
        assertThat(index.query(new QueryContext("username:*@matrix sex:male").defaultOperator(Operator.OR)), Contains.contains(neo, trinity));
        restartTx();
    }
    index.delete();
}
Also used : Node(org.neo4j.graphdb.Node) QueryContext(org.neo4j.index.lucene.QueryContext) Test(org.junit.Test)

Example 17 with QueryContext

use of org.neo4j.index.lucene.QueryContext in project neo4j by neo4j.

the class TestLuceneIndex method testSortingWithTopHitsInPartCommittedPartLocal.

@Test
public void testSortingWithTopHitsInPartCommittedPartLocal() {
    Index<Node> index = nodeIndex(LuceneIndexImplementation.FULLTEXT_CONFIG);
    Node first = graphDb.createNode();
    Node second = graphDb.createNode();
    Node third = graphDb.createNode();
    Node fourth = graphDb.createNode();
    String key = "key";
    index.add(third, key, "ccc");
    index.add(second, key, "bbb");
    restartTx();
    index.add(fourth, key, "ddd");
    index.add(first, key, "aaa");
    assertContainsInOrder(index.query(key, new QueryContext("*").sort(key)), first, second, third, fourth);
    assertContainsInOrder(index.query(key, new QueryContext("*").sort(key).top(2)), first, second);
}
Also used : Node(org.neo4j.graphdb.Node) QueryContext(org.neo4j.index.lucene.QueryContext) Test(org.junit.Test)

Example 18 with QueryContext

use of org.neo4j.index.lucene.QueryContext in project neo4j by neo4j.

the class DatabaseActions method getIndexedNodesByQuery.

public ListRepresentation getIndexedNodesByQuery(String indexName, String key, String query, String sort) {
    if (!graphDb.index().existsForNodes(indexName)) {
        throw new NotFoundException();
    }
    if (query == null) {
        return toListNodeRepresentation();
    }
    Index<Node> index = graphDb.index().forNodes(indexName);
    IndexResultOrder order = getOrdering(sort);
    QueryContext queryCtx = order.updateQueryContext(new QueryContext(query));
    IndexHits<Node> result = index.query(key, queryCtx);
    return toListNodeRepresentation(result, order);
}
Also used : Node(org.neo4j.graphdb.Node) 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 19 with QueryContext

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

the class ImdbExampleTest method doQueriesForNodes.

@Test
public void doQueriesForNodes() {
    IndexManager index = graphDb.index();
    Index<Node> actors = index.forNodes("actors");
    Index<Node> movies = index.forNodes("movies");
    Set<String> found = new HashSet<String>();
    @SuppressWarnings("serial") Set<String> expectedActors = new HashSet<String>() {

        {
            add("Monica Bellucci");
            add("Keanu Reeves");
        }
    };
    @SuppressWarnings("serial") Set<String> expectedMovies = new HashSet<String>() {

        {
            add("The Matrix");
        }
    };
    // START SNIPPET: actorsQuery
    for (Node actor : actors.query("name", "*e*")) {
        // This will return Reeves and Bellucci
        // END SNIPPET: actorsQuery
        found.add((String) actor.getProperty("name"));
    // START SNIPPET: actorsQuery
    }
    // END SNIPPET: actorsQuery
    assertEquals(expectedActors, found);
    found.clear();
    // START SNIPPET: matrixQuery
    for (Node movie : movies.query("title:*Matrix* AND year:1999")) {
        // This will return "The Matrix" from 1999 only.
        // END SNIPPET: matrixQuery
        found.add((String) movie.getProperty("title"));
    // START SNIPPET: matrixQuery
    }
    // END SNIPPET: matrixQuery
    assertEquals(expectedMovies, found);
    // START SNIPPET: matrixSingleQuery
    Node matrix = movies.query("title:*Matrix* AND year:2003").getSingle();
    // END SNIPPET: matrixSingleQuery
    assertEquals("The Matrix Reloaded", matrix.getProperty("title"));
    // START SNIPPET: queryWithScore
    IndexHits<Node> hits = movies.query("title", "The*");
    for (Node movie : hits) {
        System.out.println(movie.getProperty("title") + " " + hits.currentScore());
        // END SNIPPET: queryWithScore
        assertTrue(((String) movie.getProperty("title")).startsWith("The"));
    // START SNIPPET: queryWithScore
    }
    // END SNIPPET: queryWithScore
    assertEquals(2, hits.size());
    // START SNIPPET: queryWithRelevance
    hits = movies.query("title", new QueryContext("The*").sortByScore());
    // END SNIPPET: queryWithRelevance
    float previous = Float.MAX_VALUE;
    // START SNIPPET: queryWithRelevance
    for (Node movie : hits) {
        // hits sorted by relevance (score)
        // END SNIPPET: queryWithRelevance
        assertTrue(hits.currentScore() <= previous);
        previous = hits.currentScore();
    // START SNIPPET: queryWithRelevance
    }
    // END SNIPPET: queryWithRelevance
    assertEquals(2, hits.size());
    // START SNIPPET: termQuery
    // a TermQuery will give exact matches
    Node actor = actors.query(new TermQuery(new Term("name", "Keanu Reeves"))).getSingle();
    // END SNIPPET: termQuery
    assertEquals("Keanu Reeves", actor.getProperty("name"));
    Node theMatrix = movies.get("title", "The Matrix").getSingle();
    Node theMatrixReloaded = movies.get("title", "The Matrix Reloaded").getSingle();
    // START SNIPPET: wildcardTermQuery
    hits = movies.query(new WildcardQuery(new Term("title", "The Matrix*")));
    for (Node movie : hits) {
        System.out.println(movie.getProperty("title"));
        // END SNIPPET: wildcardTermQuery
        assertTrue(((String) movie.getProperty("title")).startsWith("The Matrix"));
    // START SNIPPET: wildcardTermQuery
    }
    // END SNIPPET: wildcardTermQuery
    assertEquals(2, hits.size());
    // START SNIPPET: numericRange
    movies.add(theMatrix, "year-numeric", new ValueContext(1999L).indexNumeric());
    movies.add(theMatrixReloaded, "year-numeric", new ValueContext(2003L).indexNumeric());
    // Query for range
    long startYear = 1997;
    long endYear = 2001;
    hits = movies.query(NumericRangeQuery.newLongRange("year-numeric", startYear, endYear, true, true));
    // END SNIPPET: numericRange
    assertEquals(theMatrix, hits.getSingle());
    // START SNIPPET: compoundQueries
    hits = movies.query("title:*Matrix* AND year:1999");
    // END SNIPPET: compoundQueries
    assertEquals(theMatrix, hits.getSingle());
    // START SNIPPET: defaultOperator
    QueryContext query = new QueryContext("title:*Matrix* year:1999").defaultOperator(Operator.AND);
    hits = movies.query(query);
    // END SNIPPET: defaultOperator
    // with OR the result would be 2 hits
    assertEquals(1, hits.size());
    // START SNIPPET: sortedResult
    hits = movies.query("title", new QueryContext("*").sort("title"));
    for (Node hit : hits) {
    // all movies with a title in the index, ordered by title
    }
    // END SNIPPET: sortedResult
    assertEquals(3, hits.size());
    // START SNIPPET: sortedResult
    // or
    hits = movies.query(new QueryContext("title:*").sort("year", "title"));
    for (Node hit : hits) {
    // all movies with a title in the index, ordered by year, then title
    }
    // END SNIPPET: sortedResult
    assertEquals(3, hits.size());
}
Also used : ValueContext(org.neo4j.index.lucene.ValueContext) TermQuery(org.apache.lucene.search.TermQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) Node(org.neo4j.graphdb.Node) QueryContext(org.neo4j.index.lucene.QueryContext) Term(org.apache.lucene.index.Term) IndexManager(org.neo4j.graphdb.index.IndexManager) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with QueryContext

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

the class TestLuceneIndex method testSorting.

@Test
public void testSorting() {
    Index<Node> index = nodeIndex("sort", 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)

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