Building and Searching an Elasticsearch Index with Sigmie in PHP
Tags: php, elasticsearch, sigmie, beginners
Published: 2023-06-04 11:12:08

Elasticsearch is a powerful search engine that allows you to index and search through large volumes of data quickly and efficiently. In this blog post, we will explore how to create an Elasticsearch index, populate it with documents, and perform searches using the [Sigmie Library](https://sigmie.com) in PHP.

FInd the instruction to install [Sigmie here](https://sigmie.com/docs/v0/installation).

### Connection
Before we can interact with Elasticsearch using Sigmie, we need to set up the Sigmie client. We need to connect to Elasticsearch and create an instance of the Sigmie class.
```php
use Sigmie\Base\Http\ElasticsearchConnection;
use Sigmie\Http\JSONClient;
use Sigmie\Sigmie;

$http = JSONClient::create(['localhost:9200']);

$connection = new ElasticsearchConnection($http);

$sigmie = new Sigmie($connection);
```

## Creating an Index
To begin, we need to set up an Elasticsearch, let’s use the `movies` Index name.  We also need to define the index properties. To do this we create a new instance of the `Sigmie\Mappings\NewProperties` builder class and specifying two text field, the `name` and `description` fields. Also we enable **lowercasing** that will improve our Search relevance.

```php
use Sigmie\Mappings\NewProperties;

$properties = new NewProperties;
$properties->text('name');
$properties->text('description');

$index = $sigmie->newIndex('movies')
    ->properties($properties)
    ->lowercase()
    ->create();
```


### Populating the Index
Once the index is created, we can populate it with documents. We create an array of the `Sigmie\Document\Document`  class and fill the `name` and `description` keys with values.

In this example, we add three documents representing fictional movies.
```php
$index->merge([
    new Document([
        'name' => 'Mickey',
        'description' => 'Adventure in the woods',
    ]),
    new Document([
        'name' => 'Goofy',
        'description' => 'Mickey and his friends',
    ]),
    new Document([
        'name' => 'Donald',
        'description' => 'Chasing Goofy',
    ]),
]);

```


### Performing a Search
After populating the index, we can search it. We create a new search instance, specifying the index name and passing the same properties that we defined earlier. We set the search query string to `mickey` and specify the fields we want to search and to retrieve. In our example we search only the `name` field, but we retrieve both `name` and `description`. Finally, we execute the search and retrieve the hits as a JSON response.
```php
$search = $sigmie->newSearch('movies')
    ->properties($props)
    ->queryString('mickey')
    ->fields(['name'])
    ->retrieve(['name', 'description'])
    ->get();

$hits = $search->json('hits');
```

Conclusion
We have covered a simple approach to searching in Elasticsearch using Sigmie. We have seen how to set up an index, populate it with documents, and perform basic search. 

However, it is important to note that search is more than just finding a single word in a sea of documents. In future posts, we will explore dive deeper and explore various techniques to enhance your search.