-
Notifications
You must be signed in to change notification settings - Fork 7
Usage
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);
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.
To enable faceting into a field, you just need to use the "_filterable" parameter into your schema.
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 that using QueryBuilder to build queries is deprecated and will be removed in 1.0. Use QuerySegment instead) 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
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')
)
)
);
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);
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
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.
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.