Search in sources :

Example 11 with QueryContext

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

the class TestLuceneIndex method testTopHits.

@Test
public void testTopHits() {
    Index<Relationship> index = relationshipIndex("topdocs", 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 12 with QueryContext

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

the class TestLuceneIndex method testSortingWithTopHitsInPartCommittedPartLocal.

@Test
public void testSortingWithTopHitsInPartCommittedPartLocal() {
    Index<Node> index = nodeIndex("mix", 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 13 with QueryContext

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

the class TestLuceneIndex method shouldNotGetLatestTxModificationsWhenChoosingSpeedQueries.

@Test
public void shouldNotGetLatestTxModificationsWhenChoosingSpeedQueries() {
    Index<Node> index = nodeIndex("indexFooBar", LuceneIndexImplementation.EXACT_CONFIG);
    Node node = graphDb.createNode();
    index.add(node, "key", "value");
    QueryContext queryContext = new QueryContext("value").tradeCorrectnessForSpeed();
    assertThat(index.query("key", queryContext), isEmpty());
    assertThat(index.query("key", "value"), contains(node));
}
Also used : Node(org.neo4j.graphdb.Node) QueryContext(org.neo4j.index.lucene.QueryContext) Test(org.junit.Test)

Example 14 with QueryContext

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

the class TestLuceneIndex method queryAndSortNodesByNumericProperty.

private void queryAndSortNodesByNumericProperty(Index<Node> index, String numericProperty, Function<Integer, ? extends Number> expectedValueProvider) {
    try (Transaction transaction = graphDb.beginTx()) {
        QueryContext queryContext = new QueryContext(numericProperty + ":**");
        queryContext.sort(new Sort(new SortedNumericSortField(numericProperty, SortField.Type.INT, false)));
        IndexHits<Node> nodes = index.query(queryContext);
        int nodeIndex = 0;
        for (Node node : nodes) {
            assertEquals("Nodes should be sorted by numeric property", expectedValueProvider.apply(nodeIndex++), node.getProperty(numericProperty));
        }
        transaction.success();
    }
}
Also used : SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Sort(org.apache.lucene.search.Sort) QueryContext(org.neo4j.index.lucene.QueryContext)

Example 15 with QueryContext

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

the class TestLuceneIndex method queryAndSortNodesByStringProperty.

private void queryAndSortNodesByStringProperty(Index<Node> index, String stringProperty, Function<Integer, String> expectedValueProvider) {
    try (Transaction transaction = graphDb.beginTx()) {
        QueryContext queryContext = new QueryContext(stringProperty + ":**");
        queryContext.sort(new Sort(new SortedSetSortField(stringProperty, true)));
        IndexHits<Node> nodes = index.query(queryContext);
        int nodeIndex = 0;
        for (Node node : nodes) {
            assertEquals("Nodes should be sorted by string property", expectedValueProvider.apply(nodeIndex++), node.getProperty(stringProperty));
        }
        transaction.success();
    }
}
Also used : SortedSetSortField(org.apache.lucene.search.SortedSetSortField) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Sort(org.apache.lucene.search.Sort) QueryContext(org.neo4j.index.lucene.QueryContext)

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