Search in sources :

Example 6 with ValueContext

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

the class TestLuceneIndex method updateIndex.

@Test
public void updateIndex() throws Exception {
    String TEXT = "text";
    String NUMERIC = "numeric";
    String TEXT_1 = "text_1";
    Index<Node> index = nodeIndex(LuceneIndexImplementation.EXACT_CONFIG);
    Node n = graphDb.createNode();
    index.add(n, NUMERIC, new ValueContext(5).indexNumeric());
    index.add(n, TEXT, "text");
    index.add(n, TEXT_1, "text");
    commitTx();
    beginTx();
    assertNotNull(index.query(QueryContext.numericRange(NUMERIC, 5, 5, true, true)).getSingle());
    assertNotNull(index.get(TEXT_1, "text").getSingle());
    index.remove(n, TEXT, "text");
    index.add(n, TEXT, "text 1");
    commitTx();
    beginTx();
    assertNotNull(index.get(TEXT_1, "text").getSingle());
    assertNotNull(index.query(QueryContext.numericRange(NUMERIC, 5, 5, true, true)).getSingle());
}
Also used : ValueContext(org.neo4j.index.lucene.ValueContext) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 7 with ValueContext

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

the class TestLuceneIndex method queryIndexWithSortByNumeric.

@Test
public void queryIndexWithSortByNumeric() throws Exception {
    Index<Node> index = nodeIndex();
    commitTx();
    String numericProperty = "NODE_ID";
    try (Transaction transaction = graphDb.beginTx()) {
        for (int i = 0; i < 15; i++) {
            Node node = graphDb.createNode();
            node.setProperty(numericProperty, i);
            index.add(node, numericProperty, new ValueContext(i).indexNumeric());
        }
        transaction.success();
    }
    queryAndSortNodesByNumericProperty(index, numericProperty);
}
Also used : ValueContext(org.neo4j.index.lucene.ValueContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 8 with ValueContext

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

the class TestLuceneIndex method testRemoveNumericValues.

@Test
public void testRemoveNumericValues() {
    Index<Node> index = nodeIndex(LuceneIndexImplementation.EXACT_CONFIG);
    Node node1 = graphDb.createNode();
    Node node2 = graphDb.createNode();
    String key = "key";
    index.add(node1, key, new ValueContext(15).indexNumeric());
    index.add(node2, key, new ValueContext(5).indexNumeric());
    index.remove(node1, key, new ValueContext(15).indexNumeric());
    assertThat(index.query(NumericRangeQuery.newIntRange(key, 0, 20, false, false)), Contains.contains(node2));
    index.remove(node2, key, new ValueContext(5).indexNumeric());
    assertThat(index.query(NumericRangeQuery.newIntRange(key, 0, 20, false, false)), emptyIterable());
    restartTx();
    assertThat(index.query(NumericRangeQuery.newIntRange(key, 0, 20, false, false)), emptyIterable());
    index.add(node1, key, new ValueContext(15).indexNumeric());
    index.add(node2, key, new ValueContext(5).indexNumeric());
    restartTx();
    assertThat(index.query(NumericRangeQuery.newIntRange(key, 0, 20, false, false)), Contains.contains(node1, node2));
    index.remove(node1, key, new ValueContext(15).indexNumeric());
    assertThat(index.query(NumericRangeQuery.newIntRange(key, 0, 20, false, false)), Contains.contains(node2));
    restartTx();
    assertThat(index.query(NumericRangeQuery.newIntRange(key, 0, 20, false, false)), Contains.contains(node2));
}
Also used : ValueContext(org.neo4j.index.lucene.ValueContext) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 9 with ValueContext

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

the class TestLuceneIndex method queryIndexWithSortByNumericAfterOtherPropertyUpdate.

@Test
public void queryIndexWithSortByNumericAfterOtherPropertyUpdate() {
    Index<Node> index = nodeIndex();
    commitTx();
    String yearProperty = "year";
    String priceProperty = "price";
    Label nodeLabel = Label.label("priceyNodes");
    try (Transaction transaction = graphDb.beginTx()) {
        for (int i = 0; i < 15; i++) {
            Node node = graphDb.createNode(nodeLabel);
            node.setProperty(yearProperty, i);
            node.setProperty(priceProperty, i);
            index.add(node, yearProperty, new ValueContext(i).indexNumeric());
            index.add(node, priceProperty, new ValueContext(i).indexNumeric());
        }
        transaction.success();
    }
    doubleNumericPropertyValueForAllNodesWithLabel(index, priceProperty, nodeLabel);
    queryAndSortNodesByNumericProperty(index, yearProperty);
    queryAndSortNodesByNumericProperty(index, priceProperty, (value) -> value * 2);
}
Also used : ValueContext(org.neo4j.index.lucene.ValueContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) Test(org.junit.Test)

Example 10 with ValueContext

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

the class TestLuceneBatchInsert method testNumericValueArrays.

@Test
public void testNumericValueArrays() {
    BatchInserterIndexProvider provider = new LuceneBatchInserterIndexProviderNewImpl(inserter);
    BatchInserterIndex batchIndex = provider.nodeIndex("mine", EXACT_CONFIG);
    long nodeId1 = inserter.createNode(null);
    batchIndex.add(nodeId1, map("number", new ValueContext[] { numeric(45), numeric(98) }));
    long nodeId2 = inserter.createNode(null);
    batchIndex.add(nodeId2, map("number", new ValueContext[] { numeric(47), numeric(100) }));
    batchIndex.flush();
    IndexHits<Long> batchIndexResult1 = batchIndex.query("number", newIntRange("number", 47, 98, true, true));
    assertThat(batchIndexResult1, contains(nodeId1, nodeId2));
    assertThat(batchIndexResult1.size(), is(2));
    IndexHits<Long> batchIndexResult2 = batchIndex.query("number", newIntRange("number", 44, 46, true, true));
    assertThat(batchIndexResult2, contains(nodeId1));
    assertThat(batchIndexResult2.size(), is(1));
    IndexHits<Long> batchIndexResult3 = batchIndex.query("number", newIntRange("number", 99, 101, true, true));
    assertThat(batchIndexResult3, contains(nodeId2));
    assertThat(batchIndexResult3.size(), is(1));
    IndexHits<Long> batchIndexResult4 = batchIndex.query("number", newIntRange("number", 47, 98, false, false));
    assertThat(batchIndexResult4, Matchers.emptyIterable());
    provider.shutdown();
    switchToGraphDatabaseService();
    try (Transaction transaction = db.beginTx()) {
        Node node1 = db.getNodeById(nodeId1);
        Node node2 = db.getNodeById(nodeId2);
        Index<Node> index = db.index().forNodes("mine");
        IndexHits<Node> indexResult1 = index.query("number", newIntRange("number", 47, 98, true, true));
        assertThat(indexResult1, contains(node1, node2));
        assertThat(indexResult1.size(), is(2));
        IndexHits<Node> indexResult2 = index.query("number", newIntRange("number", 44, 46, true, true));
        assertThat(indexResult2, contains(node1));
        assertThat(indexResult2.size(), is(1));
        IndexHits<Node> indexResult3 = index.query("number", newIntRange("number", 99, 101, true, true));
        assertThat(indexResult3, contains(node2));
        assertThat(indexResult3.size(), is(1));
        IndexHits<Node> indexResult4 = index.query("number", newIntRange("number", 47, 98, false, false));
        assertThat(indexResult4, Matchers.emptyIterable());
        transaction.success();
    }
}
Also used : LuceneBatchInserterIndexProviderNewImpl(org.neo4j.index.impl.lucene.legacy.LuceneBatchInserterIndexProviderNewImpl) ValueContext(org.neo4j.index.lucene.ValueContext) LuceneBatchInserterIndexProvider(org.neo4j.index.lucene.unsafe.batchinsert.LuceneBatchInserterIndexProvider) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Aggregations

ValueContext (org.neo4j.index.lucene.ValueContext)10 Node (org.neo4j.graphdb.Node)9 Test (org.junit.Test)8 Transaction (org.neo4j.graphdb.Transaction)5 Label (org.neo4j.graphdb.Label)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Document (org.apache.lucene.document.Document)1 Term (org.apache.lucene.index.Term)1 TermQuery (org.apache.lucene.search.TermQuery)1 WildcardQuery (org.apache.lucene.search.WildcardQuery)1 IndexManager (org.neo4j.graphdb.index.IndexManager)1 LuceneBatchInserterIndexProviderNewImpl (org.neo4j.index.impl.lucene.legacy.LuceneBatchInserterIndexProviderNewImpl)1 QueryContext (org.neo4j.index.lucene.QueryContext)1 LuceneBatchInserterIndexProvider (org.neo4j.index.lucene.unsafe.batchinsert.LuceneBatchInserterIndexProvider)1