And so, let's say we have a co-worker:

{   
    "first_name" : "John",    
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "music" ]
}

We find all the users who are addicted to rock climbing ("about" : "I love to go rock climbing",).

To do this, add another user:

$curl -XPUT 'http://localhost:9200/megacorp/employee/100 ' -d '
{ 
    "name":"Jane",
    "last_name" :"Smith",
    "age" :32,
    "about" :"I like to collect rock albums",    
    "interests": [ "sports", "music" ]
}'

"about" :"I like to collect rock albums". Our new user is interested in collecting rock albums.

Full-text search

We begin to look for.

Command

GET /megacorp/employee/_search
{
    "query" : 
    {
        "match" : 
        {
            "about" : "rock climbing"    
        }    
    }
}

Search on phrases

If we want to find employees who have the phrase "rock" and "climbing". And which are displayed next to each other in the phrase "rock climbing"

To do this, we change the parameter with match to match_phrase

Command:

GET /megacorp/employee/_search
{
    "query" : 
    {
        "match_phrase" : 
        {    
            "about" : "rock climbing"    
        }
    }
}

CURL:

$curl -XPOST 'localhost:9200/megacorp/employee/_search?pretty' -d '
{
    "query" : 
    {
        "match" : 
        {    
            "about" : "rock climbing"
        }
    }
}'

We get something like:

{
    ...
    "hits": {
    "total":2,
    "max_score":0.16273327,
    "hits": [
    {
        ...
        "_score":0.16273327,
        "_source": {
        "first_name":"John",
        "last_name":"Smith",
        "age":25,
        "about":"I love to go rock climbing",
        "interests": [ "sports", "music" ]
    }
},
{
    ...
    "_score":0.016878016,
    "_source": 
    {
        "first_name":"Jane",
        "last_name":"Smith",
        "age":32,
        "about":"I like to collect rock albums",
        "interests": [ "music" ]
    }
}

We got two users. Elasticsearch sort the results by relevance (that best corresponds to the search - first). The first user has the words "rock climbing" (exact match with those words, what we were looking for) and the user is shown a first. The second user has the word "rock" (I like to collect rock albums) and he shown second.

PHP:

require 'vendor/autoload.php';
$client = Elasticsearch\ClientBuilder::create()->build();
$params = [
            'index' => 'megacorp',
            'type'  => 'employee',
            'body'  => [
                'query' => [
                    'match' => [
                         "about" => "rock climbing"
                    ]
                ]
            ]
        ];
try {
    $response = $client->search($params);
 catch (Exception $e) {
    var_dump($e->getMessage());        
}
print_r($response);

Yii2:

$params = [
    'match' => [
        "about" => "rock climbing"
    ]
];
$model = Megacorp::find()->query($params)->all();
var_dump($model);

Additionally

Previous part: Elasticsearch. Search, filters - Query DSL