Skip to content
Vincent Foulon edited this page Nov 20, 2019 · 25 revisions

Indexing documents

For indexing documents you'll just need to have an array (or an object with public fields) matching any schema and use the update function. Be sure that you provided an "id" (a unique ID for your document) and a "type" (corresponding to a schema name)

$doc = [
    "id" => 1,
    "type" => "example-post",
    "title" => "this is my first blog post !",
    "content" => "I am very happy to post this first post in my blog !",
    "categories" => [
        "party",
        "misc."
    ],
    "date" => "2018/01/01",
    "comments" => [
        [
            "author" => "vincent",
            "date" => "2018/01/01",
            "message" => "Hello world!"
        ],
        [
            "author" => "someone",
            "date" => "2018/01/02",
            "message" => "Welcome !"
        ]
    ]
];
$engine->update($doc);

Searching through your documents

You can use the search function with your term in first parameter:

$response = $engine->search('second post');

There is also a second parameter to this function if you want more features like limit, offsets, or facets

$result = $engine->search("ipsum", ['limit' => 100, 'offset' => 3, 'facets' => ["categories","author"]]);

This line above will search the term "ipsum" through your documents, and will fetch 100 documents skipping the 3 first and at last it'll give you a list of every categories and authors stored in the index and how much of each there is.

More about faceting

To enable faceting into a field, you just need to use the "_filterable" parameter into your schema.

Using QueryBuilder to help create queries

Since version 0.6 you are able to use the QueryBuilder and its functions to simplify the creation of your queries. You can insert a QueryBuilder object as the first parameter of the search function, and the engine will handle it for you. Here's an example of Query Builder's usage:

<?php
use VFou\Search\Query\QueryBuilder;

$qb = new QueryBuilder("some search string"); // create the queryBuilder with a set search string
$qb->addExactSearch("year", 2019); // Search exactly the term "2019" into the "year" field
$qb->addFieldSearch("author", "Vincent"); // Regular search on the single field "author"
$qb->orderBy("title", "ASC"); // order the results by the title
$qb->addFacet("category"); // ask to display the "Category" facet
$qb->setLimit(12); // You want to retrieve 12 items
$qb->setOffset(3); // you want to skip the 3 first items
$results = $engine->search($qb); // execute the query

Autocomplete search

You can call a function that will try to autocomplete the last word of your search, it'll give you a list of possible values that you can display as autocomplete :

$result = $engine->suggest("I'm se"); // will output an array like ["I'm searching", "I'm seeing"] considering that "searching" and "seeing" are tokens

Clearing the cache

This search engine use some heavy caching, if you need to force clear the cache you have this function:

$engine->getIndex()->clearCache();

The engine will automatically clear its cache when updating data, so you won't have much reasons to do this manually.

Rebuilding the index

If for some reason you have to update every documents (for example because you changed the schema or the types or an update added some new features that requires you to rebuild your index) you can call a function that will reindex every stored documents:

$errors = $engine->getIndex()->rebuild(); // you can then take the result and check if the array is empty

Be careful when using this function ! You'll need to set your max execution time to a huge value if your index contains thousands of documents. The process can take time so don't worry if the page is loading for a long time.

Clone this wiki locally