Search in sources :

Example 6 with QueryContext

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

the class TestLuceneIndex method testTopHits.

@Test
public void testTopHits() {
    Index<Relationship> index = relationshipIndex(LuceneIndexImplementation.FULLTEXT_CONFIG);
    EntityCreator<Relationship> creator = RELATIONSHIP_CREATOR;
    String key = "text";
    Relationship rel1 = creator.create(key, "one two three four five six seven eight nine ten");
    Relationship rel2 = creator.create(key, "one two three four five six seven eight other things");
    Relationship rel3 = creator.create(key, "one two three four five six some thing else");
    Relationship rel4 = creator.create(key, "one two three four five what ever");
    Relationship rel5 = creator.create(key, "one two three four all that is good and bad");
    Relationship rel6 = creator.create(key, "one two three hill or something");
    Relationship rel7 = creator.create(key, "one two other time than this");
    index.add(rel2, key, rel2.getProperty(key));
    index.add(rel1, key, rel1.getProperty(key));
    index.add(rel3, key, rel3.getProperty(key));
    index.add(rel7, key, rel7.getProperty(key));
    index.add(rel5, key, rel5.getProperty(key));
    index.add(rel4, key, rel4.getProperty(key));
    index.add(rel6, key, rel6.getProperty(key));
    String query = "one two three four five six seven";
    for (int i = 0; i < 2; i++) {
        assertContainsInOrder(index.query(key, new QueryContext(query).top(3).sort(Sort.RELEVANCE)), rel1, rel2, rel3);
        restartTx();
    }
}
Also used : Relationship(org.neo4j.graphdb.Relationship) QueryContext(org.neo4j.index.lucene.QueryContext) Test(org.junit.Test)

Example 7 with QueryContext

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

the class ImdbDocTest 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<>();
    Set<String> expectedActors = new HashSet<String>() {

        {
            add("Monica Bellucci");
            add("Keanu Reeves");
        }
    };
    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();
    Node malena = movies.get("title", "Malèna").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(1999).indexNumeric());
    movies.add(theMatrixReloaded, "year-numeric", new ValueContext(2003).indexNumeric());
    movies.add(malena, "year-numeric", new ValueContext(2000).indexNumeric());
    int from = 1997;
    int to = 1999;
    hits = movies.query(QueryContext.numericRange("year-numeric", from, to));
    // END SNIPPET: numericRange
    assertEquals(theMatrix, hits.getSingle());
    // START SNIPPET: sortedNumericRange
    hits = movies.query(QueryContext.numericRange("year-numeric", from, null).sortNumeric("year-numeric", false));
    // END SNIPPET: sortedNumericRange
    List<String> sortedMovies = new ArrayList<>();
    List<String> expectedSortedMovies = new ArrayList<String>() {

        {
            add("The Matrix");
            add("Malèna");
            add("The Matrix Reloaded");
        }
    };
    for (Node hit : hits) {
        sortedMovies.add((String) hit.getProperty("title"));
    }
    assertEquals(expectedSortedMovies, sortedMovies);
    // START SNIPPET: exclusiveRange
    movies.add(theMatrix, "score", new ValueContext(8.7).indexNumeric());
    movies.add(theMatrixReloaded, "score", new ValueContext(7.1).indexNumeric());
    movies.add(malena, "score", new ValueContext(7.4).indexNumeric());
    // include 8.0, exclude 9.0
    hits = movies.query(QueryContext.numericRange("score", 8.0, 9.0, true, false));
    // END SNIPPET: exclusiveRange
    found.clear();
    for (Node hit : hits) {
        found.add((String) hit.getProperty("title"));
    }
    assertEquals(expectedMovies, found);
    // 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) ArrayList(java.util.ArrayList) 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 8 with QueryContext

use of org.neo4j.index.lucene.QueryContext in project eol-globi-data by jhpoelen.

the class NodeFactoryNeo4j method findLocationByLatitude.

private Node findLocationByLatitude(Location location) throws NodeFactoryException {
    Node matchingLocation = null;
    validate(location);
    QueryContext queryOrQueryObject = QueryContext.numericRange(LocationConstant.LATITUDE, location.getLatitude(), location.getLatitude());
    IndexHits<Node> matchingLocations = locations.query(queryOrQueryObject);
    for (Node node : matchingLocations) {
        final LocationNode foundLocation = new LocationNode(node);
        if (isSameLocation(location, foundLocation)) {
            matchingLocation = node;
            break;
        }
    }
    matchingLocations.close();
    return matchingLocation;
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) Node(org.neo4j.graphdb.Node) QueryContext(org.neo4j.index.lucene.QueryContext)

Example 9 with QueryContext

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

the class TestPerformanceAndSanity method testInsertionSpeed.

private <T extends PropertyContainer> void testInsertionSpeed(Index<T> index, EntityCreator<T> creator) {
    long t = System.currentTimeMillis();
    for (int i = 0; i < 300000; i++) {
        T entity = creator.create();
        if (i % 5000 == 5) {
            index.query(new TermQuery(new Term("name", "The name " + i)));
        }
        IteratorUtil.lastOrNull((Iterable<T>) index.query(new QueryContext(new TermQuery(new Term("name", "The name " + i))).tradeCorrectnessForSpeed()));
        IteratorUtil.lastOrNull((Iterable<T>) index.get("name", "The name " + i));
        index.add(entity, "name", "The name " + i);
        index.add(entity, "title", "Some title " + i);
        index.add(entity, "something", i + "Nothing");
        index.add(entity, "else", i + "kdfjkdjf" + i);
        if (i % 10000 == 0) {
            restartTx();
            System.out.println(i);
        }
    }
    System.out.println("insert:" + (System.currentTimeMillis() - t));
    t = System.currentTimeMillis();
    int count = 1000;
    int resultCount = 0;
    for (int i = 0; i < count; i++) {
        resultCount += IteratorUtil.count((Iterator<T>) index.get("name", "The name " + i * 900));
    }
    System.out.println("get(" + resultCount + "):" + (double) (System.currentTimeMillis() - t) / (double) count);
    t = System.currentTimeMillis();
    resultCount = 0;
    for (int i = 0; i < count; i++) {
        resultCount += IteratorUtil.count((Iterator<T>) index.get("something", i * 900 + "Nothing"));
    }
    System.out.println("get(" + resultCount + "):" + (double) (System.currentTimeMillis() - t) / (double) count);
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Iterator(java.util.Iterator) Term(org.apache.lucene.index.Term) QueryContext(org.neo4j.index.lucene.QueryContext)

Example 10 with QueryContext

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

the class TestLuceneIndex method makeSureCompositeQueriesCanBeAsked.

@Test
public void makeSureCompositeQueriesCanBeAsked() {
    Index<Node> index = nodeIndex("index", 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(neo));
        assertThat(index.query(new QueryContext("username:*@matrix sex:male").defaultOperator(Operator.AND)), contains(neo));
        assertThat(index.query("username:*@matrix OR sex:male"), contains(neo, trinity));
        assertThat(index.query(new QueryContext("username:*@matrix sex:male").defaultOperator(Operator.OR)), contains(neo, trinity));
        restartTx();
    }
    index.delete();
}
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