divengine / nodes

No-SQL Database System for PHP. Is a library for storage relational and serialized data using only PHP. The data can be organized into schemas.
https://divengine.org
GNU General Public License v3.0
3 stars 4 forks source link

Search node query #2

Open coxy17 opened 8 months ago

coxy17 commented 8 months ago

Hi I tried to search for a node using a partial or full string, but not having any luck returning a node. Is this supported?

e.g. $db->search('something')

coxy17 commented 2 weeks ago

Hi, is there any update to this? thanks

rafageist commented 1 week ago

Hello,

search() is not available in version 2.0.0. However, I am working on the new version 3.0.0 and you can try this feature in the master branch.

This is an example that receives a phrase from the command line. Create a file with the following code:


use divengine\nodes;

// include "../src/nodes.php";
include "vendor/autoload.php";

$phrase = $_SERVER['argv'][1];

$db = new nodes("database/contacts");

$db->delNodes();

// 1. Create indexer before create nodes
$db->addIndexer('name_index', function ($node) {
    return $node['name'];
});

// 2. Create the nodes
$db->addNode([
    "name" => "John Doe",
    "age" => 30,
    "city" => "New York"
]);

$db->addNode([
    "name" => "Jane Doe",
    "age" => 25,
    "city" => "New York"
]);

$db->addNode([
    "name" => "John Smith",
    "age" => 35,
    "city" => "Florida"
]);

$db->addNode([
    "name" => "Jane Smith",
    "age" => 40,
    "city" => "Florida"
]);

$db->addNode([
    "name" => "Peter Nash",
    "age" => 25,
    "city" => "NY"
]);

$db->addNode([
    "name" => "Carl Nash",
    "age" => 35,
    "city" => "NY"
]);

$db->addNode([
    "name" => "Peter Smith",
    "age" => 15,
    "city" => "California"
]);

$db->addNode([
    "name" => "Carl Smith",
    "age" => 45,
    "city" => "California"
]);

// 3. Then search and get the indexes

$indexes = $db->search($phrase);

// 4. Load the nodes
foreach ($indexes as $indexId => $index) {
    $node = $db->getNode($index['id']);
    echo "- {$node['name']}\n";
}

And then:

$ php test.php doe
- John Doe
- Jane Doe

$ php test.php john
- John Doe
- John Smith

Regards