Skip to content
Vincent Foulon edited this page Oct 27, 2020 · 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 in your result.

More about faceting

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

Using QueryBuilder and QuerySegment 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.

Note: Using QueryBuilder to build queries is deprecated and will be removed in 1.0. Use QuerySegment (see below) to build queries and QueryBuilder to complete with the limit, offset, facets and order.

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
// REMOVED IN 1.0 SEE BELOW $qb->addExactSearch("year", 2019); Search exactly the term "2019" into the "year" field
// REMOVED IN 1.0 SEE BELOW $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
$qb->enableConnex(); // enables the connex search (see feature list for more informations)
$results = $engine->search($qb); // execute the query

Since 0.7, QuerySegment is introduced to the project. This class contains many static methods that can be nested to one another to create advanced queries. Here's an example:

use VFou\Search\Query\QuerySegment;

$seg = QuerySegment::and(
    QuerySegment::not(QuerySegment::exactSearch('genre','Drama')),
    QuerySegment::or(
        QuerySegment::exactSearch('year',"2011"),
        QuerySegment::and(
            QuerySegment::greaterSearch('rating',"8"),
            QuerySegment::exactSearch('author','nobody')
        )
    )
);

Note : if you need to have many querySegments searching in the same field you can try using bulkExactSearch or bulkFieldSearch. These methods returns an array of QuerySegments, ready to be given to and and or.

QuerySegment also provide a debug static method that converts a segment to a human readable string query:
-genre:"Drama" AND ( year:"2011" OR ( rating>:"8" AND author:"nobody"))

QuerySegment's and and or methods allow you to provide as many QuerySegments as you like in parameters, or you can provide it with an array of your QuerySegments if you prefer like this.
Next you'll create your QueryBuilder with your search string as first parameter and your QuerySegment as second parameter:

$query = new QueryBuilder($search, $seg);

Autocomplete search

The engine provides two functions to do autocompletion : suggestField and suggestToken:

suggestField will try to autocomplete a specific field of your schema. A third parameter can be provided so the engine will wrap your input with span tags in the results. if this third parameter is just true, there will just be a span. if the parameter is any string, they will be a span with a class containing the string input.

$result = $engine->suggestField('title', 'example'); // will search for titles having "example" inside.

suggestToken will instead try to autocomplete the last word of your search. (this function was called suggest before version 1.1.0)

$result = $engine->suggestToken("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 ! Backup your documents folder in case something go wrong ! 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.
This function can lose your documents or data if interrupted abruptly or if your configuration is broken. Note that this function returns an array of errors encountered when rebuilding